浏览代码

fix: GridView not restore selected positions but getCheckedItemCount ill return right value

David Wiesner 9 年之前
父节点
当前提交
4f7d3d4fd1

+ 3 - 0
res/values/strings.xml

@@ -454,5 +454,8 @@
 
     <string name="permission_storage_access">Additional permissions required to upload &amp; download files.</string>
     <string name="local_file_not_found_toast">The file was not found in the local file system</string>
+    <string name="confirmation_remove_files_alert">Do you really want to remove the selected items?</string>
+    <string name="confirmation_remove_folders_alert">Do you really want to remove the selected items and their contents?</string>
+    <string name="confirmation_remove_files">selected items</string>
 
 </resources>

+ 1 - 0
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -39,6 +39,7 @@ import android.content.res.Resources.NotFoundException;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.os.Parcelable;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;

+ 74 - 54
src/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -1,25 +1,24 @@
 /**
- *   ownCloud Android client application
- *
- *   @author Bartek Przybylski
- *   @author Tobias Kaminsky
- *   @author David A. Velasco
- *   @author masensio
- *   Copyright (C) 2011  Bartek Przybylski
- *   Copyright (C) 2016 ownCloud Inc.
- *
- *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2,
- *   as published by the Free Software Foundation.
- *
- *   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 <http://www.gnu.org/licenses/>.
+ * ownCloud Android client application
  *
+ * @author Bartek Przybylski
+ * @author Tobias Kaminsky
+ * @author David A. Velasco
+ * @author masensio
+ * Copyright (C) 2011  Bartek Przybylski
+ * Copyright (C) 2016 ownCloud Inc.
+ * <p>
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ * <p>
+ * 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.
+ * <p>
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 package com.owncloud.android.ui.adapter;
 
@@ -43,6 +42,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AbsListView;
 import android.widget.BaseAdapter;
+import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
@@ -86,8 +86,11 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
     private Account mAccount;
     private ComponentsGetter mTransferServiceGetter;
     private boolean mGridMode;
+    private boolean isGridViewSelectionRestored = true;
 
-    private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
+    private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM}
+
+    ;
 
     private HashSet<Long> mSelection = new LinkedHashSet<Long>();
 
@@ -95,7 +98,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
             boolean justFolders,
             Context context,
             ComponentsGetter transferServiceGetter
-            ) {
+    ) {
 
         mJustFolders = justFolders;
         mContext = context;
@@ -149,6 +152,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
+        restoreGridViewSelection((AbsListView) parent);
 
         View view = convertView;
         OCFile file = null;
@@ -161,9 +165,9 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
         // Find out which layout should be displayed
         ViewType viewType;
-        if (!mGridMode){
+        if (!mGridMode) {
             viewType = ViewType.LIST_ITEM;
-        } else if (file.isImage()){
+        } else if (file.isImage()) {
             viewType = ViewType.GRID_IMAGE;
         } else {
             viewType = ViewType.GRID_ITEM;
@@ -189,7 +193,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
         view.invalidate();
 
-        if (file != null){
+        if (file != null) {
 
             ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
 
@@ -200,7 +204,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
             LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ListItemLayout);
             linearLayout.setContentDescription("LinearLayout-" + name);
 
-            switch (viewType){
+            switch (viewType) {
                 case LIST_ITEM:
                     TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
                     TextView fileSizeSeparatorV = (TextView) view.findViewById(R.id.file_separator);
@@ -211,7 +215,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                     lastModV.setText(DisplayUtils.getRelativeTimestamp(mContext, file.getModificationTimestamp()));
 
 
-
                     fileSizeSeparatorV.setVisibility(View.VISIBLE);
                     fileSizeV.setVisibility(View.VISIBLE);
                     fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
@@ -251,7 +254,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                         sharedIconV.setImageResource(R.drawable.shared_via_link);
                         sharedIconV.setVisibility(View.VISIBLE);
                         sharedIconV.bringToFront();
-                    } else if (file.isSharedWithSharee() || file.isSharedWithMe() ) {
+                    } else if (file.isSharedWithSharee() || file.isSharedWithMe()) {
                         sharedIconV.setImageResource(R.drawable.shared_via_users);
                         sharedIconV.setVisibility(View.VISIBLE);
                         sharedIconV.bringToFront();
@@ -275,22 +278,22 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                     localStateView.setVisibility(View.INVISIBLE);   // default first
 
                     if ( //synchronizing
-                                opsBinder != null &&
-                                opsBinder.isSynchronizing(mAccount, file)
+                            opsBinder != null &&
+                                    opsBinder.isSynchronizing(mAccount, file)
                             ) {
                         localStateView.setImageResource(R.drawable.ic_synchronizing);
                         localStateView.setVisibility(View.VISIBLE);
 
                     } else if ( // downloading
-                                downloaderBinder != null &&
-                                downloaderBinder.isDownloading(mAccount, file)
+                            downloaderBinder != null &&
+                                    downloaderBinder.isDownloading(mAccount, file)
                             ) {
                         localStateView.setImageResource(R.drawable.ic_synchronizing);
                         localStateView.setVisibility(View.VISIBLE);
 
                     } else if ( //uploading
-                                uploaderBinder != null &&
-                                uploaderBinder.isUploading(mAccount, file)
+                            uploaderBinder != null &&
+                                    uploaderBinder.isUploading(mAccount, file)
                             ) {
                         localStateView.setImageResource(R.drawable.ic_synchronizing);
                         localStateView.setVisibility(View.VISIBLE);
@@ -311,9 +314,9 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
             checkBoxV.setVisibility(View.GONE);
             view.setBackgroundColor(Color.WHITE);
 
-            AbsListView parentList = (AbsListView)parent;
+            AbsListView parentList = (AbsListView) parent;
             if (parentList.getChoiceMode() != AbsListView.CHOICE_MODE_NONE
-                    && parentList.getCheckedItemCount() > 0){
+                    && parentList.getCheckedItemCount() > 0) {
                 if (isItemSelected(position)) {
                     view.setBackgroundColor(mContext.getResources().getColor(
                             R.color.selected_item_background));
@@ -326,7 +329,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                 }
                 checkBoxV.setVisibility(View.VISIBLE);
             }
-            
+
             // For all Views
 
             // this if-else is needed even though favorite icon is visible by default
@@ -339,12 +342,12 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
             // No Folder
             if (!file.isFolder()) {
-                if (file.isImage() && file.getRemoteId() != null){
+                if (file.isImage() && file.getRemoteId() != null) {
                     // Thumbnail in Cache?
                     Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
                             String.valueOf(file.getRemoteId())
-                            );
-                    if (thumbnail != null && !file.needsUpdateThumbnail()){
+                    );
+                    if (thumbnail != null && !file.needsUpdateThumbnail()) {
                         fileIcon.setImageBitmap(thumbnail);
                     } else {
                         // generate new Thumbnail
@@ -352,7 +355,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
                             final ThumbnailsCacheManager.ThumbnailGenerationTask task =
                                     new ThumbnailsCacheManager.ThumbnailGenerationTask(
                                             fileIcon, mStorageManager, mAccount
-                                            );
+                                    );
                             if (thumbnail == null) {
                                 thumbnail = ThumbnailsCacheManager.mDefaultImg;
                             }
@@ -392,6 +395,19 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         return view;
     }
 
+    private void restoreGridViewSelection(AbsListView parent) {
+        if (parent instanceof GridView && !isGridViewSelectionRestored) {
+            isGridViewSelectionRestored = true;
+            parent.clearChoices();
+            final Vector<OCFile> files = mFiles;
+            for (int i = 0; i < files.size(); ++i) {
+                if(mSelection.contains(files.get(i).getFileId())){
+                    parent.setItemChecked(i, true);
+                }
+            }
+        }
+    }
+
     @Override
     public int getViewTypeCount() {
         return 1;
@@ -409,10 +425,11 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
     /**
      * Change the adapted directory for a new one
-     * @param directory                 New file to adapt. Can be NULL, meaning
-     *                                  "no content to adapt".
-     * @param updatedStorageManager     Optional updated storage manager; used to replace
-     *                                  mStorageManager if is different (and not NULL)
+     *
+     * @param directory             New file to adapt. Can be NULL, meaning
+     *                              "no content to adapt".
+     * @param updatedStorageManager Optional updated storage manager; used to replace
+     *                              mStorageManager if is different (and not NULL)
      */
     public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager
             /*, boolean onlyOnDevice*/) {
@@ -440,6 +457,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
 
     /**
      * Filter for getting only the folders
+     *
      * @param files
      * @return Vector<OCFile>
      */
@@ -478,12 +496,12 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         mGridMode = gridMode;
     }
 
