Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Support fragment (#232)
Browse files Browse the repository at this point in the history
* Update libraries to latest versions.

* Move from bintray to jitpack.

* Update travis file.

* Make RxPermissionsFragment inherit from support.

*  Move from Bintray to JitPack. (#231)

* Update libraries to latest versions.

* Move from bintray to jitpack.

* Update travis file.

* Make RxPermissionsFragment inherit from support.

* Make RxPermissionsFragment inherit from support.

* Prepare version to 0.10.0.
  • Loading branch information
epool authored Jul 6, 2018
1 parent 81438b6 commit d31e5dd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.9.6'
implementation 'com.github.tbruyelle:rxpermissions:0.10.0'
}
```

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 13 additions & 15 deletions lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -84,7 +82,7 @@ public ObservableSource<Boolean> apply(Observable<T> o) {
.buffer(permissions.length)
.flatMap(new Function<List<Permission>, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(List<Permission> permissions) throws Exception {
public ObservableSource<Boolean> apply(List<Permission> 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
Expand Down Expand Up @@ -136,7 +134,7 @@ public ObservableSource<Permission> apply(Observable<T> o) {
.buffer(permissions.length)
.flatMap(new Function<List<Permission>, ObservableSource<Permission>>() {
@Override
public ObservableSource<Permission> apply(List<Permission> permissions) throws Exception {
public ObservableSource<Permission> apply(List<Permission> permissions) {
if (permissions.isEmpty()) {
return Observable.empty();
}
Expand Down Expand Up @@ -169,18 +167,18 @@ public Observable<Permission> requestEach(final String... permissions) {
* Request permissions immediately, <b>must be invoked during initialization phase
* of your application</b>.
*/
public Observable<Permission> requestEachCombined(final String... permissions){
public Observable<Permission> requestEachCombined(final String... permissions) {
return Observable.just(TRIGGER).compose(ensureEachCombined(permissions));
}

Observable<Permission> request(final Observable<?> trigger, final String... permissions) {
private Observable<Permission> 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<Object, Observable<Permission>>() {
@Override
public Observable<Permission> apply(Object o) throws Exception {
public Observable<Permission> apply(Object o) {
return requestImplementation(permissions);
}
});
Expand All @@ -203,7 +201,7 @@ private Observable<?> oneOf(Observable<?> trigger, Observable<?> pending) {
}

@TargetApi(Build.VERSION_CODES.M)
Observable<Permission> requestImplementation(final String... permissions) {
private Observable<Permission> requestImplementation(final String... permissions) {
List<Observable<Permission>> list = new ArrayList<>(permissions.length);
List<String> unrequestedPermissions = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -88,8 +99,8 @@ public boolean containsByPermission(@NonNull String permission) {
return mSubjects.containsKey(permission);
}

public PublishSubject<Permission> setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject<Permission> subject) {
return mSubjects.put(permission, subject);
public void setSubjectForPermission(@NonNull String permission, @NonNull PublishSubject<Permission> subject) {
mSubjects.put(permission, subject);
}

void log(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Activity> activityController = Robolectric.buildActivity(Activity.class);
ActivityController<FragmentActivity> activityController = Robolectric.buildActivity(FragmentActivity.class);
mActivity = spy(activityController.setup().get());
mRxPermissions = spy(new RxPermissions(mActivity));
mRxPermissions.mRxPermissionsFragment = spy(mRxPermissions.mRxPermissionsFragment);
Expand Down Expand Up @@ -521,7 +522,7 @@ public void shouldShowRequestPermissionRationale_allDenied_allRationale() {
when(activity.shouldShowRequestPermissionRationale(anyString())).thenReturn(true);

TestObserver<Boolean> sub = new TestObserver<>();
mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"})
mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2")
.subscribe(sub);

sub.assertComplete();
Expand All @@ -537,7 +538,7 @@ public void shouldShowRequestPermissionRationale_allDenied_oneRationale() {
when(activity.shouldShowRequestPermissionRationale("p1")).thenReturn(true);

TestObserver<Boolean> sub = new TestObserver<>();
mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"})
mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2")
.subscribe(sub);

sub.assertComplete();
Expand All @@ -552,7 +553,7 @@ public void shouldShowRequestPermissionRationale_allDenied_noRationale() {
Activity activity = mock(Activity.class);

TestObserver<Boolean> sub = new TestObserver<>();
mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"})
mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2")
.subscribe(sub);

sub.assertComplete();
Expand All @@ -569,7 +570,7 @@ public void shouldShowRequestPermissionRationale_oneDeniedRationale() {
when(activity.shouldShowRequestPermissionRationale("p2")).thenReturn(true);

TestObserver<Boolean> sub = new TestObserver<>();
mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"})
mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2")
.subscribe(sub);

sub.assertComplete();
Expand All @@ -585,7 +586,7 @@ public void shouldShowRequestPermissionRationale_oneDeniedNotRationale() {
when(mRxPermissions.isGranted("p2")).thenReturn(true);

TestObserver<Boolean> sub = new TestObserver<>();
mRxPermissions.shouldShowRequestPermissionRationale(activity, new String[]{"p1", "p2"})
mRxPermissions.shouldShowRequestPermissionRationale(activity, "p1", "p2")
.subscribe(sub);

sub.assertComplete();
Expand Down

0 comments on commit d31e5dd

Please sign in to comment.