• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.car.kitchensink.bluetooth;
18 
19 import android.annotation.Nullable;
20 import android.app.Activity;
21 import android.content.pm.PackageManager;
22 
23 import androidx.activity.result.contract.ActivityResultContracts;
24 import androidx.fragment.app.Fragment;
25 
26 public final class BluetoothPermissionChecker {
BluetoothPermissionChecker()27     private BluetoothPermissionChecker() {
28     }
29 
isPermissionGranted(Activity mActivity, String permission)30     static boolean isPermissionGranted(Activity mActivity, String permission) {
31         return mActivity.checkSelfPermission(permission)
32                 == PackageManager.PERMISSION_GRANTED;
33     }
34 
requestPermission(String permission, Fragment fragment, @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable)35     static void requestPermission(String permission, Fragment fragment,
36             @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable) {
37         fragment.registerForActivityResult(new ActivityResultContracts.RequestPermission(),
38                 isGranted -> {
39                     if (isGranted) {
40                         if (isGrantedRunnable != null) {
41                             isGrantedRunnable.run();
42                         }
43                     } else {
44                         if (isNotGrantedRunnable != null) {
45                             isNotGrantedRunnable.run();
46                         }
47                     }
48                 }).launch(permission);
49     }
50 
requestMultiplePermissions(String[] permissions, Fragment fragment, @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable)51     static void requestMultiplePermissions(String[] permissions, Fragment fragment,
52             @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable) {
53         fragment.registerForActivityResult(
54                 new ActivityResultContracts.RequestMultiplePermissions(),
55                 isGrantedMap -> {
56                     // I.e., true if *all* permissions in the map have been granted
57                     if (!isGrantedMap.containsValue(false)) {
58                         if (isGrantedRunnable != null) {
59                             isGrantedRunnable.run();
60                         }
61                     } else {
62                         if (isNotGrantedRunnable != null) {
63                             isNotGrantedRunnable.run();
64                         }
65                     }
66                 }).launch(permissions);
67     }
68 }
69