Skip to content

Commit

Permalink
Ask shouldShow after the permission request
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Oct 16, 2016
1 parent 874c8cd commit 30a9e6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ RxPermissions.getInstance(this)
.subscribe(permission -> { // will emit 2 Permission objects
if (permission.granted) {
// `permission.name` is granted !
} else if (permission.shouldShowRequestPermissionRationale)
// Denied permission without ask never again
} else {
// Denied permission with ask never again
// Need to go to the settings
}
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ protected void onNewIntent(Intent intent) {

private void handleIntent(Intent intent) {
String[] permissions = intent.getStringArrayExtra("permissions");
requestPermissions(permissions, 42);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
shouldShowRequestPermissionRationale = new boolean[permissions.length];

for (int i = 0; i < permissions.length; i++) {
shouldShowRequestPermissionRationale[i] = shouldShowRequestPermissionRationale(permissions[i]);
}

requestPermissions(permissions, 42);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
RxPermissions.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults, shouldShowRequestPermissionRationale);
finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;
import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.IOException;
Expand All @@ -35,12 +36,12 @@ protected void onCreate(Bundle savedInstanceState) {

RxView.clicks(findViewById(R.id.enableCamera))
// Ask for permissions when button is clicked
.compose(rxPermissions.ensure(Manifest.permission.CAMERA))
.subscribe(new Action1<Boolean>() {
.compose(rxPermissions.ensureEach(Manifest.permission.CAMERA))
.subscribe(new Action1<Permission>() {
@Override
public void call(Boolean granted) {
Log.i(TAG, "Permission result " + granted);
if (granted) {
public void call(Permission permission) {
Log.i(TAG, "Permission result " + permission);
if (permission.granted) {
releaseCamera();
camera = Camera.open(0);
try {
Expand All @@ -49,7 +50,14 @@ public void call(Boolean granted) {
} 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();
Expand Down

0 comments on commit 30a9e6e

Please sign in to comment.