Fix 6.0 permission related crash

This commit is contained in:
Alexey Kulish 2016-03-09 18:03:40 +03:00
parent 861fdd1748
commit f3bd5e00f6
5 changed files with 73 additions and 13 deletions

View file

@ -164,6 +164,7 @@
</LinearLayout>
<View
android:id="@+id/divExtStorage"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="fill_horizontal"

View file

@ -9,6 +9,7 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="storage_permission_restart_is_required">Now the application is allowed to write to external storage. A manual application restart is required.</string>
<string name="release_2_3">
\u2022 OSM Live. Support map contributors and developers and get hourly map updates. \n\n
\u2022 Map markers. A new way to quickly select places on the map.\n\n

View file

@ -1,5 +1,6 @@
package net.osmand.plus.activities;
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
@ -7,8 +8,10 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.AsyncTask;
@ -17,9 +20,11 @@ import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.NotificationCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
@ -71,6 +76,7 @@ import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.dialogs.ErrorBottomSheetDialog;
import net.osmand.plus.dialogs.RateUsBottomSheetDialog;
import net.osmand.plus.dialogs.WhatsNewDialogFragment;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.ui.DataStoragePlaceDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
@ -104,6 +110,8 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -584,10 +592,29 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
if (app.isExternalStorageDirectoryReadOnly()
&& getSupportFragmentManager().findFragmentByTag(DataStoragePlaceDialogFragment.TAG) == null) {
DataStoragePlaceDialogFragment.showInstance(getSupportFragmentManager(), true);
if (DownloadActivity.hasPermissionToWriteExternalStorage(this)) {
DataStoragePlaceDialogFragment.showInstance(getSupportFragmentManager(), true);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
private void restartApp() {
AlertDialog.Builder bld = new AlertDialog.Builder(this);
bld.setMessage(R.string.storage_permission_restart_is_required);
bld.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
}
});
bld.show();
}
private void parseNavigationIntent(final Uri data) {
final String schemeSpecificPart = data.getSchemeSpecificPart();
@ -1184,12 +1211,31 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
OsmandPlugin.onRequestPermissionsResult(requestCode, permissions, grantResults);
MapControlsLayer mcl = mapView.getLayerByClass(MapControlsLayer.class);
if (mcl != null) {
mcl.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
&& grantResults.length > 0 && permissions.length > 0
&& Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
restartApp();
}
}, 1);
} else {
AccessibleToast.makeText(this,
R.string.missing_write_external_storage_permission,
Toast.LENGTH_LONG).show();
DataStoragePlaceDialogFragment.showInstance(getSupportFragmentManager(), true);
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

View file

@ -2,6 +2,7 @@ package net.osmand.plus.download;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
@ -81,7 +82,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
public static final int UPDATES_TAB_NUMBER = 2;
public static final int LOCAL_TAB_NUMBER = 1;
public static final int DOWNLOAD_TAB_NUMBER = 0;
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
public static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
private List<LocalIndexInfo> localIndexInfos = new ArrayList<>();
@ -602,9 +603,9 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
final boolean firstTime = getMyApplication().getAppInitializer().isFirstTime();
final boolean externalExists =
getMyApplication().getSettings().getSecondaryStorage() != null;
if (firstTime && (externalExists || !hasPermissionToWriteExternalStorage())
if (firstTime && (externalExists || !hasPermissionToWriteExternalStorage(this))
&& DataStoragePlaceDialogFragment.isInterestedInFirstTime) {
if (!hasPermissionToWriteExternalStorage()) {
if (!hasPermissionToWriteExternalStorage(this)) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
@ -621,8 +622,8 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
}
}
private boolean hasPermissionToWriteExternalStorage() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
public static boolean hasPermissionToWriteExternalStorage(Context ctx) {
return ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
}

View file

@ -36,6 +36,7 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
private File externalStorage;
public static boolean isInterestedInFirstTime = true;
boolean storageReadOnly;
boolean hasExternalStoragePermission;
@Override
public void onStart() {
@ -52,9 +53,14 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
hasExternalStoragePermission = DownloadActivity.hasPermissionToWriteExternalStorage(getActivity());
internalStorage = getInternalStorageDirectory(getActivity());
sharedStorage = getSharedStorageDirectory(getActivity());
externalStorage = getMyApplication().getSettings().getSecondaryStorage();
if (hasExternalStoragePermission) {
externalStorage = getMyApplication().getSettings().getSecondaryStorage();
}
Bundle args = null;
if (savedInstanceState != null) {
@ -92,11 +98,16 @@ public class DataStoragePlaceDialogFragment extends BottomSheetDialogFragment {
sharedMemoryDescription.setText(getFreeSpace(sharedStorage));
View memoryStickRow = view.findViewById(R.id.memoryStickRow);
memoryStickRow.setOnClickListener(externalMemoryOnClickListener);
ImageView memoryStickImageView = (ImageView) view.findViewById(R.id.memoryStickImageView);
memoryStickImageView.setImageDrawable(getContentIcon(R.drawable.ic_sdcard));
TextView memoryStickDescription = (TextView) view.findViewById(R.id.memoryStickDescription);
memoryStickDescription.setText(getFreeSpace(externalStorage));
if (hasExternalStoragePermission) {
memoryStickRow.setOnClickListener(externalMemoryOnClickListener);
ImageView memoryStickImageView = (ImageView) view.findViewById(R.id.memoryStickImageView);
memoryStickImageView.setImageDrawable(getContentIcon(R.drawable.ic_sdcard));
TextView memoryStickDescription = (TextView) view.findViewById(R.id.memoryStickDescription);
memoryStickDescription.setText(getFreeSpace(externalStorage));
} else {
view.findViewById(R.id.divExtStorage).setVisibility(View.GONE);
memoryStickRow.setVisibility(View.GONE);
}
final ImageButton closeImageButton = (ImageButton) view.findViewById(R.id.closeImageButton);
closeImageButton.setImageDrawable(getContentIcon(R.drawable.ic_action_remove_dark));