• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.android.inputmethod.latin.permissions;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.os.Build;
23 import androidx.annotation.NonNull;
24 import androidx.core.app.ActivityCompat;
25 import androidx.core.content.ContextCompat;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Utility class for permissions.
32  */
33 public class PermissionsUtil {
34 
35     /**
36      * Returns the list of permissions not granted from the given list of permissions.
37      * @param context Context
38      * @param permissions list of permissions to check.
39      * @return the list of permissions that do not have permission to use.
40      */
getDeniedPermissions(Context context, String... permissions)41     public static List<String> getDeniedPermissions(Context context,
42                                                           String... permissions) {
43         final List<String> deniedPermissions = new ArrayList<>();
44         for (String permission : permissions) {
45             if (ContextCompat.checkSelfPermission(context, permission)
46                     != PackageManager.PERMISSION_GRANTED) {
47                 deniedPermissions.add(permission);
48             }
49         }
50         return deniedPermissions;
51     }
52 
53     /**
54      * Uses the given activity and requests the user for permissions.
55      * @param activity activity to use.
56      * @param requestCode request code/id to use.
57      * @param permissions String array of permissions that needs to be requested.
58      */
requestPermissions(Activity activity, int requestCode, String[] permissions)59     public static void requestPermissions(Activity activity, int requestCode,
60                                           String[] permissions) {
61         ActivityCompat.requestPermissions(activity, permissions, requestCode);
62     }
63 
64     /**
65      * Checks if all the permissions are granted.
66      */
allGranted(@onNull int[] grantResults)67     public static boolean allGranted(@NonNull int[] grantResults) {
68         for (int result : grantResults) {
69             if (result != PackageManager.PERMISSION_GRANTED) {
70                 return false;
71             }
72         }
73         return true;
74     }
75 
76     /**
77      * Queries if al the permissions are granted for the given permission strings.
78      */
checkAllPermissionsGranted(Context context, String... permissions)79     public static boolean checkAllPermissionsGranted(Context context, String... permissions) {
80         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
81             // For all pre-M devices, we should have all the premissions granted on install.
82             return true;
83         }
84 
85         for (String permission : permissions) {
86             if (ContextCompat.checkSelfPermission(context, permission)
87                     != PackageManager.PERMISSION_GRANTED) {
88                 return false;
89             }
90         }
91         return true;
92     }
93 }
94