• 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 GlobalContext from './GlobalContext';
20import deviceInfo from '@ohos.deviceInfo';
21import promptAction from '@ohos.promptAction';
22import { HiLog } from '../common/HiLog';
23
24const TAG = 'AlertMessage';
25
26export class GetAlertMessage {
27  public static async phoneHandle(
28    context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext,
29    error: BusinessError
30  ) {
31    try {
32      let resource = GetAlertMessage.getToastMessage(error);
33      let toastMessage = await context.resourceManager.getStringValue(resource);
34      promptAction.showToast({
35        message: toastMessage,
36        duration: Constants.SHARE_SET_TIMEOUT
37      });
38      HiLog.info(TAG, 'showToast end.');
39    } catch (error) {
40      HiLog.error(TAG, `showToast error: ${JSON.stringify(error)}`);
41    } finally {
42      setTimeout(() => {
43        context.terminateSelf();
44      }, Constants.SHARE_SET_TIMEOUT);
45    }
46  }
47
48  public static getAlertMessage(err: BusinessError, defaultTitle?: Resource, defaultMessage?: Resource) {
49    switch (err && err.code) {
50      case Constants.ERR_JS_ACCOUNT_NOT_FOUND:
51      case Constants.ERR_JS_GET_ACCOUNT_ERROR:
52        return { 'msg': $r('app.string.MESSAGE_APP_GET_ACCOUNT_ERROR') } as Record<string, Resource>;
53      case Constants.ERR_JS_APP_NO_ACCOUNT_ERROR:
54      case Constants.ERR_JS_ACCOUNT_NOT_LOGIN:
55        return { 'msg': $r('app.string.MESSAGE_APP_NO_ACCOUNT_ERROR') } as Record<string, Resource>;
56      case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR:
57        return { 'msg': $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR') } as Record<string, Resource>;
58      case Constants.ERR_JS_RELEASE_FILE_OPEN:
59      case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION:
60        return { 'msg': $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION') } as Record<string, Resource>;
61      case Constants.ERR_JS_NETWORK_INVALID:
62      case Constants.ERR_JS_APP_NETWORK_INVALID:
63        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
64      case Constants.ERR_JS_CREDENTIAL_SERVICE_ERROR:
65      case Constants.ERR_JS_CREDENTIAL_SERVER_ERROR:
66      case Constants.ERR_JS_CREDENTIAL_TIMEOUT:
67      case Constants.ERR_JS_APP_INSIDE_ERROR:
68        return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>;
69      case Constants.ERR_JS_NOT_DLP_FILE:
70        return { 'msg': $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR') } as Record<string, Resource>;
71      case Constants.ERR_JS_USER_NO_PERMISSION:
72        return { 'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM') } as Record<string, Resource>;
73      case Constants.ERR_JS_OTHER_APP_OPEN_FILE:
74        return { 'msg': $r('app.string.This_File_is_Open_By_Other_App') } as Record<string, Resource>;
75      case Constants.ERR_JS_OFFLINE:
76        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
77      case Constants.ERR_JS_NO_SPACE:
78        return { 'msg': $r('app.string.MESSAGE_NO_SPACE_ERROR') } as Record<string, Resource>;
79      default:
80        if (defaultTitle !== undefined && defaultMessage != undefined) {
81          return { 'title': defaultTitle, 'msg': defaultMessage } as Record<string, Resource>;
82        } else {
83          return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>;
84        }
85    }
86  }
87
88  public static getAlertTitleMessage(err: BusinessError) {
89    switch (err && err.code) {
90      case Constants.ERR_JS_USER_NO_PERMISSION:
91        return {
92          'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'),
93          'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM_VISIT', err.message.split(', contact:')?.[1])
94        } as Record<string, Resource>;
95      case Constants.ERR_JS_APP_PARAM_ERROR:
96        return {
97          'title': $r('app.string.TITLE_APP_ERROR'),
98          'msg': $r('app.string.MESSAGE_APP_PARAM_ERROR')
99        } as Record<string, Resource>;
100      case Constants.ERR_JS_APP_OPEN_REJECTED:
101        return {
102          'title': $r('app.string.header_title'),
103          'msg': $r('app.string.MESSAGE_DLP_OPEN_REJECT')
104        } as Record<string, Resource>;
105      case Constants.ERR_JS_APP_ENCRYPTION_REJECTED:
106        return {
107          'title': $r('app.string.header_title'),
108          'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION_REJECTED')
109        } as Record<string, Resource>;
110      case Constants.ERR_JS_APP_ENCRYPTING:
111        // not available in dialog, only in Session error.
112        return {
113          'title': $r('app.string.header_title'),
114          'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION', err.data)
115        } as Record<string, Resource>;
116      case Constants.ERR_JS_FILE_EXPIRATION:
117        return {
118          'title': $r('app.string.Permission_has_expired'),
119          'msg': $r('app.string.Permission_has_expired_description', err.message.split(', contact:')?.[1])
120        } as Record<string, Resource>;
121      case Constants.ERR_JS_DLP_FILE_READ_ONLY:
122        return {
123          'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'),
124          'msg': $r('app.string.MESSAGE_DLP_READ_ONLY', AppStorage.get('contactAccount'))
125        } as Record<string, Resource>;
126      case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED:
127        return {
128          'title': $r('app.string.TITLE_APP_VERSION_LOWER'),
129          'msg': $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED')
130        } as Record<string, Resource>;
131      default:
132        return {
133          'title': $r('app.string.header_title'),
134          'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR')
135        } as Record<string, Resource>;
136    }
137  }
138
139  public static getAlertButtonMessage(err: BusinessError) {
140    switch (err && err.code) {
141      case Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED:
142        return {
143          'title': $r('app.string.header_title'),
144          'msg': $r('app.string.MESSAGE_DLP_SYSTEM_IS_AUTHENTICATED'),
145          'cancel': $r('app.string.ban'),
146          'ok': $r('app.string.SYSTEM_IS_AUTHENTICATED_LOGIN')
147        } as Record<string, Resource>;
148      default:
149        return {
150          'title': $r('app.string.header_title'),
151          'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR')
152        } as Record<string, Resource>;
153    }
154  }
155
156  public static getToastMessage(err: BusinessError) {
157    switch (err && err.code) {
158      case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR:
159        return $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR');
160      case Constants.ERR_JS_NETWORK_INVALID:
161      case Constants.ERR_JS_APP_NETWORK_INVALID:
162      case Constants.ERR_JS_OFFLINE:
163        return $r('app.string.network_invalid');
164      case Constants.ERR_JS_NOT_DLP_FILE:
165        return $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR');
166      case Constants.ERR_JS_USER_NO_PERMISSION:
167        return $r('app.string.MESSAGE_APP_NOT_HAVE_PERM');
168      case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED:
169        return $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED');
170      case Constants.ERR_JS_APP_CANNOT_OPEN:
171        return $r('app.string.THIS_FILE_NOT_SUPPORT_ENCRYPTION_PROTECTION');
172      case Constants.ERR_JS_OTHER_APP_OPEN_FILE:
173        return $r('app.string.This_File_is_Open_By_Other_App');
174      case Constants.ERR_JS_FILE_EXPIRATION:
175        return $r('app.string.Permission_has_expired');
176      case Constants.ERR_JS_NO_SPACE:
177        return $r('app.string.MESSAGE_NO_SPACE_ERROR');
178      default:
179        return $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR');
180    }
181  }
182
183  public static async requestModalUIExtension(context: common.UIAbilityContext | common.ServiceExtensionContext,
184    error: BusinessError) {
185    if (deviceInfo.deviceType !== '2in1') {
186      GetAlertMessage.phoneHandle(context, error);
187      return;
188    }
189    // system dialog with UIExtension window, only support one depth want param.
190    let code = error.code;
191    let message = error.message;
192    let uiExtWant: Want = {
193      bundleName: Constants.DLP_MANAGER_BUNDLE_NAME,
194      abilityName: 'DialogUIExtAbility',
195      moduleName: 'entry',
196      parameters: {
197        'bundleName': AppStorage.get('paramCallerBundleName') ?? '',
198        'ability.want.params.uiExtensionType': 'sys/commonUI',
199        'errorCode': code,
200        'errorMessage': message,
201        'accountType': GlobalContext.load('accountType')
202      }
203    };
204    try {
205      await context.requestModalUIExtension(uiExtWant);
206      HiLog.info(TAG, 'requestModalUIExtension succeed');
207    } catch (err) {
208      let code = (err as BusinessError).code;
209      let message = (err as BusinessError).message;
210      HiLog.error(TAG, `requestModalUIExtension failed. Cause: error code: ${code},error messagae:${message}`);
211    }
212  }
213}