diff --git a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java index 2cd69cac61..2a67d85f18 100644 --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -35,6 +35,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Build; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -61,6 +62,8 @@ import com.owncloud.android.utils.FileStorageUtils; import com.owncloud.android.utils.MimetypeIconUtil; import java.util.Vector; +import java.util.HashSet; +import java.util.LinkedHashSet; /** @@ -69,6 +72,8 @@ import java.util.Vector; */ public class FileListListAdapter extends BaseAdapter implements ListAdapter { + private static final String SELECTION_KEY = "multiFileSelectionsKey"; + private Context mContext; private OCFile mFile = null; private Vector mFiles = null; @@ -82,8 +87,8 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM }; - private HashMap mSelection = new HashMap(); - + private HashSet mSelection = new LinkedHashSet(); + public FileListListAdapter( boolean justFolders, Context context, @@ -382,7 +387,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { } } - if (mSelection.get(position) != null) { + if (mSelection.contains(getItemId(position))) { view.setBackgroundColor(mContext.getResources().getColor(R.color.selected_item_background)); } else { view.setBackgroundColor(Color.WHITE); @@ -471,6 +476,59 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { mGridMode = gridMode; } + // TODO Tobi: all methods needed? + public void setNewSelection(int position, boolean checked) { + if(checked){ + mSelection.add(getItemId(position)); + notifyDataSetChanged(); + } else { + removeSelection(position); + } + } + + public void removeSelection(int position) { + mSelection.remove(getItemId(position)); + notifyDataSetChanged(); + } + + public void removeSelection(){ + mSelection.clear(); + notifyDataSetChanged(); + } + + public ArrayList getCheckedItems() { + ArrayList files = new ArrayList(); + 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){ + if (savedInstanceState == null) { + return; + } + long[] selectionState = savedInstanceState.getLongArray(SELECTION_KEY); + mSelection.clear(); + if(selectionState != null) { + for (long id : selectionState) { + mSelection.add(id); + } + } + } + + public void saveSelectionState(Bundle outState) { + long[] selectionStatePrimitive = new long[mSelection.size()]; + int i = 0; + for (Long id : mSelection) { + selectionStatePrimitive[i++] = id; + } + outState.putLongArray(SELECTION_KEY, selectionStatePrimitive); + } + public boolean isGridMode() { return mGridMode; } diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 36a5feac5f..e30e0aff99 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -31,7 +31,6 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.widget.SwipeRefreshLayout; import android.view.ActionMode; -import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -109,6 +108,7 @@ public class OCFileListFragment extends ExtendedListFragment { private boolean hideFab = true; private boolean miniFabClicked = false; + private ActionMode mActiveActionMode; @Override public void onCreate(Bundle savedInstanceState) { @@ -186,6 +186,7 @@ public class OCFileListFragment extends ExtendedListFragment { getActivity(), mContainerActivity ); + mAdapter.restoreSelectionState(savedInstanceState); setListAdapter(mAdapter); registerLongClickListener(); @@ -210,7 +211,7 @@ public class OCFileListFragment extends ExtendedListFragment { removeFabLabels(); } } - } + } /** * adds labels to all mini FABs. @@ -348,27 +349,18 @@ public class OCFileListFragment extends ExtendedListFragment { setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { - private Menu menu; - @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - if (checked) { - mAdapter.setNewSelection(position, checked); - } else { - mAdapter.removeSelection(position); - } - - updateActionsMenu(mode); - + mAdapter.setNewSelection(position, checked); + mode.invalidate(); } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { + mActiveActionMode = mode; createContextActionBar(menu); - this.menu = menu; - - updateActionsMenu(mode); + mode.invalidate(); //set gray color if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -381,7 +373,8 @@ public class OCFileListFragment extends ExtendedListFragment { return true; } - private void updateActionsMenu(ActionMode mode) { + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { final int checkedCount = getListView().getCheckedItemCount(); mode.setTitle(checkedCount + " selected"); @@ -394,16 +387,11 @@ public class OCFileListFragment extends ExtendedListFragment { targetFiles, mContainerActivity.getStorageManager().getAccount(), mContainerActivity, - getActivity() - ); + getActivity()); mf.filter(menu); } } - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return false; + return true; } @Override @@ -414,6 +402,7 @@ public class OCFileListFragment extends ExtendedListFragment { @Override public void onDestroyActionMode(ActionMode mode) { mAdapter.removeSelection(); + mActiveActionMode = null; // reset to primary dark color if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -425,17 +414,18 @@ public class OCFileListFragment extends ExtendedListFragment { setFabEnabled(true); } } + }); } // TODO Tobi needed? private void showFileAction(int fileIndex) { Bundle args = getArguments(); - PopupMenu pm = new PopupMenu(getActivity(),null); + PopupMenu pm = new PopupMenu(getActivity(), null); Menu menu = pm.getMenu(); boolean allowContextualActions = - (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true); + (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true); if (allowContextualActions) { MenuInflater inflater = getActivity().getMenuInflater(); @@ -445,19 +435,19 @@ public class OCFileListFragment extends ExtendedListFragment { if (mContainerActivity.getStorageManager() != null) { FileMenuFilter mf = new FileMenuFilter( - targetFile, - mContainerActivity.getStorageManager().getAccount(), - mContainerActivity, - getActivity() + targetFile, + mContainerActivity.getStorageManager().getAccount(), + mContainerActivity, + getActivity() ); mf.filter(menu); } /// TODO break this direct dependency on FileDisplayActivity... if possible MenuItem item = menu.findItem(R.id.action_open_file_with); - FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment(); + FileFragment frag = ((FileDisplayActivity) getActivity()).getSecondFragment(); if (frag != null && frag instanceof FileDetailFragment && - frag.getFile().getFileId() == targetFile.getFileId()) { + frag.getFile().getFileId() == targetFile.getFileId()) { item = menu.findItem(R.id.action_see_details); if (item != null) { item.setVisible(false); @@ -466,7 +456,7 @@ public class OCFileListFragment extends ExtendedListFragment { } FileActionsDialogFragment dialog = FileActionsDialogFragment.newInstance(menu, - fileIndex, targetFile.getFileName()); + fileIndex, targetFile.getFileName()); dialog.setTargetFragment(this, 0); dialog.show(getFragmentManager(), FileActionsDialogFragment.FTAG_FILE_ACTIONS); } @@ -479,6 +469,7 @@ public class OCFileListFragment extends ExtendedListFragment { public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(KEY_FILE, mFile); + mAdapter.saveSelectionState(outState); } @Override @@ -816,6 +807,13 @@ public class OCFileListFragment extends ExtendedListFragment { switchToListView(); } } + invalidateActionMode(); + } + + private void invalidateActionMode() { + if(mActiveActionMode != null){ + mActiveActionMode.invalidate(); + } } private String generateFooterText(int filesCount, int foldersCount) {