Search in sources :

Example 1 with Permission

use of com.tbruyelle.rxpermissions3.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.rxpermissions3.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 3 with Permission

use of com.tbruyelle.rxpermissions3.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)

Example 4 with Permission

use of com.tbruyelle.rxpermissions3.Permission in project grpc-java by grpc.

the class RbacFilterTest method compositeRules.

@Test
@SuppressWarnings("unchecked")
public void compositeRules() {
    MetadataMatcher metadataMatcher = MetadataMatcher.newBuilder().build();
    List<Permission> permissionList = Arrays.asList(Permission.newBuilder().setOrRules(Permission.Set.newBuilder().addRules(Permission.newBuilder().setMetadata(metadataMatcher).build()).build()).build());
    List<Principal> principalList = Arrays.asList(Principal.newBuilder().setNotId(Principal.newBuilder().setMetadata(metadataMatcher).build()).build());
    ConfigOrError<? extends FilterConfig> result = parse(permissionList, principalList);
    assertThat(result.errorDetail).isNull();
    assertThat(result.config).isInstanceOf(RbacConfig.class);
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    GrpcAuthorizationEngine engine = new GrpcAuthorizationEngine(((RbacConfig) result.config).authConfig());
    AuthDecision decision = engine.evaluate(new Metadata(), serverCall);
    assertThat(decision.decision()).isEqualTo(GrpcAuthorizationEngine.Action.ALLOW);
}
Also used : MetadataMatcher(io.envoyproxy.envoy.type.matcher.v3.MetadataMatcher) AuthDecision(io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine.AuthDecision) Permission(io.envoyproxy.envoy.config.rbac.v3.Permission) Metadata(io.grpc.Metadata) GrpcAuthorizationEngine(io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine) Principal(io.envoyproxy.envoy.config.rbac.v3.Principal) Test(org.junit.Test)

Example 5 with Permission

use of com.tbruyelle.rxpermissions3.Permission in project grpc-java by grpc.

the class RbacFilterTest method portRangeParser.

@Test
@SuppressWarnings({ "unchecked", "deprecation" })
public void portRangeParser() {
    List<Permission> permissionList = Arrays.asList(Permission.newBuilder().setDestinationPortRange(Int32Range.newBuilder().setStart(1010).setEnd(65535).build()).build());
    List<Principal> principalList = Arrays.asList(Principal.newBuilder().setRemoteIp(CidrRange.newBuilder().setAddressPrefix("10.10.10.0").setPrefixLen(UInt32Value.of(24)).build()).build());
    ConfigOrError<?> result = parse(permissionList, principalList);
    assertThat(result.errorDetail).isNull();
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    Attributes attributes = Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress("10.10.10.0", 1)).set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, new InetSocketAddress("10.10.10.0", 9090)).build();
    when(serverCall.getAttributes()).thenReturn(attributes);
    when(serverCall.getMethodDescriptor()).thenReturn(method().build());
    GrpcAuthorizationEngine engine = new GrpcAuthorizationEngine(((RbacConfig) result.config).authConfig());
    AuthDecision decision = engine.evaluate(new Metadata(), serverCall);
    assertThat(decision.decision()).isEqualTo(GrpcAuthorizationEngine.Action.DENY);
}
Also used : AuthDecision(io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine.AuthDecision) InetSocketAddress(java.net.InetSocketAddress) Permission(io.envoyproxy.envoy.config.rbac.v3.Permission) Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata) GrpcAuthorizationEngine(io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine) Principal(io.envoyproxy.envoy.config.rbac.v3.Principal) Test(org.junit.Test)

Aggregations

Permission (io.envoyproxy.envoy.config.rbac.v3.Permission)9 Principal (io.envoyproxy.envoy.config.rbac.v3.Principal)9 Test (org.junit.Test)9 GrpcAuthorizationEngine (io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine)6 AuthDecision (io.grpc.xds.internal.rbac.engine.GrpcAuthorizationEngine.AuthDecision)6 Permission (com.tbruyelle.rxpermissions2.Permission)5 RxPermissions (com.tbruyelle.rxpermissions2.RxPermissions)5 Metadata (io.grpc.Metadata)5 Attributes (io.grpc.Attributes)3 Permission (com.tbruyelle.rxpermissions3.Permission)2 HeaderMatcher (io.envoyproxy.envoy.config.route.v3.HeaderMatcher)2 PathMatcher (io.envoyproxy.envoy.type.matcher.v3.PathMatcher)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 TargetApi (android.annotation.TargetApi)1 Message (com.google.protobuf.Message)1 RxPermissions (com.tbruyelle.rxpermissions3.RxPermissions)1 CidrRange (io.envoyproxy.envoy.config.core.v3.CidrRange)1 Policy (io.envoyproxy.envoy.config.rbac.v3.Policy)1 MetadataMatcher (io.envoyproxy.envoy.type.matcher.v3.MetadataMatcher)1