-    public boolean isItemSelected(int position){
+    public boolean isItemSelected(int position) {
         return mSelection.contains(getItemId(position));
     }
 
     public void setNewSelection(int position, boolean checked) {
-        if(checked){
+        if (checked) {
             mSelection.add(getItemId(position));
             notifyDataSetChanged();
         } else {
@@ -496,33 +514,35 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         notifyDataSetChanged();
     }
 
-    public void clearSelection(){
-         mSelection.clear();
+    public void clearSelection() {
+        mSelection.clear();
         notifyDataSetChanged();
     }
 
     public ArrayList<OCFile> getCheckedItems() {
         ArrayList<OCFile> files = new ArrayList<OCFile>();
-        if (mFiles != null && mFiles.size() != 0){
-            for(OCFile file: mFiles){
-                if(mSelection.contains(file.getFileId())){
+        if (mFiles != null && mFiles.size() != 0) {
+            for (OCFile file : mFiles) {
+                if (mSelection.contains(file.getFileId())) {
                     files.add(file);
                 }
             }
         }
         return files;
     }
-    public void restoreSelectionState(Bundle savedInstanceState){
+
+    public void restoreSelectionState(Bundle savedInstanceState) {
         if (savedInstanceState == null) {
             return;
         }
         long[] selectionState = savedInstanceState.getLongArray(SELECTION_KEY);
         mSelection.clear();
-        if(selectionState != null) {
+        if (selectionState != null) {
             for (long id : selectionState) {
                 mSelection.add(id);
             }
         }
+        isGridViewSelectionRestored = false;
     }
 
     public void saveSelectionState(Bundle outState) {

+ 6 - 18
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -372,8 +372,8 @@ public class OCFileListFragment extends ExtendedListFragment {
 
             @Override
             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
-                final int checkedCount = getListView().getCheckedItemCount();
 
+                final int checkedCount = getListView().getCheckedItemCount();
                 mode.setTitle(checkedCount + " selected");
 
                 if (checkedCount > 0) {
@@ -392,6 +392,11 @@ public class OCFileListFragment extends ExtendedListFragment {
                 return true;
             }
 
+            @Override
+            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+                return onFileActionChosen(item.getItemId());
+            }
+
             @Override
             public void onDestroyActionMode(ActionMode mode) {
                 mActiveActionMode = null;
@@ -659,23 +664,6 @@ public class OCFileListFragment extends ExtendedListFragment {
         }
     }
 
-    /**
-     * Query all selected Files
-     * @return selected Files
-     */
-    public ArrayList<OCFile> getCheckedItems() {
-        ArrayList<OCFile> files = new ArrayList<OCFile>();
-        SparseBooleanArray mSelection = getListView().getCheckedItemPositions();
-        for (int i = 0; i < mSelection.size(); ++i) {
-            if (mSelection.valueAt(i)) {
-                final int position = mSelection.keyAt(i);
-                files.add((OCFile) mAdapter.getItem(position));
-            }
-        }
-        return files;
-    }
-
-
     /**
      * Use this to query the {@link OCFile} that is currently
      * being displayed by this fragment