Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 83091d6

Browse files
committed
Remove Helper --> EP calls
1 parent 6d8c936 commit 83091d6

File tree

6 files changed

+67
-60
lines changed

6 files changed

+67
-60
lines changed

easypermissions/src/main/java/pub/devrel/easypermissions/EasyPermissions.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import android.app.Activity;
2020
import android.content.Context;
2121
import android.content.pm.PackageManager;
22+
import android.os.Build;
2223
import android.support.annotation.NonNull;
24+
import android.support.annotation.RequiresApi;
2325
import android.support.annotation.StringRes;
2426
import android.support.v4.app.ActivityCompat;
27+
import android.support.v4.app.Fragment;
28+
import android.support.v4.content.ContextCompat;
2529
import android.util.Log;
2630

2731
import java.lang.reflect.InvocationTargetException;
@@ -60,7 +64,22 @@ public interface PermissionCallbacks extends ActivityCompat.OnRequestPermissions
6064
* @see Manifest.permission
6165
*/
6266
public static boolean hasPermissions(@NonNull Context context, @NonNull String... perms) {
63-
return PermissionHelper.hasPermissions(context, perms);
67+
// Always return true for SDK < M, let the system deal with the permissions
68+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
69+
Log.w(TAG, "hasPermissions: API version < M, returning true by default");
70+
71+
// DANGER ZONE!!! Changing this will break the library.
72+
return true;
73+
}
74+
75+
for (String perm : perms) {
76+
if (ContextCompat.checkSelfPermission(context, perm)
77+
!= PackageManager.PERMISSION_GRANTED) {
78+
return false;
79+
}
80+
}
81+
82+
return true;
6483
}
6584

