From 784d90dc2fa7f56994f05398e462518cb7b72944 Mon Sep 17 00:00:00 2001 From: liwuli Date: Wed, 31 Jan 2024 11:50:29 +0800 Subject: [PATCH] fixed 09e47c3 from https://gitee.com/Liwuli123/applications_admin_provisioning/pulls/55 fix hap error Signed-off-by: liwuli --- .../ets/MainAbility/AutoManagerAbility.ts | 8 ++-- entry/src/main/ets/MainAbility/MainAbility.ts | 28 ++--------- .../common/appManagement/appDetailData.ets | 5 +- entry/src/main/ets/pages/applicationInfo.ets | 27 ++++------- .../ets/pages/autoManager/managerStart.ets | 9 ++-- .../pages/autoManager/setFinishSuccess.ets | 48 ++++++++++--------- .../autoManager/doubleButtonComponent.ets | 10 ++-- .../ets/pages/component/headComponent.ets | 4 +- entry/src/main/module.json | 2 +- entry/src/main/module.json5 | 2 +- .../main/resources/base/element/string.json | 10 ++-- .../main/resources/en_US/element/string.json | 10 ++-- .../main/resources/zh_CN/element/string.json | 10 ++-- 13 files changed, 74 insertions(+), 99 deletions(-) diff --git a/entry/src/main/ets/MainAbility/AutoManagerAbility.ts b/entry/src/main/ets/MainAbility/AutoManagerAbility.ts index a39cc1c..cebe8db 100644 --- a/entry/src/main/ets/MainAbility/AutoManagerAbility.ts +++ b/entry/src/main/ets/MainAbility/AutoManagerAbility.ts @@ -18,15 +18,15 @@ import logger from '../common/logger'; import type Want from '@ohos.app.ability.Want'; import type AbilityConstant from '@ohos.app.ability.AbilityConstant'; import type window from '@ohos.window'; -import { GlobalContext } from './MainAbility'; const TAG = 'AutoManagerAbility'; export default class AutoManagerAbility extends UIAbility { + private localStorage: LocalStorage = new LocalStorage(); + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { logger.info(TAG, 'onCreate'); - GlobalContext.getContext().setObject('autoManagerAbilityWant', want); - GlobalContext.getContext().setObject('autoManagerAbilityContext', this.context); + this.localStorage.setOrCreate('autoManagerAbilityWant', want); } onDestroy(): void { @@ -36,7 +36,7 @@ export default class AutoManagerAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability logger.info(TAG, 'onWindowStageCreate'); - windowStage.loadContent('pages/autoManager/managerStart', null); + windowStage.loadContent('pages/autoManager/managerStart', this.localStorage); } onWindowStageDestroy(): void { diff --git a/entry/src/main/ets/MainAbility/MainAbility.ts b/entry/src/main/ets/MainAbility/MainAbility.ts index 8ca92e7..d2f510c 100644 --- a/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/entry/src/main/ets/MainAbility/MainAbility.ts @@ -16,34 +16,14 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import Logger from '../common/logger'; -export class GlobalContext { - private constructor() {} - private static instance: GlobalContext; - private _objects = new Map(); - - public static getContext(): GlobalContext { - if (!GlobalContext.instance) { - GlobalContext.instance = new GlobalContext(); - } - return GlobalContext.instance; - } - - getObject(value: string): Object | undefined { - return this._objects.get(value); - } - - setObject(key: string, objectClass: Object): void { - this._objects.set(key, objectClass); - } -} - const TAG = 'MainAbility'; export default class MainAbility extends UIAbility { + private localStorage: LocalStorage = new LocalStorage(); + onCreate(want, launchParam): void { Logger.info(TAG, 'onCreate'); - GlobalContext.getContext().setObject('adminProvisioningWant', want); - GlobalContext.getContext().setObject('adminProvisioningContext', this.context); + this.localStorage.setOrCreate('adminProvisioningWant', want); } onDestroy(): void { @@ -53,7 +33,7 @@ export default class MainAbility extends UIAbility { onWindowStageCreate(windowStage): void { // Main window is created, set main page for this ability Logger.info(TAG, 'onWindowStageCreate'); - windowStage.setUIContent(this.context, 'pages/applicationInfo', null); + windowStage.setUIContent(this.context, 'pages/applicationInfo', this.localStorage); } onWindowStageDestroy(): void { diff --git a/entry/src/main/ets/common/appManagement/appDetailData.ets b/entry/src/main/ets/common/appManagement/appDetailData.ets index e19c73b..6b3e483 100644 --- a/entry/src/main/ets/common/appManagement/appDetailData.ets +++ b/entry/src/main/ets/common/appManagement/appDetailData.ets @@ -21,7 +21,6 @@ import utils from '../utils' import { AppPermission, MyApplicationInfo } from '../myApplicationInfo' import Want from '@ohos.app.ability.Want'; import resourceManager from '@ohos.resourceManager'; -import { GlobalContext } from '../../MainAbility/MainAbility'; import common from '@ohos.app.ability.common'; let icon_default = $r('app.media.icon'); @@ -54,7 +53,7 @@ export class AppDetailData { } let data: Array = []; try { - bundle.queryExtensionAbilityInfo(want, bundle.ExtensionAbilityType.ENTERPRISE_ADMIN, + await bundle.queryExtensionAbilityInfo(want, bundle.ExtensionAbilityType.ENTERPRISE_ADMIN, bundle.ExtensionAbilityFlag.GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION, userId.localId).then((result)=>{ data = result; }) @@ -141,7 +140,7 @@ export class AppDetailData { async terminateAbilityPage() { logger.info(TAG, 'terminateAbilityPage in:'); - await (GlobalContext.getContext().getObject("adminProvisioningContext") as common.UIAbilityContext).terminateSelf(); + await (getContext(this) as common.UIAbilityContext).terminateSelf(); } getPermissionInfoVal(permissionName: string): number { diff --git a/entry/src/main/ets/pages/applicationInfo.ets b/entry/src/main/ets/pages/applicationInfo.ets index 35bd5ee..fb9b3a6 100644 --- a/entry/src/main/ets/pages/applicationInfo.ets +++ b/entry/src/main/ets/pages/applicationInfo.ets @@ -26,7 +26,7 @@ import { AppPermission, MyApplicationInfo } from '../common/myApplicationInfo' import Want from '@ohos.app.ability.Want'; import adminManager from '@ohos.enterprise.adminManager'; import { BusinessError } from '@ohos.base'; -import { GlobalContext } from '../MainAbility/MainAbility'; +import common from '@ohos.app.ability.common'; let appInfo: MyApplicationInfo; let elementNameVal: Want; @@ -51,6 +51,7 @@ struct ApplicationInfo { @State deviceAdminDisActive: string = ''; @State enterpriseName: string = ''; @State enterpriseDescription: string = ''; + private storage = LocalStorage.getShared(); build() { Column() { @@ -116,7 +117,7 @@ struct ApplicationInfo { }); List() { - ForEach(appInfo.appPermissionList, (item: AppPermission) => { + ForEach(this.applicationInfo.appPermissionList, (item: AppPermission) => { ListItem() { permissionListComponent({ permissionName: item.permissionLabel, @@ -208,14 +209,7 @@ struct ApplicationInfo { .backgroundColor($r('sys.color.ohos_id_color_sub_background')) } - aboutToAppear(): void { - appInfo = { - appIcon: '', - appTitle: '', - appBundleName: '', - appPermissionList: [], - }; - + async aboutToAppear(): Promise { elementNameVal = { abilityName: '', bundleName: '', @@ -228,7 +222,7 @@ struct ApplicationInfo { isAdminTypeVal = 0; logger.info(TAG, 'aboutToAppear in'); - this.getCheckAbilityList(appInfo, elementNameVal, isAdminTypeVal); + await this.getCheckAbilityList(this.applicationInfo, elementNameVal, isAdminTypeVal); logger.info(TAG, 'aboutToAppear out'); } @@ -340,7 +334,8 @@ struct ApplicationInfo { async getAbilityWantVal(appInfo: MyApplicationInfo, elementNameVal: Want, adminType: adminManager.AdminType) { logger.info(TAG, 'getAbilityWantVal in:'); - let data = GlobalContext.getContext().getObject("adminProvisioningWant") as Want; + this.storage = LocalStorage.getShared(); + let data = this.storage.get('adminProvisioningWant'); if (!utils.checkObjPropertyValid(data, 'parameters.elementName.abilityName') || !utils.checkObjPropertyValid(data, 'parameters.enterprise.name') @@ -348,6 +343,7 @@ struct ApplicationInfo { || !utils.isValid(data.parameters?.activeType) || !utils.isValid((data.parameters?.enterprise as Record).description)) { logger.warn(TAG, 'data.parameters = ' + JSON.stringify(data.parameters)); + (getContext(this) as common.UIAbilityContext).terminateSelf(); return; } @@ -387,13 +383,10 @@ struct ApplicationInfo { await appDetailData.getBundleInfoItem(appInfo.appBundleName, appInfo); } - deviceActiveOne = await this.getSelfResourceVal($r('app.string.device_active_1')); - deviceActiveTwo = await this.getSelfResourceVal($r('app.string.device_active_2')); - deviceDeactivate = await this.getSelfResourceVal($r('app.string.device_deactivate')); enterName = await this.getSelfResourceVal($r('app.string.enterprise_name')); enterDescription = await this.getSelfResourceVal($r('app.string.enterprise_description')); - this.deviceAdminActive = deviceActiveOne + `${appInfo.appTitle}` + deviceActiveTwo; - this.deviceAdminDisActive = deviceDeactivate + `${appInfo.appTitle}` + deviceActiveTwo; + this.deviceAdminActive = getContext().resourceManager.getStringSync($r('app.string.textApplicationInfoActive').id, `${appInfo.appTitle}`); + this.deviceAdminDisActive = getContext().resourceManager.getStringSync($r('app.string.textApplicationInfoInactive').id, `${appInfo.appTitle}`); let want: Want = { bundleName: elementNameVal.bundleName, diff --git a/entry/src/main/ets/pages/autoManager/managerStart.ets b/entry/src/main/ets/pages/autoManager/managerStart.ets index 180f413..4ebdce1 100644 --- a/entry/src/main/ets/pages/autoManager/managerStart.ets +++ b/entry/src/main/ets/pages/autoManager/managerStart.ets @@ -17,8 +17,8 @@ import router from '@ohos.router'; import utils from '../../common/utils' import doubleButtonComponent from '../component/autoManager/doubleButtonComponent' import logger from '../../common/logger' -import { GlobalContext } from '../../MainAbility/MainAbility'; import Want from '@ohos.app.ability.Want'; +import common from '@ohos.app.ability.common'; const TAG = 'ManagerStart'; @@ -31,6 +31,7 @@ struct ManagerStart { @State textManagerAfter: Resource = $r('app.string.textManagerAfter'); @State textTerms: Resource = $r('app.string.textTerms'); @State textTermsBefore: Resource = $r('app.string.textTermsBefore'); + private storage = LocalStorage.getShared(); build() { Column() { @@ -176,7 +177,8 @@ struct ManagerStart { } parseManageParameter() { - let data = GlobalContext.getContext().getObject('autoManagerAbilityWant') as Want; + this.storage = LocalStorage.getShared(); + let data = this.storage.get('autoManagerAbilityWant'); if (!utils.checkObjPropertyValid(data, 'parameters.elementName.abilityName') || !utils.checkObjPropertyValid(data, 'parameters.enterprise.name') @@ -185,7 +187,8 @@ struct ManagerStart { || !utils.isValid(data.parameters?.termsName) || (!utils.isValid(data.parameters?.termsContent) || !(data.parameters?.termsContent instanceof Array)) || !utils.isValid((data.parameters?.enterprise as Record).description)) { - logger.error(TAG, 'parseManageParameter fail! parameters is not valid') + logger.error(TAG, 'parseManageParameter fail! parameters is not valid'); + (getContext(this) as common.UIAbilityContext).terminateSelf(); return; } diff --git a/entry/src/main/ets/pages/autoManager/setFinishSuccess.ets b/entry/src/main/ets/pages/autoManager/setFinishSuccess.ets index 36cb42b..4323ad8 100644 --- a/entry/src/main/ets/pages/autoManager/setFinishSuccess.ets +++ b/entry/src/main/ets/pages/autoManager/setFinishSuccess.ets @@ -21,9 +21,20 @@ import utils from '../../common/utils' @Component struct SetFinishSuccess { @State textContext: Resource = $r('app.string.setupComplete'); - @State textManager: Resource = $r('app.string.unitManage'); - @State textManagerBefore: Resource = $r('app.string.textManagerBeforeFinish'); - @State textManagerAfter: Resource = $r('app.string.textManagerAfter'); + @State textManager: string = getContext().resourceManager.getStringSync($r('app.string.unitManage').id); + private textContent: string = getContext().resourceManager.getStringSync($r('app.string.textManageSuccessContent').id, this.textManager); + private getTextList: (source: string, target: string) => Array = (source: string, target: string) => { + let result: Array = new Array(); + if (source) { + let position: number = source.indexOf(target); + let before: string = source.substring(0, position); + let after: string = source.substring(position + target.length); + result.push(before); + result.push(target); + result.push(after); + } + return result; + } build() { Column() { @@ -58,26 +69,19 @@ struct SetFinishSuccess { Column() { Row() { Text() { - Span(this.textManagerBefore) - .fontWeight(FontWeight.Regular) - .fontSize($r('app.float.font_vp_16')) - .fontFamily('HarmonyHeiTi') - - Span(this.textManager) - .decoration({ type: TextDecorationType.None }) - .fontColor(0x007DFF) - .fontSize($r('app.float.font_vp_16')) - .fontFamily('HarmonyHeiTi') - .fontWeight(FontWeight.Regular) - .onClick(() => { - router.pushUrl({url: 'pages/autoManager/unitManagerShowPage'}) - }) - - Span(this.textManagerAfter) - .fontWeight(FontWeight.Regular) - .fontSize($r('app.float.font_vp_16')) - .fontFamily('HarmonyHeiTi') + ForEach(this.getTextList(this.textContent, this.textManager), (item: string) => { + Span(item) + .fontColor(item === this.textManager ? 0x007DFF: 0x000000) + .onClick(() => { + if (item === this.textManager) { + router.pushUrl({url: 'pages/autoManager/unitManagerShowPage'}) + } + }) + }) } + .fontWeight(FontWeight.Regular) + .fontSize($r('app.float.font_vp_16')) + .fontFamily('HarmonyHeiTi') .lineHeight($r('app.float.lineHeight_vp_21_5')) } .useSizeType({ diff --git a/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets b/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets index e061ed0..a134255 100644 --- a/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets +++ b/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets @@ -14,13 +14,11 @@ */ import bundle from '@ohos.bundle.bundleManager'; -import router from '@ohos.router'; import baseData from '../../../common/baseData' import logger from '../../../common/logger' import resetFactory from '../../../common/resetFactory' import utils from '../../../common/utils' import Want from '@ohos.app.ability.Want'; -import { GlobalContext } from '../../../MainAbility/MainAbility'; import common from '@ohos.app.ability.common'; const TAG = 'DoubleButtonComponent'; @@ -58,7 +56,7 @@ export default struct DoubleButtonComponent { if (this.returnDialogFlag) { this.buttonDialog.open(); } else { - router.back(); + (getContext(this) as common.UIAbilityContext).terminateSelf(); } }) } @@ -111,8 +109,7 @@ export default struct DoubleButtonComponent { logger.info(TAG, 'startSpecificActionAbility data len=' + data.length + ' | data content=' + data[0].name) try { - await (GlobalContext.getContext() - .getObject("autoManagerAbilityContext") as common.UIAbilityContext).startAbility({ + await (getContext(this) as common.UIAbilityContext).startAbility({ bundleName: this.nextBundleName, abilityName: data[0].name }); @@ -120,8 +117,7 @@ export default struct DoubleButtonComponent { logger.error(TAG, "startSpecificActionAbility startAbility fail! " + e) } } - await (GlobalContext.getContext() - .getObject("autoManagerAbilityContext") as common.UIAbilityContext).terminateSelf(); + await (getContext(this) as common.UIAbilityContext).terminateSelf(); } } diff --git a/entry/src/main/ets/pages/component/headComponent.ets b/entry/src/main/ets/pages/component/headComponent.ets index 57af216..2d4f724 100644 --- a/entry/src/main/ets/pages/component/headComponent.ets +++ b/entry/src/main/ets/pages/component/headComponent.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import router from '@ohos.router'; +import common from '@ohos.app.ability.common'; @Component export default struct HeadComponent { @@ -32,7 +32,7 @@ export default struct HeadComponent { .backgroundColor($r('app.color.color_00000000_transparent')) .visibility(Visibility.Visible) .onClick(() => { - router.back(); + (getContext(this) as common.UIAbilityContext).terminateSelf(); }) Text(this.headName) .height($r('app.float.wh_value_56')) diff --git a/entry/src/main/module.json b/entry/src/main/module.json index 27d2995..8ff0f32 100644 --- a/entry/src/main/module.json +++ b/entry/src/main/module.json @@ -65,7 +65,7 @@ "name": "ohos.permission.MANAGE_LOCAL_ACCOUNTS" }, { - "name": "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED" + "name": "ohos.permission.GET_BUNDLE_INFO" }, { "name": "ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN" diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index 58397e8..a4d785a 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -65,7 +65,7 @@ "name": "ohos.permission.MANAGE_LOCAL_ACCOUNTS" }, { - "name": "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED" + "name": "ohos.permission.GET_BUNDLE_INFO" }, { "name": "ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN" diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index 739703a..aefbf3c 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -162,23 +162,23 @@ }, { "name": "textManageSuccessContentVal1", - "value": "%1$s" + "value": "%s" }, { "name": "textManageSuccessContentVal2", - "value": "%2$s" + "value": "%s" }, { "name": "textManageSuccessContent", - "value": "如有疑问,请与您的%1$s联系。" + "value": "如有疑问,请与您的%s联系。" }, { "name": "textApplicationInfoActive", - "value": "此管理器已激活,允许应用\"%1$s\"执行以下操作:" + "value": "此管理器已激活,允许应用\"%s\"执行以下操作:" }, { "name": "textApplicationInfoInactive", - "value": "激活此管理器可允许应用\"%1$s\"执行以下操作:" + "value": "激活此管理器可允许应用\"%s\"执行以下操作:" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index eb8c58d..123b902 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -162,23 +162,23 @@ }, { "name": "textManageSuccessContentVal1", - "value": "%1$s" + "value": "%s" }, { "name": "textManageSuccessContentVal2", - "value": "%2$s" + "value": "%s" }, { "name": "textManageSuccessContent", - "value": "if you have any questions, please contact your %1$s contact." + "value": "if you have any questions, please contact your %s contact." }, { "name": "textApplicationInfoActive", - "value": "This device administrator is currently active and allows \"%1$s\" to perform the following operations:" + "value": "This device administrator is currently active and allows \"%s\" to perform the following operations:" }, { "name": "textApplicationInfoInactive", - "value": "Activating this administrator will allow \"%1$s\" to perform the following operations:" + "value": "Activating this administrator will allow \"%s\" to perform the following operations:" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 641f1fe..abab789 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -162,23 +162,23 @@ }, { "name": "textManageSuccessContentVal1", - "value": "%1$s" + "value": "%s" }, { "name": "textManageSuccessContentVal2", - "value": "%2$s" + "value": "%s" }, { "name": "textManageSuccessContent", - "value": "如有疑问,请与您的%1$s联系。" + "value": "如有疑问,请与您的%s联系。" }, { "name": "textApplicationInfoActive", - "value": "此管理器已激活,允许应用\"%1$s\"执行以下操作:" + "value": "此管理器已激活,允许应用\"%s\"执行以下操作:" }, { "name": "textApplicationInfoInactive", - "value": "激活此管理器可允许应用\"%1$s\"执行以下操作:" + "value": "激活此管理器可允许应用\"%s\"执行以下操作:" } ] } \ No newline at end of file -- Gitee