• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 { common } from '@kit.AbilityKit';
17import Constants from '../../common/constant';
18import OpeningDialogManager from '../manager/OpeningDialogManager';
19import { HiLog } from '../../common/HiLog';
20import { OpenDlpFileManager } from '../manager/OpenDlpFileManager';
21import ViewAbilityService from '../../rpc/ViewAbility/service/ViewAbilityService';
22import ApplyEfficiencyManager from '../manager/ApplyEfficiencyManager';
23
24const TAG = 'TerminateView';
25
26export default class TerminateView {
27  private static _viewAbilityPhoneToastCount: number = 0;
28
29  public static setViewAbilityPhoneToastCount(value: number) {
30    HiLog.debug(TAG, `setViewAbilityPhoneToastCount ${value}`)
31    TerminateView._viewAbilityPhoneToastCount = value;
32  }
33
34  public static getViewAbilityPhoneToastCount(): number {
35    return TerminateView._viewAbilityPhoneToastCount;
36  }
37
38  public static async terminate(): Promise<void> {
39    HiLog.info(TAG, 'TerminateView terminate start');
40    const viewContext = AppStorage.get('viewContext') as common.ServiceExtensionContext;
41    if (!viewContext) {
42      HiLog.error(TAG, 'TerminateView viewContext null');
43      return;
44    }
45    const manager = OpenDlpFileManager.getInstance();
46    const getSizeRet = manager.getHasDecryptedSize();
47    if (getSizeRet.errcode !== Constants.ERR_CODE_SUCCESS) {
48      HiLog.error(TAG, 'TerminateView getHasDecryptedSize error');
49      return;
50    }
51    const hasDecryptedSize = getSizeRet.result;
52    const hasCallback = OpeningDialogManager.getInstance().getHasCallback();
53    const isDecrypting = OpeningDialogManager.getInstance().getIsDecrypting();
54    const toastSize = TerminateView._viewAbilityPhoneToastCount;
55    const isWaitingShowToast = OpeningDialogManager.getInstance().getIsWaitingShowToast();
56    HiLog.info(TAG, `TerminateView hasDecryptedSize: ${hasDecryptedSize}, hasCallback: ${hasCallback},
57    isDecrypting: ${isDecrypting}, toastSize: ${toastSize}, isWaitingShowToast: ${isWaitingShowToast}`);
58    if (hasDecryptedSize === 0 && hasCallback && !isDecrypting && toastSize === 0 && !isWaitingShowToast) {
59      try {
60        ApplyEfficiencyManager.getInstance().releaseEfficiencyResource();
61        OpeningDialogManager.getInstance().setIsTerminalSelf(true);
62        await ViewAbilityService.getInstance().sendDisconnectMsg();
63        await viewContext.terminateSelf();
64        HiLog.info(TAG, 'viewAbility terminateSelf success');
65      } catch (error) {
66        HiLog.wrapError(TAG, error, 'terminateSelf exception');
67      }
68    }
69    OpeningDialogManager.getInstance().setIsTerminalSelf(false);
70  }
71}