• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-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
16// @ts-ignore
17import print from '@ohos.print';
18import wifi from '@ohos.wifi';
19import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
20import bundleManager from '@ohos.bundle.bundleManager';
21import { Log } from './Log';
22import { WIFI_INACTIVE } from '../model/ErrorMessage';
23
24const TAG: string = 'Permission';
25
26export function checkWifiEnable(): boolean {
27  let wifiStatus: boolean = wifi.isWifiActive();
28  if (!wifiStatus) {
29    //wifi 关闭
30    Log.error(TAG, 'wifi is inactive');
31    //wifi关闭了, 上报异常
32    print.updateExtensionInfo(JSON.stringify(WIFI_INACTIVE));
33  }
34  return wifiStatus;
35}
36
37/**
38 * checkPermission
39 *
40 * @param accessTokenId
41 */
42export async function checkPermission(): Promise<boolean> {
43  Log.debug(TAG, 'checkPermission enter');
44  let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
45  if (bundleInfo === undefined || bundleInfo === null) {
46    Log.error(TAG, 'bundleInfo is undefined');
47    return false;
48  }
49  if (bundleInfo.appInfo === undefined || bundleInfo === null) {
50    Log.error(TAG, 'bundleInfo.appInfo is undefined');
51    return false;
52  }
53  const accessTokenId = bundleInfo.appInfo.accessTokenId;
54  let atManager: abilityAccessCtrl.AtManager = <abilityAccessCtrl.AtManager>abilityAccessCtrl.createAtManager();
55  if (atManager === undefined) {
56    Log.error(TAG, 'atManager is undefined');
57    return false;
58  }
59  Log.debug(TAG, 'get accessTokenId');
60  let status = atManager.verifyAccessTokenSync(accessTokenId, 'ohos.permission.APPROXIMATELY_LOCATION');
61  if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) {
62    Log.info(TAG, 'hasPermission APPROXIMATELY_LOCATION: false');
63    return false;
64  }
65  status = atManager.verifyAccessTokenSync(accessTokenId, 'ohos.permission.LOCATION');
66  if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) {
67    Log.info(TAG, 'hasPermission APPROXIMATELY_LOCATION: false');
68    return false;
69  }
70  Log.info(TAG, 'hasPermission: true');
71  return true;
72}
73
74export function requestPermission(context, callback: () => void): void {
75  Log.info(TAG, 'requestPermission enter');
76  let atManager = abilityAccessCtrl.createAtManager();
77  try {
78    atManager.requestPermissionsFromUser(context, ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION']).then((requestResult) => {
79      let isAuth = true;
80      for (let result of requestResult.authResults) {
81        if (result !== 0) {
82          isAuth = false;
83        }
84      }
85      if (isAuth) {
86        Log.info(TAG, 'request permission success');
87        callback();
88      } else {
89        Log.error(TAG, 'require permission failed');
90      }
91    }).catch((err) => {
92      Log.error(TAG, 'data:' + JSON.stringify(err));
93    });
94  } catch (err) {
95    Log.error(`catch err->${JSON.stringify(err)}`);
96  }
97}