From d66816d72bf0797f5953e4cc8d535f083243a7e5 Mon Sep 17 00:00:00 2001 From: yangmingliang Date: Tue, 18 Jun 2024 19:42:06 +0800 Subject: [PATCH] refactor usb right dialog Signed-off-by: yangmingliang Change-Id: I3d4ffa997afd3c42ca8ec9573f4128cec37dc13a --- .../ets/ServiceExtAbility/UsbDialogAbility.ts | 8 +- .../UsbFunctionSwitchWindowAbility.ts | 2 +- .../src/main/ets/pages/UsbDialog.ets | 127 +++++++----------- .../main/resources/base/element/string.json | 8 ++ .../src/main/resources/zh/element/string.json | 8 ++ services/native/include/usb_host_manager.h | 1 + services/native/include/usb_right_manager.h | 4 + services/native/include/usb_service.h | 4 +- services/native/src/usb_host_manager.cpp | 16 +++ services/native/src/usb_right_manager.cpp | 49 +++++++ services/native/src/usb_service.cpp | 10 ++ services/usb_service.cfg | 1 + 12 files changed, 156 insertions(+), 82 deletions(-) diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts index 1dcad578..8a6a8fd5 100644 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts @@ -29,11 +29,12 @@ class UsbDialogStub extends rpc.RemoteObject { } } -const BG_COLOR = '#33000000'; +const BG_COLOR = '#00000000'; const ENTERPRISE_MANAGE_USB = 'ohos.permission.ENTERPRISE_MANAGE_USB'; const ACCESS_TYPE_MASK = 0b11; const SHIFT_DIGIT = 27; const TOKEN_NATIVE = 1; +const COLOR_MODE_NOT_SET = -1; export default class UsbDialogAbility extends extension { /** @@ -44,6 +45,11 @@ export default class UsbDialogAbility extends extension { globalThis.extensionContext = this.context; globalThis.want = want; globalThis.windowNum = 0; + try { + this.context?.getApplicationContext()?.setColorMode(COLOR_MODE_NOT_SET); + } catch (err) { + console.error('onCreate setColorMode failed: ' + JSON.stringify(err)); + } } onConnect(want): rpc.RemoteObject { diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbFunctionSwitchWindowAbility.ts b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbFunctionSwitchWindowAbility.ts index 8818379b..583f5505 100755 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbFunctionSwitchWindowAbility.ts +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbFunctionSwitchWindowAbility.ts @@ -36,7 +36,7 @@ export default class UsbFunctionSwitchAbility extends UIExtensionAbility { (session as UIExtensionContentSession)?.loadContent('pages/Index'); try { (session as UIExtensionContentSession)?.setWindowBackgroundColor(BG_COLOR); - this.context?.getApplicationContext().setColorMode(COLOR_MODE_NOT_SET); + this.context?.getApplicationContext()?.setColorMode(COLOR_MODE_NOT_SET); } catch (error) { console.error(TAG + 'UsbFunctionSwitchAbility onSessionCreate error: ' + error?.code); (session as UIExtensionContentSession)?.terminateSelf(); diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/pages/UsbDialog.ets b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/pages/UsbDialog.ets index 378f2e68..fbdda571 100644 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/pages/UsbDialog.ets +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/pages/UsbDialog.ets @@ -13,94 +13,63 @@ * limitations under the License. */ +import { AlertDialog } from '@ohos.arkui.advanced.Dialog' import usbMgr from '@ohos.usbManager' -// allow,notAllow button style -@Extend(Text) function controlBtn(fontColor: string) { - .fontColor(fontColor) - .backgroundColor(0xffffff) - .width(100) - .height(22) - .fontSize(20) - .textAlign(TextAlign.Center) -} - -// content style -@Extend(Text) function controlFontSize(fontSize: number) { - .fontSize(fontSize) - .width('180%') - .fontColor('rgba(10, 10, 10, 1.00)') - .textAlign(TextAlign.Center) - .padding({ top: 30, left: 50, right: 50 }) - .alignSelf(ItemAlign.Center) - .margin({top: 10}) -} - -@CustomDialog -export default -struct ConfirmDialog { - content: string - notAllowFontColor: string = '#E84026' - allowFontColor: string = '#ff1a0af7' - controller: CustomDialogController - // title style - @Builder TipTextStyle(tip: string, fontSize: number) { - Text(tip) - .controlFontSize(fontSize) - .visibility(tip.length > 0 ? Visibility.Visible : Visibility.None) - } - build() { - Column() { - this.TipTextStyle(this.content, 22) - Flex({ justifyContent: FlexAlign.SpaceAround }) { - Text($r('app.string.allowButton')) - .controlBtn(this.allowFontColor) - .onClick(() => { - try { - usbMgr.addDeviceAccessRight(globalThis.want.parameters['tokenId'], globalThis.want.parameters['deviceName']) - this.destruction() - } catch { - console.info('USBRight ConfirmDialog onClick error!'); - } - }) - Text('|') - .fontSize(10) - Text($r('app.string.notAllowButton')) - .controlBtn(this.notAllowFontColor) - .onClick(() => { - this.destruction() - }) - }.margin({ top: 43, bottom: 15, left: 20, right: 20 }) +@Entry +@Component +struct IndexHapComponent { + confirmDialogController: CustomDialogController = new CustomDialogController({ + builder: AlertDialog({ + primaryTitle: $r('app.string.usb_right_title'), + content: $r('app.string.usb_right_content', globalThis.want?.parameters?.['appName'], globalThis.want?.parameters?.['productName']), + primaryButton: { + value: $r('app.string.notAllowButton'), + action: () => { + console.info('USBRight not allowed'); + this.closeDialog() + }, + }, + secondaryButton: { + value: $r('app.string.allowButton'), + role: ButtonRole.ERROR, + action: () => { + try { + console.info('USBRight allowed'); + let res = usbMgr.addDeviceAccessRight(globalThis.want.parameters['tokenId'], + globalThis.want.parameters['deviceName']) + console.info(`USBRight addDeviceAccessRight result:${res}`); + } catch { + console.error('USBRight ConfirmDialog onClick error!'); + } finally { + this.closeDialog() + } + } + }, + }), + autoCancel: false, + cancel: () => { + console.info('USBRight dialog canceled !'); + this.closeDialog() } - } - destruction() { - this.controller.close() + }) + + closeDialog() { + console.info('USBRight close dialog'); + this.confirmDialogController.close() try { globalThis.window.destroy() globalThis.extensionContext.terminateSelf() - } catch { - console.info('USBRight ConfirmDialog destruction error!'); + } catch (err) { + console.error('USBRight ConfirmDialog closeDialog error!', err); } } -} -@Entry -@Component -struct IndexHapComponent { - private content: string = '' - private allow: string = '' - private use: string = '' - confirmDialogController: CustomDialogController = new CustomDialogController( { - builder: ConfirmDialog({content: this.content}), - autoCancel: false - }) + aboutToAppear(): void { + console.info('USBRight show dialog', JSON.stringify(globalThis.want)); + this.confirmDialogController.open(); + } - async aboutToAppear() { - this.allow = await globalThis.getContext().resourceManager.getStringValue($r('app.string.allow')); - this.use = await globalThis.getContext().resourceManager.getStringValue($r('app.string.use')); - this.content = this.allow + globalThis.want.parameters.bundleName + this.use + - globalThis.want.parameters.deviceName; - this.confirmDialogController.open() + build() { } - build() {} } \ No newline at end of file diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/base/element/string.json b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/base/element/string.json index 559582f5..dbf304ce 100644 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/base/element/string.json +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/base/element/string.json @@ -99,6 +99,14 @@ { "name": "USB_notify_text", "value": "Touch for more option" + }, + { + "name": "usb_right_content", + "value": "Whether %s allowed to use the %s ?" + }, + { + "name": "usb_right_title", + "value": "USB Access Right" } ] } \ No newline at end of file diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/zh/element/string.json b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/zh/element/string.json index b9ee994d..ec04dca9 100755 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/zh/element/string.json +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/resources/zh/element/string.json @@ -71,6 +71,14 @@ { "name": "USB_notify_text", "value": "点按即可查看更多选项" + }, + { + "name": "usb_right_content", + "value": "是否允许 %s 使用 %s ?" + }, + { + "name": "usb_right_title", + "value": "USB访问授权" } ] } \ No newline at end of file diff --git a/services/native/include/usb_host_manager.h b/services/native/include/usb_host_manager.h index 3328b52f..eddbd394 100644 --- a/services/native/include/usb_host_manager.h +++ b/services/native/include/usb_host_manager.h @@ -33,6 +33,7 @@ public: explicit UsbHostManager(SystemAbility *systemAbility); ~UsbHostManager(); void GetDevices(MAP_STR_DEVICE &devices); + bool GetProductName(const std::string &deviceName, std::string &productName); bool DelDevice(uint8_t busNum, uint8_t devNum); bool AddDevice(UsbDevice *dev); bool Dump(int fd, const std::string &args); diff --git a/services/native/include/usb_right_manager.h b/services/native/include/usb_right_manager.h index 4c14e9ae..c5835fb5 100644 --- a/services/native/include/usb_right_manager.h +++ b/services/native/include/usb_right_manager.h @@ -25,6 +25,7 @@ #include "ability_connect_callback_stub.h" #include "bundle_mgr_interface.h" +#include "bundle_resource_interface.h" #include "usb_common.h" #include "parameter.h" @@ -62,6 +63,9 @@ private: bool CheckSaPermission(); bool IsAllDigits(const std::string &bundleName); sptr GetBundleMgr(); + sptr GetBundleResMgr(); + bool GetAppName(const std::string &bundleName, std::string &appName); + bool GetProductName(const std::string &devName, std::string &productName); static sem_t waitDialogDisappear_; class UsbAbilityConn : public AAFwk::AbilityConnectionStub { diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index ded7ecaf..c7ede508 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -68,7 +68,7 @@ public: { return handler_; } - + static sptr GetGlobalInstance(); int32_t SetUsbd(const sptr &usbd); int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; bool CheckDevicePermission(uint8_t busNum, uint8_t devAddr); @@ -129,6 +129,8 @@ public: int32_t ManageInterfaceType(const std::vector &disableType, bool disable) override; int32_t GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, bool &unactivated) override; int32_t GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed) override; + + bool GetDeviceProductName(const std::string &deviceName, std::string &productName); private: class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { public: diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index c290d5cb..3fe7e147 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -44,6 +44,22 @@ void UsbHostManager::GetDevices(MAP_STR_DEVICE &devices) devices = devices_; } +bool UsbHostManager::GetProductName(const std::string &deviceName, std::string &productName) +{ + auto iter = devices_.find(deviceName); + if (iter == devices_.end()) { + return false; + } + + UsbDevice *dev = iter->second; + if (dev == nullptr) { + return false; + } + + productName = dev->GetProductName(); + return true; +} + bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum) { std::string name = std::to_string(busNum) + "-" + std::to_string(devNum); diff --git a/services/native/src/usb_right_manager.cpp b/services/native/src/usb_right_manager.cpp index a425283e..826a3859 100644 --- a/services/native/src/usb_right_manager.cpp +++ b/services/native/src/usb_right_manager.cpp @@ -35,6 +35,7 @@ #include "usb_right_db_helper.h" #include "usb_napi_errors.h" #include "usb_srv_support.h" +#include "usb_service.h" using namespace OHOS::AppExecFwk; using namespace OHOS::EventFwk; @@ -253,11 +254,23 @@ bool UsbRightManager::ShowUsbDialog( return false; } + std::string appName; + if (!GetAppName(bundleName, appName)) { + appName = bundleName; + } + + std::string productName; + if (!GetProductName(busDev, productName)) { + productName = busDev; + } + AAFwk::Want want; want.SetElementName("com.usb.right", "UsbServiceExtAbility"); want.SetParam("bundleName", bundleName); want.SetParam("deviceName", busDev); want.SetParam("tokenId", tokenId); + want.SetParam("appName", appName); + want.SetParam("productName", productName); sptr usbAbilityConn_ = new (std::nothrow) UsbAbilityConn(); sem_init(&waitDialogDisappear_, 1, 0); @@ -306,6 +319,42 @@ sptr UsbRightManager::GetBundleMgr() return bundleMgr; } +sptr UsbRightManager::GetBundleResMgr() +{ + auto bundleMgr = GetBundleMgr(); + if (bundleMgr == nullptr) { + return nullptr; + } + return bundleMgr->GetBundleResourceProxy(); +} + +bool UsbRightManager::GetAppName(const std::string &bundleName, std::string &appName) +{ + auto resMgr = GetBundleResMgr(); + if (resMgr == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "GetAppName: get res mgr failed"); + return false; + } + + BundleResourceInfo info; + auto ret = resMgr->GetBundleResourceInfo(bundleName, (uint32_t)ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL, info); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "GetAppName: get res info failed: %{public}d", ret); + return false; + } + appName = info.label; + return true; +} + +bool UsbRightManager::GetProductName(const std::string &devName, std::string &productName) +{ + auto usbService = UsbService::GetGlobalInstance(); + if (usbService == nullptr) { + return false; + } + return usbService->GetDeviceProductName(devName, productName); +} + bool UsbRightManager::IsSystemApp() { uint64_t tokenid = IPCSkeleton::GetCallingFullTokenID(); diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 28557c36..ea72547c 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -1688,6 +1688,11 @@ void UsbService::UsbdDeathRecipient::OnRemoteDied(const wptr &obj } } +sptr UsbService::GetGlobalInstance() +{ + return g_serviceInstance; +} + int32_t UsbService::PreCallFunction() { usbd_ = OHOS::HDI::Usb::V1_1::IUsbInterface::Get(); @@ -1893,5 +1898,10 @@ int32_t UsbService::GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &spe USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceSpeedImpl:%{public}u", speed); return ret; } + +bool UsbService::GetDeviceProductName(const std::string &deviceName, std::string &productName) +{ + return usbHostManager_->GetProductName(deviceName, productName); +} } // namespace USB } // namespace OHOS diff --git a/services/usb_service.cfg b/services/usb_service.cfg index c16dedf2..5005ca44 100644 --- a/services/usb_service.cfg +++ b/services/usb_service.cfg @@ -11,6 +11,7 @@ "ohos.permission.DISTRIBUTED_DATASYNC", "ohos.permission.ENTERPRISE_MANAGE_USB", "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + "ohos.permission.GET_BUNDLE_RESOURCES", "ohos.permission.START_SYSTEM_DIALOG" ], "apl" : "system_basic", -- Gitee