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 userAuth from '@ohos.userIAM.userAuth';
17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
18import Constants from '../vm/Constants';
19import LogUtils from './LogUtils';
20
21const TAG = 'AuthUtils';
22
23export default class AuthUtils {
24  private static authUtilsInstance: AuthUtils;
25
26  public static getInstance(): AuthUtils {
27    if (!AuthUtils.authUtilsInstance) {
28      AuthUtils.authUtilsInstance = new AuthUtils();
29    }
30    return AuthUtils.authUtilsInstance;
31  }
32
33  sendNotice(cmd: string, type: Array<string>): void {
34    try {
35      const eventData = {
36        widgetContextId: AppStorage.get("widgetContextId"),
37        event: cmd,
38        version: Constants.noticeVersion,
39        payload: {
40          type: type
41        }
42      };
43      const jsonEventData = JSON.stringify(eventData);
44      LogUtils.info(TAG, 'sendNotice start eventData: ' + jsonEventData);
45      userAuth.sendNotice(userAuth.NoticeType.WIDGET_NOTICE, jsonEventData);
46      LogUtils.info(TAG, 'sendNotice success');
47    } catch (error) {
48      LogUtils.error(TAG, 'sendNotice catch error: ' + error?.code);
49      (AppStorage.get("session") as UIExtensionContentSession)?.terminateSelf();
50    }
51  }
52}