diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-01 09:45:13 +0200 |
---|---|---|
committer | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-01 17:19:32 +0200 |
commit | 2611b5c25551c38d047d8be058177bc7fbfc672d (patch) | |
tree | cd858ea35a15414f217abe80ea145e216877f2bf /android/source | |
parent | 4003042af09d8335003c6b9faf3696221a44916c (diff) |
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 <m.weghorn@posteo.de>
Diffstat (limited to 'android/source')
3 files changed, 63 insertions, 28 deletions
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<String> recentFileStrings = prefs.getStringSet(RECENT_DOCUMENTS_KEY, new HashSet<String>()); - final ArrayList<IFile> recentFiles = new ArrayList<IFile>(); + final List<RecentFile> 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<IFile, Void, File>() { @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<String>()).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<String> recentsSet = prefs.getStringSet(RECENT_DOCUMENTS_KEY, new HashSet<String>()); //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<ShortcutInfo> shortcuts = new ArrayList<ShortcutInfo>(); - 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<RecentFilesAdapter.ViewHolder> { private LibreOfficeUIActivity mActivity; - private List<IFile> recentFiles; + private List<RecentFile> recentFiles; - RecentFilesAdapter(LibreOfficeUIActivity activity, List<IFile> recentFiles) { + RecentFilesAdapter(LibreOfficeUIActivity activity, List<RecentFile> recentFiles) { this.mActivity = activity; this.recentFiles = recentFiles; } @@ -41,17 +40,16 @@ class RecentFilesAdapter extends RecyclerView.Adapter<RecentFilesAdapter.ViewHol @Override public void onBindViewHolder(ViewHolder holder, int position) { - final IFile iFile = recentFiles.get(position); + final RecentFile entry = recentFiles.get(position); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - mActivity.open(iFile); + mActivity.openDocument(entry.getUri()); } }); - String filename = iFile.getName(); - + final String filename = entry.getDisplayName(); holder.textView.setText(filename); int compoundDrawableInt = 0; |