android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java
 |   67 ++++++----
 
android/app/src/main/java/org/libreoffice/androidapp/ui/RecentFilesAdapter.java 
   |   59 +++++---
 2 files changed, 80 insertions(+), 46 deletions(-)

New commits:
commit 59296ecbaab04a1dfd6ba5a80ff108fa6e65dc4c
Author:     Jan Holesovsky <ke...@collabora.com>
AuthorDate: Fri Feb 21 18:38:38 2020 +0100
Commit:     Jan Holesovsky <ke...@collabora.com>
CommitDate: Sat Feb 22 08:29:21 2020 +0100

    android shell: Correct names for the Dynamic shortcuts + make them 
read/write.
    
    Dynamic shortcuts == the long press on the app icon to edit recent
    documents.
    
    Also fixes crashing with too many recent documents.
    
    Change-Id: I844e60de6523039889539cfe1e3c1bb70dc062bc
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/89235
    Tested-by: Jan Holesovsky <ke...@collabora.com>
    Reviewed-by: Jan Holesovsky <ke...@collabora.com>

diff --git 
a/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java
 
b/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java
index a4a69b2fc..4651a85bb 100644
--- 
a/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java
+++ 
b/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java
@@ -87,6 +87,8 @@ import androidx.recyclerview.widget.GridLayoutManager;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
+import static 
androidx.core.content.pm.ShortcutManagerCompat.getMaxShortcutCountPerActivity;
+
 public class LibreOfficeUIActivity extends AppCompatActivity implements 
SettingsListenerModel.OnSettingsPreferenceChangedListener {
     private String LOGTAG = LibreOfficeUIActivity.class.getSimpleName();
     private SharedPreferences prefs;
@@ -524,13 +526,8 @@ public class LibreOfficeUIActivity extends 
AppCompatActivity implements Settings
             prefs.edit().putString(EXPLORER_VIEW_TYPE_KEY, LIST_VIEW).apply();
     }
 
-    /** Start editing of the given Uri. */
-    public void open(final Uri uri) {
-        if (uri == null)
-            return;
-
-        addDocumentToRecents(uri);
-
+    /** Build Intent to edit a Uri. */
+    public Intent getIntentToEdit(Uri uri) {
         Intent i = new Intent(Intent.ACTION_EDIT, uri);
         i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
         i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
@@ -539,6 +536,17 @@ public class LibreOfficeUIActivity extends 
AppCompatActivity implements Settings
         ComponentName componentName = new ComponentName(packageName, 
LOActivity.class.getName());
         i.setComponent(componentName);
 
+        return i;
+    }
+
+    /** Start editing of the given Uri. */
+    public void open(final Uri uri) {
+        if (uri == null)
+            return;
+
+        addDocumentToRecents(uri);
+
+        Intent i = getIntentToEdit(uri);
         startActivityForResult(i, LO_ACTIVITY_REQUEST_CODE);
     }
 
@@ -949,19 +957,27 @@ public class LibreOfficeUIActivity extends 
AppCompatActivity implements Settings
         String joined = TextUtils.join("\n", recentsArrayList);
         prefs.edit().putString(RECENT_DOCUMENTS_KEY, joined).apply();
 
-        //update app shortcuts (7.0 and above)
+        // 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);
 
-            //Remove all shortcuts, and apply new ones.
+            // Remove all shortcuts, and apply new ones.
             shortcutManager.removeAllDynamicShortcuts();
 
             ArrayList<ShortcutInfo> shortcuts = new ArrayList<ShortcutInfo>();
+            int i = 0;
             for (String pathString : recentsArrayList) {
                 if (pathString.isEmpty())
                     continue;
 
-                //find the appropriate drawable
+                // I cannot see more than 3 anyway, and with too many we get
+                // an exception, so let's limit to 3
+                if (i >= 3 || i >= getMaxShortcutCountPerActivity(this))
+                    break;
+
+                ++i;
+
+                // Find the appropriate drawable
                 int drawable = 0;
                 switch (FileUtilities.getType(pathString)) {
                     case FileUtilities.DOC:
@@ -978,27 +994,26 @@ public class LibreOfficeUIActivity extends 
AppCompatActivity implements Settings
                         break;
                 }
 
-                // TODO better way to get the filename for content: uris
-                File file = new File(pathString);
+                Uri shortcutUri = Uri.parse(pathString);
+                String filename = RecentFilesAdapter.getUriFilename(this, 
shortcutUri);
 
-                //for some reason, getName uses %20 instead of space
-                String filename = file.getName().replace("%20", " ");
-
-                Intent intent = new Intent(Intent.ACTION_VIEW, 
Uri.parse(pathString));
-                String packageName = 
this.getApplicationContext().getPackageName();
-                ComponentName componentName = new ComponentName(packageName, 
LOActivity.class.getName());
-                intent.setComponent(componentName);
-
-                ShortcutInfo shortcut = new ShortcutInfo.Builder(this, 
filename)
+                Intent intent = getIntentToEdit(shortcutUri);
+                ShortcutInfo.Builder builder = new ShortcutInfo.Builder(this, 
filename)
                         .setShortLabel(filename)
                         .setLongLabel(filename)
-                        .setIcon(Icon.createWithResource(this, drawable))
-                        .setIntent(intent)
-                        .build();
+                        .setIntent(intent);
+
+                if (drawable != 0)
+                    builder.setIcon(Icon.createWithResource(this, drawable));
 
-                shortcuts.add(shortcut);
+                shortcuts.add(builder.build());
+            }
+
+            try {
+                shortcutManager.setDynamicShortcuts(shortcuts);
+            } catch (Exception e) {
+                Log.e(LOGTAG, "Failed to set the dynamic shortcuts: " + 
e.getMessage());
             }
-            shortcutManager.setDynamicShortcuts(shortcuts);
         }
     }
 
