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.dialer.util;
18 
19 import android.Manifest.permission;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.PackageManager;
25 import android.support.v4.content.ContextCompat;
26 import android.support.v4.content.LocalBroadcastManager;
27 import com.android.dialer.common.LogUtil;
28 
29 /** Utility class to help with runtime permissions. */
30 public class PermissionsUtil {
31 
32   private static final String PERMISSION_PREFERENCE = "dialer_permissions";
33   private static final String CEQUINT_PERMISSION = "com.cequint.ecid.CALLER_ID_LOOKUP";
34 
hasPhonePermissions(Context context)35   public static boolean hasPhonePermissions(Context context) {
36     return hasPermission(context, permission.CALL_PHONE);
37   }
38 
hasContactsReadPermissions(Context context)39   public static boolean hasContactsReadPermissions(Context context) {
40     return hasPermission(context, permission.READ_CONTACTS);
41   }
42 
hasLocationPermissions(Context context)43   public static boolean hasLocationPermissions(Context context) {
44     return hasPermission(context, permission.ACCESS_FINE_LOCATION);
45   }
46 
hasCameraPermissions(Context context)47   public static boolean hasCameraPermissions(Context context) {
48     return hasPermission(context, permission.CAMERA);
49   }
50 
hasMicrophonePermissions(Context context)51   public static boolean hasMicrophonePermissions(Context context) {
52     return hasPermission(context, permission.RECORD_AUDIO);
53   }
54 
hasCallLogReadPermissions(Context context)55   public static boolean hasCallLogReadPermissions(Context context) {
56     return hasPermission(context, permission.READ_CALL_LOG);
57   }
58 
hasCallLogWritePermissions(Context context)59   public static boolean hasCallLogWritePermissions(Context context) {
60     return hasPermission(context, permission.WRITE_CALL_LOG);
61   }
62 
hasCequintPermissions(Context context)63   public static boolean hasCequintPermissions(Context context) {
64     return hasPermission(context, CEQUINT_PERMISSION);
65   }
66 
hasReadVoicemailPermissions(Context context)67   public static boolean hasReadVoicemailPermissions(Context context) {
68     return hasPermission(context, permission.READ_VOICEMAIL);
69   }
70 
hasWriteVoicemailPermissions(Context context)71   public static boolean hasWriteVoicemailPermissions(Context context) {
72     return hasPermission(context, permission.WRITE_VOICEMAIL);
73   }
74 
hasAddVoicemailPermissions(Context context)75   public static boolean hasAddVoicemailPermissions(Context context) {
76     return hasPermission(context, permission.ADD_VOICEMAIL);
77   }
78 
hasPermission(Context context, String permission)79   public static boolean hasPermission(Context context, String permission) {
80     return ContextCompat.checkSelfPermission(context, permission)
81         == PackageManager.PERMISSION_GRANTED;
82   }
83 
84   /**
85    * Checks {@link android.content.SharedPreferences} if a permission has been requested before.
86    *
87    * <p>It is important to note that this method only works if you call {@link
88    * PermissionsUtil#permissionRequested(Context, String)} in {@link
89    * android.app.Activity#onRequestPermissionsResult(int, String[], int[])}.
90    */
isFirstRequest(Context context, String permission)91   public static boolean isFirstRequest(Context context, String permission) {
92     return context
93         .getSharedPreferences(PERMISSION_PREFERENCE, Context.MODE_PRIVATE)
94         .getBoolean(permission, true);
95   }
96 
97   /**
98    * Records in {@link android.content.SharedPreferences} that the specified permission has been
99    * requested at least once.
100    *
101    * <p>This method should be called in {@link android.app.Activity#onRequestPermissionsResult(int,
102    * String[], int[])}.
103    */
permissionRequested(Context context, String permission)104   public static void permissionRequested(Context context, String permission) {
105     context
106         .getSharedPreferences(PERMISSION_PREFERENCE, Context.MODE_PRIVATE)
107         .edit()
108         .putBoolean(permission, false)
109         .apply();
110   }
111 
112   /**
113    * Rudimentary methods wrapping the use of a LocalBroadcastManager to simplify the process of
114    * notifying other classes when a particular fragment is notified that a permission is granted.
115    *
116    * <p>To be notified when a permission has been granted, create a new broadcast receiver and
117    * register it using {@link #registerPermissionReceiver(Context, BroadcastReceiver, String)}
118    *
119    * <p>E.g.
120    *
121    * <p>final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void
122    * onReceive(Context context, Intent intent) { refreshContactsView(); } }
123    *
124    * <p>PermissionsUtil.registerPermissionReceiver(getActivity(), receiver, READ_CONTACTS);
125    *
126    * <p>If you register to listen for multiple permissions, you can identify which permission was
127    * granted by inspecting {@link Intent#getAction()}.
128    *
129    * <p>In the fragment that requests for the permission, be sure to call {@link
130    * #notifyPermissionGranted(Context, String)} when the permission is granted so that any
131    * interested listeners are notified of the change.
132    */
registerPermissionReceiver( Context context, BroadcastReceiver receiver, String permission)133   public static void registerPermissionReceiver(
134       Context context, BroadcastReceiver receiver, String permission) {
135     LogUtil.i("PermissionsUtil.registerPermissionReceiver", permission);
136     final IntentFilter filter = new IntentFilter(permission);
137     LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter);
138   }
139 
unregisterPermissionReceiver(Context context, BroadcastReceiver receiver)140   public static void unregisterPermissionReceiver(Context context, BroadcastReceiver receiver) {
141     LogUtil.i("PermissionsUtil.unregisterPermissionReceiver", null);
142     LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
143   }
144 
notifyPermissionGranted(Context context, String permission)145   public static void notifyPermissionGranted(Context context, String permission) {
146     LogUtil.i("PermissionsUtil.notifyPermissionGranted", permission);
147     final Intent intent = new Intent(permission);
148     LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
149   }
150 }
151