Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User changes granted Permission from Settings screen

Application requires two permissions:

  1. Read Phone state
  2. Camera

Application has below screens:

Screen A: Splash (which handles screen navigation based on permission status)

Screen B: Permission screen (If user has not granted phone permission)

Screen C: Terms & Conditions screen

When it starts, App will check if phone permission is granted or not. If granted, app will open screen C, else It will remain on same screen B.

Steps:

  1. Open App -> Splash -> Permission screen
  2. Click on 'Grant Permission' -> Permission dialog -> Deny permission
  3. Click on 'Open Settings'
  4. Grant 'Camera' permission (not phone permission)
  5. Tap on 'Back' button of device and return to app.
  6. Permission screen will be displayed as no 'phone' permission granted.
  7. Again repeat step #2 and #3
  8. Now remove 'camera' permission and grant 'phone' permission(As we have changed camera permission app process will be killed and start again).
  9. Repeat step #5, Splash and then Screen C (Terms & Conditions) will be displayed.
  10. Again tap on 'Back' button, Same screen C will be displayed( screen C has two instances in back stack)

I can use 'CLEAR_TASK' and 'CLEAR_TOP' flags and startActivity, Purpose is to know what happens to our application when user changes, permission from settings.

Observation:

  1. When user grants permission, our application process will have no effect.

  2. When user removes granted permission, Android app will kill and restart application. (If this is happening, then why two instances of T&C screens?)

I am little confused here!!

like image 440
AndiGeeky Avatar asked Nov 08 '22 17:11

AndiGeeky


1 Answers

@AndiGeeky Here is some complete code to inform the user if Allow or Deny was selected and it will manage two other scenarios user selected Never Ask Again or did the user change the setting after it was allowed to not allowed

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //.... write file into storage ...
            System.out.println("SDK > BuildVersion TRUE");
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 666);  // Comment 26
            System.out.println("go to requestPermissions");
        }
    }
    onLoad();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {

        case 666: // Allowed was selected so Permission granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Permission Granted",Snackbar.LENGTH_LONG);
                View snackbarView = s.getView();
                TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.RED);
                textView.setTextSize(18);
                textView.setMaxLines(6);
                s.show();

                // do your work here

            } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {
                // User selected the Never Ask Again Option Change settings in app settings manually
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setTitle("Change Permissions in Settings");
                alertDialogBuilder
                        .setMessage("" +
                                "\nClick SETTINGS to Manually Set\n"+"Permissions to use Database Storage")
                        .setCancelable(false)
                        .setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivityForResult(intent, 1000);     // Comment 3.
                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            } else {
                    // User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                    alertDialogBuilder.setTitle("Second Chance");
                    alertDialogBuilder
                            .setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
                            .setCancelable(false)
                            .setPositiveButton("RETRY", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    //ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
                                    Intent i = new Intent(MainActivity.this,MainActivity.class);
                                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(i);
                                    }
                            })
                            .setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    finish();
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }
            }
            break;
    }};
like image 123
Vector Avatar answered Dec 11 '22 02:12

Vector