diff --git 
a/android/app/src/main/java/org/libreoffice/androidapp/ui/RecentFilesAdapter.java
 
b/android/app/src/main/java/org/libreoffice/androidapp/ui/RecentFilesAdapter.java
index 7c098af80..df162477b 100644
--- 
a/android/app/src/main/java/org/libreoffice/androidapp/ui/RecentFilesAdapter.java
+++ 
b/android/app/src/main/java/org/libreoffice/androidapp/ui/RecentFilesAdapter.java
@@ -9,6 +9,7 @@
 
 package org.libreoffice.androidapp.ui;
 
+import android.app.Activity;
 import android.database.Cursor;
 import android.net.Uri;
 import android.provider.OpenableColumns;
@@ -46,29 +47,13 @@ class RecentFilesAdapter extends 
RecyclerView.Adapter<RecentFilesAdapter.ViewHol
         return new ViewHolder(item);
     }
 
-    @Override
-    public void onBindViewHolder(ViewHolder holder, int position) {
-        final Uri uri = recentUris.get(position);
-
-        holder.itemView.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                mActivity.open(uri);
-            }
-        });
-
+    /** Return the filename of the given Uri. */
+    public static String getUriFilename(Activity activity, Uri uri) {
         String filename = "";
-        long length = 0;
-        // TODO Date not avaiable now
-        //Date date = null;
-
-        // Try to get it from the content resolver first, fallback to path
-        Cursor cursor = mActivity.getContentResolver().query(uri, null, null, 
null, null);
+        Cursor cursor = activity.getContentResolver().query(uri, null, null, 
null, null);
         try {
-            if (cursor != null && cursor.moveToFirst()) {
+            if (cursor != null && cursor.moveToFirst())
                 filename = 
cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
-                length = 
cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
-            }
         } finally {
             if (cursor != null)
                 cursor.close();
@@ -80,10 +65,44 @@ class RecentFilesAdapter extends 
RecyclerView.Adapter<RecentFilesAdapter.ViewHol
                 filename = segments.get(segments.size() - 1);
         }
 
+        return filename;
+    }
+
+    /** Return the size of the given Uri. */
+    public static long getUriFileLength(Activity activity, Uri uri) {
+        long length = 0;
+        Cursor cursor = activity.getContentResolver().query(uri, null, null, 
null, null);
+        try {
+            if (cursor != null && cursor.moveToFirst())
+                length = 
cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
+        } finally {
+            if (cursor != null)
+                cursor.close();
+        }
+
         if (length == 0) {
             // TODO maybe try to get File & return File.length()?
         }
 
+        return length;
+    }
+
+    @Override
+    public void onBindViewHolder(ViewHolder holder, int position) {
+        final Uri uri = recentUris.get(position);
+
+        holder.itemView.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                mActivity.open(uri);
+            }
+        });
+
+        String filename = getUriFilename(mActivity, uri);
+        long length = getUriFileLength(mActivity, uri);
+        // TODO Date not avaiable now
+        //Date date = null;
+
         holder.filenameView.setText(filename);
 
         int compoundDrawableInt = 0;
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to