21
21
import android .content .pm .PackageManager ;
22
22
import android .os .Build ;
23
23
import android .support .annotation .NonNull ;
24
- import android .support .annotation .RequiresApi ;
25
24
import android .support .annotation .StringRes ;
26
25
import android .support .v4 .app .ActivityCompat ;
27
- import android .support .v4 .app .Fragment ;
28
26
import android .support .v4 .content .ContextCompat ;
29
27
import android .util .Log ;
30
28
@@ -63,7 +61,7 @@ public interface PermissionCallbacks extends ActivityCompat.OnRequestPermissions
63
61
* yet granted.
64
62
* @see Manifest.permission
65
63
*/
66
- public static boolean hasPermissions (@ NonNull Context context , @ NonNull String ... perms ) {
64
+ public static boolean hasPermissions (Context context , @ NonNull String ... perms ) {
67
65
// Always return true for SDK < M, let the system deal with the permissions
68
66
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
69
67
Log .w (TAG , "hasPermissions: API version < M, returning true by default" );
@@ -72,6 +70,12 @@ public static boolean hasPermissions(@NonNull Context context, @NonNull String..
72
70
return true ;
73
71
}
74
72
73
+ // Null context may be passed if we have detected Low API (less than M) so getting
74
+ // to this point with a null context should not be possible.
75
+ if (context == null ) {
76
+ throw new IllegalArgumentException ("Can't check permissions for null context" );
77
+ }
78
+
75
79
for (String perm : perms ) {
76
80
if (ContextCompat .checkSelfPermission (context , perm )
77
81
!= PackageManager .PERMISSION_GRANTED ) {
@@ -91,7 +95,14 @@ public static void requestPermissions(@NonNull Object host,
91
95
@ NonNull String rationale ,
92
96
int requestCode ,
93
97
@ NonNull String ... perms ) {
94
- PermissionHelper .getInstance (host ).requestPermissions (rationale , requestCode , perms );
98
+
99
+ // Use default Android 'OK' and 'Cancel' buttons
100
+ requestPermissions (host ,
101
+ rationale ,
102
+ android .R .string .ok ,
103
+ android .R .string .cancel ,
104
+ requestCode ,
105
+ perms );
95
106
}
96
107
97
108
/**
@@ -106,21 +117,24 @@ public static void requestPermissions(@NonNull Object host,
106
117
* @param perms a set of permissions to be requested.
107
118
* @see Manifest.permission
108
119
*/
109
- @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
110
120
public static void requestPermissions (@ NonNull Object host ,
111
121
@ NonNull String rationale ,
112
122
@ StringRes int positiveButton ,
113
123
@ StringRes int negativeButton ,
114
124
int requestCode ,
115
125
@ NonNull String ... perms ) {
116
126
117
- // Check for permissions before dispatching
118
- if (hasPermissions (getContext (host ), perms )) {
127
+ // Get a permission helper for the calling object
128
+ PermissionHelper helper = PermissionHelper .getInstance (host );
129
+
130
+ // Check for permissions before dispatching the request
131
+ if (hasPermissions (helper .getContext (), perms )) {
119
132
notifyAlreadyHasPermissions (host , requestCode , perms );
120
133
return ;
121
134
}
122
135
123
- PermissionHelper .getInstance (host ).requestPermissions (rationale , positiveButton ,
136
+ // Request permissions
137
+ helper .requestPermissions (rationale , positiveButton ,
124
138
negativeButton , requestCode , perms );
125
139
}
126
140
@@ -225,26 +239,6 @@ public static boolean somePermissionDenied(@NonNull Object host, @NonNull String
225
239
return PermissionHelper .getInstance (host ).somePermissionDenied (perms );
226
240
}
227
241
228
- /**
229
- * Get an {@link Context} from an object that could be an Activity or Fragment.
230
- */
231
- @ NonNull
232
- @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
233
- private static Context getContext (@ NonNull Object object ) {
234
- // TODO(samstern): I'd really like to remove this method so that there is no
235
- // type-checking in this class but LowApiPermissionHelper stops me
236
- // since it can't provide a Context for its host.
237
- if (object instanceof Activity ) {
238
- return ((Activity ) object );
239
- } else if (object instanceof Fragment ) {
240
- return ((Fragment ) object ).getActivity ();
241
- } else if (object instanceof android .app .Fragment ) {
242
- return ((android .app .Fragment ) object ).getActivity ();
243
- } else {
244
- throw new IllegalArgumentException ("Cannot get context from object: " + object );
245
- }
246
- }
247
-
248
242
/**
249
243
* Run permission callbacks on an object that requested permissions but already has them
250
244
* by simulating {@link PackageManager#PERMISSION_GRANTED}.
0 commit comments