diff options
Diffstat (limited to 'android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java')
-rw-r--r-- | android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java b/android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java index fb4236969cb2..02d58d329122 100644 --- a/android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java +++ b/android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java @@ -14,7 +14,13 @@ import java.io.File; import org.libreoffice.storage.IFile; import org.libreoffice.R; +import android.Manifest; +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.Build; import android.os.Environment; +import android.support.v4.content.ContextCompat; +import android.util.Log; /** * A convenience IDocumentProvider to browse the /sdcard/Documents directory. @@ -29,11 +35,31 @@ public class LocalDocumentsDirectoryProvider extends LocalDocumentsProvider { super(id); } + private static File getDocumentsDir() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + // DIRECTORY_DOCUMENTS is 19 or later only + return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); + } else { + return new File(Environment.getExternalStorageDirectory() + "/Documents"); + } + } + @Override - public IFile getRootDirectory() { - File documentsDirectory = new File( - Environment.getExternalStorageDirectory(), "Documents"); - documentsDirectory.mkdirs(); + public IFile getRootDirectory(Context context) { + File documentsDirectory = getDocumentsDir(); + if (!documentsDirectory.exists()) { + // might be a little counter-intuitive: if we were granted READ permission already, we're also granted the write-permission + // when we ask for it, since they are both in the same storage group (for 5.1 and lower it is granted at install-time already) + // seehttps://developer.android.com/guide/topics/permissions/requesting.html#perm-groups + if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + if(!documentsDirectory.mkdirs()) { + // fallback to the toplevel dir - might be due to the dir not mounted/used as USB-Mass-Storage or similar + // TODO: handle unavailability of the storage/failure of the mkdir properly + Log.e("LocalDocumentsProvider", "not sure how we ended up here - if we have read permissions to use it in the first place, we also should have the write-permissions.."); + documentsDirectory = Environment.getExternalStorageDirectory(); + } + } + } return new LocalFile(documentsDirectory); } @@ -41,4 +67,10 @@ public class LocalDocumentsDirectoryProvider extends LocalDocumentsProvider { public int getNameResource() { return R.string.local_documents; } + + @Override + public boolean checkProviderAvailability(Context context) { + File documentsDirectory = getDocumentsDir(); + return documentsDirectory.exists() || ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + } } |