mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-02-11 17:30:39 +01:00
add a check for writing to SD card for Android 5+
This commit is contained in:
parent
8849f56c10
commit
e511702fd5
@ -37,4 +37,12 @@ public class Config {
|
|||||||
public void setShowHidden(boolean show) {
|
public void setShowHidden(boolean show) {
|
||||||
mPrefs.edit().putBoolean(Constants.SHOW_HIDDEN, show).apply();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.simplemobiletools.filemanager;
|
|||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String PATH = "path";
|
public static final String PATH = "path";
|
||||||
|
public static final String TREE_URI = "tree_uri";
|
||||||
|
|
||||||
// shared preferences
|
// shared preferences
|
||||||
public static final String PREFS_KEY = "File Manager";
|
public static final String PREFS_KEY = "File Manager";
|
||||||
|
@ -5,6 +5,7 @@ import android.content.Context
|
|||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.support.v4.content.ContextCompat
|
import android.support.v4.content.ContextCompat
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import com.simplemobiletools.filepicker.extensions.getSDCardPath
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
object Utils {
|
object Utils {
|
||||||
@ -29,4 +30,8 @@ object Utils {
|
|||||||
val matcher = pattern.matcher(name)
|
val matcher = pattern.matcher(name)
|
||||||
return matcher.matches()
|
return matcher.matches()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isPathOnSD(context: Context, path: String): Boolean {
|
||||||
|
return path.startsWith(context.getSDCardPath())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,7 +137,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
if (files != null) {
|
if (files != null) {
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
final String curPath = file.getAbsolutePath();
|
final String curPath = file.getAbsolutePath();
|
||||||
final String curName = Utils.getFilename(curPath);
|
final String curName = Utils.INSTANCE.getFilename(curPath);
|
||||||
if (!mShowHidden && curName.startsWith("."))
|
if (!mShowHidden && curName.startsWith("."))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
} else {
|
} else {
|
||||||
final String path = item.getPath();
|
final String path = item.getPath();
|
||||||
final File file = new File(path);
|
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)
|
if (mimeType == null)
|
||||||
mimeType = "text/plain";
|
mimeType = "text/plain";
|
||||||
|
|
||||||
@ -192,7 +192,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
if (!tryGenericMimeType(intent, mimeType, file)) {
|
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()) {
|
if (uris.isEmpty()) {
|
||||||
Utils.showToast(getContext(), R.string.no_files_selected);
|
Utils.INSTANCE.showToast(getContext(), R.string.no_files_selected);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -493,7 +493,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copyFailed() {
|
public void copyFailed() {
|
||||||
Utils.showToast(getContext(), R.string.copy_failed);
|
Utils.INSTANCE.showToast(getContext(), R.string.copy_failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ItemInteractionListener {
|
public interface ItemInteractionListener {
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package com.simplemobiletools.filemanager.activities
|
package com.simplemobiletools.filemanager.activities
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
|
import android.annotation.TargetApi
|
||||||
|
import android.app.Activity
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.support.v4.app.ActivityCompat
|
import android.support.v4.app.ActivityCompat
|
||||||
@ -14,11 +17,14 @@ import com.simplemobiletools.filemanager.Utils
|
|||||||
import com.simplemobiletools.filemanager.fragments.ItemsFragment
|
import com.simplemobiletools.filemanager.fragments.ItemsFragment
|
||||||
import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog
|
import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog
|
||||||
import com.simplemobiletools.filepicker.extensions.getInternalStoragePath
|
import com.simplemobiletools.filepicker.extensions.getInternalStoragePath
|
||||||
|
import com.simplemobiletools.filepicker.extensions.getSDCardPath
|
||||||
import com.simplemobiletools.filepicker.models.FileDirItem
|
import com.simplemobiletools.filepicker.models.FileDirItem
|
||||||
import com.simplemobiletools.filepicker.views.Breadcrumbs
|
import com.simplemobiletools.filepicker.views.Breadcrumbs
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener {
|
class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener {
|
||||||
|
val OPEN_DOCUMENT_TREE = 1
|
||||||
var mBasePath = getInternalStoragePath()
|
var mBasePath = getInternalStoragePath()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -112,8 +118,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
|
|||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
StoragePickerDialog(this@MainActivity, mBasePath, object : StoragePickerDialog.OnStoragePickerListener {
|
StoragePickerDialog(this@MainActivity, mBasePath, object : StoragePickerDialog.OnStoragePickerListener {
|
||||||
override fun onPick(pickedPath: String) {
|
override fun onPick(pickedPath: String) {
|
||||||
mBasePath = pickedPath
|
changePath(pickedPath)
|
||||||
openPath(pickedPath)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} 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 {
|
companion object {
|
||||||
private val STORAGE_PERMISSION = 1
|
private val STORAGE_PERMISSION = 1
|
||||||
private val BACK_PRESS_TIMEOUT = 5000
|
private val BACK_PRESS_TIMEOUT = 5000
|
||||||
|
Loading…
x
Reference in New Issue
Block a user