From df4f37b55489b26e838e8e2f8277db110c8fb1b9 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Mon, 13 Jan 2025 15:28:27 +0800 Subject: [PATCH] =?utf8?q?=E8=A7=A3=E5=86=B3=E8=93=9D=E7=89=99=E8=80=B3?= =?utf8?q?=E6=9C=BA=E7=95=8C=E9=9D=A2=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= =?utf8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../src/BluetoothDeviceCaseListAdapter.cpp | 282 +++++++++--------- 1 file changed, 146 insertions(+), 136 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothDeviceCaseListAdapter.cpp b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothDeviceCaseListAdapter.cpp index ed75a08c8..dfda43dda 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothDeviceCaseListAdapter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/bluetooth/src/BluetoothDeviceCaseListAdapter.cpp @@ -13,159 +13,169 @@ #include "UiConfig.h" #include "BluetoothPresenter.h" -namespace OHOS { -BluetoothDeviceCaseListAdapter::~BluetoothDeviceCaseListAdapter() +namespace OHOS { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter::~BluetoothDeviceCaseListAdapter"); - // UIView::OnClickListener *click = item->GetOnClickListener(); - // if (click != nullptr) { - // delete click; - // click = nullptr; - // } - if (listener != nullptr) { - delete listener; - listener = nullptr; - } -} - -uint16_t BluetoothDeviceCaseListAdapter::GetCount() -{ - return BluetoothModel::GetInstance()->GetConnedDevicesCase().Size(); -} - -class BluetoothDeviceOnClickListListener : public UIView::OnClickListener { -public: - BluetoothDeviceOnClickListListener(BluetoothCaseInfo *bluetoothInfo) - { - if (bluetoothInfo != nullptr) { - (void)memcpy_s(addr.addr, BD_ADDR_LEN, - bluetoothInfo->addr, BD_ADDR_LEN); // 源和目标内存长度一样,不做返回值判断 - (void)memcpy_s(bluetoothInfo_.addr, BD_ADDR_LEN, - bluetoothInfo->addr, BD_ADDR_LEN); // 源和目标内存长度一样,不做返回值判断 - (void)memcpy_s(bluetoothInfo_.deviceName, BD_NAME_LEN, - bluetoothInfo->deviceName, BD_NAME_LEN); // 源和目标内存长度一样,不做返回值判断 - bluetoothInfo_.index = bluetoothInfo->index; - bluetoothInfo_.connIndex = bluetoothInfo->connIndex; - } - } - ~BluetoothDeviceOnClickListListener() {} - bool OnClick(UIView& view, const ClickEvent& event) override + class BluetoothDeviceOnClickListListener; + static BluetoothDeviceOnClickListListener *g_btOnClickListListener = nullptr; + class BluetoothDeviceOnClickListListener : public UIView::OnClickListener { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClickListListener"); - UNUSED(event); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClickListListener addr: %02x%02x%02x%02x%02x%02x", - bluetoothInfo_.addr[5], bluetoothInfo_.addr[4], bluetoothInfo_.addr[3], /* mac下标 0 1 2 3 4 5 */ - bluetoothInfo_.addr[2], bluetoothInfo_.addr[1], bluetoothInfo_.addr[0]); /* mac下标 0 1 2 3 4 5 */ - BluetoothModel::GetInstance()->SetTempMacValue(&bluetoothInfo_); - /* 点击事件 */ - // 跳转可选页 - if (BluetoothModel::GetInstance()->GetBluetoothCalleridStatus() == CALLERPLAYERS) { - JumpView *jumpView = JumpView::GetInstance(); - if (jumpView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClick jumpView GetInstance fail"); - return false; - } - jumpView->ChangeView(JumpView::BluetoothJumpViewNum::BLUETOOTHDEVICELISTVIEW, - JumpView::BluetoothJumpViewNum::BLUETOOTHEOPTIONSVIEW); - } else if (BluetoothModel::GetInstance()->GetBluetoothCalleridStatus() == CALLERSETTING) { - MainBluetoothView *mainBluetoothView = MainBluetoothView::GetInstance(); - if (mainBluetoothView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClick mainBluetoothView GetInstance fail"); - return false; + public: + BluetoothDeviceOnClickListListener() {} + ~BluetoothDeviceOnClickListListener() {} + void SetItemInfo(const BluetoothCaseInfo *bluetoothInfo) + { + if (bluetoothInfo != nullptr) + { + (void)memcpy_s(addr.addr, BD_ADDR_LEN, + bluetoothInfo->addr, BD_ADDR_LEN); // 源和目标内存长度一样,不做返回值判断 + (void)memcpy_s(bluetoothInfo_.addr, BD_ADDR_LEN, + bluetoothInfo->addr, BD_ADDR_LEN); // 源和目标内存长度一样,不做返回值判断 + (void)memcpy_s(bluetoothInfo_.deviceName, BD_NAME_LEN, + bluetoothInfo->deviceName, BD_NAME_LEN); // 源和目标内存长度一样,不做返回值判断 + bluetoothInfo_.index = bluetoothInfo->index; + bluetoothInfo_.connIndex = bluetoothInfo->connIndex; } - if(view.GetViewType() == UIViewType::UI_BUTTON) + } + bool OnClick(UIView &view, const ClickEvent &event) override + { + UNUSED(event); + BluetoothItemView *itemView = static_cast(view.GetParent()); + BluetoothCaseInfo *g_BluetoothCaseInfo = itemView->GetItemInfo(); + SetItemInfo(g_BluetoothCaseInfo); // 保存点击的设备信息 + BluetoothModel::GetInstance()->SetTempMacValue(&bluetoothInfo_); + if (BluetoothModel::GetInstance()->GetBluetoothCalleridStatus() == CALLERPLAYERS) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UIViewType::UI_BUTTON,%s",reinterpret_cast(bluetoothInfo_.deviceName)); - bd_addr_t *lastAddr = BluetoothModel::GetInstance()->GetLastConnedAddr(); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "lastAddr:%02X %02X %02X * * *", - lastAddr->addr[0],lastAddr->addr[1],lastAddr->addr[2]); - - bool ret = bt_is_acl_connected(lastAddr); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bt_is_acl_connected:%d lastAddr:%02X %02X %02X * * *", - ret, lastAddr->addr[0],lastAddr->addr[1],lastAddr->addr[2]); - - //地址不是全0并且ret为真 - if (!( lastAddr->addr[0] == 0 && lastAddr->addr[1] == 0 && lastAddr->addr[2] == 0 && - lastAddr->addr[3] == 0 && lastAddr->addr[4] == 0 && lastAddr->addr[5] == 0 ) && ret) { - BluetoothModel::GetInstance()->SetTagConnectNew(false); - errcode_t retVal = gap_disconnect_remote_device(lastAddr); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[gap_disconnect_remote_device] retVal = %d", retVal); - } else { - mainBluetoothView->ChangeView(BluetoothModel::GetInstance()->GetCurView(), BLUETOOTHRECONNECTVIEW); - errcode_t retVal = BluetoothModel::GetInstance()->ConnectRemoteDevice(&addr); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[ConnectRemoteDevice] retVal = %d", retVal); + JumpView *jumpView = JumpView::GetInstance(); + if (jumpView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClick jumpView GetInstance fail"); + return false; } + jumpView->ChangeView(JumpView::BluetoothJumpViewNum::BLUETOOTHDEVICELISTVIEW, + JumpView::BluetoothJumpViewNum::BLUETOOTHEOPTIONSVIEW); } - else if(view.GetViewType() == UIViewType::UI_IMAGE_VIEW) /*点击的是图片*/ + else if (BluetoothModel::GetInstance()->GetBluetoothCalleridStatus() == CALLERSETTING) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UIViewType::UI_IMAGE_VIEW"); - mainBluetoothView->ChangeView(BluetoothModel::GetInstance()->GetCurView(), BLUETOOTHEOPTIONSVIEW); - BluetoothOptionsView *bluetoothOptionsView = BluetoothOptionsView::GetInstance(); - if (bluetoothOptionsView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnClick bluetoothOptionsView new fail"); + MainBluetoothView *mainBluetoothView = MainBluetoothView::GetInstance(); + if (mainBluetoothView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceOnClick mainBluetoothView GetInstance fail"); return false; } - bluetoothOptionsView->SetCurrentAddr(addr); - bluetoothOptionsView->RefreshText(); + if (view.GetViewType() == UIViewType::UI_BUTTON) // 点击的是按钮 + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UIViewType::UI_BUTTON,%s", reinterpret_cast(bluetoothInfo_.deviceName)); + bd_addr_t *lastAddr = BluetoothModel::GetInstance()->GetLastConnedAddr(); + bool ret = bt_is_acl_connected(lastAddr); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "bt_is_acl_connected:%d lastAddr:%02X %02X %02X * * *", + ret, lastAddr->addr[0], lastAddr->addr[1], lastAddr->addr[2]); + + // 地址不是全0并且ret为真 + if (!(lastAddr->addr[0] == 0 && lastAddr->addr[1] == 0 && lastAddr->addr[2] == 0 && + lastAddr->addr[3] == 0 && lastAddr->addr[4] == 0 && lastAddr->addr[5] == 0) && + ret) + { + BluetoothModel::GetInstance()->SetTagConnectNew(false); + errcode_t retVal = gap_disconnect_remote_device(lastAddr); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[gap_disconnect_remote_device] retVal = %d", retVal); + } + else + { + mainBluetoothView->ChangeView(BluetoothModel::GetInstance()->GetCurView(), BLUETOOTHRECONNECTVIEW); + errcode_t retVal = BluetoothModel::GetInstance()->ConnectRemoteDevice(&addr); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[ConnectRemoteDevice] retVal = %d", retVal); + } + } + else if (view.GetViewType() == UIViewType::UI_IMAGE_VIEW) /*点击的是图片*/ + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UIViewType::UI_IMAGE_VIEW,%s", reinterpret_cast(bluetoothInfo_.deviceName)); + mainBluetoothView->ChangeView(BluetoothModel::GetInstance()->GetCurView(), BLUETOOTHEOPTIONSVIEW); + BluetoothOptionsView *bluetoothOptionsView = BluetoothOptionsView::GetInstance(); + if (bluetoothOptionsView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnClick bluetoothOptionsView new fail"); + return false; + } + bluetoothOptionsView->SetCurrentAddr(addr); + bluetoothOptionsView->RefreshText(); + } } - } - - return true; - } -private: - BluetoothCaseInfo bluetoothInfo_{}; - bd_addr_t addr; -}; + return true; + } + private: + BluetoothCaseInfo bluetoothInfo_{}; + bd_addr_t addr; + }; -UIView *BluetoothDeviceCaseListAdapter::GetView(UIView *inView, int16_t index) -{ - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter GetView"); - List &bluetoothCaseList = BluetoothModel::GetInstance()->GetConnedDevicesCase(); - if (bluetoothCaseList.IsEmpty()) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bluetoothCaseList IsEmpty"); - return nullptr; - } - if ((index > bluetoothCaseList.Size() - 1) || (index < 0)) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter::GetView err,%d,%d",index,bluetoothCaseList.Size()); - return nullptr; + BluetoothDeviceCaseListAdapter::~BluetoothDeviceCaseListAdapter() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter::~BluetoothDeviceCaseListAdapter"); + if (g_btOnClickListListener != nullptr) + { + delete g_btOnClickListListener; + g_btOnClickListListener = nullptr; + } } - ListNode* node = bluetoothCaseList.Begin(); - for (uint16_t i = 0; i < index; i++) { - node = node->next_; + + uint16_t BluetoothDeviceCaseListAdapter::GetCount() + { + return BluetoothModel::GetInstance()->GetConnedDevicesCase().Size(); } - node->data_.index = index; - BluetoothCaseInfo itemData = node->data_; - if (inView == nullptr) { - item = new BluetoothItemView(); - if (item == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetView item err"); + + UIView *BluetoothDeviceCaseListAdapter::GetView(UIView *inView, int16_t index) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter GetView"); + List &bluetoothCaseList = BluetoothModel::GetInstance()->GetConnedDevicesCase(); + if (bluetoothCaseList.IsEmpty()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "bluetoothCaseList IsEmpty"); return nullptr; } - } else { - item = static_cast(inView); - } - item->SetViewIndex(index); - item->SetItemInfo(itemData, false); - item->SetConnectionstatus(itemData); + if ((index > bluetoothCaseList.Size() - 1) || (index < 0)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter::GetView err,%d,%d", index, bluetoothCaseList.Size()); + return nullptr; + } + ListNode *node = bluetoothCaseList.Begin(); + for (uint16_t i = 0; i < index; i++) + { + node = node->next_; + } + node->data_.index = index; + BluetoothCaseInfo itemData = node->data_; + if (inView == nullptr) + { + item = new BluetoothItemView(); + if (item == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetView item err"); + return nullptr; + } + } + else + { + item = static_cast(inView); + } + item->SetViewIndex(index); + item->SetItemInfo(itemData, false); + item->SetConnectionstatus(itemData); - listener = item->GetOnClickListener(); - if (listener != nullptr) { - delete listener; - listener = nullptr; - } - listener = new BluetoothDeviceOnClickListListener(&node->data_); - if (listener == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetView BluetoothDeviceOnClickListListener err"); - return nullptr; + if (g_btOnClickListListener == nullptr) + { + g_btOnClickListListener = new BluetoothDeviceOnClickListListener(); + if (g_btOnClickListListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BluetoothDeviceCaseListAdapter::GetView new fail"); + return nullptr; + } + } + listener = static_cast(g_btOnClickListListener); + item->SetViewOnClickListener(listener); + sprintf_s(addrTmp, sizeof(addrTmp), "%02x%02x%02x%02x%02x%02x", node->data_.addr[0], node->data_.addr[1], + node->data_.addr[2], node->data_.addr[3], node->data_.addr[4], node->data_.addr[5]); // 没有%s类型,不做返回判断 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "addrTmp %s", addrTmp); + return item; } - sprintf_s(addrTmp, sizeof(addrTmp), "%02x%02x%02x%02x%02x%02x", node->data_.addr[0], node->data_.addr[1], - node->data_.addr[2], node->data_.addr[3], node->data_.addr[4], node->data_.addr[5]); // 没有%s类型,不做返回判断 - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "addrTmp %s", addrTmp); - - // item->SetOnClickListener(listener); - item->SetViewOnClickListener(listener); - return item; -} } // namespace OHOS \ No newline at end of file -- 2.17.1