6685
/**
@@ -87,12 +106,20 @@ public static void requestPermissions(@NonNull Object host,
87106
* @param perms a set of permissions to be requested.
88107
* @see Manifest.permission
89108
*/
109+
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
90110
public static void requestPermissions(@NonNull Object host,
91111
@NonNull String rationale,
92112
@StringRes int positiveButton,
93113
@StringRes int negativeButton,
94114
int requestCode,
95115
@NonNull String... perms) {
116+
117+
// Check for permissions before dispatching
118+
if (hasPermissions(getContext(host), perms)) {
119+
notifyAlreadyHasPermissions(host, requestCode, perms);
120+
return;
121+
}
122+
96123
PermissionHelper.getInstance(host).requestPermissions(rationale, positiveButton,
97124
negativeButton, requestCode, perms);
98125
}
@@ -198,6 +225,44 @@ public static boolean somePermissionDenied(@NonNull Object host, @NonNull String
198225
return PermissionHelper.getInstance(host).somePermissionDenied(perms);
199226
}
200227

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+
/**
249+
* Run permission callbacks on an object that requested permissions but already has them
250+
* by simulating {@link PackageManager#PERMISSION_GRANTED}.
251+
* @param object the object requesting permissions.
252+
* @param requestCode the permission request code.
253+
* @param perms a list of permissions requested.
254+
*/
255+
private static void notifyAlreadyHasPermissions(@NonNull Object object,
256+
int requestCode,
257+
@NonNull String[] perms) {
258+
int[] grantResults = new int[perms.length];
259+
for (int i = 0; i < perms.length; i++) {
260+
grantResults[i] = PackageManager.PERMISSION_GRANTED;
261+
}
262+
263+
onRequestPermissionsResult(requestCode, perms, grantResults, object);
264+
}
265+
201266
/**
202267
* Find all methods annotated with {@link AfterPermissionGranted} on a given object with the
203268
* correc requestCode argument.

easypermissions/src/main/java/pub/devrel/easypermissions/helper/ActivityPermissionHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public void requestPermissions(@NonNull String rationale,
2323
int requestCode,
2424
@NonNull String... perms) {
2525

26-
// Check for permissions before dispatching
27-
if (hasPermissions(getHost(), perms)) {
28-
notifyAlreadyHasPermissions(getHost(), requestCode, perms);
29-
return;
30-
}
31-
3226
if (shouldShowRationale(perms)) {
3327
showRationaleDialogFragment(
3428
getHost().getFragmentManager(),

easypermissions/src/main/java/pub/devrel/easypermissions/helper/FrameworkFragmentPermissionHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public void requestPermissions(@NonNull String rationale,
2121
@StringRes int negativeButton, int requestCode,
2222
@NonNull String... perms) {
2323

24-
// Check for permissions before dispatching
25-
if (hasPermissions(getHost().getActivity(), perms)) {
26-
notifyAlreadyHasPermissions(getHost(), requestCode, perms);
27-
return;
28-
}
29-
3024
if (shouldShowRationale(perms)) {
3125
showRationaleDialogFragment(
3226
getHost().getChildFragmentManager(),

easypermissions/src/main/java/pub/devrel/easypermissions/helper/LowApiPermissionsHelper.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pub.devrel.easypermissions.helper;
22

3+
import android.content.Context;
34
import android.support.annotation.NonNull;
45
import android.support.annotation.StringRes;
56

@@ -19,12 +20,6 @@ public void requestPermissions(@NonNull String rationale,
1920
int requestCode,
2021
@NonNull String... perms) {
2122

22-
// Check for permissions before dispatching
23-
if (hasPermissions(null, perms)) {
24-
notifyAlreadyHasPermissions(getHost(), requestCode, perms);
25-
return;
26-
}
27-
2823
throw new IllegalStateException("Should never be requesting permissions on API < 23!");
2924
}
3025

easypermissions/src/main/java/pub/devrel/easypermissions/helper/PermissionHelper.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package pub.devrel.easypermissions.helper;
22

33
import android.app.Activity;
4-
import android.content.Context;
5-
import android.content.pm.PackageManager;
64
import android.os.Build;
75
import android.support.annotation.NonNull;
86
import android.support.annotation.RequiresApi;
97
import android.support.annotation.RestrictTo;
108
import android.support.annotation.StringRes;
119
import android.support.v4.app.Fragment;
12-
import android.support.v4.content.ContextCompat;
13-
import android.util.Log;
1410

1511
import java.util.List;
1612

17-
import pub.devrel.easypermissions.EasyPermissions;
1813
import pub.devrel.easypermissions.RationaleDialogFragment;
1914

2015
/**
@@ -53,25 +48,6 @@ public PermissionHelper(@NonNull T host) {
5348
this.mHost = host;
5449
}
5550

56-
public static boolean hasPermissions(Context context, String... perms) {
57-
// Always return true for SDK < M, let the system deal with the permissions
58-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
59-
Log.w(TAG, "hasPermissions: API version < M, returning true by default");
60-
61-
// DANGER ZONE!!! Changing this will break the library.
62-
return true;
63-
}
64-
65-
for (String perm : perms) {
66-
if (ContextCompat.checkSelfPermission(context, perm)
67-
!= PackageManager.PERMISSION_GRANTED) {
68-
return false;
69-
}
70-
}
71-
72-
return true;
73-
}
74-
7551
public void requestPermissions(@NonNull String rationale,
7652
int requestCode,
7753
@NonNull String... perms) {
@@ -142,17 +118,6 @@ protected void showRationaleDialogFragment(@NonNull android.app.FragmentManager
142118
.show(fragmentManager, RationaleDialogFragment.TAG);
143119
}
144120

145-
protected void notifyAlreadyHasPermissions(Object object,
146-
int requestCode,
147-
@NonNull String[] perms) {
148-
int[] grantResults = new int[perms.length];
149-
for (int i = 0; i < perms.length; i++) {
150-
grantResults[i] = PackageManager.PERMISSION_GRANTED;
151-
}
152-
153-
EasyPermissions.onRequestPermissionsResult(requestCode, perms, grantResults, object);
154-
}
155-
156121
@NonNull
157122
protected T getHost() {
158123
return mHost;

easypermissions/src/main/java/pub/devrel/easypermissions/helper/SupportFragmentPermissionHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ public void requestPermissions(@NonNull String rationale,
2424
int requestCode,
2525
@NonNull String... perms) {
2626

27-
// Check for permissions before dispatching
28-
if (hasPermissions(getHost().getContext(), perms)) {
29-
notifyAlreadyHasPermissions(getHost(), requestCode, perms);
30-
return;
31-
}
32-
3327
if (shouldShowRationale(perms)) {
3428
RationaleDialogFragmentCompat
3529
.newInstance(positiveButton, negativeButton, rationale, requestCode, perms)

0 commit comments

Comments
 (0)