From 2611b5c25551c38d047d8be058177bc7fbfc672d Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Thu, 1 Apr 2021 09:45:13 +0200 Subject: android: Show files opened using system picker for recently used Switch the list of recently used documents in LibreOffice Android Viewer to show the documents opened using the system file picker instead of those shown using the custom UI elements for file selection. This way, files provided by DocumentsProviders, like Nextcloud, can also be handled. As described at [1], this requires persisting permissions in order to be able to access the files after a device reboot. The corresponding method to do this, 'ContentResolver#takePersistableUriPermission', is only available from SDK level 19 on, so drop entries for older SDK levels (current minSdkVersion is 16). [1] https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions Change-Id: Ifbf7148cda687a8a2e3f0c14fe66651509b2f19a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113459 Tested-by: Jenkins Reviewed-by: Michael Weghorn --- .../org/libreoffice/ui/LibreOfficeUIActivity.java | 54 +++++++++++++--------- .../src/java/org/libreoffice/ui/RecentFile.java | 25 ++++++++++ .../org/libreoffice/ui/RecentFilesAdapter.java | 12 ++--- 3 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 android/source/src/java/org/libreoffice/ui/RecentFile.java (limited to 'android/source') diff --git a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java index f9afa9578d56..e968f97b3a32 100644 --- a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java +++ b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java @@ -21,6 +21,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; +import android.database.Cursor; import android.graphics.drawable.Icon; import android.hardware.usb.UsbManager; import android.net.Uri; @@ -28,6 +29,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.provider.OpenableColumns; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; @@ -263,15 +265,12 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings Set recentFileStrings = prefs.getStringSet(RECENT_DOCUMENTS_KEY, new HashSet()); - final ArrayList recentFiles = new ArrayList(); + final List recentFiles = new ArrayList(); for (String recentFileString : recentFileStrings) { - try { - if(documentProvider != null) - recentFiles.add(documentProvider.createFromUri(this, new URI(recentFileString))); - } catch (URISyntaxException e) { - e.printStackTrace(); - } catch (RuntimeException e){ - e.printStackTrace(); + Uri uri = Uri.parse(recentFileString); + String filename = FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), uri); + if (!filename.isEmpty()) { + recentFiles.add(new RecentFile(uri, filename)); } } @@ -615,7 +614,6 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings } public void open(final IFile document) { - addDocumentToRecents(document); new AsyncTask() { @Override protected File doInBackground(IFile... document) { @@ -659,10 +657,13 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings }.execute(document); } - private void openDocument(final Uri documentUri) { + public void openDocument(final Uri documentUri) { // "forward" to LibreOfficeMainActivity to open the file Intent intent = new Intent(Intent.ACTION_VIEW, documentUri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + + addDocumentToRecents(documentUri); + String packageName = getApplicationContext().getPackageName(); ComponentName componentName = new ComponentName(packageName, LibreOfficeMainActivity.class.getName()); @@ -1082,8 +1083,20 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings return (int) (dp * scale + 0.5f); } - private void addDocumentToRecents(IFile iFile) { - String newRecent = iFile.getUri().toString(); + private void addDocumentToRecents(Uri fileUri) { + if (Build.VERSION.SDK_INT < 19) { + // ContentResolver#takePersistableUriPermission only available from SDK level 19 on + Log.i(LOGTAG, "Recently used files not supported, requires SDK version >= 19."); + // drop potential entries + prefs.edit().putStringSet(RECENT_DOCUMENTS_KEY, new HashSet()).apply(); + return; + } + + // preserve permissions across device reboots, + // s. https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions + getContentResolver().takePersistableUriPermission(fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + + String newRecent = fileUri.toString(); Set recentsSet = prefs.getStringSet(RECENT_DOCUMENTS_KEY, new HashSet()); //create array to work with @@ -1111,7 +1124,6 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings prefs.edit().putStringSet(RECENT_DOCUMENTS_KEY, recentsSet).apply(); - //update app shortcuts (7.0 and above) if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); @@ -1120,11 +1132,16 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings shortcutManager.removeAllDynamicShortcuts(); ArrayList shortcuts = new ArrayList(); - for (String pathString : recentsArrayList) { + for (String recentDoc : recentsArrayList) { + Uri docUri = Uri.parse(recentDoc); + String filename = FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), docUri); + if (filename.isEmpty()) { + continue; + } //find the appropriate drawable int drawable = 0; - switch (FileUtilities.getType(pathString)) { + switch (FileUtilities.getType(filename)) { case FileUtilities.DOC: drawable = R.drawable.writer; break; @@ -1139,12 +1156,7 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings break; } - File file = new File(pathString); - - //for some reason, getName uses %20 instead of space - String filename = file.getName().replace("%20", " "); - - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); + Intent intent = new Intent(Intent.ACTION_VIEW, docUri); String packageName = this.getApplicationContext().getPackageName(); ComponentName componentName = new ComponentName(packageName, LibreOfficeMainActivity.class.getName()); intent.setComponent(componentName); diff --git a/android/source/src/java/org/libreoffice/ui/RecentFile.java b/android/source/src/java/org/libreoffice/ui/RecentFile.java new file mode 100644 index 000000000000..77cf3c1b116d --- /dev/null +++ b/android/source/src/java/org/libreoffice/ui/RecentFile.java @@ -0,0 +1,25 @@ +package org.libreoffice.ui; + +import android.net.Uri; + +/** + * An entry for a recently used file in the RecentFilesAdapter. + */ +public class RecentFile { + + private Uri uri; + private String displayName; + + public RecentFile(Uri docUri, String name) { + uri = docUri; + displayName = name; + } + + public Uri getUri() { + return uri; + } + + public String getDisplayName() { + return displayName; + } +} diff --git a/android/source/src/java/org/libreoffice/ui/RecentFilesAdapter.java b/android/source/src/java/org/libreoffice/ui/RecentFilesAdapter.java index fc16d06a48d7..c25fa8270f69 100644 --- a/android/source/src/java/org/libreoffice/ui/RecentFilesAdapter.java +++ b/android/source/src/java/org/libreoffice/ui/RecentFilesAdapter.java @@ -18,16 +18,15 @@ import android.widget.ImageView; import android.widget.TextView; import org.libreoffice.R; -import org.libreoffice.storage.IFile; import java.util.List; class RecentFilesAdapter extends RecyclerView.Adapter { private LibreOfficeUIActivity mActivity; - private List recentFiles; + private List recentFiles; - RecentFilesAdapter(LibreOfficeUIActivity activity, List recentFiles) { + RecentFilesAdapter(LibreOfficeUIActivity activity, List recentFiles) { this.mActivity = activity; this.recentFiles = recentFiles; } @@ -41,17 +40,16 @@ class RecentFilesAdapter extends RecyclerView.Adapter