1/** 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import LogUtils from '../common/utils/LogUtils'; 17import UserAuthExtensionAbility from '@ohos.app.ability.UserAuthExtensionAbility'; 18import WindowPrivacyUtils from '../common/utils/WindowPrivacyUtils'; 19import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 20import Constants, { WantParams } from '../common/vm/Constants'; 21import type Want from '@ohos.app.ability.Want'; 22import AuthUtils from '../common/utils/AuthUtils'; 23 24const TAG = 'UserAuthAbility'; 25// The current interface only support string type 26const TRANSPARENT_COLOR = '#00000000'; 27const MASK_THIN_COLOR = '#33182431'; 28 29export default class UserAuthAbility extends UserAuthExtensionAbility { 30 onCreate(): void { 31 LogUtils.info(TAG, 'UserAuthExtensionAbility onCreate'); 32 AppStorage.setOrCreate('context', this.context); 33 } 34 35 onForeground(): void { 36 LogUtils.info(TAG, 'UserAuthExtensionAbility onForeground'); 37 } 38 39 onBackground(): void { 40 LogUtils.info(TAG, 'UserAuthExtensionAbility onBackground'); 41 (AppStorage.get('session') as UIExtensionContentSession)?.terminateSelf(); 42 } 43 44 onDestroy(): void | Promise<void> { 45 LogUtils.info(TAG, 'UserAuthExtensionAbility onDestroy'); 46 } 47 48 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 49 LogUtils.info(TAG, 'UserAuthExtensionAbility onSessionCreate'); 50 AppStorage.setOrCreate('wantParams', want?.parameters?.useriamCmdData); 51 AppStorage.setOrCreate('session', session); 52 (session as UIExtensionContentSession)?.loadContent('pages/Index'); 53 try { 54 if ((AppStorage.get('wantParams') as WantParams)?.windowModeType === 'DIALOG_BOX') { 55 (session as UIExtensionContentSession)?.setWindowBackgroundColor(MASK_THIN_COLOR); 56 } else { 57 (session as UIExtensionContentSession)?.setWindowBackgroundColor(TRANSPARENT_COLOR); 58 } 59 } catch (error) { 60 LogUtils.error(TAG, 'UserAuthExtensionAbility onSessionCreate error: ' + error?.code); 61 (session as UIExtensionContentSession)?.terminateSelf(); 62 } 63 WindowPrivacyUtils.setWindowPrivacyMode(session, true); 64 } 65 66 onSessionDestroy(session): void { 67 LogUtils.info(TAG, 'UserAuthExtensionAbility onSessionDestroy'); 68 AuthUtils.getInstance().sendNotice(Constants.noticeEventProcessTerminate, [Constants.noticeTypePin]); 69 WindowPrivacyUtils.setWindowPrivacyMode(session, false); 70 } 71} 72