• 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 display from '@ohos.display';
18import StartOptions from '@ohos.app.ability.StartOptions';
19import common from '@ohos.app.ability.common';
20import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
21import osAccount from '@ohos.account.osAccount';
22import account_osAccount from '@ohos.account.osAccount';
23import Constants from './constant';
24import GlobalContext from './GlobalContext';
25import deviceInfo from '@ohos.deviceInfo';
26import promptAction from '@ohos.promptAction';
27import { HiLog } from '../common/HiLog';
28
29const TAG = 'AlertMessage';
30
31export class GetAlertMessage {
32
33  public static async startAlertAbility(
34    context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext,
35    error: BusinessError,
36    session?: UIExtensionContentSession
37  ) {
38    if (deviceInfo.deviceType !== '2in1') {
39      GetAlertMessage.phoneHandle(context, error);
40      return;
41    }
42    let dis = display.getDefaultDisplaySync();
43    let xNumber = Math.floor(
44      (dis.width - Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH) * Constants.RATIO_HALF
45    );
46    let yNumber = Math.floor(
47      (dis.height - Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT) * Constants.RATIO_HALF
48    );
49    let options: StartOptions = {
50      withAnimation: true,
51      windowLeft: xNumber,
52      windowTop: yNumber,
53      windowWidth: Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH,
54      windowHeight: Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT
55    };
56    HiLog.info(TAG, `set options: ${JSON.stringify(options)}`);
57    context.startAbility({
58      bundleName: Constants.DLP_MANAGER_BUNDLE_NAME,
59      abilityName: 'AlertAbility',
60      parameters: {
61        error: error,
62        accountType: GlobalContext.load('accountType')
63      }
64    }, options, async (err: BusinessError) => {
65      if (err && err.code !== 0) {
66        HiLog.error(TAG, `start AlertAbility failed: ${JSON.stringify(err)}`);
67      }
68      if (session) {
69        session.terminateSelfWithResult({
70          'resultCode': 0,
71          'want': {
72            'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME,
73          },
74        });
75      } else {
76        context.terminateSelf();
77      }
78    })
79  }
80
81  public static async phoneHandle(
82    context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext,
83    error: BusinessError
84  ) {
85    try {
86      let resource = GetAlertMessage.getToastMessage(error);
87      let toastMessage = await context.resourceManager.getStringValue(resource);
88      promptAction.showToast({
89        message: toastMessage,
90        duration: Constants.SHARE_SET_TIMEOUT
91      });
92      HiLog.info(TAG, 'showToast end.');
93    } catch (error) {
94      HiLog.error(TAG, `showToast error: ${JSON.stringify(error)}`);
95    } finally {
96      setTimeout(() => {
97        context.terminateSelf();
98      }, Constants.SHARE_SET_TIMEOUT);
99    }
100  }
101
102  public static getAlertMessage(err: BusinessError, defaultTitle?: Resource, defaultMessage?: Resource) {
103    switch (err && err.code) {
104      case Constants.ERR_JS_ACCOUNT_NOT_FOUND:
105      case Constants.ERR_JS_GET_ACCOUNT_ERROR:
106        return { 'msg': $r('app.string.MESSAGE_APP_GET_ACCOUNT_ERROR') } as Record<string, Resource>;
107      case Constants.ERR_JS_APP_NO_ACCOUNT_ERROR:
108      case Constants.ERR_JS_ACCOUNT_NOT_LOGIN:
109        return { 'msg': $r('app.string.MESSAGE_APP_NO_ACCOUNT_ERROR') } as Record<string, Resource>;
110      case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR:
111        return { 'msg': $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR') } as Record<string, Resource>;
112      case Constants.ERR_JS_RELEASE_FILE_OPEN:
113      case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION:
114        return { 'msg': $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION') } as Record<string, Resource>;
115      case Constants.ERR_JS_NETWORK_INVALID:
116      case Constants.ERR_JS_APP_NETWORK_INVALID:
117        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
118      case Constants.ERR_JS_CREDENTIAL_SERVICE_ERROR:
119      case Constants.ERR_JS_CREDENTIAL_SERVER_ERROR:
120      case Constants.ERR_JS_CREDENTIAL_TIMEOUT:
121      case Constants.ERR_JS_APP_INSIDE_ERROR:
122        return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>;
123      case Constants.ERR_JS_NOT_DLP_FILE:
124        return { 'msg': $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR') } as Record<string, Resource>;
125      case Constants.ERR_JS_USER_NO_PERMISSION:
126        return { 'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM') } as Record<string, Resource>;
127      case Constants.ERR_JS_OTHER_APP_OPEN_FILE:
128        return { 'msg': $r('app.string.This_File_is_Open_By_Other_App') } as Record<string, Resource>;
129      case Constants.ERR_JS_OFFLINE:
130        return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>;
131      default:
132        if (defaultTitle !== undefined && defaultMessage != undefined) {
133          return { 'title': defaultTitle, 'msg': defaultMessage } as Record<string, Resource>;
134        } else {
135          return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>;
136        }
137    }
138  }
139
140  public static getAlertTitleMessage(err: BusinessError) {
141    switch (err && err.code) {
142      case Constants.ERR_JS_USER_NO_PERMISSION:
143        return {
144          'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'),
145          'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM_VISIT', err.message.split(', contact:')?.[1])
146        } as Record<string, Resource>;
147      case Constants.ERR_JS_APP_PARAM_ERROR:
148        return {
149          'title': $r('app.string.TITLE_APP_ERROR'),
150          'msg': $r('app.string.MESSAGE_APP_PARAM_ERROR')
151        } as Record<string, Resource>;
152      case Constants.ERR_JS_APP_OPEN_REJECTED:
153        return {
154          'title': $r('app.string.header_title'),
155          'msg': $r('app.string.MESSAGE_DLP_OPEN_REJECT')
156        } as Record<string, Resource>;
157      case Constants.ERR_JS_APP_ENCRYPTION_REJECTED:
158        return {
159          'title': $r('app.string.header_title'),
160          'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION_REJECTED')
161        } as Record<string, Resource>;
162      case Constants.ERR_JS_APP_ENCRYPTING:
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.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED')
181        } as Record<string, Resource>;
182      default:
183        return {
184          'title': $r('app.string.header_title'),
185          'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR')
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.MESSAGE_SERVICE_INSIDE_ERROR')
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.MESSAGE_APP_FILE_PARAM_ERROR');
217      case Constants.ERR_JS_USER_NO_PERMISSION:
218        return $r('app.string.MESSAGE_APP_NOT_HAVE_PERM');
219      case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED:
220        return $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED');
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      default:
226        return $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR');
227    }
228  }
229
230  public static checkAccountInfo(accountName: string): Promise<boolean> {
231    return new Promise((resolve, reject) => {
232      let accountDomain = AppStorage.get('accountDomain') as string;
233      let domainAccountInfo: osAccount.DomainAccountInfo = {
234        domain: accountDomain,
235        accountName: accountName
236      };
237      try {
238        account_osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo)
239          .then((result: account_osAccount.DomainAccountInfo) => {
240            resolve(true);
241          }).catch((err: BusinessError) => {
242            resolve(false);
243          })
244      } catch (err) {
245        resolve(false);
246      }
247    })
248  }
249}