1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ciimport abilityAccessCtrl from '@ohos.abilityAccessCtrl';
16f6603c60Sopenharmony_ciimport type { Permissions } from '@ohos.abilityAccessCtrl';
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ciconst TAG = 'PermissionUtil';
19f6603c60Sopenharmony_ci
20f6603c60Sopenharmony_ciexport class PermissionUtil {
21f6603c60Sopenharmony_ci  private static permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR'];
22f6603c60Sopenharmony_ci
23f6603c60Sopenharmony_ci  public static async requestOAIDTrackingConsentPermissions(context: any): Promise<void> {
24f6603c60Sopenharmony_ci    let atManager = abilityAccessCtrl.createAtManager();
25f6603c60Sopenharmony_ci    return new Promise<void>(async (resolve, reject) => {
26f6603c60Sopenharmony_ci      console.log(TAG, 'requestPermission begin');
27f6603c60Sopenharmony_ci      let data = await atManager.requestPermissionsFromUser(context, ['ohos.permission.APP_TRACKING_CONSENT']);
28f6603c60Sopenharmony_ci      console.log(TAG, 'requestPermissionsFromUser data=' + JSON.stringify(data));
29f6603c60Sopenharmony_ci      if (data && data.authResults) {
30f6603c60Sopenharmony_ci        let result = 0;
31f6603c60Sopenharmony_ci        for (let i = 0; i < data.authResults.length; i++) {
32f6603c60Sopenharmony_ci          result += data.authResults[i];
33f6603c60Sopenharmony_ci        }
34f6603c60Sopenharmony_ci        if (result === 0) {
35f6603c60Sopenharmony_ci          resolve();
36f6603c60Sopenharmony_ci        } else {
37f6603c60Sopenharmony_ci          console.log(TAG, 'requestPermission user rejected');
38f6603c60Sopenharmony_ci          reject();
39f6603c60Sopenharmony_ci        }
40f6603c60Sopenharmony_ci      } else {
41f6603c60Sopenharmony_ci        console.log(TAG, 'requestPermission failed');
42f6603c60Sopenharmony_ci        reject();
43f6603c60Sopenharmony_ci      }
44f6603c60Sopenharmony_ci      console.log(TAG, 'requestPermission end');
45f6603c60Sopenharmony_ci    });
46f6603c60Sopenharmony_ci  }
47f6603c60Sopenharmony_ci}