Search in sources :

Example 1 with Permission

use of com.tbruyelle.rxpermissions2.Permission in project RxPermissions by tbruyelle.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions.setLogging(true);
    setContentView(R.layout.act_main);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    RxJavaInterop.toV2Observable(RxView.clicks(findViewById(R.id.enableCamera)).map(new Func1<Void, Object>() {

        @Override
        public Object call(final Void aVoid) {
            return new Object();
        }
    })).compose(rxPermissions.ensureEach(permission.CAMERA)).subscribe(new Consumer<Permission>() {

        @Override
        public void accept(Permission permission) {
            Log.i(TAG, "Permission result " + permission);
            if (permission.granted) {
                releaseCamera();
                camera = Camera.open(0);
                try {
                    camera.setPreviewDisplay(surfaceView.getHolder());
                    camera.startPreview();
                } catch (IOException e) {
                    Log.e(TAG, "Error while trying to display the camera preview", e);
                }
            } else if (permission.shouldShowRequestPermissionRationale) {
                // Denied permission without ask never again
                Toast.makeText(MainActivity.this, "Denied permission without ask never again", Toast.LENGTH_SHORT).show();
            } else {
                // Denied permission with ask never again
                // Need to go to the settings
                Toast.makeText(MainActivity.this, "Permission denied, can't enable the camera", Toast.LENGTH_SHORT).show();
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable t) {
            Log.e(TAG, "onError", t);
        }
    }, new Action() {

        @Override
        public void run() {
            Log.i(TAG, "OnComplete");
        }
    });
}
Also used : Action(io.reactivex.functions.Action) RxPermissions(com.tbruyelle.rxpermissions2.RxPermissions) Permission(com.tbruyelle.rxpermissions2.Permission) IOException(java.io.IOException) Func1(rx.functions.Func1)

Example 2 with Permission

use of com.tbruyelle.rxpermissions2.Permission in project RxPermissions by tbruyelle.

the class RxPermissionsSampleTest method test_permission_denied_dont_ask_again.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void test_permission_denied_dont_ask_again() throws Exception {
    // mocks
    final String permissionString = Manifest.permission.READ_PHONE_STATE;
    final boolean granted = false;
    final boolean shouldShowRequestPermissionRationale = false;
    final Permission permission = new Permission(permissionString, granted, shouldShowRequestPermissionRationale);
    when(rxPermissions.requestEach(permissionString)).thenReturn(Observable.just(permission));
    // test
    rxPermissions.requestEach(permissionString).test().assertNoErrors().assertValue(permission);
}
Also used : Permission(com.tbruyelle.rxpermissions2.Permission) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 3 with Permission

use of com.tbruyelle.rxpermissions2.Permission in project DevRing by LJYcoder.

the class PermissionManager method requestEachCombined.

// 请求多个权限建议用这个
public void requestEachCombined(Activity activity, final PermissionListener listener, String... permissions) {
    if (activity != null) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions.requestEachCombined(permissions).subscribe(new Consumer<Permission>() {

            @Override
            public void accept(Permission permission) throws Exception {
                if (permission.granted) {
                    // All permissions are granted !
                    if (listener != null) {
                        listener.onGranted(permission.name);
                    }
                } else if (permission.shouldShowRequestPermissionRationale) {
                    // At least one denied permission without ask never again
                    if (listener != null) {
                        listener.onDenied(permission.name);
                    }
                } else {
                    // Need to go to the settings
                    if (listener != null) {
                        listener.onDeniedWithNeverAsk(permission.name);
                    }
                }
            }
        });
    }
}
Also used : RxPermissions(com.tbruyelle.rxpermissions2.RxPermissions) Permission(com.tbruyelle.rxpermissions2.Permission)

Example 4 with Permission

use of com.tbruyelle.rxpermissions2.Permission in project DevRing by LJYcoder.

the class PermissionManager method requestEach.

// 请求单个权限建议用这个
public void requestEach(Activity activity, final PermissionListener listener, String... permissions) {
    if (activity != null) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions.requestEach(permissions).subscribe(new Consumer<Permission>() {

            @Override
            public void accept(Permission permission) throws Exception {
                if (permission.granted) {
                    // `permission.name` is granted !
                    if (listener != null) {
                        listener.onGranted(permission.name);
                    }
                } else if (permission.shouldShowRequestPermissionRationale) {
                    // Denied permission without ask never again
                    if (listener != null) {
                        listener.onDenied(permission.name);
                    }
                } else {
                    // Need to go to the settings
                    if (listener != null) {
                        listener.onDeniedWithNeverAsk(permission.name);
                    }
                }
            }
        });
    }
}
Also used : RxPermissions(com.tbruyelle.rxpermissions2.RxPermissions) Permission(com.tbruyelle.rxpermissions2.Permission)

Aggregations

Permission (com.tbruyelle.rxpermissions2.Permission)4 RxPermissions (com.tbruyelle.rxpermissions2.RxPermissions)3 TargetApi (android.annotation.TargetApi)1 Action (io.reactivex.functions.Action)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 Func1 (rx.functions.Func1)1