add a check for writing to SD card for Android 5+

This commit is contained in:
tibbi 2016-11-05 15:54:24 +01:00
parent 8849f56c10
commit e511702fd5
5 changed files with 61 additions and 7 deletions

View file

@ -37,4 +37,12 @@ public class Config {
public void setShowHidden(boolean show) {
mPrefs.edit().putBoolean(Constants.SHOW_HIDDEN, show).apply();
}
public String getTreeUri() {
return mPrefs.getString(Constants.TREE_URI, "");
}
public void setTreeUri(String uri) {
mPrefs.edit().putString(Constants.TREE_URI, uri).apply();
}
}

View file

@ -2,6 +2,7 @@ package com.simplemobiletools.filemanager;
public class Constants {
public static final String PATH = "path";
public static final String TREE_URI = "tree_uri";
// shared preferences
public static final String PREFS_KEY = "File Manager";

View file

@ -5,6 +5,7 @@ import android.content.Context
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import java.util.regex.Pattern
object Utils {
@ -29,4 +30,8 @@ object Utils {
val matcher = pattern.matcher(name)
return matcher.matches()
}
fun isPathOnSD(context: Context, path: String): Boolean {
return path.startsWith(context.getSDCardPath())
}
}

View file

@ -137,7 +137,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
if (files != null) {
for (File file : files) {
final String curPath = file.getAbsolutePath();
final String curName = Utils.getFilename(curPath);
final String curName = Utils.INSTANCE.getFilename(curPath);
if (!mShowHidden && curName.startsWith("."))
continue;
@ -181,7 +181,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
} else {
final String path = item.getPath();
final File file = new File(path);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.getFileExtension(path));
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.INSTANCE.getFileExtension(path));
if (mimeType == null)
mimeType = "text/plain";
@ -192,7 +192,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
startActivity(intent);
} catch (ActivityNotFoundException e) {
if (!tryGenericMimeType(intent, mimeType, file)) {
Utils.showToast(getContext(), R.string.no_app_found);
Utils.INSTANCE.showToast(getContext(), R.string.no_app_found);
}
}
}
@ -302,7 +302,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
}
if (uris.isEmpty()) {
Utils.showToast(getContext(), R.string.no_files_selected);
Utils.INSTANCE.showToast(getContext(), R.string.no_files_selected);
return;
}
@ -493,7 +493,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
@Override
public void copyFailed() {
Utils.showToast(getContext(), R.string.copy_failed);
Utils.INSTANCE.showToast(getContext(), R.string.copy_failed);
}
public interface ItemInteractionListener {

View file

@ -1,8 +1,11 @@
package com.simplemobiletools.filemanager.activities
import android.Manifest
import android.annotation.TargetApi
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.ActivityCompat
@ -14,11 +17,14 @@ import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.fragments.ItemsFragment
import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog
import com.simplemobiletools.filepicker.extensions.getInternalStoragePath
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.filepicker.models.FileDirItem
import com.simplemobiletools.filepicker.views.Breadcrumbs
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener {
val OPEN_DOCUMENT_TREE = 1
var mBasePath = getInternalStoragePath()
override fun onCreate(savedInstanceState: Bundle?) {
@ -112,8 +118,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
if (id == 0) {
StoragePickerDialog(this@MainActivity, mBasePath, object : StoragePickerDialog.OnStoragePickerListener {
override fun onPick(pickedPath: String) {
mBasePath = pickedPath
openPath(pickedPath)
changePath(pickedPath)
}
})
} else {
@ -122,6 +127,41 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
}
}
fun changePath(pickedPath: String) {
if (checkStupidAndroidFiveSDCardWritePermission(pickedPath)) {
mBasePath = pickedPath
openPath(pickedPath)
}
}
fun checkStupidAndroidFiveSDCardWritePermission(pickedPath: String): Boolean {
val file = File(pickedPath)
return if (!file.canWrite() && Utils.isPathOnSD(applicationContext, pickedPath) && mConfig.treeUri.isEmpty()) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, OPEN_DOCUMENT_TREE)
false
} else
true
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == OPEN_DOCUMENT_TREE) {
if (resultCode == Activity.RESULT_OK && resultData != null) {
val treeUri = resultData.data
mConfig.treeUri = resultData.data.toString()
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(treeUri, takeFlags)
changePath(getSDCardPath())
} else {
changePath(getInternalStoragePath())
}
}
}
companion object {
private val STORAGE_PERMISSION = 1
private val BACK_PRESS_TIMEOUT = 5000