[Libreoffice-commits] core.git: android/source

Michael Weghorn (via logerrit) logerrit at kemper.freedesktop.org
Fri Apr 9 15:19:47 UTC 2021


 android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java |   18 ++++++++++
 1 file changed, 18 insertions(+)

New commits:
commit 7d9db806d65cb814af1e99a1e79c3db5aa7c17d5
Author:     Michael Weghorn <m.weghorn at posteo.de>
AuthorDate: Fri Apr 9 11:24:16 2021 +0200
Commit:     Michael Weghorn <m.weghorn at posteo.de>
CommitDate: Fri Apr 9 17:19:01 2021 +0200

    android: Request PERMISSION_WRITE_EXTERNAL_STORAGE again
    
    Requesting the permission on app start had been dropped in
    
        commit a23bd42e9b2f6401c710ac95afcc3aa8f360d65c
        Date:   Tue Apr 6 14:26:06 2021 +0200
    
            android: Drop custom file abstraction + UI
    
    Since the app now uses the Android Storage Access Framework [1]
    in order to open or create files from within the app,
    it not longer needs PERMISSION_WRITE_EXTERNAL_STORAGE for this.
    However, at least PDF export currently still directly writes
    to local storage ("Documents" directory), which fails without
    that permission being granted.
    
    In addition, opening files passed with a 'file://' URI
    in an 'Intent.ACTION_VIEW' also did not work anymore.
    I'm not sure whether this use case is particularly
    relevant in practice, though; at least all of the (few)
    apps I used during testing passed 'content://' URIs in their
    Intent.
    
    In addition, in Android 11 (API level 30) or higher,
    PERMISSION_WRITE_EXTERNAL_STORAGE no longer has any effect;
    from [2]:
    
    > More recent versions of Android rely more on a file's purpose than its
    > location for determining an app's ability to access, and write to, a
    > given file. In particular, if your app targets Android 11 (API level 30)
    > or higher, the WRITE_EXTERNAL_STORAGE permission doesn't have any
    > effect on your app's access to storage. This purpose-based storage
    > model improves user privacy because apps are given access only to
    > the areas of the device's file system that they actually use.
    >
    > Android 11 introduces the MANAGE_EXTERNAL_STORAGE permission, which
    > provides write access to files outside the app-specific directory and
    > MediaStore. To learn more about this permission, and why most apps don't
    > need to declare it to fulfill their use cases, see the guide on how to
    > manage all files [3] on a storage device.
    
    For now, request the permission again, at least as long
    as PDF export doesn't use the storage framework to
    ask where to save files.
    It certainly makes sense to reconsider this in the future (and decide to either
    drop the permission completely or request MANAGE_EXTERNAL_STORAGE for API
    level >=30).
    
    [1] https://developer.android.com/training/data-storage/shared/documents-files
    [2] https://developer.android.com/training/data-storage#permissions
    [3] https://developer.android.com/training/data-storage/manage-all-files
    
    Change-Id: Icc4c9c9b7b315d2a0b6a025439ae7e431cdd5b37
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113840
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <m.weghorn at posteo.de>

diff --git a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
index 9f89bbedc525..e7bb998bb826 100644
--- a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
+++ b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
@@ -9,11 +9,13 @@
 
 package org.libreoffice.ui;
 
+import android.Manifest;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
 import android.content.pm.ShortcutInfo;
 import android.content.pm.ShortcutManager;
 import android.graphics.drawable.Icon;
@@ -21,7 +23,10 @@ import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.support.annotation.NonNull;
 import android.support.design.widget.FloatingActionButton;
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.content.ContextCompat;
 import android.support.v4.view.ViewCompat;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
@@ -120,6 +125,8 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
     private static final int REQUEST_CODE_OPEN_FILECHOOSER = 12345;
     private static final int REQUEST_CODE_CREATE_NEW_DOCUMENT = 12346;
 
+    private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 0;
+
     private Animation fabOpenAnimation;
     private Animation fabCloseAnimation;
     private boolean isFabMenuOpen = false;
@@ -146,6 +153,17 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
         fabCloseAnimation = AnimationUtils.loadAnimation(this, R.anim.fab_close);
     }
 
+    @Override
+    protected void onStart() {
+        super.onStart();
+        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
+            Log.i(LOGTAG, "no permission to read external storage - asking for permission");
+            ActivityCompat.requestPermissions(this,
+                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+                PERMISSION_WRITE_EXTERNAL_STORAGE);
+        }
+    }
+
     @Override
     protected void attachBaseContext(Context newBase) {
         super.attachBaseContext(LocaleHelper.onAttach(newBase));


More information about the Libreoffice-commits mailing list