only show "SDCards can be used to swap" if an SD Card is present

This commit is contained in:
Hans-Christoph Steiner 2018-12-21 18:06:25 +01:00
parent 20ebc00e5e
commit 43ee48ac2b

View File

@ -85,11 +85,15 @@ class NearbyViewBinder {
Environment.isExternalStorageRemovable(activity.getExternalFilesDir(""))); Environment.isExternalStorageRemovable(activity.getExternalFilesDir("")));
File[] dirs = activity.getExternalFilesDirs(""); File[] dirs = activity.getExternalFilesDirs("");
if (dirs != null) { if (dirs != null) {
for (File f : dirs) { for (File dir : dirs) {
if (f != null && Environment.isExternalStorageRemovable(f)) { if (dir != null && Environment.isExternalStorageRemovable(dir)) {
// remove Android/data/org.fdroid.fdroid/files to get root String state = Environment.getExternalStorageState(dir);
externalStorage = f.getParentFile().getParentFile().getParentFile().getParentFile(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)
break; || Environment.MEDIA_MOUNTED.equals(state)) {
// remove Android/data/org.fdroid.fdroid/files to get root
externalStorage = dir.getParentFile().getParentFile().getParentFile().getParentFile();
break;
}
} }
} }
} }
@ -100,7 +104,11 @@ class NearbyViewBinder {
externalStorage = Environment.getExternalStorageDirectory(); externalStorage = Environment.getExternalStorageDirectory();
} }
if (externalStorage != null) { final String writeExternalStorage = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (externalStorage != null
|| PackageManager.PERMISSION_GRANTED
!= ContextCompat.checkSelfPermission(activity, writeExternalStorage)) {
nearbySplash.setVisibility(View.GONE); nearbySplash.setVisibility(View.GONE);
View readExternalStorage = swapView.findViewById(R.id.readExternalStorage); View readExternalStorage = swapView.findViewById(R.id.readExternalStorage);
readExternalStorage.setVisibility(View.VISIBLE); readExternalStorage.setVisibility(View.VISIBLE);
@ -116,9 +124,8 @@ class NearbyViewBinder {
msg += "|" + f.getName(); msg += "|" + f.getName();
} }
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show(); Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
final String writeExternalStorage = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (Build.VERSION.SDK_INT >= 23 if (Build.VERSION.SDK_INT >= 23
&& !externalStorage.canRead() && (externalStorage == null || !externalStorage.canRead())
&& PackageManager.PERMISSION_GRANTED && PackageManager.PERMISSION_GRANTED
!= ContextCompat.checkSelfPermission(activity, writeExternalStorage)) { != ContextCompat.checkSelfPermission(activity, writeExternalStorage)) {
ActivityCompat.requestPermissions(activity, new String[]{writeExternalStorage}, ActivityCompat.requestPermissions(activity, new String[]{writeExternalStorage},