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 30 /** 31 * Determines if the activity has the required permissions 32 * 33 * @param mActivity activity to query for permission 34 * @param permission permission to verify 35 * @return {@code true} if the activity has the permission, {@code false} otherwise 36 */ isPermissionGranted(Activity mActivity, String permission)37 public static boolean isPermissionGranted(Activity mActivity, String permission) { 38 return mActivity.checkSelfPermission(permission) 39 == PackageManager.PERMISSION_GRANTED; 40 } 41 42 /** 43 * Request permission from user 44 * 45 * @param permission permission to request 46 * @param fragment parent fragment where request originated 47 * @param isGrantedRunnable callback that is triggered if the permission is grated 48 * @param isNotGrantedRunnable callback that is triggered is the permission is not grated 49 */ requestPermission(String permission, Fragment fragment, @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable)50 public static void requestPermission(String permission, Fragment fragment, 51 @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable) { 52 fragment.registerForActivityResult(new ActivityResultContracts.RequestPermission(), 53 isGranted -> { 54 if (isGranted) { 55 if (isGrantedRunnable != null) { 56 isGrantedRunnable.run(); 57 } 58 } else { 59 if (isNotGrantedRunnable != null) { 60 isNotGrantedRunnable.run(); 61 } 62 } 63 }).launch(permission); 64 } 65 requestMultiplePermissions(String[] permissions, Fragment fragment, @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable)66 static void requestMultiplePermissions(String[] permissions, Fragment fragment, 67 @Nullable Runnable isGrantedRunnable, @Nullable Runnable isNotGrantedRunnable) { 68 fragment.registerForActivityResult( 69 new ActivityResultContracts.RequestMultiplePermissions(), 70 isGrantedMap -> { 71 // I.e., true if *all* permissions in the map have been granted 72 if (!isGrantedMap.containsValue(false)) { 73 if (isGrantedRunnable != null) { 74 isGrantedRunnable.run(); 75 } 76 } else { 77 if (isNotGrantedRunnable != null) { 78 isNotGrantedRunnable.run(); 79 } 80 } 81 }).launch(permissions); 82 } 83 } 84