18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciimport { HiLog } from '../util/HiLog';
168779efd5Sopenharmony_ciimport abilityAccessCtrl from '@ohos.abilityAccessCtrl';
178779efd5Sopenharmony_ci
188779efd5Sopenharmony_ciconst TAG = 'PermissionManager';
198779efd5Sopenharmony_ci
208779efd5Sopenharmony_ciexport class PermissionManager {
218779efd5Sopenharmony_ci  private _isAllPermissionsGranted: boolean = false;
228779efd5Sopenharmony_ci  private static instance: PermissionManager;
238779efd5Sopenharmony_ci  private constructor() {
248779efd5Sopenharmony_ci  }
258779efd5Sopenharmony_ci
268779efd5Sopenharmony_ci  public static getInstance(): PermissionManager {
278779efd5Sopenharmony_ci    if (!PermissionManager.instance) {
288779efd5Sopenharmony_ci      PermissionManager.instance = new PermissionManager();
298779efd5Sopenharmony_ci    }
308779efd5Sopenharmony_ci    return PermissionManager.instance;
318779efd5Sopenharmony_ci  }
328779efd5Sopenharmony_ci
338779efd5Sopenharmony_ci  public isAllPermissionsGranted() {
348779efd5Sopenharmony_ci    return this._isAllPermissionsGranted;
358779efd5Sopenharmony_ci  }
368779efd5Sopenharmony_ci
378779efd5Sopenharmony_ci  async initPermissions() {
388779efd5Sopenharmony_ci    let requestPermissions: Array<any> = [
398779efd5Sopenharmony_ci      "ohos.permission.READ_CONTACTS",
408779efd5Sopenharmony_ci      "ohos.permission.WRITE_CONTACTS",
418779efd5Sopenharmony_ci      "ohos.permission.MANAGE_VOICEMAIL",
428779efd5Sopenharmony_ci      "ohos.permission.READ_CALL_LOG",
438779efd5Sopenharmony_ci      "ohos.permission.WRITE_CALL_LOG"
448779efd5Sopenharmony_ci    ];
458779efd5Sopenharmony_ci    let AtManager = abilityAccessCtrl.createAtManager();
468779efd5Sopenharmony_ci    AtManager.requestPermissionsFromUser(globalThis.context, requestPermissions)
478779efd5Sopenharmony_ci      .then((data) => {
488779efd5Sopenharmony_ci        HiLog.i(TAG, "Auth result is: " + JSON.stringify(data.authResults));
498779efd5Sopenharmony_ci        let authFlag = true;
508779efd5Sopenharmony_ci        for (let i = 0; i < data.authResults.length; i++) {
518779efd5Sopenharmony_ci          if (data.authResults[i] == -1) {
528779efd5Sopenharmony_ci            authFlag = authFlag && false;
538779efd5Sopenharmony_ci          }
548779efd5Sopenharmony_ci        }
558779efd5Sopenharmony_ci        HiLog.i(TAG, "authFlag: " + JSON.stringify(authFlag));
568779efd5Sopenharmony_ci        this._isAllPermissionsGranted = authFlag;
578779efd5Sopenharmony_ci      })
588779efd5Sopenharmony_ci      .catch(err => {
598779efd5Sopenharmony_ci        HiLog.e(TAG, "requestPermissionsFromUser err:" + JSON.stringify(err));
608779efd5Sopenharmony_ci      });
618779efd5Sopenharmony_ci    HiLog.i(TAG, 'Application requestPermissionsFromUser end');
628779efd5Sopenharmony_ci  }
638779efd5Sopenharmony_ci}