From 2df6042557d8299103ce5a8bf11f74fb2378bac7 Mon Sep 17 00:00:00 2001 From: liwuli Date: Mon, 6 Nov 2023 21:21:27 +0800 Subject: [PATCH] mdm depoly Signed-off-by: liwuli --- build-profile.json5 | 10 +- .../ets/MainAbility/AutoManagerAbility.ts | 5 +- entry/src/main/ets/MainAbility/MainAbility.ts | 25 +++- .../ets/MainAbility/UIExtensionAbility.ets | 47 +++++++ entry/src/main/ets/common/accountManager.ets | 14 +- .../common/appManagement/appDetailData.ets | 69 ++++++---- .../src/main/ets/common/myApplicationInfo.ets | 2 +- entry/src/main/ets/common/resetFactory.ets | 3 +- entry/src/main/ets/common/utils.ts | 2 +- entry/src/main/ets/pages/applicationInfo.ets | 86 ++++++------ .../main/ets/pages/autoDeploy/MdmDeploy.ets | 123 ++++++++++++++++++ .../ets/pages/autoManager/loadingInfo.ets | 16 ++- .../ets/pages/autoManager/managerStart.ets | 32 ++--- .../ets/pages/autoManager/termsShowPage.ets | 4 +- .../pages/autoManager/unitManagerShowPage.ets | 40 +++--- .../autoManager/doubleButtonComponent.ets | 29 +++-- .../autoManager/termsListComponent.ets | 4 +- .../ets/pages/component/entryComponent.ets | 4 +- .../component/permissionListComponent.ets | 6 +- entry/src/main/module.json5 | 14 +- .../main/resources/base/element/float.json | 12 ++ .../main/resources/base/element/string.json | 12 ++ .../main/resources/base/media/mdm_start.svg | 12 ++ .../resources/base/profile/main_pages.json | 1 + .../main/resources/en_US/element/string.json | 12 ++ .../main/resources/zh_CN/element/string.json | 12 ++ 26 files changed, 453 insertions(+), 143 deletions(-) create mode 100644 entry/src/main/ets/MainAbility/UIExtensionAbility.ets create mode 100644 entry/src/main/ets/pages/autoDeploy/MdmDeploy.ets create mode 100644 entry/src/main/resources/base/media/mdm_start.svg diff --git a/build-profile.json5 b/build-profile.json5 index 672f1a4..5a0ef31 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,12 +1,18 @@ { "app": { "signingConfigs": [], - "compileSdkVersion": 9, - "compatibleSdkVersion": 9, "products": [ { "name": "default", "signingConfig": "default", + "compileSdkVersion": 11, + //指定OpenHarmony应用/服务编译时的版本 + "compatibleSdkVersion": 11, + //指定OpenHarmony应用/服务兼容的最低版本。 + "targetSdkVersion": 11, + //指定OpenHarmony应用/服务目标版本。若没有设置,默认为compatibleSdkVersion + "runtimeOS": "OpenHarmony", + //指定为OpenHarmony } ] }, diff --git a/entry/src/main/ets/MainAbility/AutoManagerAbility.ts b/entry/src/main/ets/MainAbility/AutoManagerAbility.ts index 8b0a870..5313522 100644 --- a/entry/src/main/ets/MainAbility/AutoManagerAbility.ts +++ b/entry/src/main/ets/MainAbility/AutoManagerAbility.ts @@ -18,14 +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/MainAbility'; const TAG = 'AutoManagerAbility'; export default class AutoManagerAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { logger.info(TAG, 'onCreate'); - globalThis.autoManagerAbilityWant = want; - globalThis.autoManagerAbilityContext = this.context; + GlobalContext.getContext().setObject("autoManagerAbilityWant", want); + GlobalContext.getContext().setObject("autoManagerAbilityContext", this.context); } onDestroy(): void { diff --git a/entry/src/main/ets/MainAbility/MainAbility.ts b/entry/src/main/ets/MainAbility/MainAbility.ts index acbf4f2..b9d2167 100644 --- a/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/entry/src/main/ets/MainAbility/MainAbility.ts @@ -18,11 +18,32 @@ import Logger from '../common/logger'; const TAG = 'MainAbility'; +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); + } +} + export default class MainAbility extends UIAbility { onCreate(want, launchParam): void { Logger.info(TAG, 'onCreate'); - globalThis.adminProvisioningWant = want; - globalThis.adminProvisioningContext = this.context; + GlobalContext.getContext().setObject('adminProvisioningWant', want); + GlobalContext.getContext().setObject('adminProvisioningContext', this.context); } onDestroy(): void { diff --git a/entry/src/main/ets/MainAbility/UIExtensionAbility.ets b/entry/src/main/ets/MainAbility/UIExtensionAbility.ets new file mode 100644 index 0000000..5e72a8a --- /dev/null +++ b/entry/src/main/ets/MainAbility/UIExtensionAbility.ets @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; +import logger from '../common/logger'; +import Want from '@ohos.app.ability.Want'; +import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; + +const TAG = 'AutoDeployAbility'; + +export default class AutoDeployAbility extends UIExtensionAbility { + private localStorage: LocalStorage = new LocalStorage(); + + onSessionCreate(want: Want, session: UIExtensionContentSession): void { + logger.info(TAG, 'onSessionCreate'); + this.localStorage.setOrCreate('abilityName', 'com.mdm.testAbility'); + this.localStorage.setOrCreate('bundleName', 'com.mdm.testBundle'); + this.localStorage.setOrCreate('session', session); + session.loadContent('pages/autoDeploy/index', this.localStorage); + } + + onDestroy(): void { + logger.info(TAG, 'onDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + logger.info(TAG, 'onForeground'); + } + + onBackground(): void { + // Ability has back to background + logger.info(TAG, 'onBackground'); + } +}; diff --git a/entry/src/main/ets/common/accountManager.ets b/entry/src/main/ets/common/accountManager.ets index 16aa936..09b259f 100644 --- a/entry/src/main/ets/common/accountManager.ets +++ b/entry/src/main/ets/common/accountManager.ets @@ -19,15 +19,19 @@ import utils from './utils' const TAG = 'AccountManager'; +export interface UserId { + localId: number; +} + export class AccountManager { - async getAccountUserId(accountInfo) : Promise { - let userId = await account_osAccount.getAccountManager().queryCurrentOsAccount(); - if (!utils.isValid(userId)) { + async getAccountUserId(userId: UserId) : Promise { + let accountInfo = await account_osAccount.getAccountManager().queryCurrentOsAccount(); + if (!utils.isValid(accountInfo)) { logger.warn(TAG, 'getAccountUserId queryCurrentOsAccount fail! userId is null'); return false; } - accountInfo.localId = userId.localId; - logger.info(TAG, 'getAccountUserId userId.localId=' + userId.localId); + userId.localId = accountInfo.localId; + logger.info(TAG, 'getAccountUserId accountInfo.localId=' + accountInfo.localId); return true; } } diff --git a/entry/src/main/ets/common/appManagement/appDetailData.ets b/entry/src/main/ets/common/appManagement/appDetailData.ets index fc07305..186f180 100644 --- a/entry/src/main/ets/common/appManagement/appDetailData.ets +++ b/entry/src/main/ets/common/appManagement/appDetailData.ets @@ -14,59 +14,65 @@ */ import bundle from '@ohos.bundle.bundleManager'; -import accountManager from '../accountManager'; +import accountManager, { UserId } from '../accountManager'; import baseData from '../baseData'; import logger from '../logger' import utils from '../utils' -import { AppPermission } from '../myApplicationInfo' +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'); const TAG = 'AppDetailData'; const permissionsDetailList: Array = [ { - "permissionName": 'ohos.permission.ENTERPRISE_SET_DATETIME', - "permissionLabel": $r('app.string.permissionLabel'), - "permissionDescription": $r('app.string.permissionDescription') - }, + permissionName: 'ohos.permission.ENTERPRISE_SET_DATETIME', + permissionLabel: $r('app.string.permissionLabel'), + permissionDescription: $r('app.string.permissionDescription') + } as AppPermission, ]; export class AppDetailData { - public ret = { val: true }; + public ret: boolean = true; - async checkAppItem(elementNameVal) { + async checkAppItem(elementNameVal: Want) { logger.info(TAG, 'checkAppItem in bundleName:' + elementNameVal.bundleName + ' | abilityName:' + elementNameVal.abilityName); - let want = { + let want: Want = { "bundleName": elementNameVal.bundleName, "abilityName": elementNameVal.abilityName }; - let userId = { localId : 0 }; + let userId: UserId = {localId: 0}; let retVal = await accountManager.getAccountUserId(userId); if (!retVal) { logger.warn(TAG, 'checkAppItem getAccountUserId fail!'); - this.ret.val = false; + this.ret = false; return this.ret; } - let data; + let data: Array = []; try { - data = await bundle.queryExtensionAbilityInfo(want, bundle.ExtensionAbilityType.ENTERPRISE_ADMIN, - bundle.ExtensionAbilityFlag.GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION, userId.localId); + bundle.queryExtensionAbilityInfo(want, bundle.ExtensionAbilityType.ENTERPRISE_ADMIN, + bundle.ExtensionAbilityFlag.GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION, userId.localId).then((result)=>{ + data = result; + }) } catch (e) { logger.error(TAG, 'checkAppItem queryExtensionAbilityInfo try fail! ' + JSON.stringify(e)); } if (!utils.isValid(data) || data.length <= 0) { logger.warn(TAG, 'checkAppItem queryExtensionAbilityInfo fail! data is null'); - this.ret.val = false; + this.ret = false; return this.ret; } logger.info(TAG, 'checkAppItem success'); return this.ret; } - async getBundleInfoItem(bundleName: string, appInfo) { + async getBundleInfoItem(bundleName: string, appInfo: MyApplicationInfo) { logger.info(TAG, 'getBundleInfoItem in bundleName:' + bundleName); - let userId = { localId : 0 }; + let userId: UserId = {localId: 0}; let retVal = await accountManager.getAccountUserId(userId); if (!retVal) { logger.warn(TAG, 'getBundleInfoItem getAccountUserId fail!'); @@ -79,7 +85,7 @@ export class AppDetailData { logger.info(TAG, 'getBundleInfoItem out'); } - async getAppInfoLabel(resMgr, id) { + async getAppInfoLabel(resMgr: resourceManager.ResourceManager, id: number) { let label = await resMgr.getString(id); logger.info(TAG, 'getAppInfoLabel start label:' + label); if (!utils.isValid(label) || label === baseData.EMPTY_STR) { @@ -88,7 +94,7 @@ export class AppDetailData { return label; } - async getAppInfoIcon(resMgr, id) { + async getAppInfoIcon(resMgr: resourceManager.ResourceManager, id: number) { let iconVal = await resMgr.getMediaBase64(id); logger.info(TAG, 'getAppInfoIcon start iconVal:' + iconVal); if (!utils.isValid(iconVal) || iconVal === baseData.EMPTY_STR) { @@ -97,10 +103,10 @@ export class AppDetailData { return iconVal; } - async getResourceItem(bundleName, data, appInfo) { + async getResourceItem(bundleName: string, data: bundle.BundleInfo, appInfo: MyApplicationInfo) { logger.info(TAG, 'getResourceItem getResourceManager in'); - let bundleContext = await globalThis.adminProvisioningContext.createBundleContext(bundleName); - let resMgr = await bundleContext.resourceManager; + let bundleContext = getContext().createBundleContext(bundleName); + let resMgr = bundleContext.resourceManager; let appInfoTemp = data.appInfo; let label = ''; let iconVal = ''; @@ -116,7 +122,14 @@ export class AppDetailData { if (appInfoTemp.iconId > 0) { iconVal = await this.getAppInfoIcon(resMgr, appInfoTemp.iconId); if (iconVal === baseData.EMPTY_STR) { - appInfo.appIcon = icon_default; + let icon: resourceManager.Resource = { + bundleName: icon_default.bundleName, + moduleName: icon_default.moduleName, + id: icon_default.id + } + resMgr.getMediaContentBase64(icon).then((data) => { + appInfo.appIcon = data; + }) appInfo.appTitle = label; } appInfo.appIcon = iconVal; @@ -128,10 +141,10 @@ export class AppDetailData { async terminateAbilityPage() { logger.info(TAG, 'terminateAbilityPage in:'); - await globalThis.adminProvisioningContext.terminateSelf(); + await (GlobalContext.getContext().getObject("adminProvisioningContext") as common.UIAbilityContext).terminateSelf(); } - getPermissionInfoVal(permissionName): number { + getPermissionInfoVal(permissionName: string): number { for (let i = 0; i < permissionsDetailList.length; i++) { if (permissionsDetailList[i].permissionName === permissionName) { return i; @@ -140,11 +153,11 @@ export class AppDetailData { return -1; } - async getPermissionList(data, appInfo) { + async getPermissionList(data: bundle.BundleInfo, appInfo: MyApplicationInfo) { let permissions = data.reqPermissionDetails; - if (permissions !== null && permissions !== []) { + if (permissions !== null && permissions.length !== 0) { logger.info(TAG, 'getPermissionList permission length:' + permissions.length); - for (var i = 0; i < permissions.length; i++) { + for (let i = 0; i < permissions.length; i++) { logger.info(TAG, 'getPermissionList permission is in val:' + permissions[i].name); let j = this.getPermissionInfoVal(permissions[i].name); if (j >= 0) { diff --git a/entry/src/main/ets/common/myApplicationInfo.ets b/entry/src/main/ets/common/myApplicationInfo.ets index 54c06a5..78f7e3e 100644 --- a/entry/src/main/ets/common/myApplicationInfo.ets +++ b/entry/src/main/ets/common/myApplicationInfo.ets @@ -24,4 +24,4 @@ export interface AppPermission { permissionName: string, permissionLabel: Resource, permissionDescription: Resource -} \ No newline at end of file +} diff --git a/entry/src/main/ets/common/resetFactory.ets b/entry/src/main/ets/common/resetFactory.ets index 339efa4..e1658e6 100644 --- a/entry/src/main/ets/common/resetFactory.ets +++ b/entry/src/main/ets/common/resetFactory.ets @@ -15,6 +15,7 @@ import update from '@ohos.update'; import logger from './logger' +import { BusinessError } from '@ohos.base'; const TAG = 'ResetFactory'; @@ -23,7 +24,7 @@ export class ResetFactory { let restorer = update.getRestorer(); restorer.factoryReset().then(() => { logger.info(TAG, 'rebootAndCleanUserData factoryReset success') - }).catch((err) => { + }).catch((err: BusinessError) => { logger.error(TAG, 'rebootAndCleanUserData err=' + JSON.stringify(err)) }) } diff --git a/entry/src/main/ets/common/utils.ts b/entry/src/main/ets/common/utils.ts index 318cdd3..e7bb325 100644 --- a/entry/src/main/ets/common/utils.ts +++ b/entry/src/main/ets/common/utils.ts @@ -22,7 +22,7 @@ class Utils { return item !== null && item !== undefined; } - checkObjPropertyValid(obj: unknown, tree: string): boolean { + checkObjPropertyValid(obj: T, tree: string): boolean { if (!this.isValid(obj) || !this.isValid(tree)) { return false; } diff --git a/entry/src/main/ets/pages/applicationInfo.ets b/entry/src/main/ets/pages/applicationInfo.ets index cd3fb8a..87ebad9 100644 --- a/entry/src/main/ets/pages/applicationInfo.ets +++ b/entry/src/main/ets/pages/applicationInfo.ets @@ -22,23 +22,27 @@ import headComponent from './component/headComponent'; import logger from '../common/logger' import permissionListComponent from './component/permissionListComponent'; import utils from '../common/utils' -import { MyApplicationInfo } from '../common/myApplicationInfo' - -let appInfo; -let elementNameVal; -let isAdminTypeVal; -let isActiveState; -let enterInfo; +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'; + +let appInfo: MyApplicationInfo; +let elementNameVal: Want; +let isAdminTypeVal: adminManager.AdminType; +let isActiveState: boolean; +let enterInfo: adminManager.EnterpriseInfo; const TAG = 'ApplicationInfo'; @Entry @Component struct ApplicationInfo { @StorageLink('applicationInfo') applicationInfo: MyApplicationInfo = { - "appIcon": '', - "appTitle": '', - "appBundleName": '', - "appPermissionList": [] + appIcon: '', + appTitle: '', + appBundleName: '', + appPermissionList: [] }; @State isShowActive: boolean = false; @State isEnableButton: boolean = false; @@ -112,14 +116,14 @@ struct ApplicationInfo { }); List() { - ForEach(appInfo.appPermissionList, (item) => { + ForEach(appInfo.appPermissionList, (item: AppPermission) => { ListItem() { permissionListComponent({ permissionName: item.permissionLabel, permissionDescription: item.permissionDescription, }); } - }, item => JSON.stringify(item)); + }, (item: AppPermission) => JSON.stringify(item)); } } .alignItems(HorizontalAlign.Start) @@ -222,7 +226,7 @@ struct ApplicationInfo { description: '', }; - isAdminTypeVal = { admintype: 0 }; + isAdminTypeVal = 0; logger.info(TAG, 'aboutToAppear in'); this.getCheckAbilityList(appInfo, elementNameVal, isAdminTypeVal); logger.info(TAG, 'aboutToAppear out'); @@ -253,12 +257,12 @@ struct ApplicationInfo { } async isAdminActive() { - let wantTemp = { + let wantTemp: Want = { bundleName: elementNameVal.bundleName, abilityName: elementNameVal.abilityName, }; - let retAppState; - let retSuperState; + let retAppState: boolean; + let retSuperState: boolean; logger.info(TAG, 'isAdminActive elementNameVal.bundleName=' + elementNameVal.bundleName + ' | elementNameVal.abilityName=' + elementNameVal.abilityName); if (elementNameVal.bundleName === baseData.EMPTY_STR || elementNameVal.abilityName === baseData.EMPTY_STR) { @@ -267,7 +271,7 @@ struct ApplicationInfo { } retAppState = await edmEnterpriseDeviceManager.isAdminEnabled(wantTemp); - retSuperState = await edmEnterpriseDeviceManager.isSuperAdmin(elementNameVal.bundleName); + retSuperState = await edmEnterpriseDeviceManager.isSuperAdmin((elementNameVal.bundleName) as string); if (!retAppState) { this.isShowActive = false; this.isEnableButton = true; @@ -284,9 +288,9 @@ struct ApplicationInfo { isActiveState = this.isShowActive; } - async activateAdmin(adminType) { + async activateAdmin(adminType: adminManager.AdminType) { logger.info(TAG, 'activateAdmin isShowActive:' + this.isShowActive); - let wantTemp = { + let wantTemp: Want = { bundleName: elementNameVal.bundleName, abilityName: elementNameVal.abilityName, }; @@ -297,7 +301,7 @@ struct ApplicationInfo { await edmEnterpriseDeviceManager.enableAdmin(wantTemp, { name: enterInfo.name, description: enterInfo.description }, edmEnterpriseDeviceManager.AdminType.ADMIN_TYPE_NORMAL) - .catch((error) => { + .catch((error: BusinessError) => { ret = false; logger.info(TAG, 'errorCode : ' + error.code + 'errorMessage : ' + error.message); }); @@ -308,7 +312,7 @@ struct ApplicationInfo { } else { if (this.isEnableButton) { await edmEnterpriseDeviceManager.disableAdmin(wantTemp) - .catch((error) => { + .catch((error: BusinessError) => { ret = false; logger.info(TAG, 'errorCode : ' + error.code + 'errorMessage : ' + error.message); }); @@ -322,8 +326,8 @@ struct ApplicationInfo { } } - async getSelfResourceVal(resource) { - let resMgr = await globalThis.adminProvisioningContext.resourceManager; + async getSelfResourceVal(resource: Resource) { + let resMgr = getContext().resourceManager; logger.info(TAG, 'getSelfResourceVal in:'); let value = await resMgr.getString(resource.id); logger.info(TAG, 'getSelfResourceVal finish value:' + value); @@ -333,32 +337,32 @@ struct ApplicationInfo { return value; } - async getAbilityWantVal(appInfo, elementNameVal, adminType) { + async getAbilityWantVal(appInfo: MyApplicationInfo, elementNameVal: Want, adminType: adminManager.AdminType) { logger.info(TAG, 'getAbilityWantVal in:'); - let data = await globalThis.adminProvisioningWant; + let data = GlobalContext.getContext().getObject("adminProvisioningWant") as Want; if (!utils.checkObjPropertyValid(data, 'parameters.elementName.abilityName') || !utils.checkObjPropertyValid(data, 'parameters.enterprise.name') - || !utils.isValid(data.parameters.elementName.bundleName) - || !utils.isValid(data.parameters.activeType) - || !utils.isValid(data.parameters.enterprise.description)) { + || !utils.isValid((data.parameters?.elementName as Record).bundleName) + || !utils.isValid(data.parameters?.activeType) + || !utils.isValid((data.parameters?.enterprise as Record).description)) { logger.warn(TAG, 'data.parameters = ' + JSON.stringify(data.parameters)); return; } - elementNameVal.abilityName = data.parameters.elementName.abilityName; - elementNameVal.bundleName = data.parameters.elementName.bundleName; - appInfo.appBundleName = data.parameters.elementName.bundleName; - adminType.admintype = data.parameters.activeType; - AppStorage.SetOrCreate('activeType', adminType.admintype); + elementNameVal.abilityName = (data.parameters?.elementName as Record).abilityName; + elementNameVal.bundleName = (data.parameters?.elementName as Record).bundleName; + appInfo.appBundleName = (data.parameters?.elementName as Record).bundleName; + adminType = data.parameters?.activeType as adminManager.AdminType; + AppStorage.SetOrCreate('activeType', adminType); - enterInfo.name = data.parameters.enterprise.name.substring(0, baseData.MAX_LEN); - enterInfo.description = data.parameters.enterprise.description.substring(0, baseData.MAX_LEN); + enterInfo.name = (data.parameters?.enterprise as Record).name.substring(0, baseData.MAX_LEN); + enterInfo.description = (data.parameters?.enterprise as Record).description.substring(0, baseData.MAX_LEN); logger.info(TAG, 'getAbilityWantVal out isAdminTypeVal.admintype:' - + isAdminTypeVal.admintype + ' enter.name=' + enterInfo.name + ' enter.des=' + enterInfo.description); + + isAdminTypeVal + ' enter.name=' + enterInfo.name + ' enter.des=' + enterInfo.description); } - async getCheckAbilityList(appInfo, elementNameVal, adminType) { + async getCheckAbilityList(appInfo: MyApplicationInfo, elementNameVal: Want, adminType: adminManager.AdminType) { let deviceActiveOne = ''; let deviceActiveTwo = ''; let deviceDeactivate = ''; @@ -370,7 +374,7 @@ struct ApplicationInfo { await this.getAbilityWantVal(appInfo, elementNameVal, adminType); let ret = await appDetailData.checkAppItem(elementNameVal); - if (!ret.val) { + if (!ret) { logger.info(TAG, 'aboutToAppear not exist bundleName:' + appInfo.appBundleName); appDetailData.terminateAbilityPage(); return; @@ -390,11 +394,11 @@ struct ApplicationInfo { this.deviceAdminActive = deviceActiveOne + `${appInfo.appTitle}` + deviceActiveTwo; this.deviceAdminDisActive = deviceDeactivate + `${appInfo.appTitle}` + deviceActiveTwo; - let want = { + let want: Want = { bundleName: elementNameVal.bundleName, abilityName: elementNameVal.abilityName, }; - let getEnterInfo = { + let getEnterInfo: adminManager.EnterpriseInfo = { name: '', description: '', }; diff --git a/entry/src/main/ets/pages/autoDeploy/MdmDeploy.ets b/entry/src/main/ets/pages/autoDeploy/MdmDeploy.ets new file mode 100644 index 0000000..a61cedb --- /dev/null +++ b/entry/src/main/ets/pages/autoDeploy/MdmDeploy.ets @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import adminManager from '@ohos.enterprise.adminManager'; +import Want from '@ohos.app.ability.Want'; +import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; +import { BusinessError } from '@ohos.base'; + +let title: Resource = $r('app.string.unitManageDevice'); +let autoDeployText: Resource = $r('app.string.AutoDeployText'); + +let storage = LocalStorage.getShared(); +let ability: string | undefined = storage.get('abilityName'); +let bundle: string | undefined = storage.get('bundleName'); + +@Entry +@Component +struct MdmDeploy { + private session: UIExtensionContentSession | undefined = storage.get('session'); + build() { + Column() { + Image($r('app.media.mdm_start')) + .width($r('app.float.wh_value_48')) + .height($r('app.float.wh_value_48')) + .margin({ top: '72vp', bottom: '16vp' }) + .objectFit(ImageFit.Contain) + + Text(title) + .height('40vp') + .textAlign(TextAlign.Center) + .fontWeight(FontWeight.Medium) + .lineHeight($r('app.float.lineHeight_vp_35')) + .fontSize($r('app.float.font_vp_30')) + .fontFamily('HarmonyHeiTi') + + Text(autoDeployText) + .height('25vp') + .textAlign(TextAlign.Center) + .fontWeight(FontWeight.Medium) + .lineHeight($r('app.float.lineHeight_vp_25')) + .fontSize($r('app.float.font_vp_18')) + .fontFamily('HarmonyHeiTi') + .margin({top: '80vp'}) + + Row() { + Button() { + Text($r('app.string.PreviousStep')) + .fontSize($r('app.float.font_vp_16')) + .fontFamily('HarmonyHeiTi') + .fontWeight(FontWeight.Medium) + .fontColor(0xFFFFFF) + .lineHeight($r('app.float.lineHeight_vp_22')) + } + .backgroundColor(0x007DFF) + .onClick(() => { + this.terminateAbility(1); + }) + .width('100%') + .height('40vp') + .margin({right: '16vp'}) + + Button() { + Text($r('app.string.continue')) + .fontSize($r('app.float.font_vp_16')) + .fontFamily('HarmonyHeiTi') + .fontWeight(FontWeight.Medium) + .fontColor(0xFFFFFF) + .lineHeight($r('app.float.lineHeight_vp_22')) + } + .backgroundColor(0x007DFF) + .onClick(() => { + this.EnableAdmin(); + }) + .width('100%') + .height('40vp') + } + .width('100%') + .margin({ bottom: '100vp' }) + } + } + + private terminateAbility(flag: number) { + this.session?.terminateSelfWithResult({ + resultCode: flag == 1 ? 101: 100, + want: { + bundleName: 'test', + abilityName: 'test', + } + }); + storage.clear(); + } + + private EnableAdmin() { + let admin: Want = { + bundleName: bundle, + abilityName: ability + } + let entInfo : adminManager.EnterpriseInfo = { + name: 'test', + description: 'test' + } + adminManager.enableAdmin(admin, entInfo, adminManager.AdminType.ADMIN_TYPE_SUPER).then((result) => { + + }).catch((err: BusinessError) => { + this.terminateAbility(1); + }) + } +} + + + diff --git a/entry/src/main/ets/pages/autoManager/loadingInfo.ets b/entry/src/main/ets/pages/autoManager/loadingInfo.ets index 64233ed..1048e68 100644 --- a/entry/src/main/ets/pages/autoManager/loadingInfo.ets +++ b/entry/src/main/ets/pages/autoManager/loadingInfo.ets @@ -15,12 +15,14 @@ import enterpriseDeviceManager from '@ohos.enterprise.adminManager'; import router from '@ohos.router'; -import accountManager from '../../common/accountManager' +import accountManager, { UserId } from '../../common/accountManager' import appDetailData from '../../common/appManagement/appDetailData' import baseData from '../../common/baseData' import doubleButtonComponent from '../component/autoManager/doubleButtonComponent' import logger from '../../common/logger' import utils from '../../common/utils' +import Want from '@ohos.app.ability.Want'; +import adminManager from '@ohos.enterprise.adminManager'; const TAG = 'LoadingInfo'; @@ -96,12 +98,12 @@ struct LoadingInfo { } async checkAppValidAndActivate() { - let admin = { + let admin: Want = { bundleName: this.manageBundleName, abilityName: this.manageAbilityName } - let enterpriseInfo = { + let enterpriseInfo: adminManager.EnterpriseInfo = { name: this.manageEnterpriseName, description: this.manageEnterpriseDescription } @@ -109,14 +111,18 @@ struct LoadingInfo { + ' | enterpriseInfo=' + JSON.stringify(enterpriseInfo)); let ret = await appDetailData.checkAppItem(admin); - if (!(ret.val)) { + if (!(ret)) { + logger.info(TAG, 'checkAppItem fail'); router.pushUrl({ url: 'pages/autoManager/setFinishFail' }); return; } - let userId = { localId: 0 }; + let userId: UserId = {localId : 0}; let retVal = await accountManager.getAccountUserId(userId); + logger.info(TAG, 'getAccountUserId retVal : ' + retVal); + logger.info(TAG, 'getAccountUserId userId : ' + userId.localId); if (!retVal || userId.localId !== baseData.DEFAULT_USER_ID) { + logger.info(TAG, 'getAccountUserId fail'); router.pushUrl({ url: 'pages/autoManager/setFinishFail' }); return; } diff --git a/entry/src/main/ets/pages/autoManager/managerStart.ets b/entry/src/main/ets/pages/autoManager/managerStart.ets index 2e3d76a..ed3e020 100644 --- a/entry/src/main/ets/pages/autoManager/managerStart.ets +++ b/entry/src/main/ets/pages/autoManager/managerStart.ets @@ -17,6 +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'; const TAG = 'ManagerStart'; @@ -174,31 +176,31 @@ struct ManagerStart { } parseManageParameter() { - let data = globalThis.autoManagerAbilityWant; + let data = GlobalContext.getContext().getObject('adminProvisioningWant') as Want; if (!utils.checkObjPropertyValid(data, 'parameters.elementName.abilityName') || !utils.checkObjPropertyValid(data, 'parameters.enterprise.name') - || !utils.isValid(data.parameters.elementName.bundleName) - || !utils.isValid(data.parameters.url) - || !utils.isValid(data.parameters.termsName) - || (!utils.isValid(data.parameters.termsContent) || !Array.isArray(data.parameters.termsContent)) - || !utils.isValid(data.parameters.enterprise.description)) { + || !utils.isValid((data.parameters?.elementName as Record).bundleName) + || !utils.isValid(data.parameters?.url) + || !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') return; } let mapVal = new Map([ - ['manageAbilityName', data.parameters.elementName.abilityName], - ['manageBundleName', data.parameters.elementName.bundleName], - ['manageUrl', data.parameters.url], - ['manageTermsName', data.parameters.termsName], - ['manageTermsContent', data.parameters.termsContent], - ['manageEnterpriseName', data.parameters.enterprise.name], - ['manageEnterpriseDescription', data.parameters.enterprise.description] + ['manageAbilityName', (data.parameters?.elementName as Record).abilityName], + ['manageBundleName', (data.parameters?.elementName as Record).bundleName], + ['manageUrl', data.parameters?.url], + ['manageTermsName', data.parameters?.termsName], + ['manageTermsContent', data.parameters?.termsContent], + ['manageEnterpriseName', (data.parameters?.enterprise as Record).name], + ['manageEnterpriseDescription', (data.parameters?.enterprise as Record).description] ]); - for (let [key, value] of mapVal) { - AppStorage.SetOrCreate(key, value); + for (let item of Array.from(mapVal.keys())) { + AppStorage.setOrCreate(item, mapVal.get(item)); } } } \ No newline at end of file diff --git a/entry/src/main/ets/pages/autoManager/termsShowPage.ets b/entry/src/main/ets/pages/autoManager/termsShowPage.ets index 9635803..77c5936 100644 --- a/entry/src/main/ets/pages/autoManager/termsShowPage.ets +++ b/entry/src/main/ets/pages/autoManager/termsShowPage.ets @@ -20,8 +20,8 @@ import utils from '../../common/utils' @Entry @Component struct TermsShowPage { - @StorageLink('manageTermsName') manageTermsName: string = AppStorage.Get('manageTermsName'); - @StorageLink('manageTermsContent') manageTermsContent: string[] = AppStorage.Get('manageTermsContent'); + @StorageLink('manageTermsName') manageTermsName: string = AppStorage.Get('manageTermsName') as string; + @StorageLink('manageTermsContent') manageTermsContent: string[] = AppStorage.Get('manageTermsContent') as string[]; build() { Column() { diff --git a/entry/src/main/ets/pages/autoManager/unitManagerShowPage.ets b/entry/src/main/ets/pages/autoManager/unitManagerShowPage.ets index 84d295d..5316ffb 100644 --- a/entry/src/main/ets/pages/autoManager/unitManagerShowPage.ets +++ b/entry/src/main/ets/pages/autoManager/unitManagerShowPage.ets @@ -24,7 +24,7 @@ const TAG = 'UnitManagerShowPage'; @Entry @Component struct UnitManagerShowPage { - @StorageLink('manageUrl') manageUrl: string = AppStorage.Get('manageUrl'); + @StorageLink('manageUrl') manageUrl: string = AppStorage.Get('manageUrl') as string; webviewController: web_webview.WebviewController = new web_webview.WebviewController(); build() { @@ -55,24 +55,16 @@ struct UnitManagerShowPage { .cacheMode(CacheMode.Online) .fileAccess(false) .javaScriptAccess(false) - .onErrorReceive((event?: { - request: WebResourceRequest, - error: WebResourceError - }) => { - logger.warn(TAG, 'error code=' + event.error.getErrorCode() - + ', error info=' + event.error.getErrorInfo()) + .onErrorReceive((event?: ErrorReceiveEvent) => { + logger.warn(TAG, 'error code=' + event?.error.getErrorCode() + + ', error info=' + event?.error.getErrorInfo()) }) - .onHttpErrorReceive((event?: { - request: WebResourceRequest, - response: WebResourceResponse - }) => { - logger.warn(TAG, 'response code=' + event.response.getResponseCode() - + ', response message=' + event.response.getReasonMessage()) + .onHttpErrorReceive((event?: HttpErrorReceiveEvent) => { + logger.warn(TAG, 'response code=' + event?.response.getResponseCode() + + ', response message=' + event?.response.getReasonMessage()) }) - .onRenderExited((event?: { - renderExitReason: RenderExitReason - }) => { - logger.warn(TAG, 'renderExitReason=' + event.renderExitReason); + .onRenderExited((event?: RenderExitedEvent) => { + logger.warn(TAG, 'renderExitReason=' + event?.renderExitReason); }) .useSizeType({ xs: { span: 4, offset: 2 }, sm: { span: 4, offset: 2 }, @@ -97,4 +89,18 @@ struct UnitManagerShowPage { this.manageUrl = ''; } } +} + +interface ErrorReceiveEvent { + request: WebResourceRequest, + error: WebResourceError +} + +interface HttpErrorReceiveEvent { + request: WebResourceRequest, + response: WebResourceResponse +} + +interface RenderExitedEvent { + renderExitReason: RenderExitReason } \ No newline at end of file diff --git a/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets b/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets index 75a88f4..e061ed0 100644 --- a/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets +++ b/entry/src/main/ets/pages/component/autoManager/doubleButtonComponent.ets @@ -19,16 +19,17 @@ 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'; @Component -export default -struct DoubleButtonComponent { +export default struct DoubleButtonComponent { private nextFlag: boolean = false; private returnDialogFlag: boolean = true; @StorageLink('manageBundleName') nextBundleName: string = ''; - buttonDialog: CustomDialogController = new CustomDialogController({ builder: DialogButtonLayout(), autoCancel: false, @@ -37,7 +38,7 @@ struct DoubleButtonComponent { }) build() { - Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems:ItemAlign.Center }) { + Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { Row() { Image($r('app.media.ic_public_arrow_left')) .width($r('app.float.wh_value_24')) @@ -94,10 +95,10 @@ struct DoubleButtonComponent { async startSpecificActionAbility() { let actionVal = 'ohos.action.ENTERPRISE_DEVICE_ADMIN_POLICY_COMPLIANCE'; - let queryWant = { action: actionVal, bundleName: this.nextBundleName }; + let queryWant: Want = { action: actionVal, bundleName: this.nextBundleName }; logger.info(TAG, 'startSpecificActionAbility startAbility in:'); - let data; + let data: Array = []; try { data = await bundle.queryAbilityInfo(queryWant, bundle.AbilityFlag.GET_ABILITY_INFO_DEFAULT, baseData.DEFAULT_USER_ID); @@ -110,19 +111,23 @@ struct DoubleButtonComponent { logger.info(TAG, 'startSpecificActionAbility data len=' + data.length + ' | data content=' + data[0].name) try { - await globalThis.autoManagerAbilityContext.startAbility({ bundleName: this.nextBundleName, - abilityName: data[0].name }); + await (GlobalContext.getContext() + .getObject("autoManagerAbilityContext") as common.UIAbilityContext).startAbility({ + bundleName: this.nextBundleName, + abilityName: data[0].name + }); } catch (e) { logger.error(TAG, "startSpecificActionAbility startAbility fail! " + e) } } - await globalThis.autoManagerAbilityContext.terminateSelf(); + await (GlobalContext.getContext() + .getObject("autoManagerAbilityContext") as common.UIAbilityContext).terminateSelf(); } } @CustomDialog struct DialogButtonLayout { - controller: CustomDialogController + controller?: CustomDialogController build() { Column() { @@ -173,7 +178,7 @@ struct DialogButtonLayout { .height('40vp') .flexGrow(1) .onClick(() => { - this.controller.close(); + this.controller?.close(); }) Divider() @@ -194,7 +199,7 @@ struct DialogButtonLayout { .height('40vp') .flexGrow(1) .onClick(() => { - this.controller.close(); + this.controller?.close(); resetFactory.rebootAndCleanUserData(); }) } diff --git a/entry/src/main/ets/pages/component/autoManager/termsListComponent.ets b/entry/src/main/ets/pages/component/autoManager/termsListComponent.ets index 598ecba..b7ac260 100644 --- a/entry/src/main/ets/pages/component/autoManager/termsListComponent.ets +++ b/entry/src/main/ets/pages/component/autoManager/termsListComponent.ets @@ -64,7 +64,7 @@ struct TermsListComponent { ListItem() { Column() { - ForEach(this.contentOfEnterpriseTerms, (item, index) => { + ForEach(this.contentOfEnterpriseTerms, (item: string, index) => { Row() { Text(item) .fontWeight(FontWeight.Regular) @@ -77,7 +77,7 @@ struct TermsListComponent { }) } .width('100%') - }, item => JSON.stringify(item)); + }, (item: string) => JSON.stringify(item)); } .flexShrink(1) } diff --git a/entry/src/main/ets/pages/component/entryComponent.ets b/entry/src/main/ets/pages/component/entryComponent.ets index bdc9503..4f2adc6 100644 --- a/entry/src/main/ets/pages/component/entryComponent.ets +++ b/entry/src/main/ets/pages/component/entryComponent.ets @@ -15,8 +15,8 @@ @Component export default struct EntryComponent { - @Prop appIcon: string; - @Prop appTitle: string; + @Prop appIcon: string = ''; + @Prop appTitle: string = ''; build() { Flex({ justifyContent: FlexAlign.SpaceBetween }) { diff --git a/entry/src/main/ets/pages/component/permissionListComponent.ets b/entry/src/main/ets/pages/component/permissionListComponent.ets index 79fb3ba..f5e36c7 100644 --- a/entry/src/main/ets/pages/component/permissionListComponent.ets +++ b/entry/src/main/ets/pages/component/permissionListComponent.ets @@ -38,8 +38,7 @@ export default struct PermissionListComponent { .maxLines(3) .textOverflow({ overflow: TextOverflow.Ellipsis }) .visibility(('' === this.permissionName || 'undefined' === typeof (this.permissionName) - || null === this.permissionName || 0 === Object.keys(this.permissionName).length) - ? Visibility.None : Visibility.Visible) + || null === this.permissionName) ? Visibility.None : Visibility.Visible) .margin({ bottom: $r('app.float.wh_value_2'), }); @@ -53,8 +52,7 @@ export default struct PermissionListComponent { .maxLines(3) .textOverflow({ overflow: TextOverflow.Ellipsis }) .visibility(('' === this.permissionDescription || 'undefined' === typeof (this.permissionDescription) - || null === this.permissionDescription || 0 === Object.keys(this.permissionDescription).length) - ? Visibility.None : Visibility.Visible) + || null === this.permissionDescription) ? Visibility.None : Visibility.Visible) } .alignItems(HorizontalAlign.Start) } diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index 3fae571..6a07bf4 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -7,7 +7,8 @@ "mainElement": "com.ohos.adminprovisioning.MainAbility", "deviceTypes": [ "default", - "tablet" + "tablet", + "2in1" ], "deliveryWithInstall": true, "installationFree": false, @@ -49,6 +50,17 @@ "startWindowBackground": "$color:color_00000000_transparent" } ], + "extensionAbilities": [ + { + "name": "AdminUIExtensionAbility", + "icon": "$media:icon", + "description": "UIExtensionAbility", + "exported": true, + "type": "ui", + "visible": true, + "srcEntrance": "./ets/MainAbility/UIExtensionAbility.ets" + } + ], "requestPermissions": [ { "name": "ohos.permission.MANAGE_LOCAL_ACCOUNTS" diff --git a/entry/src/main/resources/base/element/float.json b/entry/src/main/resources/base/element/float.json index c3b4f06..d70b432 100644 --- a/entry/src/main/resources/base/element/float.json +++ b/entry/src/main/resources/base/element/float.json @@ -92,6 +92,10 @@ "name": "font_vp_16", "value": "16vp" }, + { + "name": "font_vp_18", + "value": "18vp" + }, { "name": "font_vp_24", "value": "24vp" @@ -108,6 +112,14 @@ "name": "lineHeight_vp_22", "value": "22vp" }, + { + "name": "lineHeight_vp_25", + "value": "25vp" + }, + { + "name": "lineHeight_vp_35", + "value": "35vp" + }, { "name": "lineHeight_vp_41", "value": "41vp" diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index 653ba37..bdf13c3 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -159,6 +159,18 @@ { "name": "setupComplete", "value": "设置完成" + }, + { + "name": "AutoDeployText", + "value": "此设备由您所属的单位配置和管理,如有疑问,请联系您的单位处理。" + }, + { + "name": "continue", + "value": "继续。" + }, + { + "name": "PreviousStep", + "value": "上一步。" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/base/media/mdm_start.svg b/entry/src/main/resources/base/media/mdm_start.svg new file mode 100644 index 0000000..25fce55 --- /dev/null +++ b/entry/src/main/resources/base/media/mdm_start.svg @@ -0,0 +1,12 @@ + + + 编组 2 + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 4bd2e7e..2f00fed 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -1,5 +1,6 @@ { "src": [ + "pages/autoDeploy/MdmDeploy", "pages/applicationInfo", "pages/autoManager/managerStart", "pages/autoManager/unitManagerShowPage", diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index e0821ab..2234338 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -159,6 +159,18 @@ { "name": "setupComplete", "value": "setup complete" + }, + { + "name": "AutoDeployText", + "value": "This device is configured and managed by your organization, contact your organization if you have questions." + }, + { + "name": "continue", + "value": "continue." + }, + { + "name": "PreviousStep", + "value": "previous step." } ] } \ 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 f18d8ea..e5fc0fc 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -159,6 +159,18 @@ { "name": "setupComplete", "value": "设置完成" + }, + { + "name": "AutoDeployText", + "value": "此设备由您所属的单位配置和管理,如有疑问,请联系您的单位处理。" + }, + { + "name": "continue", + "value": "继续。" + }, + { + "name": "PreviousStep", + "value": "上一步。" } ] } \ No newline at end of file -- Gitee