diff --git a/README.md b/README.md index 40c8280..f2fc076 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ allprojects { } dependencies { - implementation 'com.github.tbruyelle:rxpermissions:0.9.6' + implementation 'com.github.tbruyelle:rxpermissions:0.10.0' } ``` diff --git a/build.gradle b/build.gradle index fed0c05..2fe4a44 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,7 @@ ext { supportLibraryVersion = '27.1.1' appCompat = "com.android.support:appcompat-v7:$supportLibraryVersion" supportAnnotations = "com.android.support:support-annotations:$supportLibraryVersion" + supportFragment = "com.android.support:support-fragment:$supportLibraryVersion" junit = 'junit:junit:4.12' mockito = 'org.mockito:mockito-core:1.10.19' diff --git a/lib/build.gradle b/lib/build.gradle index 48fd65b..20d896c 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -27,6 +27,7 @@ android { dependencies { implementation rootProject.ext.rxJava implementation rootProject.ext.supportAnnotations + implementation rootProject.ext.supportFragment testImplementation rootProject.ext.junit testImplementation rootProject.ext.mockito diff --git a/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java b/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java index 91c1caa..a8fb3ff 100644 --- a/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java +++ b/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java @@ -16,9 +16,10 @@ import android.annotation.TargetApi; import android.app.Activity; -import android.app.FragmentManager; import android.os.Build; import android.support.annotation.NonNull; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.text.TextUtils; import java.util.ArrayList; @@ -27,9 +28,6 @@ import io.reactivex.Observable; import io.reactivex.ObservableSource; import io.reactivex.ObservableTransformer; -import io.reactivex.Single; -import io.reactivex.SingleSource; -import io.reactivex.SingleTransformer; import io.reactivex.functions.Function; import io.reactivex.subjects.PublishSubject; @@ -40,16 +38,16 @@ public class RxPermissions { RxPermissionsFragment mRxPermissionsFragment; - public RxPermissions(@NonNull Activity activity) { + public RxPermissions(@NonNull FragmentActivity activity) { mRxPermissionsFragment = getRxPermissionsFragment(activity); } - private RxPermissionsFragment getRxPermissionsFragment(Activity activity) { + private RxPermissionsFragment getRxPermissionsFragment(@NonNull FragmentActivity activity) { RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(activity); boolean isNewInstance = rxPermissionsFragment == null; if (isNewInstance) { rxPermissionsFragment = new RxPermissionsFragment(); - FragmentManager fragmentManager = activity.getFragmentManager(); + FragmentManager fragmentManager = activity.getSupportFragmentManager(); fragmentManager .beginTransaction() .add(rxPermissionsFragment, TAG) @@ -59,8 +57,8 @@ private RxPermissionsFragment getRxPermissionsFragment(Activity activity) { return rxPermissionsFragment; } - private RxPermissionsFragment findRxPermissionsFragment(Activity activity) { - return (RxPermissionsFragment) activity.getFragmentManager().findFragmentByTag(TAG); + private RxPermissionsFragment findRxPermissionsFragment(@NonNull FragmentActivity activity) { + return (RxPermissionsFragment) activity.getSupportFragmentManager().findFragmentByTag(TAG); } public void setLogging(boolean logging) { @@ -84,7 +82,7 @@ public ObservableSource apply(Observable o) { .buffer(permissions.length) .flatMap(new Function, ObservableSource>() { @Override - public ObservableSource apply(List permissions) throws Exception { + public ObservableSource apply(List permissions) { if (permissions.isEmpty()) { // Occurs during orientation change, when the subject receives onComplete. // In that case we don't want to propagate that empty list to the @@ -136,7 +134,7 @@ public ObservableSource apply(Observable o) { .buffer(permissions.length) .flatMap(new Function, ObservableSource>() { @Override - public ObservableSource apply(List permissions) throws Exception { + public ObservableSource apply(List permissions) { if (permissions.isEmpty()) { return Observable.empty(); } @@ -169,18 +167,18 @@ public Observable requestEach(final String... permissions) { * Request permissions immediately, must be invoked during initialization phase * of your application. */ - public Observable requestEachCombined(final String... permissions){ + public Observable requestEachCombined(final String... permissions) { return Observable.just(TRIGGER).compose(ensureEachCombined(permissions)); } - Observable request(final Observable trigger, final String... permissions) { + private Observable request(final Observable trigger, final String... permissions) { if (permissions == null || permissions.length == 0) { throw new IllegalArgumentException("RxPermissions.request/requestEach requires at least one input permission"); } return oneOf(trigger, pending(permissions)) .flatMap(new Function>() { @Override - public Observable apply(Object o) throws Exception { + public Observable apply(Object o) { return requestImplementation(permissions); } }); @@ -203,7 +201,7 @@ private Observable oneOf(Observable trigger, Observable pending) { } @TargetApi(Build.VERSION_CODES.M) - Observable requestImplementation(final String... permissions) { + private Observable requestImplementation(final String... permissions) { List> list = new ArrayList<>(permissions.length); List unrequestedPermissions = new ArrayList<>(); diff --git a/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissionsFragment.java b/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissionsFragment.java index c4ca288..4c927c4 100644 --- a/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissionsFragment.java +++ b/lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissionsFragment.java @@ -1,16 +1,19 @@ package com.tbruyelle.rxpermissions2; import android.annotation.TargetApi; -import android.app.Fragment; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.util.Log; -import io.reactivex.subjects.PublishSubject; + import java.util.HashMap; import java.util.Map; +import io.reactivex.subjects.PublishSubject; + public class RxPermissionsFragment extends Fragment { private static final int PERMISSIONS_REQUEST_CODE = 42; @@ -68,12 +71,20 @@ void onRequestPermissionsResult(String permissions[], int[] grantResults, boolea @TargetApi(Build.VERSION_CODES.M) boolean isGranted(String permission) { - return getActivity().checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; + final FragmentActivity fragmentActivity = getActivity(); + if (fragmentActivity == null) { + throw new IllegalStateException("This fragment must be attached to an activity."); + } + return fragmentActivity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; } @TargetApi(Build.VERSION_CODES.M) boolean isRevoked(String permission) { - return getActivity().getPackageManager().isPermissionRevokedByPolicy(permission, getActivity().getPackageName()); + final FragmentActivity fragmentActivity = getActivity(); + if (fragmentActivity == null) { + throw new IllegalStateException("This fragment must be attached to an activity."); + } + return fragmentActivity.getPackageManager().isPermissionRevokedByPolicy(permission, getActivity().getPackageName()); } public void setLogging(boolean logging) { @@ -88,8 +99,8 @@ public boolean containsByPermission(@NonNull String permission) { return mSubjects.containsKey(permission); } - public PublishSubject setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject subject) { - return mSubjects.put(permission, subject); + public void setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject subject) { + mSubjects.put(permission, subject); } void log(String message) { diff --git a/lib/src/test/java/com/tbruyelle/rxpermissions2/RxPermissionsTest.java b/lib/src/test/java/com/tbruyelle/rxpermissions2/RxPermissionsTest.java index bc4ffd3..f5ebfe2 100644 --- a/lib/src/test/java/com/tbruyelle/rxpermissions2/RxPermissionsTest.java +++ b/lib/src/test/java/com/tbruyelle/rxpermissions2/RxPermissionsTest.java @@ -19,6 +19,7 @@ import android.app.Activity; import android.content.pm.PackageManager; import android.os.Build; +import android.support.v4.app.FragmentActivity; import org.junit.Before; import org.junit.Test; @@ -27,8 +28,8 @@ import org.mockito.Matchers; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; -import org.robolectric.util.ActivityController; import io.reactivex.Observable; import io.reactivex.observers.TestObserver; @@ -50,13 +51,13 @@ @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.M) public class RxPermissionsTest { - private Activity mActivity; + private FragmentActivity mActivity; private RxPermissions mRxPermissions; @Before public void setup() { - ActivityController activityController = Robolectric.buildActivity(Activity.class); + ActivityController activityController = Robolectric.buildActivity(FragmentActivity.class); mActivity = spy(activityController.setup().get()); mRxPermissions = spy(new RxPermissions(mActivity)); mRxPermissions.mRxPermissionsFragment = spy(mRxPermissions.mRxPermissionsFragment); @@ -521,7 +522,7 @@ public void shouldShowRequestPermissionRationale_allDenied_allRationale() { when(activity.shouldShowRequestPermissionRationale(anyString())).thenReturn(true); TestObserver sub = new TestObserver<>(); - mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"}) + mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2") .subscribe(sub); sub.assertComplete(); @@ -537,7 +538,7 @@ public void shouldShowRequestPermissionRationale_allDenied_oneRationale() { when(activity.shouldShowRequestPermissionRationale("p1")).thenReturn(true); TestObserver sub = new TestObserver<>(); - mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"}) + mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2") .subscribe(sub); sub.assertComplete(); @@ -552,7 +553,7 @@ public void shouldShowRequestPermissionRationale_allDenied_noRationale() { Activity activity = mock(Activity.class); TestObserver sub = new TestObserver<>(); - mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"}) + mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2") .subscribe(sub); sub.assertComplete(); @@ -569,7 +570,7 @@ public void shouldShowRequestPermissionRationale_oneDeniedRationale() { when(activity.shouldShowRequestPermissionRationale("p2")).thenReturn(true); TestObserver sub = new TestObserver<>(); - mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"}) + mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2") .subscribe(sub); sub.assertComplete(); @@ -585,7 +586,7 @@ public void shouldShowRequestPermissionRationale_oneDeniedNotRationale() { when(mRxPermissions.isGranted("p2")).thenReturn(true); TestObserver sub = new TestObserver<>(); - mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"}) + mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2") .subscribe(sub); sub.assertComplete();