improvements for #657 issue

This commit is contained in:
Dmytro Vodnik 2017-07-13 16:26:07 +03:00
parent 691acbf983
commit 0ce99bc10e
2 changed files with 90 additions and 9 deletions

View file

@ -1,8 +1,8 @@
package fr.free.nrw.commons.contributions; package fr.free.nrw.commons.contributions;
import android.Manifest; import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -10,6 +10,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -106,19 +107,57 @@ public class ContributionsListFragment extends Fragment {
case R.id.menu_from_gallery: case R.id.menu_from_gallery:
//Gallery crashes before reach ShareActivity screen so must implement permissions check here //Gallery crashes before reach ShareActivity screen so must implement permissions check here
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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 // Here, thisActivity is the current activity
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); if (ContextCompat.checkSelfPermission(getActivity(),
return true; 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 { } else {
controller.startGalleryPick(); controller.startGalleryPick();
return true; return true;
} }
}
else { } else {
controller.startGalleryPick(); controller.startGalleryPick();
return true; return true;
} }
return true;
case R.id.menu_from_camera: case R.id.menu_from_camera:
controller.startCameraCapture(); controller.startCameraCapture();
return true; return true;
@ -129,6 +168,9 @@ public class ContributionsListFragment extends Fragment {
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Timber.d("onRequestPermissionsResult: req code = " + " perm = " + permissions + " grant =" + grantResults);
switch (requestCode) { switch (requestCode) {
// 1 = Storage allowed when gallery selected // 1 = Storage allowed when gallery selected
case 1: { case 1: {

View file

@ -101,8 +101,47 @@ public class NearbyActivity extends NavigationBaseActivity {
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
startLookingForNearby(); startLookingForNearby();
} else { } else {
ActivityCompat.requestPermissions(this, if (ContextCompat.checkSelfPermission(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST); 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 { } else {
startLookingForNearby(); startLookingForNearby();