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 { WantParams } from '../common/vm/Constants';
21
22const TAG = 'UserAuthAbility';
23// The current interface only support string type
24const TRANSPARENT_COLOR = '#00000000';
25const MASK_THIN_COLOR = '#33182431';
26
27export default class UserAuthAbility extends UserAuthExtensionAbility {
28  onCreate() {
29    LogUtils.info(TAG, 'UserAuthExtensionAbility onCreate');
30    AppStorage.setOrCreate("context", this.context);
31  }
32
33  onForeground(): void {
34    LogUtils.info(TAG, 'UserAuthExtensionAbility onForeground');
35  }
36
37  onBackground(): void {
38    LogUtils.info(TAG, 'UserAuthExtensionAbility onBackground');
39    (AppStorage.get("session") as UIExtensionContentSession)?.terminateSelf();
40  }
41
42  onDestroy(): void | Promise<void> {
43    LogUtils.info(TAG, 'UserAuthExtensionAbility onDestroy');
44  }
45
46  onSessionCreate(want, session): void {
47    LogUtils.info(TAG, 'UserAuthExtensionAbility onSessionCreate');
48    AppStorage.setOrCreate("wantParams", want?.parameters?.useriamCmdData);
49    AppStorage.setOrCreate("session", session);
50    (session as UIExtensionContentSession)?.loadContent('pages/Index');
51    try {
52      if ((AppStorage.get("wantParams") as WantParams)?.windowModeType === 'DIALOG_BOX') {
53        (session as UIExtensionContentSession)?.setWindowBackgroundColor(MASK_THIN_COLOR);
54      } else {
55        (session as UIExtensionContentSession)?.setWindowBackgroundColor(TRANSPARENT_COLOR);
56      }
57    } catch (error) {
58      LogUtils.error(TAG, 'UserAuthExtensionAbility onSessionCreate error: ' + error?.code);
59      (session as UIExtensionContentSession)?.terminateSelf();
60    }
61    WindowPrivacyUtils.setWindowPrivacyMode(session, true);
62  }
63
64  onSessionDestroy(session): void {
65    LogUtils.info(TAG, 'UserAuthExtensionAbility onSessionDestroy');
66    WindowPrivacyUtils.setWindowPrivacyMode(session, false);
67  }
68}
69