diff --git a/tedpermission/build.gradle b/tedpermission/build.gradle old mode 100644 new mode 100755 index ddc8239..7067df9 --- a/tedpermission/build.gradle +++ b/tedpermission/build.gradle @@ -23,7 +23,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.squareup:otto:1.3.7' - + compile 'com.android.support:appcompat-v7:23.3.0' + compile 'com.squareup:otto:1.3.8' } diff --git a/tedpermission/src/main/java/com/gun0912/tedpermission/TedPermissionActivity.java b/tedpermission/src/main/java/com/gun0912/tedpermission/TedPermissionActivity.java old mode 100644 new mode 100755 index 82b8ad2..941c3fc --- a/tedpermission/src/main/java/com/gun0912/tedpermission/TedPermissionActivity.java +++ b/tedpermission/src/main/java/com/gun0912/tedpermission/TedPermissionActivity.java @@ -47,6 +47,8 @@ public class TedPermissionActivity extends AppCompatActivity { String deniedCloseButtonText; String rationaleConfirmText; + private AlertDialog rationalDialog; + private AlertDialog denyDialog; @Override @@ -72,7 +74,7 @@ private void setupFromSavedInstanceState(Bundle savedInstanceState) { deniedCloseButtonText = savedInstanceState.getString(EXTRA_DENIED_DIALOG_CLOSE_TEXT); - settingButtonText =savedInstanceState.getString(EXTRA_SETTING_BUTTON_TEXT); + settingButtonText = savedInstanceState.getString(EXTRA_SETTING_BUTTON_TEXT); } else { Intent intent = getIntent(); @@ -123,30 +125,22 @@ private void checkPermissions(boolean fromOnActivityResult) { ArrayList needPermissions = new ArrayList<>(); - for (String permission : permissions) { - - - if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { needPermissions.add(permission); } - } boolean showRationale = false; - for(String permission:needPermissions){ - - if(ActivityCompat.shouldShowRequestPermissionRationale(this,permission)){ - showRationale=true; + for (String permission : needPermissions) { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { + showRationale = true; } - } - if (needPermissions.isEmpty()) { permissionGranted(); } @@ -156,17 +150,11 @@ else if (fromOnActivityResult) { } //Need Show Rationale else if (showRationale && !TextUtils.isEmpty(rationale_message)) { - showRationaleDialog(needPermissions); - - } //Need Request Permissions else { - requestPermissions(needPermissions); - - } @@ -174,11 +162,9 @@ else if (showRationale && !TextUtils.isEmpty(rationale_message)) { public void requestPermissions(ArrayList needPermissions) { ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQ_CODE_PERMISSION_REQUEST); - } - @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { @@ -189,44 +175,34 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, for (int i = 0; i < permissions.length; i++) { String permission = permissions[i]; if (grantResults[i] == PackageManager.PERMISSION_DENIED) { - - deniedPermissions.add(permission); - } } - if (deniedPermissions.isEmpty()) { - permissionGranted(); + if (permissions.length > 0) { + if (deniedPermissions.isEmpty()) { + permissionGranted(); + } else { + showPermissionDenyDialog(deniedPermissions); + } } else { - - - showPermissionDenyDialog(deniedPermissions); - - + checkPermissions(false); } - - } - private void showRationaleDialog(final ArrayList needPermissions) { - - new AlertDialog.Builder(this) + rationalDialog = new AlertDialog.Builder(this) .setMessage(rationale_message) .setCancelable(false) - .setNegativeButton(rationaleConfirmText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { requestPermissions(needPermissions); } - }) - .show(); - - + }).create(); + rationalDialog.show(); } public void showPermissionDenyDialog(final ArrayList deniedPermissions) { @@ -239,11 +215,8 @@ public void showPermissionDenyDialog(final ArrayList deniedPermissions) AlertDialog.Builder builder = new AlertDialog.Builder(this); - - builder.setMessage(denyMessage) .setCancelable(false) - .setNegativeButton(deniedCloseButtonText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { @@ -254,7 +227,7 @@ public void onClick(DialogInterface dialogInterface, int i) { if (hasSettingButton) { - if(TextUtils.isEmpty(settingButtonText)){ + if (TextUtils.isEmpty(settingButtonText)) { settingButtonText = getString(R.string.tedpermission_setting); } @@ -278,9 +251,8 @@ public void onClick(DialogInterface dialog, int which) { } - - builder.show(); - + denyDialog = builder.create(); + denyDialog.show(); } @@ -296,4 +268,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } + @Override + protected void onPause() { + if (rationalDialog != null && rationalDialog.isShowing()) { + rationalDialog.dismiss(); + } + if (denyDialog != null && denyDialog.isShowing()) { + denyDialog.dismiss(); + } + super.onPause(); + } }