2121import android .content .pm .PackageManager ;
2222import android .os .Build ;
2323import android .support .annotation .NonNull ;
24- import android .support .annotation .RequiresApi ;
2524import android .support .annotation .StringRes ;
2625import android .support .v4 .app .ActivityCompat ;
27- import android .support .v4 .app .Fragment ;
2826import android .support .v4 .content .ContextCompat ;
2927import android .util .Log ;
3028
@@ -63,7 +61,7 @@ public interface PermissionCallbacks extends ActivityCompat.OnRequestPermissions
6361 * yet granted.
6462 * @see Manifest.permission
6563 */
66- public static boolean hasPermissions (@ NonNull Context context , @ NonNull String ... perms ) {
64+ public static boolean hasPermissions (Context context , @ NonNull String ... perms ) {
6765 // Always return true for SDK < M, let the system deal with the permissions
6866 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
6967 Log .w (TAG , "hasPermissions: API version < M, returning true by default" );
@@ -72,6 +70,12 @@ public static boolean hasPermissions(@NonNull Context context, @NonNull String..
7270 return true ;
7371 }
7472
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+
7579 for (String perm : perms ) {
7680 if (ContextCompat .checkSelfPermission (context , perm )
7781 != PackageManager .PERMISSION_GRANTED ) {
@@ -91,7 +95,14 @@ public static void requestPermissions(@NonNull Object host,
9195 @ NonNull String rationale ,
9296 int requestCode ,
9397 @ 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 );
95106 }
96107
97108 /**
@@ -106,21 +117,24 @@ public static void requestPermissions(@NonNull Object host,
106117 * @param perms a set of permissions to be requested.
107118 * @see Manifest.permission
108119 */
109- @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
110120 public static void requestPermissions (@ NonNull Object host ,
111121 @ NonNull String rationale ,
112122 @ StringRes int positiveButton ,
113123 @ StringRes int negativeButton ,
114124 int requestCode ,
115125 @ NonNull String ... perms ) {
116126
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 )) {
119132 notifyAlreadyHasPermissions (host , requestCode , perms );
120133 return ;
121134 }
122135
123- PermissionHelper .getInstance (host ).requestPermissions (rationale , positiveButton ,
136+ // Request permissions
137+ helper .requestPermissions (rationale , positiveButton ,
124138 negativeButton , requestCode , perms );
125139 }
126140
@@ -225,26 +239,6 @@ public static boolean somePermissionDenied(@NonNull Object host, @NonNull String
225239 return PermissionHelper .getInstance (host ).somePermissionDenied (perms );
226240 }
227241
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-
248242 /**
249243 * Run permission callbacks on an object that requested permissions but already has them
250244 * by simulating {@link PackageManager#PERMISSION_GRANTED}.
0 commit comments