Browse Source

FileMenuFilter: properly filter lock/unlock depending on lock status

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
Álvaro Brey Vilas 3 years ago
parent
commit
a902fb6f84

+ 38 - 0
app/src/main/java/com/nextcloud/android/files/FileLockingHelper.kt

@@ -0,0 +1,38 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Álvaro Brey Vilas
+ * Copyright (C) 2022 Álvaro Brey Vilas
+ * Copyright (C) 2022 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.android.files
+
+import com.owncloud.android.datamodel.OCFile
+import com.owncloud.android.lib.resources.files.model.FileLockType
+
+object FileLockingHelper {
+    /**
+     * Checks whether the given `userId` can unlock the [OCFile].
+     */
+    @JvmStatic
+    fun canUserUnlockFile(userId: String, file: OCFile): Boolean {
+        if (!file.isLocked || file.lockOwnerId == null || file.lockType != FileLockType.MANUAL) {
+            return false
+        }
+        return file.lockOwnerId == userId
+    }
+}

+ 33 - 22
app/src/main/java/com/owncloud/android/files/FileMenuFilter.java

@@ -21,12 +21,14 @@
 
 
 package com.owncloud.android.files;
 package com.owncloud.android.files;
 
 
+import android.accounts.AccountManager;
 import android.content.ContentResolver;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Context;
 import android.view.Menu;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuItem;
 
 
 import com.google.gson.Gson;
 import com.google.gson.Gson;
+import com.nextcloud.android.files.FileLockingHelper;
 import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.User;
 import com.owncloud.android.R;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
@@ -58,12 +60,13 @@ public class FileMenuFilter {
     private static final int SINGLE_SELECT_ITEMS = 1;
     private static final int SINGLE_SELECT_ITEMS = 1;
     public static final String SEND_OFF = "off";
     public static final String SEND_OFF = "off";
 
 
-    private int numberOfAllFiles;
-    private Collection<OCFile> files;
-    private ComponentsGetter componentsGetter;
-    private Context context;
-    private boolean overflowMenu;
-    private User user;
+    private final int numberOfAllFiles;
+    private final Collection<OCFile> files;
+    private final ComponentsGetter componentsGetter;
+    private final Context context;
+    private final boolean overflowMenu;
+    private final User user;
+    private final String userId;
 
 
     /**
     /**
      * Constructor
      * Constructor
@@ -88,6 +91,10 @@ public class FileMenuFilter {
         this.context = context;
         this.context = context;
         this.overflowMenu = overflowMenu;
         this.overflowMenu = overflowMenu;
         this.user = user;
         this.user = user;
+        userId = AccountManager
+            .get(context)
+            .getUserData(this.user.toPlatformAccount(),
+                         com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
     }
     }
 
 
     /**
     /**
@@ -202,6 +209,7 @@ public class FileMenuFilter {
         filterSetPictureAs(toShow, toHide);
         filterSetPictureAs(toShow, toHide);
         filterStream(toShow, toHide);
         filterStream(toShow, toHide);
         filterLock(toShow, toHide);
         filterLock(toShow, toHide);
+        filterUnlock(toShow, toHide);
     }
     }
 
 
     private void filterShareFile(List<Integer> toShow, List<Integer> toHide, OCCapability capability) {
     private void filterShareFile(List<Integer> toShow, List<Integer> toHide, OCCapability capability) {
@@ -254,16 +262,28 @@ public class FileMenuFilter {
     }
     }
 
 
     private void filterLock(List<Integer> toShow, List<Integer> toHide) {
     private void filterLock(List<Integer> toShow, List<Integer> toHide) {
-        // TODO only allow locking files (not dirs), and only allow unlocking file if lock is owned by the user
-        if (files.isEmpty()) {
+        if (files.isEmpty() || !isSingleSelection()) {
             toHide.add(R.id.action_lock_file);
             toHide.add(R.id.action_lock_file);
-            toHide.add(R.id.action_unlock_file);
-        } else if (allLocked()) {
-            toHide.add(R.id.action_lock_file);
-            toShow.add(R.id.action_unlock_file);
         } else {
         } else {
+            OCFile file = files.iterator().next();
+            if (file.isLocked() || file.isFolder()) {
+                toHide.add(R.id.action_lock_file);
+            } else {
+                toShow.add(R.id.action_lock_file);
+            }
+        }
+    }
+
+    private void filterUnlock(List<Integer> toShow, List<Integer> toHide) {
+        if (files.isEmpty() || !isSingleSelection()) {
             toHide.add(R.id.action_unlock_file);
             toHide.add(R.id.action_unlock_file);
-            toShow.add(R.id.action_lock_file);
+        } else {
+            OCFile file = files.iterator().next();
+            if (FileLockingHelper.canUserUnlockFile(userId, file)) {
+                toShow.add(R.id.action_unlock_file);
+            } else {
+                toHide.add(R.id.action_unlock_file);
+            }
         }
         }
     }
     }
 
 
@@ -587,13 +607,4 @@ public class FileMenuFilter {
         }
         }
         return true;
         return true;
     }
     }
-
-    private boolean allLocked() {
-        for (OCFile file : files) {
-            if (!file.isLocked()) {
-                return false;
-            }
-        }
-        return true;
-    }
 }
 }