From 0ce99bc10e3b764573eb48c5c3251fa0f801c0c7 Mon Sep 17 00:00:00 2001 From: Dmytro Vodnik Date: Thu, 13 Jul 2017 16:26:07 +0300 Subject: [PATCH] improvements for #657 issue --- .../ContributionsListFragment.java | 56 ++++++++++++++++--- .../nrw/commons/nearby/NearbyActivity.java | 43 +++++++++++++- 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java index b181ba2aa..8025b94bf 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java @@ -1,8 +1,8 @@ package fr.free.nrw.commons.contributions; import android.Manifest; -import android.content.ActivityNotFoundException; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -10,6 +10,7 @@ import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; +import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -106,19 +107,57 @@ public class ContributionsListFragment extends Fragment { case R.id.menu_from_gallery: //Gallery crashes before reach ShareActivity screen so must implement permissions check here if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - //See http://stackoverflow.com/questions/33169455/onrequestpermissionsresult-not-being-called-in-dialog-fragment - requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); - return true; + + // Here, thisActivity is the current activity + if (ContextCompat.checkSelfPermission(getActivity(), + Manifest.permission.READ_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + // Should we show an explanation? + if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) { + + // Show an explanation to the user *asynchronously* -- don't block + // this thread waiting for the user's response! After the user + // sees the explanation, try again to request the permission. + + new AlertDialog.Builder(getActivity()) + .setMessage(getString(R.string.storage_permission_rationale)) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + requestPermissions( + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + 1); + dialog.dismiss(); + } + }) + .setNegativeButton("Cancel", null) + .create() + .show(); + + } else { + + // No explanation needed, we can request the permission. + + requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + 1); + + // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an + // app-defined int constant. The callback method gets the + // result of the request. + } } else { controller.startGalleryPick(); return true; } - } - else { + + } else { controller.startGalleryPick(); return true; } + + return true; case R.id.menu_from_camera: controller.startCameraCapture(); return true; @@ -129,6 +168,9 @@ public class ContributionsListFragment extends Fragment { @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + + Timber.d("onRequestPermissionsResult: req code = " + " perm = " + permissions + " grant =" + grantResults); + switch (requestCode) { // 1 = Storage allowed when gallery selected case 1: { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java index ec7d62887..b48f10634 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java @@ -101,8 +101,47 @@ public class NearbyActivity extends NavigationBaseActivity { Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { startLookingForNearby(); } else { - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST); + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + + // Should we show an explanation? + if (ActivityCompat.shouldShowRequestPermissionRationale(this, + Manifest.permission.ACCESS_FINE_LOCATION)) { + + // Show an explanation to the user *asynchronously* -- don't block + // this thread waiting for the user's response! After the user + // sees the explanation, try again to request the permission. + + new AlertDialog.Builder(this) + .setMessage(getString(R.string.location_permission_rationale)) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + ActivityCompat.requestPermissions(NearbyActivity.this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + LOCATION_REQUEST); + dialog.dismiss(); + } + }) + .setNegativeButton("Cancel", null) + .create() + .show(); + + } else { + + // No explanation needed, we can request the permission. + + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + LOCATION_REQUEST); + + // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an + // app-defined int constant. The callback method gets the + // result of the request. + } + } } } else { startLookingForNearby();