diff --git a/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogModel.ets b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogModel.ets new file mode 100644 index 0000000000000000000000000000000000000000..b0999c72cc40b7d99d16d1ad2df01c870a39ef00 --- /dev/null +++ b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogModel.ets @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2025 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 { BaseModel } from '../common/base/BaseModel'; +import { audio } from '@kit.AudioKit'; +import { camera } from '@kit.CameraKit'; +import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit'; +import { Log } from '../common/utils/utils'; +import { PermissionGroup } from '../common/model/definition'; +import { PermissionApplications } from '../common/model/typedef'; +import { GlobalDialogViewState } from './GlobalDialogViewState'; +import router from '@ohos.router'; +import window from '@ohos.window'; +import display from '@ohos.display'; +import common from '@ohos.app.ability.common'; + +const BG_COLOR = '#00000000'; +const CAMERA = 'camera'; +const MICROPHONE = 'microphone'; + +export class GlobalDialogModel extends BaseModel { + private static instance: GlobalDialogModel; + + public static getInstance(): GlobalDialogModel { + if (!GlobalDialogModel.instance) { + GlobalDialogModel.instance = new GlobalDialogModel(); + } + return GlobalDialogModel.instance; + } + + /** + * 判断麦克风全局状态 + * return boolean + */ + private microphoneStatus(): boolean { + try { + let audioManager = audio.getAudioManager(); + let audioVolumeManager = audioManager.getVolumeManager(); + let groupId = audio.DEFAULT_VOLUME_GROUP_ID; + let audioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(groupId); + let muteState = audioVolumeGroupManager.isPersistentMicMute(); + Log.info(`microphoneStatus: ${muteState}.`); + return muteState; + } catch (err) { + Log.error('Failed to obtain the microphone disabled status.'); + return false; + } + } + + /** + * 判断相机全局状态 + * @param context ServiceExtensionContext + * return boolean + */ + private cameraStatus(context: Context): boolean { + try { + let cameraManager = camera.getCameraManager(context); + let isMuteSupported = cameraManager.isCameraMuteSupported(); + if (!isMuteSupported) { + Log.info('The current device does not support disabling the camera.'); + return false; + } + let muteState = cameraManager.isCameraMuted(); + Log.info(`cameraStatus: ${muteState}.`); + return muteState; + } catch (err) { + Log.error('Failed to obtain the camera disabled status.'); + return false; + } + } + + /** + * 判断弹窗申请权限的全局状态 + * @param context ServiceExtensionContext + * @param resource 弹窗申请的权限 + * return boolean + */ + public statusCheck(context: Context, resource: string): boolean { + switch (resource) { + case 'microphone': + if (this.microphoneStatus()) { + return true; + } else { + Log.info('The microphone is not disabled on this device.'); + return false; + } + case 'camera': + if (this.cameraStatus(context)) { + return true; + } else { + Log.info('The camera is not disabled on this device.'); + return false; + } + default: + if (this.microphoneStatus() && this.cameraStatus(context)) { + return true; + } else { + Log.info('The microphone and camera is not disabled on this device.'); + return false; + } + } + } + + /** + * 判断是否拥有控制全局开关的权限 + * return boolean + */ + public permissionCheck(): boolean { + try { + let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; + let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag); + let atManager = abilityAccessCtrl.createAtManager(); + let status = + atManager.verifyAccessTokenSync(bundleInfo.appInfo.accessTokenId, 'ohos.permission.MICROPHONE_CONTROL'); + if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { + Log.info('permission status is denied.'); + return false; + } + return true; + } catch (err) { + Log.error('verifyAccessTokenSync failed.'); + return false; + } + } + + /** + * 创建全局弹窗 + * @param context ServiceExtensionContext + * @param rect 窗口大小及位置 + * @param storage 数据管理 + * return 创建的窗口 + */ + public async createWindow( + context: Context, rect: display.Rect, storage: LocalStorage + ): Promise { + try { + Log.info('create window'); + let configuration: window.Configuration = { + ctx: context, + name: 'globalDialog', + windowType: window.WindowType.TYPE_VOICE_INTERACTION + } + const win = await window.createWindow(configuration); + await win.moveWindowTo(rect.left, rect.top); + await win.resize(rect.width, rect.height); + await win.loadContent('pages/globalSwitch', storage); + win.setWindowBackgroundColor(BG_COLOR); + await win.showWindow(); + return win; + } catch (error) { + Log.error(`window create failed, code: ${error.code}, message: ${error.message}.`); + return null; + } + } + + public initViewState(type: string): GlobalDialogViewState { + let viewState = new GlobalDialogViewState(); + if (type === MICROPHONE) { + viewState.title = $r('app.string.global_title_microphone'); + viewState.text = $r('app.string.global_desc_microphone'); + } else if (type === CAMERA) { + viewState.title = $r('app.string.global_title_camera'); + viewState.text = $r('app.string.global_desc_camera'); + } else { + viewState.title = $r('app.string.global_title_camera_and_microphone'); + viewState.text = $r('app.string.global_desc_camera_and_microphone'); + } + return viewState; + } + + /** + * 获取全局开关状态 + * @param context UIAbilityContext + * @param type 全局开关类型 + * @param backTitle 标题 + * @param list 应用列表 + * return + */ + public getMuteStateAndGoto( + context: common.UIAbilityContext, + type: PermissionGroup, + backTitle: ResourceStr, + list: PermissionApplications[] + ): void { + try { + if (type == PermissionGroup.MICROPHONE) { + let audioManager = audio.getAudioManager(); + let audioVolumeManager = audioManager.getVolumeManager(); + let audioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(audio.DEFAULT_VOLUME_GROUP_ID); + let muteState = audioVolumeGroupManager.isPersistentMicMute(); + Log.info(`get muteState success: ${muteState}.`); + router.pushUrl({ + url: 'pages/authority-tertiary-groups', + params: { list, backTitle, globalIsOn: !muteState, isMuteSupported: true } + }) + } else { + let cameraManager = camera.getCameraManager(context); + let mute = cameraManager.isCameraMuted(); + let isCameraMuteSupported = cameraManager.isCameraMuteSupported(); + Log.info(`get muteState success: ${mute}.`); + router.pushUrl({ + url: 'pages/authority-tertiary-groups', + params: { list, backTitle, globalIsOn: !mute, isMuteSupported: isCameraMuteSupported } + }) + } + } catch (error) { + Log.error(`getMuteStateAndGoto failed, code: ${error.code}, message: ${error.message}.`); + } + } + + /** + * 设置麦克风全局开关 + * @param context ServiceExtensionContext | UIAbilityContext + * @param flag 全局开关状态 + * @param isKill 是否销毁ability + * return + */ + private setMicrophoneMute( + context: common.ServiceExtensionContext | common.UIAbilityContext, flag: boolean, isKill: boolean + ): void { + try { + let audioManager = audio.getAudioManager(); + let audioVolumeManager = audioManager.getVolumeManager(); + let audioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(audio.DEFAULT_VOLUME_GROUP_ID); + audioVolumeGroupManager.setMicMutePersistent(flag, audio.PolicyType.PRIVACY).then(() => { + Log.info(`setMicMutePersistent success, mute: ${flag}.`); + isKill ? context.terminateSelf() : null; + }) + } catch (error) { + Log.error(`setMicrophoneMute failed, code: ${error.code}, message: ${error.message}.`); + } + } + + /** + * 设置相机全局开关 + * @param context ServiceExtensionContext | UIAbilityContext + * @param flag 全局开关状态 + * @param isKill 是否销毁ability + * return + */ + private setCameraMute( + context: common.ServiceExtensionContext | common.UIAbilityContext, flag: boolean, isKill: boolean + ): void { + try { + let cameraManager = camera.getCameraManager(context); + cameraManager.muteCameraPersistent(flag, camera.PolicyType.PRIVACY); + Log.info(`muteCameraPersistent success, mute: ${flag}.`); + isKill ? context.terminateSelf() : null; + } catch (error) { + Log.error(`setCameraMute failed, code: ${error.code}, message: ${error.message}.`); + } + } + + /** + * 设置全局开关状态 + * @param context ServiceExtensionContext | UIAbilityContext + * @param type 全局开关类型 + * @param flag 全局开关状态 + * @param isKill 是否销毁ability + * return + */ + public setMuteState( + context: common.ServiceExtensionContext | common.UIAbilityContext, type: string, flag: boolean, isKill: boolean + ): void { + if (type === MICROPHONE || type === PermissionGroup.MICROPHONE) { + this.setMicrophoneMute(context, flag, isKill); + } else if (type === CAMERA || type === PermissionGroup.CAMERA) { + this.setCameraMute(context, flag, isKill); + } else { + this.setCameraMute(context, flag, false); + this.setMicrophoneMute(context, flag, isKill); + } + } + +} \ No newline at end of file diff --git a/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogViewState.ets b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogViewState.ets new file mode 100644 index 0000000000000000000000000000000000000000..0e111f721926f46cf2a782aaba1f769c9cdad995 --- /dev/null +++ b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalDialogViewState.ets @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 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 { BaseState } from '../common/base/BaseState'; + +@Observed +export class GlobalDialogViewState extends BaseState { + // 全局开关类型 + public type: string | undefined = ''; + + // 全局弹窗标题 + public title: ResourceStr = ''; + + // 全局弹窗文本 + public text: ResourceStr = ''; + +} \ No newline at end of file diff --git a/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ets b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..fbcd464d50e2808dba1bf2139da9914d0009cbac --- /dev/null +++ b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ets @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 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 extension from '@ohos.app.ability.ServiceExtensionAbility'; +import window from '@ohos.window'; +import display from '@ohos.display'; +import { Want } from '@kit.AbilityKit'; +import { Log } from '../common/utils/utils'; +import { GlobalDialogModel } from './GlobalDialogModel'; + +let globalDialogModel: GlobalDialogModel = new GlobalDialogModel(); +let globalWindow: window.Window | null; + +export default class GlobalExtensionAbility extends extension { + /** + * Lifecycle function, called back when a service extension is started for initialization. + */ + onCreate(want: Want): void { + Log.info('ServiceExtensionAbility onCreate, ability name is ' + want.abilityName); + + if (!want.parameters) { + return; + } + + if (!globalDialogModel.permissionCheck()) { + this.context?.terminateSelf(); + return; + } + + if (!globalDialogModel.statusCheck(this.context, want.parameters['ohos.sensitive.resource'] as string)) { + this.context?.terminateSelf(); + return; + } + + try { + let dis = display.getDefaultDisplaySync(); + let navigationBarRect: display.Rect = { + left: 0, + top: 0, + width: dis.width, + height: dis.height + }; + Log.info('want: ' + JSON.stringify(want)); + let property: Record = { 'globalState': want.parameters['ohos.sensitive.resource'] }; + let storage: LocalStorage = new LocalStorage(property); + globalDialogModel.createWindow(this.context, navigationBarRect, storage).then(window => { + globalWindow = window; + }) + } catch (exception) { + Log.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); + }; + } + + /** + * Lifecycle function, called back when a service extension is started or recall. + */ + onRequest(want: Want, startId: number): void { + Log.info('ServiceExtensionAbility onRequest. start id is ' + startId); + } + + /** + * Lifecycle function, called back before a service extension is destroyed. + */ + onDestroy(): void { + Log.info('ServiceExtensionAbility onDestroy.'); + globalWindow?.destroyWindow(); + } + +}; \ No newline at end of file diff --git a/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ts b/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ts deleted file mode 100644 index 9fd26c620ca3042e8173862880f2e66f79edf0e1..0000000000000000000000000000000000000000 --- a/permissionmanager/src/main/ets/GlobalExtAbility/GlobalExtAbility.ts +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2022 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 extension from '@ohos.app.ability.ServiceExtensionAbility'; -import window from '@ohos.window'; -import display from '@ohos.display'; -import { GlobalContext } from '../common/utils/globalContext'; -import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit'; -import { camera } from '@kit.CameraKit'; -import { audio } from '@kit.AudioKit'; - -const TAG = 'PermissionManager_Log:'; -const BG_COLOR = '#00000000'; - -export default class GlobalExtensionAbility extends extension { - /** - * Lifecycle function, called back when a service extension is started for initialization. - */ - onCreate(want): void { - console.info(TAG + 'ServiceExtensionAbility onCreate, ability name is ' + want.abilityName); - console.info(TAG + 'want: ' + JSON.stringify(want)); - - GlobalContext.store('globalState', want.parameters['ohos.sensitive.resource']); - GlobalContext.store('context', this.context); - - if (!this.permissionCheck()) { - this.context?.terminateSelf(); - return; - } - - if (!this.statusCheck(want.parameters['ohos.sensitive.resource'])) { - this.context?.terminateSelf(); - return; - } - - try { - let dis = display.getDefaultDisplaySync(); - let navigationBarRect = { - left: 0, - top: 0, - width: dis.width, - height: dis.height - }; - this.createWindow('globalDialog', window.WindowType.TYPE_VOICE_INTERACTION, navigationBarRect); - } catch (exception) { - console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); - }; - } - - /** - * Lifecycle function, called back when a service extension is started or recall. - */ - onRequest(want, startId): void { - console.info(TAG + 'ServiceExtensionAbility onRequest. start id is ' + startId); - } - - /** - * Lifecycle function, called back before a service extension is destroyed. - */ - onDestroy(): void { - console.info(TAG + 'ServiceExtensionAbility onDestroy.'); - let win = GlobalContext.load('globalWin'); - win?.destroyWindow(); - } - - private permissionCheck(): boolean { - try { - let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; - let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag); - let atManager = abilityAccessCtrl.createAtManager(); - let status = - atManager.verifyAccessTokenSync(bundleInfo.appInfo.accessTokenId, 'ohos.permission.MICROPHONE_CONTROL'); - if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { - console.log(TAG + 'permission status is denied.'); - return false; - } - return true; - } catch (err) { - console.error(TAG + 'verifyAccessTokenSync failed.'); - return false; - } - } - - private statusCheck(resource: string): boolean { - switch (resource) { - case 'microphone': - if (this.microphoneStatus()) { - return true; - } else { - console.log(TAG + 'The microphone is not disabled on this device.'); - return false; - } - case 'camera': - if (this.cameraStatus()) { - return true; - } else { - console.log(TAG + 'The camera is not disabled on this device.'); - return false; - } - default: - if (this.microphoneStatus() && this.cameraStatus()) { - return true; - } else { - console.log(TAG + 'The microphone and camera is not disabled on this device.'); - return false; - } - } - } - - private microphoneStatus(): boolean { - try { - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager.getVolumeManager(); - let groupId = audio.DEFAULT_VOLUME_GROUP_ID; - let audioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(groupId); - let muteState = audioVolumeGroupManager.isPersistentMicMute(); - console.log(TAG + 'microphoneStatus: ' + muteState); - return muteState; - } catch (err) { - console.error(TAG + 'Failed to obtain the microphone disabled status.'); - return false; - } - } - - private cameraStatus(): boolean { - try { - let cameraManager = camera.getCameraManager(this.context); - let isMuteSupported = cameraManager.isCameraMuteSupported(); - if (!isMuteSupported) { - console.log(TAG + 'The current device does not support disabling the camera.'); - return false; - } - let muteState = cameraManager.isCameraMuted(); - console.log(TAG + 'cameraStatus: ' + muteState); - return muteState; - } catch (err) { - console.error(TAG + 'Failed to obtain the camera disabled status.'); - return false; - } - } - - private async createWindow(name: string, windowType: number, rect): Promise { - console.info(TAG + 'create window'); - try { - const win = await window.createWindow({ ctx: this.context, name, windowType }); - GlobalContext.store('globalWin', win); - await win.moveWindowTo(rect.left, rect.top); - await win.resize(rect.width, rect.height); - await win.setUIContent('pages/globalSwitch'); - win.setWindowBackgroundColor(BG_COLOR); - await win.showWindow(); - } catch { - console.info(TAG + 'window create failed!'); - } - } -}; \ No newline at end of file diff --git a/permissionmanager/src/main/ets/MainAbility/MainAbility.ts b/permissionmanager/src/main/ets/MainAbility/MainAbility.ts index 5a35a154bccd85703e385a8aafddfab008a1b9c2..e43948f638c3ca8384bc71d61a0f76618d30c473 100644 --- a/permissionmanager/src/main/ets/MainAbility/MainAbility.ts +++ b/permissionmanager/src/main/ets/MainAbility/MainAbility.ts @@ -28,7 +28,6 @@ export default class MainAbility extends UIAbility { globalThis.bundleName = want.parameters.bundleName; GlobalContext.store('bundleName', want.parameters.bundleName); - GlobalContext.store('context', this.context); } onWindowStageCreate(windowStage): void { diff --git a/permissionmanager/src/main/ets/PermissionSheet/GlobalSwitchSheetDialog.ets b/permissionmanager/src/main/ets/PermissionSheet/GlobalSwitchSheetDialog.ets index a0502b9027b2697154cd91229eb49ab93a5b2c12..1b2f90b03d49407d3593f65a4fab964a696cd10f 100644 --- a/permissionmanager/src/main/ets/PermissionSheet/GlobalSwitchSheetDialog.ets +++ b/permissionmanager/src/main/ets/PermissionSheet/GlobalSwitchSheetDialog.ets @@ -41,7 +41,7 @@ struct GlobalSwitchSheetDialog { @LocalStorageLink('want') want: Want | null = storage.get('want') || null; @LocalStorageLink('session') session: UIExtensionContentSession = storage.get('session') as UIExtensionContentSession; - private context = getContext(this) as common.ServiceExtensionContext; + private context = getContext(this) as common.UIExtensionContext; private muteSuppoted = false; private groupName: PermissionGroup = PermissionGroup.OTHER; dialogController: CustomDialogController | null = new CustomDialogController({ @@ -106,7 +106,7 @@ struct GlobalSwitchSheetDialog { this.muteSuppoted = true; globalIsOn = !audioVolumeGroupManager.isPersistentMicMute(); } else { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); + let cameraManager = camera.getCameraManager(this.context); this.muteSuppoted = cameraManager.isCameraMuteSupported(); globalIsOn = !cameraManager.isCameraMuted(); } @@ -139,7 +139,7 @@ struct GlobalSwitchSheetDialog { @CustomDialog struct applicationItem { - private context = getContext(this) as common.ServiceExtensionContext; + private context = getContext(this) as common.UIExtensionContext; @State globalIsOn: boolean = false; @State backTitle: string = ''; @State groupInfo: GroupInfo = new GroupInfo(PermissionGroup.OTHER, '', '', '', [], '', [], [], false); @@ -197,7 +197,7 @@ struct applicationItem { globalListen() { if (this.currentGroup == 'CAMERA') { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); + let cameraManager = camera.getCameraManager(this.context); cameraManager.on('cameraMute', (err, curMuted) => { Log.info('curMuted: ' + JSON.stringify(curMuted) + ' err: ' + JSON.stringify(err)); this.globalIsOn = !curMuted; @@ -332,7 +332,7 @@ struct applicationItem { .onChange((isOn: boolean) => { if (isOn) { if (this.currentGroup == 'CAMERA') { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); + let cameraManager = camera.getCameraManager(this.context); cameraManager.muteCamera(false); PermissionDialogReturn([Constants.PERMISSION_DIALOG_SUCCESS], session); this.context.terminateSelf(); diff --git a/permissionmanager/src/main/ets/common/model/typedef.ets b/permissionmanager/src/main/ets/common/model/typedef.ets index 49cd7cae8fbb8fabf4d9f21169120270242a66ae..2dff56cf4fcc6a7e97bca0d12dcb03d2a0abaf01 100644 --- a/permissionmanager/src/main/ets/common/model/typedef.ets +++ b/permissionmanager/src/main/ets/common/model/typedef.ets @@ -402,12 +402,20 @@ export class RouterParams1 { public backTitle: ResourceStr public group: string public globalIsOn: boolean + public isMuteSupported?: boolean - constructor(list: PermissionApplications[], backTitle: ResourceStr, group: string, globalIsOn: boolean) { + constructor( + list: PermissionApplications[], + backTitle: ResourceStr, + group: string, + globalIsOn: boolean, + isMuteSupported?: boolean + ) { this.list = list this.backTitle = backTitle this.group = group this.globalIsOn = globalIsOn + this.isMuteSupported = isMuteSupported } } diff --git a/permissionmanager/src/main/ets/common/utils/globalContext.ts b/permissionmanager/src/main/ets/common/utils/globalContext.ts index 53809b430677aed0804b623cda51fd28a5384618..ebde7d5ac00f8cba923d319133a4c65bd82f20f3 100644 --- a/permissionmanager/src/main/ets/common/utils/globalContext.ts +++ b/permissionmanager/src/main/ets/common/utils/globalContext.ts @@ -15,10 +15,8 @@ export class GlobalContext { currentPermissionGroup: string; - isMuteSupported: boolean; isVertical: boolean; bundleName: string; - globalState: string; windowNum: number; dialogSet: Set; diff --git a/permissionmanager/src/main/ets/pages/authority-management.ets b/permissionmanager/src/main/ets/pages/authority-management.ets index 612276e6902a20c89522f1f1fcf2b34be9452820..3181642f8b96b589df79d1b741f63694cbfa27c2 100644 --- a/permissionmanager/src/main/ets/pages/authority-management.ets +++ b/permissionmanager/src/main/ets/pages/authority-management.ets @@ -18,8 +18,6 @@ import { alphabetIndexerComponent } from '../common/components/alphabeticalIndex import { textInput } from '../common/components/search'; import router from '@ohos.router'; import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; -import audio from '@ohos.multimedia.audio' -import camera from '@ohos.multimedia.camera' import display from '@ohos.display'; import common from '@ohos.app.ability.common'; import { groups, globalGroup, showSubPermissionsGroup } from '../common/model/permissionGroup'; @@ -36,6 +34,7 @@ import { import { AppInfo, PermissionApplications, GroupPermission, GroupInfo } from '../common/model/typedef'; import { GlobalContext } from '../common/utils/globalContext'; import { Permission, PermissionGroup } from '../common/model/definition'; +import { GlobalDialogModel } from '../GlobalExtAbility/GlobalDialogModel'; import Constants from '../common/utils/constant'; import bundleManager from '@ohos.bundle.bundleManager'; @@ -94,27 +93,7 @@ struct authorityManagementPage { params: { list: dataList, backTitle: item.groupName } }) } else { - if (item.group == 'MICROPHONE') { - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager.getVolumeManager(); - let groupId = audio.DEFAULT_VOLUME_GROUP_ID; - audioVolumeManager.getVolumeGroupManager(groupId).then(audioVolumeGroupManager => { - let muteState = audioVolumeGroupManager.isPersistentMicMute(); - GlobalContext.store('isMuteSupported', true); - router.pushUrl({ - url: 'pages/authority-tertiary-groups', - params: { list: dataList, backTitle: item.groupName, globalIsOn: !muteState } - }) - }) - } else { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); - let mute = cameraManager.isCameraMuted(); - GlobalContext.store('isMuteSupported', cameraManager.isCameraMuteSupported()); - router.pushUrl({ - url: 'pages/authority-tertiary-groups', - params: { list: dataList, backTitle: item.groupName, globalIsOn: !mute } - }) - } + GlobalDialogModel.getInstance().getMuteStateAndGoto(this.context, item.group, item.groupName, dataList); } } }) diff --git a/permissionmanager/src/main/ets/pages/authority-tertiary-groups.ets b/permissionmanager/src/main/ets/pages/authority-tertiary-groups.ets index 10373eed24470b3de99de92d14a5e0365c97eedc..85b370150d36064c10253b9140a528a7c71fba2a 100644 --- a/permissionmanager/src/main/ets/pages/authority-tertiary-groups.ets +++ b/permissionmanager/src/main/ets/pages/authority-tertiary-groups.ets @@ -28,7 +28,9 @@ import { ApplicationObj, GroupInfo, RouterParams1, PermissionApplications, AppIn import { GlobalContext } from '../common/utils/globalContext'; import { Permission, PermissionGroup } from '../common/model/definition'; import Constants from '../common/utils/constant'; +import common from '@ohos.app.ability.common'; import { polymorphismGroup, globalGroup, groups } from '../common/model/permissionGroup'; +import { GlobalDialogModel } from '../GlobalExtAbility/GlobalDialogModel'; const locationStatus: Resource[] = [ $r('app.string.always_allow'), @@ -36,6 +38,8 @@ const locationStatus: Resource[] = [ $r('app.string.per_inquiry'), $r('app.string.allowed_only_during_use') ]; +let cameraManager: camera.CameraManager; +let audioVolumeGroupManager: audio.AudioVolumeGroupManager; let globalIsOn: boolean = (router.getParams() as RouterParams1).globalIsOn; // return title name @Entry @@ -155,8 +159,10 @@ struct locationInfoPage { @Component struct applicationItem { + private context = getContext(this) as common.UIAbilityContext; private backTitle: ResourceStr = (router.getParams() as RouterParams1).backTitle; private list: PermissionApplications[] = (router.getParams() as RouterParams1).list; + private isMuteSupported: boolean | undefined = (router.getParams() as RouterParams1).isMuteSupported; @State permissionNum: number = Constants.PERMISSION_NUM; // permission num @State toggleIsOn: boolean[] = []; // toggle switch state array @State isRisk: boolean[] = []; @@ -170,7 +176,6 @@ struct applicationItem { @State isTouch: string = ''; @State groupInfo: GroupInfo = new GroupInfo(PermissionGroup.OTHER, '', '', '', [], '', [], [], false); @State currentGroup: PermissionGroup = GlobalContext.load('currentPermissionGroup'); - @State isMuteSupported: boolean = GlobalContext.load('isMuteSupported'); @State allBundleInfo: AppInfo[] = GlobalContext.load('allBundleInfo'); scroller: Scroller = new Scroller(); @@ -185,7 +190,6 @@ struct applicationItem { value: $r('app.string.cancel'), buttonStyle: ButtonStyleMode.TEXTUAL, action: () => { - Log.info('global cancel'); this.dialogController?.close(); } }, @@ -193,20 +197,7 @@ struct applicationItem { value: $r('app.string.close'), buttonStyle: ButtonStyleMode.TEXTUAL, action: () => { - Log.info('global accept'); - if (this.currentGroup == PermissionGroup.MICROPHONE) { - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager?.getVolumeManager(); - audioVolumeManager?.getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID).then(audioVolumeGroupManager => { - audioVolumeGroupManager.setMicMutePersistent(true, audio.PolicyType.PRIVACY).then(() => { - this.dialogController?.close(); - }) - }) - } else { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); - cameraManager?.muteCameraPersistent(true, camera.PolicyType.PRIVACY); - this.dialogController?.close(); - } + GlobalDialogModel.getInstance().setMuteState(this.context, this.currentGroup, true, false); } } ], @@ -479,6 +470,8 @@ struct applicationItem { } aboutToDisappear() { + cameraManager?.off('cameraMute'); + audioVolumeGroupManager?.off('micStateChange'); this.dialogController = null; } @@ -534,7 +527,7 @@ struct applicationItem { globalListen() { this.globalIsOn = globalIsOn; if (this.currentGroup == 'CAMERA') { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); + cameraManager = camera.getCameraManager(this.context); cameraManager.on('cameraMute', (err, curMuted) => { Log.info('curMuted: ' + JSON.stringify(curMuted) + ' err: ' + JSON.stringify(err)); this.globalIsOn = !curMuted; @@ -543,12 +536,11 @@ struct applicationItem { let audioManager = audio.getAudioManager(); let audioVolumeManager = audioManager.getVolumeManager(); let groupId = audio.DEFAULT_VOLUME_GROUP_ID; - audioVolumeManager.getVolumeGroupManager(groupId).then(audioVolumeGroupManager => { - audioVolumeGroupManager.on('micStateChange', micStateChange => { - let muteState = audioVolumeGroupManager.isPersistentMicMute(); - Log.info('micStateChange: ' + JSON.stringify(muteState)); - this.globalIsOn = !muteState; - }) + audioVolumeGroupManager = audioVolumeManager.getVolumeGroupManagerSync(groupId); + audioVolumeGroupManager.on('micStateChange', micStateChange => { + let muteState = audioVolumeGroupManager.isPersistentMicMute(); + Log.info('micStateChange: ' + JSON.stringify(muteState)); + this.globalIsOn = !muteState; }) } } @@ -579,17 +571,7 @@ struct applicationItem { .padding({ right: 0 }) .onChange((isOn: boolean) => { if (isOn) { - if (this.currentGroup == 'CAMERA') { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); - cameraManager.muteCameraPersistent(false, camera.PolicyType.PRIVACY); - } else { - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager.getVolumeManager(); - let groupId = audio.DEFAULT_VOLUME_GROUP_ID; - audioVolumeManager.getVolumeGroupManager(groupId).then(audioVolumeGroupManager => { - audioVolumeGroupManager.setMicMutePersistent(false, audio.PolicyType.PRIVACY); - }) - } + GlobalDialogModel.getInstance().setMuteState(this.context, this.currentGroup, false, false); } }) Row().onClick(() => { diff --git a/permissionmanager/src/main/ets/pages/globalSwitch.ets b/permissionmanager/src/main/ets/pages/globalSwitch.ets index ecf7f7e6dca4c044ce35974ce35063a4d641d598..1d0f70acd6d1e03513a9921f2ad63c64c10662eb 100644 --- a/permissionmanager/src/main/ets/pages/globalSwitch.ets +++ b/permissionmanager/src/main/ets/pages/globalSwitch.ets @@ -14,22 +14,20 @@ */ import Constants from '../common/utils/constant'; -import audio from '@ohos.multimedia.audio'; -import camera from '@ohos.multimedia.camera'; import common from '@ohos.app.ability.common'; import { CustomContentDialog } from '@ohos.arkui.advanced.Dialog'; -import { Log } from '../common/utils/utils'; -import { GlobalContext } from '../common/utils/globalContext'; -import { BusinessError } from '@kit.BasicServicesKit'; +import { GlobalDialogModel } from '../GlobalExtAbility/GlobalDialogModel'; +import { GlobalDialogViewState } from '../GlobalExtAbility/GlobalDialogViewState'; -const MICROPHONE = 'microphone'; -const CAMERA = 'camera'; +let storage = LocalStorage.getShared(); +let globalState: string = storage.get('globalState') as string; +let globalDialogModel: GlobalDialogModel = new GlobalDialogModel(); @Entry @Component struct globalSwitch { private context = getContext(this) as common.ServiceExtensionContext; - @State globalState: string = GlobalContext.load('globalState'); + @State viewState: GlobalDialogViewState = globalDialogModel.initViewState(globalState); dialogController: CustomDialogController | null = new CustomDialogController({ builder: CustomContentDialog({ @@ -42,7 +40,6 @@ struct globalSwitch { value: $r('app.string.cancel'), buttonStyle: ButtonStyleMode.TEXTUAL, action: () => { - Log.info('global cancel'); this.context.terminateSelf(); } }, @@ -50,34 +47,7 @@ struct globalSwitch { value: $r('app.string.open'), buttonStyle: ButtonStyleMode.TEXTUAL, action: () => { - Log.info('global accept'); - if (this.globalState == MICROPHONE) { - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager.getVolumeManager(); - audioVolumeManager.getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID).then(audioVolumeGroupManager => { - audioVolumeGroupManager.setMicMutePersistent(false, audio.PolicyType.PRIVACY).then(() => { - this.context.terminateSelf(); - }) - }).catch((err: BusinessError) => { - Log.error(`getVolumeGroupManager failed: ${JSON.stringify(err)}`); - }) - } else if (this.globalState == CAMERA) { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); - cameraManager.muteCameraPersistent(false, camera.PolicyType.PRIVACY); - this.context.terminateSelf(); - } else { - let cameraManager = camera.getCameraManager(GlobalContext.load('context')); - cameraManager.muteCameraPersistent(false, camera.PolicyType.PRIVACY); - let audioManager = audio.getAudioManager(); - let audioVolumeManager = audioManager.getVolumeManager(); - audioVolumeManager.getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID).then(audioVolumeGroupManager => { - audioVolumeGroupManager.setMicMutePersistent(false, audio.PolicyType.PRIVACY).then(() => { - this.context.terminateSelf(); - }) - }).catch((err: BusinessError) => { - Log.error(`getVolumeGroupManager failed: ${JSON.stringify(err)}`); - }) - } + globalDialogModel.setMuteState(this.context, globalState, false, true); } } ], @@ -92,18 +62,14 @@ struct globalSwitch { buildContent(): void { Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { Column() { - Text(this.globalState == MICROPHONE ? $r('app.string.global_title_microphone') : - this.globalState == CAMERA ? $r('app.string.global_title_camera') : - $r('app.string.global_title_camera_and_microphone')) + Text(this.viewState.title) .fontSize(Constants.TEXT_BIG_FONT_SIZE) .fontColor($r('sys.color.font_primary')) .fontWeight(FontWeight.Medium) .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT) .width(Constants.FULL_WIDTH) .padding({ top: Constants.PADDING_14, bottom: Constants.PADDING_14 }) - Text(this.globalState == MICROPHONE ? $r('app.string.global_desc_microphone') : - this.globalState == CAMERA ? $r('app.string.global_desc_camera') : - $r('app.string.global_desc_camera_and_microphone')) + Text(this.viewState.text) .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) .fontColor($r('sys.color.font_primary')) .lineHeight(Constants.TEXT_LINE_HEIGHT) @@ -115,7 +81,6 @@ struct globalSwitch { build() {} aboutToAppear() { - Log.info('global aboutToAppear'); this.dialogController?.open(); } diff --git a/permissionmanager/src/main/module.json b/permissionmanager/src/main/module.json index b4e8048db26bf10c1b83ad2677734340dee39ce8..2d19d18660e1f020b02c788254a07fde1dc896e0 100644 --- a/permissionmanager/src/main/module.json +++ b/permissionmanager/src/main/module.json @@ -62,7 +62,7 @@ { "icon": "$media:app_icon", "name": "com.ohos.permissionmanager.GlobalExtAbility", - "srcEntry": "./ets/GlobalExtAbility/GlobalExtAbility.ts", + "srcEntry": "./ets/GlobalExtAbility/GlobalExtAbility.ets", "type": "service", "exported": true, "permissions": ["ohos.permission.GET_SENSITIVE_PERMISSIONS"] diff --git a/permissionmanager/src/main/module.json5 b/permissionmanager/src/main/module.json5 index 3dc50f0f9856b69d4bfe5241868ca8fa2831b965..3f1705373ae9edf315f16586bfcb810e8bac1e68 100644 --- a/permissionmanager/src/main/module.json5 +++ b/permissionmanager/src/main/module.json5 @@ -74,7 +74,7 @@ { "icon": "$media:app_icon", "name": "com.ohos.permissionmanager.GlobalExtAbility", - "srcEntry": "./ets/GlobalExtAbility/GlobalExtAbility.ts", + "srcEntry": "./ets/GlobalExtAbility/GlobalExtAbility.ets", "type": "service", "exported": true, "permissions": ["ohos.permission.GET_SENSITIVE_PERMISSIONS"] diff --git a/permissionmanager/src/ohosTest/ets/test/GlobalExtAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/GlobalExtAbility.test.ets deleted file mode 100644 index 7f22e9eec080f5278df00557eb01b06b21d04ee6..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/GlobalExtAbility.test.ets +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { Want } from '@kit.AbilityKit'; -import { Log } from '../../../main/ets/common/utils/utils'; -import GlobalExtensionAbility from '../../../main/ets/GlobalExtAbility/GlobalExtAbility'; - -let want1: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone' - } -}; - -let want2: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'camera' - } -}; - -export default function GlobalExtAbilityTest() { - describe('GlobalExtAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('GlobalExtAbilityTest_000', 0, () => { - Log.info(`GlobalExtAbilityTest_000 begin`); - let ability = new GlobalExtensionAbility(); - ability.onCreate(want1); - }) - - it('GlobalExtAbilityTest_001', 0, () => { - Log.info(`GlobalExtAbilityTest_001 begin`); - let ability = new GlobalExtensionAbility(); - ability.onCreate(want2); - }) - - it('GlobalExtAbilityTest_002', 0, () => { - Log.info(`GlobalExtAbilityTest_002 begin`); - let ability = new GlobalExtensionAbility(); - ability.onRequest(want1, 1); - }) - - it('GlobalExtAbilityTest_003', 0, () => { - Log.info(`GlobalExtAbilityTest_003 begin`); - let ability = new GlobalExtensionAbility(); - ability.onDestroy(); - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/GlobalSwitchSheetAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/GlobalSwitchSheetAbility.test.ets deleted file mode 100644 index cf1f4ec453ef1c27cc2a98c35dc5c5d94088b02e..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/GlobalSwitchSheetAbility.test.ets +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { Want } from '@kit.AbilityKit'; -import { Log } from '../../../main/ets/common/utils/utils'; -import GlobalSwitchSheetAbility from '../../../main/ets/PermissionSheet/GlobalSwitchSheetAbility'; -import AbilityConstant from '@ohos.app.ability.AbilityConstant'; -import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; - -let want: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone', - 'ohos.display.id': 0 - } -}; - -export default function GlobalSwitchSheetAbilityTest() { - describe('GlobalSwitchSheetAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('GlobalSwitchSheetAbilityTest_000', 0, () => { - Log.info(`GlobalSwitchSheetAbilityTest_000 begin`); - let ability = new GlobalSwitchSheetAbility(); - let param: AbilityConstant.LaunchParam = { - launchReason: 0, - lastExitReason: 0, - lastExitMessage: '' - }; - ability.onCreate(param); - }) - - it('GlobalSwitchSheetAbilityTest_002', 0, () => { - Log.info(`GlobalSwitchSheetAbilityTest_002 begin`); - let ability = new GlobalSwitchSheetAbility(); - ability.onForeground(); - }) - - it('GlobalSwitchSheetAbilityTest_003', 0, () => { - Log.info(`GlobalSwitchSheetAbilityTest_003 begin`); - let ability = new GlobalSwitchSheetAbility(); - ability.onBackground(); - }) - - it('GlobalSwitchSheetAbilityTest_004', 0, () => { - Log.info(`GlobalSwitchSheetAbilityTest_004 begin`); - let ability = new GlobalSwitchSheetAbility(); - ability.onSessionDestroy(); - }) - - it('GlobalSwitchSheetAbilityTest_005', 0, () => { - Log.info(`GlobalSwitchSheetAbilityTest_005 begin`); - let ability = new GlobalSwitchSheetAbility(); - ability.onDestroy() - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/List.test.ets b/permissionmanager/src/ohosTest/ets/test/List.test.ets index 706e0bebb836bb0f7d72d344af2235a10e169ca4..96293045102443300797cbd77b78df19c4d0d51b 100644 --- a/permissionmanager/src/ohosTest/ets/test/List.test.ets +++ b/permissionmanager/src/ohosTest/ets/test/List.test.ets @@ -13,20 +13,8 @@ * limitations under the License. */ -import GlobalExtAbilityTest from './GlobalExtAbility.test' -import MainAbilityTest from './MainAbility.test' -import SecurityExtAbilityTest from './SecurityExtAbility.test'; -import ServiceExtAbilityTest from './ServiceExtAbility.test'; -import GlobalSwitchSheetAbilityTest from './GlobalSwitchSheetAbility.test'; -import PermissionStateSheetAbilityTest from './PermissionStateSheetAbility.test' import utilsTest from './utils.test'; export default function testsuite() { - GlobalExtAbilityTest(); - MainAbilityTest(); - SecurityExtAbilityTest(); - ServiceExtAbilityTest(); - GlobalSwitchSheetAbilityTest(); - PermissionStateSheetAbilityTest() utilsTest(); } \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/MainAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/MainAbility.test.ets deleted file mode 100644 index a7da61447c8de25c252e1043b09229b27ce88f7f..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/MainAbility.test.ets +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' -import { Want } from '@kit.AbilityKit'; -import { Log } from '../../../main/ets/common/utils/utils'; -import MainAbility from '../../../main/ets/MainAbility/MainAbility'; -import window from '@ohos.window'; - -let want: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone', - 'ohos.display.id': 0 - } -}; - -// let stage: window.WindowStage = window.createWindow(); - -export default function MainAbilityTest() { - describe('MainAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('MainAbilityTest_000', 0, () => { - Log.info(`MainAbilityTest_000 begin`); - let ability = new MainAbility(); - ability.onCreate(want, null); - }) - - it('MainAbilityTest_002', 0, () => { - Log.info(`MainAbilityTest_002 begin`); - let ability = new MainAbility(); - ability.onNewWant(want); - }) - - it('MainAbilityTest_003', 0, () => { - Log.info(`MainAbilityTest_003 begin`); - let ability = new MainAbility(); - ability.onForeground(); - }) - - it('MainAbilityTest_004', 0, () => { - Log.info(`MainAbilityTest_004 begin`); - let ability = new MainAbility(); - ability.onBackground(); - }) - - it('MainAbilityTest_005', 0, () => { - Log.info(`MainAbilityTest_005 begin`); - let ability = new MainAbility(); - ability.onWindowStageDestroy(); - }) - - it('MainAbilityTest_006', 0, () => { - Log.info(`MainAbilityTest_006 begin`); - let ability = new MainAbility(); - ability.onDestroy(); - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/PermissionStateSheetAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/PermissionStateSheetAbility.test.ets deleted file mode 100644 index 0d0bca3e2028839f30b0b15927c8e2b4ac2cadae..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/PermissionStateSheetAbility.test.ets +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { Want } from '@kit.AbilityKit'; -import { Log } from '../../../main/ets/common/utils/utils'; -import PermissionStateSheetAbility from '../../../main/ets/PermissionSheet/PermissionStateSheetAbility'; -import AbilityConstant from '@ohos.app.ability.AbilityConstant'; -import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; - -let want: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone', - 'ohos.display.id': 0 - } -}; - -export default function PermissionStateSheetAbilityTest() { - describe('PermissionStateSheetAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('PermissionStateSheetAbilityTest_000', 0, () => { - Log.info(`PermissionStateSheetAbilityTest_000 begin`); - let ability = new PermissionStateSheetAbility(); - let param: AbilityConstant.LaunchParam = { - launchReason: 0, - lastExitReason: 0, - lastExitMessage: '' - }; - ability.onCreate(param); - }) - - it('PermissionStateSheetAbilityTest_002', 0, () => { - Log.info(`PermissionStateSheetAbilityTest_002 begin`); - let ability = new PermissionStateSheetAbility(); - ability.onForeground(); - }) - - it('PermissionStateSheetAbilityTest_003', 0, () => { - Log.info(`PermissionStateSheetAbilityTest_003 begin`); - let ability = new PermissionStateSheetAbility(); - ability.onBackground(); - }) - - it('PermissionStateSheetAbilityTest_004', 0, () => { - Log.info(`PermissionStateSheetAbilityTest_004 begin`); - let ability = new PermissionStateSheetAbility(); - ability.onSessionDestroy(); - }) - - it('PermissionStateSheetAbilityTest_005', 0, () => { - Log.info(`PermissionStateSheetAbilityTest_005 begin`); - let ability = new PermissionStateSheetAbility(); - ability.onDestroy() - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/SecurityExtAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/SecurityExtAbility.test.ets deleted file mode 100644 index 41b619182b2759c59ec481bdc6c9e5decda46d56..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/SecurityExtAbility.test.ets +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { Want } from '@kit.AbilityKit'; -import { Configuration } from '@ohos.app.ability.Configuration'; -import { Log } from '../../../main/ets/common/utils/utils'; -import SecurityExtAbility from '../../../main/ets/SecurityExtAbility/SecurityExtAbility'; - -let want: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone', - 'ohos.display.id': 0 - } -}; - -let configuration: Configuration = { - language: '' -} - -export default function SecurityExtAbilityTest() { - describe('SecurityExtAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('SecurityExtAbilityTest_000', 0, () => { - Log.info(`SecurityExtAbilityTest_000 begin`); - let ability = new SecurityExtAbility(); - ability.onCreate(want); - }) - - it('SecurityExtAbilityTest_001', 0, () => { - Log.info(`SecurityExtAbilityTest_001 begin`); - let ability = new SecurityExtAbility(); - ability.onRequest(want, 1); - }) - - it('SecurityExtAbilityTest_002', 0, () => { - Log.info(`SecurityExtAbilityTest_002 begin`); - let ability = new SecurityExtAbility(); - ability.onConfigurationUpdate(configuration); - }) - - it('SecurityExtAbilityTest_003', 0, () => { - Log.info(`SecurityExtAbilityTest_003 begin`); - let ability = new SecurityExtAbility(); - ability.onDestroy(); - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/ServiceExtAbility.test.ets b/permissionmanager/src/ohosTest/ets/test/ServiceExtAbility.test.ets deleted file mode 100644 index e8fe9fa88e99419d301fe28c62a29497ada62ebf..0000000000000000000000000000000000000000 --- a/permissionmanager/src/ohosTest/ets/test/ServiceExtAbility.test.ets +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { Want } from '@kit.AbilityKit'; -import { Configuration } from '@ohos.app.ability.Configuration'; -import { Log } from '../../../main/ets/common/utils/utils'; -import ServiceExtAbility from '../../../main/ets/ServiceExtAbility/ServiceExtAbility'; - -let want: Want = { - abilityName: '', - parameters: { - 'ohos.sensitive.resource': 'microphone', - 'ohos.display.id': 0 - } -}; - -export default function ServiceExtAbilityTest() { - describe('ServiceExtAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - - it('ServiceExtAbilityTest_000', 0, () => { - Log.info(`ServiceExtAbilityTest_000 begin`); - let ability = new ServiceExtAbility(); - ability.onCreate(want); - }) - - it('ServiceExtAbilityTest_001', 0, () => { - Log.info(`ServiceExtAbilityTest_001 begin`); - let ability = new ServiceExtAbility(); - ability.onRequest(want, 1); - }) - - it('ServiceExtAbilityTest_002', 0, () => { - Log.info(`ServiceExtAbilityTest_002 begin`); - let ability = new ServiceExtAbility(); - ability.onDestroy(); - }) - }) -} \ No newline at end of file diff --git a/permissionmanager/src/ohosTest/ets/test/utils.test.ets b/permissionmanager/src/ohosTest/ets/test/utils.test.ets index bd376f2d2f844376e19d984f2f0e8291454b1c93..144a0cbb2abccd197c51545f4abdf866e3459ff5 100644 --- a/permissionmanager/src/ohosTest/ets/test/utils.test.ets +++ b/permissionmanager/src/ohosTest/ets/test/utils.test.ets @@ -14,20 +14,18 @@ */ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType } from '@ohos/hypium'; -import { AppInfo } from '../../../main/ets/common/model/typedef'; import { Permission } from '../../../main/ets/common/model/definition'; -import Constants from '../../../main/ets/common/utils/constant'; import { Log, verifyAccessToken, titleTrim, - addLocalTag, getPermissionLabel, getPermissionGroup, getPermissionGroupByName, getGroupIdByPermission, checkPermissionGroup, - getFontSizeScale + getFontSizeScale, + supportPermission } from '../../../main/ets/common/utils/utils'; import { abilityAccessCtrl, bundleManager } from '@kit.AbilityKit'; @@ -59,6 +57,8 @@ export default function utilsTest() { let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag); let status = await verifyAccessToken(bundleInfo.appInfo.accessTokenId, 'ohos.permission.MICROPHONE'); expect(status).assertEqual(abilityAccessCtrl.GrantStatus.PERMISSION_DENIED); + let status1 = await verifyAccessToken(bundleInfo.appInfo.accessTokenId, 'ohos.permission.GET_BUNDLE_INFO'); + expect(status1).assertEqual(abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED); }) it('UtilsTest_001', TestType.FUNCTION, () => { @@ -68,38 +68,14 @@ export default function utilsTest() { expect(title).assertEqual(testUri); }) - it('UtilsTest_002', TestType.FUNCTION, async () => { - Log.info(`UtilsTest_002 begin`); - let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; - let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag); - let reqPermissions: Array = []; - bundleInfo.reqPermissionDetails.forEach(item => { - reqPermissions.push(item.name as Permission); - }); - let info = new AppInfo( - bundleInfo.name, - bundleInfo.targetVersion, - bundleInfo.appInfo.accessTokenId, - '', - bundleInfo.appInfo.iconId, - bundleInfo.appInfo.iconResource, - '', - bundleInfo.appInfo.labelId, - bundleInfo.appInfo.labelResource, - reqPermissions, - [], - '', - '', - '' - ); - addLocalTag(info); - }) - it('UtilsTest_004', TestType.FUNCTION, () => { Log.info(`UtilsTest_004 begin`); let testUri = Permission.MICROPHONE; let label = getPermissionLabel(testUri); expect(JSON.stringify(label)).assertEqual(JSON.stringify($r('sys.string.ohos_lab_microphone'))); + let testUri1 = ''; + let label1 = getPermissionLabel(testUri1 as Permission); + expect(label1).assertEqual(''); }) it('UtilsTest_005', TestType.FUNCTION, () => { @@ -107,6 +83,9 @@ export default function utilsTest() { let testUri = Permission.MICROPHONE; let groupInfo = getPermissionGroup(testUri); expect(groupInfo.permissions).assertContain(testUri); + let testUri1 = ''; + let groupInfo1 = getPermissionGroup(testUri1 as Permission); + expect(groupInfo1.label).assertEqual(''); }) it('UtilsTest_006', TestType.FUNCTION, () => { @@ -114,6 +93,9 @@ export default function utilsTest() { let testGroup = 'MICROPHONE'; let groupInfo = getPermissionGroupByName(testGroup); expect(groupInfo.name).assertEqual(testGroup); + let testGroup1 = ''; + let groupInfo1 = getPermissionGroupByName(testGroup1); + expect(groupInfo1.name).assertEqual('LOCATION'); }) it('UtilsTest_007', TestType.FUNCTION, () => { @@ -121,6 +103,9 @@ export default function utilsTest() { let testUri = Permission.MICROPHONE; let groupId = getGroupIdByPermission(testUri); expect(groupId).assertEqual(2); + let testUri1 = ''; + let groupId1 = getGroupIdByPermission(testUri1 as Permission); + expect(groupId1).assertEqual(-1); }) it('UtilsTest_010', TestType.FUNCTION, () => { @@ -128,6 +113,12 @@ export default function utilsTest() { let testUri = [Permission.MICROPHONE]; let groupName = checkPermissionGroup(testUri); expect(groupName).assertEqual('MICROPHONE'); + let testUri1 = []; + let groupName1 = checkPermissionGroup(testUri1); + expect(groupName1).assertEqual(null); + let testUri2 = ['' as Permission]; + let groupName2 = checkPermissionGroup(testUri2); + expect(groupName2).assertEqual(null); }) it('UtilsTest_012', TestType.FUNCTION, () => { @@ -138,9 +129,8 @@ export default function utilsTest() { it('UtilsTest_013', TestType.FUNCTION, () => { Log.info(`UtilsTest_013 begin`); - Log.warn(`UtilsTest_013 begin`); - Log.debug(`UtilsTest_013 begin`); - Log.error(`UtilsTest_013 begin`); + let support = supportPermission(); + expect(support).assertContain(Permission.CAMERA); }) }) } \ No newline at end of file