• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { BusinessError } from '@ohos.base';
17import common from '@ohos.app.ability.common';
18import Constants from '../constant';
19import CounterLock from '../CounterLock';
20import GlobalContext from '../GlobalContext';
21import deviceInfo from '@ohos.deviceInfo';
22import { HiLog } from '../HiLog';
23import TerminateView from '../../OpenDlpFile/TerminateView/TerminateView';
24import { UIContext } from '@kit.ArkUI';
25import UIContextUtil from '../UIContextUtil';
26import { OpenDlpFileError } from '../../OpenDlpFile/common/OpenDlpFileError/OpenDlpFileError';
27
28const TAG = 'AlertMessage';
29
30export class GetAlertMessage {
31  private static _counterLock: CounterLock = new CounterLock();
32
33  private static async showToast(
34    context: common.ServiceExtensionContext | common.UIAbilityContext | common.UIExtensionContext,
35    error: BusinessError
36  ): Promise<void> {
37    try {
38      const resource = GetAlertMessage.getToastMessage(error);
39      const toastMessage = await context.resourceManager.getStringValue(resource);
40      const uiContext = UIContextUtil.getInstance().getUIContext(context);
41      if (!uiContext) {
42        HiLog.error(TAG, 'Get UIContext failed.');
43        return;
44      }
45      uiContext.getPromptAction().showToast({
46        message: toastMessage,
47        duration: Constants.SHARE_SET_TIMEOUT,
48        bottom: Constants.TOAST_BOTTOM
49      });
50      HiLog.info(TAG, 'showToast success');
51    } catch (error) {
52      HiLog.wrapError(TAG, error, 'showToast failed');
53    }
54  }
55
56  public static async viewAbilityPhoneToast(
57    context: common.ServiceExtensionContext,
58    error: BusinessError
59  ): Promise<void> {
60    await GetAlertMessage._counterLock.acquire();
61    let viewAbilityPhoneToastCount = TerminateView.getViewAbilityPhoneToastCount();
62    HiLog.debug(TAG, `showToast ++ ToastCount ${viewAbilityPhoneToastCount}`);
63    TerminateView.setViewAbilityPhoneToastCount(++viewAbilityPhoneToastCount);
64    GetAlertMessage._counterLock.release();
65    await GetAlertMessage.showToast(context, error);
66    setTimeout(async () => {
67      await GetAlertMessage._counterLock.acquire();
68      try {
69        let viewAbilityPhoneToastCount = TerminateView.getViewAbilityPhoneToastCount();
70        HiLog.debug(TAG, `showToast -- ToastCount ${viewAbilityPhoneToastCount}`);
71        TerminateView.setViewAbilityPhoneToastCount(--viewAbilityPhoneToastCount);
72        if (viewAbilityPhoneToastCount === 0) {
73          UIContext.destroyUIContextWithoutWindow();
74          await TerminateView.terminate();
75        }
76      } finally {
77        GetAlertMessage._counterLock.release();
78      }
79    }, Constants.SHARE_SET_TIMEOUT);
80  }
81
82  public static async phoneHandle(
83    context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext,
84    error: BusinessError
85  ): Promise<void> {
86    await GetAlertMessage.showToast(context, error);
87    setTimeout(async () => {
88      try {
89        UIContext.destroyUIContextWithoutWindow();
90        await context.terminateSelf();
91      } catch (error) {
92        HiLog.wrapError(TAG, error, 'phoneHandle terminateSelf error');
93      }
94    }, Constants.SHARE_SET_TIMEOUT);
95  }
96
97  public static getAlertMessage(err: BusinessError, defaultTitle?: Resource, defaultMessage?: Resource) {
98    switch (err && err.code) {
99      case Constants.ERR_JS_ACCOUNT_NOT_FOUND:
100      case Constants.ERR_JS_GET_ACCOUNT_ERROR:
101        return { 'msg': $r('app.string.MESSAGE_APP_GET_ACCOUNT_ERROR') } as Record<string, Resource>;
102      case Constants.ERR_JS_APP_NO_ACCOUNT_ERROR:
103      case Constants.ERR_JS_ACCOUNT_NOT_LOGIN:
104        return { 'msg': $r('app.string.MESSAGE_APP_NO_ACCOUNT_ERROR') } as Record<string, Resource>;
105      case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR:
106        return { 'msg': $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR') } as Record<string, Resource>;
107      case Constants.ERR_JS_APP_PERMISSION_DENY:
108      case Constants.ERR_JS_RELEASE_FILE_OPEN:
109      case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION:
110        return { 'msg': $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION') } as Record<string, Resource>;
111      case Constants.ERR_JS_NETWORK_INVALID:
112      case Constants.ERR_JS_APP_NETWORK_INVALID:
113        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
114      case Constants.ERR_JS_CREDENTIAL_SERVICE_ERROR:
115      case Constants.ERR_JS_CREDENTIAL_SERVER_ERROR:
116      case Constants.ERR_JS_CREDENTIAL_TIMEOUT:
117      case Constants.ERR_JS_APP_INSIDE_ERROR:
118        return { 'msg': $r('app.string.File_cannot_be_opened') } as Record<string, Resource>;
119      case Constants.ERR_JS_NOT_DLP_FILE:
120        return { 'msg': $r('app.string.File_damaged') } as Record<string, Resource>;
121      case Constants.ERR_JS_USER_NO_PERMISSION_2C:
122        return { 'msg': $r('app.string.File_no_permission') } as Record<string, Resource>;
123      case Constants.ERR_JS_OTHER_APP_OPEN_FILE:
124        return { 'msg': $r('app.string.This_File_is_Open_By_Other_App') } as Record<string, Resource>;
125      case Constants.ERR_JS_OFFLINE:
126        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
127      case Constants.SHARE_NO_SPACE_LEFT_ON_DEVICE:
128      case Constants.ERR_JS_NO_SPACE:
129        return { 'msg': $r('app.string.MESSAGE_NO_SPACE_ERROR') } as Record<string, Resource>;
130      default:
131        if (defaultTitle !== undefined && defaultMessage !== undefined) {
132          return { 'title': defaultTitle, 'msg': defaultMessage } as Record<string, Resource>;
133        } else {
134          return { 'msg': $r('app.string.File_cannot_be_opened') } as Record<string, Resource>;
135        }
136    }
137  }
138
139  public static getAlertTitleMessage(err: BusinessError) {
140    switch (err && err.code) {
141      case Constants.ERR_JS_USER_NO_PERMISSION_2B:
142        return {
143          'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'),
144          'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM_VISIT', err.message.split(', contact:')?.[1])
145        } as Record<string, Resource>;
146      case Constants.ERR_JS_APP_PARAM_ERROR:
147        return {
148          'title': $r('app.string.TITLE_APP_ERROR'),
149          'msg': $r('app.string.MESSAGE_APP_PARAM_ERROR')
150        } as Record<string, Resource>;
151      case Constants.ERR_JS_APP_OPEN_REJECTED:
152        return {
153          'title': $r('app.string.header_title'),
154          'msg': $r('app.string.MESSAGE_DLP_OPEN_REJECT')
155        } as Record<string, Resource>;
156      case Constants.ERR_JS_APP_ENCRYPTION_REJECTED:
157        return {
158          'title': $r('app.string.header_title'),
159          'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION_REJECTED')
160        } as Record<string, Resource>;
161      case Constants.ERR_JS_APP_ENCRYPTING:
162        // not available in dialog, only in Session error.
163        return {
164          'title': $r('app.string.header_title'),
165          'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION', err.data)
166        } as Record<string, Resource>;
167      case Constants.ERR_JS_FILE_EXPIRATION:
168        return {
169          'title': $r('app.string.Permission_has_expired'),
170          'msg': $r('app.string.Permission_has_expired_description', err.message.split(', contact:')?.[1])
171        } as Record<string, Resource>;
172      case Constants.ERR_JS_DLP_FILE_READ_ONLY:
173        return {
174          'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'),
175          'msg': $r('app.string.MESSAGE_DLP_READ_ONLY', AppStorage.get('contactAccount'))
176        } as Record<string, Resource>;
177      case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED:
178        return {
179          'title': $r('app.string.TITLE_APP_VERSION_LOWER'),
180          'msg': $r('app.string.Share_File_System_Version_Low')
181        } as Record<string, Resource>;
182      default:
183        return {
184          'title': $r('app.string.header_title'),
185          'msg': $r('app.string.File_cannot_be_opened')
186        } as Record<string, Resource>;
187    }
188  }
189
190  public static getAlertButtonMessage(err: BusinessError) {
191    switch (err && err.code) {
192      case Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED:
193        return {
194          'title': $r('app.string.header_title'),
195          'msg': $r('app.string.MESSAGE_DLP_SYSTEM_IS_AUTHENTICATED'),
196          'cancel': $r('app.string.ban'),
197          'ok': $r('app.string.SYSTEM_IS_AUTHENTICATED_LOGIN')
198        } as Record<string, Resource>;
199      default:
200        return {
201          'title': $r('app.string.header_title'),
202          'msg': $r('app.string.File_cannot_be_opened')
203        } as Record<string, Resource>;
204    }
205  }
206
207  public static getToastMessage(err: BusinessError) {
208    switch (err && err.code) {
209      case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR:
210        return $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR');
211      case Constants.ERR_JS_NETWORK_INVALID:
212      case Constants.ERR_JS_APP_NETWORK_INVALID:
213      case Constants.ERR_JS_OFFLINE:
214        return $r('app.string.network_invalid');
215      case Constants.ERR_JS_NOT_DLP_FILE:
216        return $r('app.string.File_damaged');
217      case Constants.ERR_JS_USER_NO_PERMISSION:
218        return $r('app.string.File_no_permission');
219      case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED:
220        return $r('app.string.Share_File_System_Version_Low');
221      case Constants.ERR_JS_APP_CANNOT_OPEN:
222        return $r('app.string.THIS_FILE_NOT_SUPPORT_ENCRYPTION_PROTECTION');
223      case Constants.ERR_JS_OTHER_APP_OPEN_FILE:
224        return $r('app.string.This_File_is_Open_By_Other_App');
225      case Constants.ERR_JS_FILE_EXPIRATION:
226        return $r('app.string.Permission_has_expired');
227      case Constants.SHARE_NO_SPACE_LEFT_ON_DEVICE:
228      case Constants.ERR_JS_NO_SPACE:
229        return $r('app.string.MESSAGE_NO_SPACE_ERROR');
230      case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION:
231        return $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION');
232      case Constants.ERR_JS_USER_NO_PERMISSION_2B:
233        return $r('app.string.MESSAGE_APP_NOT_HAVE_PERM');
234      case Constants.ERR_JS_USER_NO_PERMISSION_2C:
235        return $r('app.string.File_no_permission');
236      default:
237        return $r('app.string.File_cannot_be_opened');
238    }
239  }
240
241  public static async requestModalUIExtension(context: common.UIAbilityContext | common.ServiceExtensionContext,
242    error: BusinessError) {
243    if (deviceInfo.deviceType !== Constants.DEVICE_2IN1 && !OpenDlpFileError.NEED_DIALOG.has(error.code)) {
244      GetAlertMessage.phoneHandle(context, error);
245      return;
246    }
247    // system dialog with UIExtension window, only support one depth want param.
248    let code = error.code;
249    let message = error.message;
250    let uiExtWant: Want = {
251      bundleName: Constants.DLP_MANAGER_BUNDLE_NAME,
252      abilityName: 'DialogUIExtAbility',
253      moduleName: 'entry',
254      parameters: {
255        'bundleName': AppStorage.get('paramCallerBundleName') ?? '',
256        'ability.want.params.uiExtensionType': 'sys/commonUI',
257        'errorCode': code,
258        'errorMessage': message,
259        'accountType': GlobalContext.load('accountType')
260      }
261    };
262    try {
263      await context.requestModalUIExtension(uiExtWant);
264      HiLog.info(TAG, 'requestModalUIExtension success');
265    } catch (err) {
266      let code = (err as BusinessError).code;
267      let message = (err as BusinessError).message;
268      HiLog.error(TAG, `requestModalUIExtension failed. Cause: error code: ${code},error messagae:${message}`);
269    }
270  }
271}