diff --git a/app/src/main/java/net/osmtracker/activity/TrackManager.java b/app/src/main/java/net/osmtracker/activity/TrackManager.java index 57cd165f5..5baa10160 100644 --- a/app/src/main/java/net/osmtracker/activity/TrackManager.java +++ b/app/src/main/java/net/osmtracker/activity/TrackManager.java @@ -52,6 +52,7 @@ public class TrackManager extends ListActivity { @SuppressWarnings("unused") private static final String TAG = TrackManager.class.getSimpleName(); + final private int RC_WRITE_PERMISSIONS_UPLOAD = 4; final private int RC_WRITE_STORAGE_DISPLAY_TRACK = 3; final private int RC_WRITE_PERMISSIONS_EXPORT_ALL = 1; final private int RC_WRITE_PERMISSIONS_EXPORT_ONE = 2; @@ -238,27 +239,15 @@ public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create().show(); - break; case R.id.trackmgr_menu_exportall: // Confirm - new AlertDialog.Builder(this) - .setTitle(R.string.menu_exportall) - .setMessage(getResources().getString(R.string.trackmgr_exportall_confirm)) - .setCancelable(true) - .setIcon(android.R.drawable.ic_dialog_alert) - .setPositiveButton(R.string.menu_exportall, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - requestPermissionAndExport(TrackManager.this.RC_WRITE_PERMISSIONS_EXPORT_ALL); - } - }) - .setNegativeButton(android.R.string.cancel, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } - }).create().show(); + if (!writeExternalStoragePermissionGranted()){ + Log.e("DisplayTrackMapWrite", "Permission asked"); + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_WRITE_PERMISSIONS_EXPORT_ALL); + } + else exportAllTracks(); break; case R.id.trackmgr_menu_settings: // Start settings activity @@ -291,40 +280,6 @@ private void startTrackLogger(){ } } - - private void requestPermissionAndExport(int typeCode){ - if (ContextCompat.checkSelfPermission(this, - Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - - // Should we show an explanation? - if (ActivityCompat.shouldShowRequestPermissionRationale(this, - Manifest.permission.WRITE_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. - // TODO: explain why we need permission. - Log.w(TAG, "we should explain why we need write permission_REQUEST"); - Toast.makeText(this, "To export the GPX trace we need to write on the storage.", Toast.LENGTH_LONG).show(); - - } else { - - // No explanation needed, we can request the permission. - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, typeCode); - } - - } else { - switch (typeCode) { - case RC_WRITE_PERMISSIONS_EXPORT_ALL: - exportAllTracks(); - - case RC_WRITE_PERMISSIONS_EXPORT_ONE: - exportOneTrack(); - } - } - } - private void exportOneTrack(){ if (trackId != -1) { new ExportToStorageTask(this, trackId).execute(); @@ -414,13 +369,22 @@ public void onClick(DialogInterface dialog, int which) { case R.id.trackmgr_contextmenu_export: trackId = info.id; - requestPermissionAndExport(this.RC_WRITE_PERMISSIONS_EXPORT_ONE); +// requestPermissionAndExport(this.RC_WRITE_PERMISSIONS_EXPORT_ONE); + if (!writeExternalStoragePermissionGranted()){ + Log.e("DisplayTrackMapWrite", "Permission asked"); + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_WRITE_PERMISSIONS_EXPORT_ONE); + } + else exportOneTrack(); break; case R.id.trackmgr_contextmenu_osm_upload: - i = new Intent(this, OpenStreetMapUpload.class); - i.putExtra(TrackContentProvider.Schema.COL_TRACK_ID, info.id); - startActivity(i); + if (!writeExternalStoragePermissionGranted()){ + Log.e("DisplayTrackMapWrite", "Permission asked"); + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_WRITE_PERMISSIONS_UPLOAD); + } + else uploadTrack(trackSelected); break; case R.id.trackmgr_contextmenu_display: @@ -442,6 +406,13 @@ public void onClick(DialogInterface dialog, int which) { return super.onContextItemSelected(item); } + private void uploadTrack(MenuItem item){ + final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + Intent i = new Intent(this, OpenStreetMapUpload.class); + i.putExtra(TrackContentProvider.Schema.COL_TRACK_ID, info.id); + startActivity(i); + } + private void displayTrack(MenuItem item){ Log.e(TAG, "On Display Track"); // Start display track activity, with or without OSM background @@ -600,6 +571,7 @@ public void onRequestPermissionsResult(int requestCode, Log.w(TAG, "we should explain why we need write permission_EXPORT_ALL"); Toast.makeText(this, "To export the GPX trace we need to write on the storage.", Toast.LENGTH_LONG).show(); } + break; } case RC_WRITE_PERMISSIONS_EXPORT_ONE: { // If request is cancelled, the result arrays are empty. @@ -617,6 +589,7 @@ public void onRequestPermissionsResult(int requestCode, Log.w(TAG, "we should explain why we need write permission_EXPORT_ONE"); Toast.makeText(this, "To export the GPX trace we need to write on the storage.", Toast.LENGTH_LONG).show(); } + break; } case RC_WRITE_STORAGE_DISPLAY_TRACK: { // If request is cancelled, the result arrays are empty. @@ -625,13 +598,30 @@ public void onRequestPermissionsResult(int requestCode, Log.e("Result", "Permission granted"); // permission was granted, yay! displayTrack(trackSelected); + } else { + // permission denied, boo! Disable the + // functionality that depends on this permission. + //TODO: add an informative message. + Log.w(TAG, "Permission not granted"); + Toast.makeText(this, "To display the track properly we need access to the storage.", Toast.LENGTH_LONG).show(); + } + break; + } + case RC_WRITE_PERMISSIONS_UPLOAD: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + Log.e("Result", "Permission granted"); + // permission was granted, yay! + uploadTrack(trackSelected); } else { // permission denied, boo! Disable the // functionality that depends on this permission. //TODO: add an informative message. Log.w(TAG, "Permission not granted"); + Toast.makeText(this, "To upload the track to OSM we need access to the storage.", Toast.LENGTH_LONG).show(); } break; }