Fixed life cycle of ExtendedListFragment (see #onCreateView method) so that selections are tracked in grid mode during rotations without tracking them in our code

This commit is contained in:
David A. Velasco 2016-07-08 14:20:06 +02:00 committed by Andy Scherzinger
parent 0a4ac6e91c
commit fdeff61ce2
5 changed files with 105 additions and 211 deletions

View file

@ -86,26 +86,40 @@ public class FileMenuFilter {
* @param menu Options or context menu to filter.
*/
public void filter(Menu menu) {
List<Integer> toShow = new ArrayList<Integer>();
List<Integer> toHide = new ArrayList<Integer>();
if (mFiles == null || mFiles.size() <= 0) {
hideAll(menu);
filter(toShow, toHide);
} else {
List<Integer> toShow = new ArrayList<Integer>();
List<Integer> toHide = new ArrayList<Integer>();
MenuItem item = null;
for (int i : toShow) {
item = menu.findItem(i);
if (item != null) {
item.setVisible(true);
item.setEnabled(true);
filter(toShow, toHide);
MenuItem item;
for (int i : toShow) {
item = menu.findItem(i);
if (item != null) {
item.setVisible(true);
item.setEnabled(true);
}
}
for (int i : toHide) {
item = menu.findItem(i);
if (item != null) {
item.setVisible(false);
item.setEnabled(false);
}
}
}
}
for (int i : toHide) {
item = menu.findItem(i);
if (item != null) {
item.setVisible(false);
item.setEnabled(false);
}
private void hideAll(Menu menu) {
MenuItem item;
for (int i=0; i<menu.size(); i++) {
item = menu.getItem(i);
item.setVisible(false);
item.setEnabled(false);
}
}

View file

@ -1806,7 +1806,7 @@ public class FileDisplayActivity extends HookActivity
}
private boolean isGridView() {
return getListOfFilesFragment().isGridView();
return getListOfFilesFragment().isGridEnabled();
}
public void allFilesOption() {

View file

@ -23,20 +23,14 @@
package com.owncloud.android.ui.adapter;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Vector;
import android.accounts.Account;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -62,11 +56,6 @@ import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.MimetypeIconUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Vector;
/**
* This Adapter populates a ListView with all files and folders in an ownCloud
@ -74,23 +63,15 @@ 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<OCFile> mFiles = null;
private Vector<OCFile> mFilesOrig = new Vector<OCFile>();
private boolean mJustFolders;
private FileDataStorageManager mStorageManager;
private Account mAccount;
private ComponentsGetter mTransferServiceGetter;
private boolean mGridMode;
private boolean isGridViewSelectionRestored = true;
private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM};
private HashSet<Long> mSelection = new LinkedHashSet<Long>();
private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM}
public FileListListAdapter(
boolean justFolders,
@ -110,8 +91,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
// initialise thumbnails cache on background thread
new ThumbnailsCacheManager.InitDiskCacheTask().execute();
mGridMode = false;
}
@Override
@ -150,7 +129,6 @@ 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;
@ -163,34 +141,34 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
// Find out which layout should be displayed
ViewType viewType;
if (!mGridMode) {
viewType = ViewType.LIST_ITEM;
} else if (file.isImage()) {
viewType = ViewType.GRID_IMAGE;
if (parent instanceof GridView) {
if (file != null && file.isImage()) {
viewType = ViewType.GRID_IMAGE;
} else {
viewType = ViewType.GRID_ITEM;
}
} else {
viewType = ViewType.GRID_ITEM;
viewType = ViewType.LIST_ITEM;
}
// create view only if differs, otherwise reuse
if (convertView == null || (convertView != null && convertView.getTag() != viewType)) {
if (convertView == null || convertView.getTag() != viewType) {
switch (viewType) {
case GRID_IMAGE:
view = inflator.inflate(R.layout.grid_image, null);
view = inflator.inflate(R.layout.grid_image, parent, false);
view.setTag(ViewType.GRID_IMAGE);
break;
case GRID_ITEM:
view = inflator.inflate(R.layout.grid_item, null);
view = inflator.inflate(R.layout.grid_item, parent, false);
view.setTag(ViewType.GRID_ITEM);
break;
case LIST_ITEM:
view = inflator.inflate(R.layout.list_item, null);
view = inflator.inflate(R.layout.list_item, parent, false);
view.setTag(ViewType.LIST_ITEM);
break;
}
}
view.invalidate();
if (file != null) {
ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
@ -243,9 +221,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
sharedIconV.setVisibility(View.GONE);
}
/*ImageView sharedWithMeIcon = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
sharedWithMeIcon.bringToFront();*/
// local state
ImageView localStateView = (ImageView) view.findViewById(R.id.localFileIndicator);
localStateView.bringToFront();
@ -291,14 +266,17 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
break;
}
// For all Views
ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
checkBoxV.setVisibility(View.GONE);
view.setBackgroundColor(Color.WHITE);
AbsListView parentList = (AbsListView) parent;
if (parentList.getChoiceMode() != AbsListView.CHOICE_MODE_NONE
&& parentList.getCheckedItemCount() > 0) {
if (isItemSelected(position)) {
if (parentList.getChoiceMode() != AbsListView.CHOICE_MODE_NONE &&
parentList.getCheckedItemCount() > 0
) {
if (parentList.isItemChecked(position)) {
view.setBackgroundColor(mContext.getResources().getColor(
R.color.selected_item_background));
checkBoxV.setImageResource(
@ -311,8 +289,6 @@ 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
// because android reuses views in listview
if (!file.isFavorite()) {
@ -376,19 +352,6 @@ 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;
@ -407,23 +370,20 @@ 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
* @param folder New folder 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
public void swapDirectory(OCFile folder, FileDataStorageManager updatedStorageManager
/*, boolean onlyOnDevice*/) {
mFile = directory;
if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
mStorageManager = updatedStorageManager;
mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
}
if (mStorageManager != null) {
// TODO Enable when "On Device" is recovered ?
mFiles = mStorageManager.getFolderContent(mFile/*, onlyOnDevice*/);
mFilesOrig.clear();
mFilesOrig.addAll(mFiles);
mFiles = mStorageManager.getFolderContent(folder/*, onlyOnDevice*/);
if (mJustFolders) {
mFiles = getFolders(mFiles);
@ -439,12 +399,12 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
/**
* Filter for getting only the folders
*
* @param files
* @return Vector<OCFile>
* @param files Collection of files to filter
* @return Folders in the input
*/
public Vector<OCFile> getFolders(Vector<OCFile> files) {
Vector<OCFile> ret = new Vector<OCFile>();
OCFile current = null;
Vector<OCFile> ret = new Vector<>();
OCFile current;
for (int i = 0; i < files.size(); i++) {
current = files.get(i);
if (current.isFolder()) {
@ -468,64 +428,19 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
}
public void setGridMode(boolean gridMode) {
mGridMode = gridMode;
}
public boolean isItemSelected(int position) {
return mSelection.contains(getItemId(position));
}
public void updateSelection(long itemId, boolean checked) {
if (checked) {
mSelection.add(itemId);
} else {
mSelection.remove(itemId);
}
notifyDataSetChanged();
}
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())) {
files.add(file);
public ArrayList<OCFile> getCheckedItems(AbsListView parentList) {
SparseBooleanArray checkedPositions = parentList.getCheckedItemPositions();
ArrayList<OCFile> files = new ArrayList<>();
Object item;
for (int i=0; i < checkedPositions.size(); i++) {
if (checkedPositions.valueAt(i)) {
item = getItem(checkedPositions.keyAt(i));
if (item != null) {
files.add((OCFile)item);
}
}
}
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);
}
}
isGridViewSelectionRestored = false;
}
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;
}
}

View file

@ -20,14 +20,10 @@
package com.owncloud.android.ui.fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
@ -35,7 +31,6 @@ import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.getbase.floatingactionbutton.FloatingActionButton;
@ -62,6 +57,7 @@ public class ExtendedListFragment extends Fragment
private static final String KEY_TOPS = "TOPS";
private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
private static final String KEY_IS_GRID_VISIBLE = "IS_GRID_VISIBLE";
protected SwipeRefreshLayout mRefreshListLayout;
private SwipeRefreshLayout mRefreshGridLayout;
@ -91,13 +87,8 @@ public class ExtendedListFragment extends Fragment
protected void setListAdapter(ListAdapter listAdapter) {
mAdapter = listAdapter;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mCurrentListView.setAdapter(listAdapter);
} else {
((ListView)mCurrentListView).setAdapter(listAdapter);
}
mCurrentListView.invalidate();
mCurrentListView.setAdapter(listAdapter);
mCurrentListView.invalidateViews();
}
protected AbsListView getListView() {
@ -121,41 +112,27 @@ public class ExtendedListFragment extends Fragment
}
public void switchToGridView() {
if ((mCurrentListView == mListView)) {
if (!isGridEnabled()) {
mListView.setAdapter(null);
mRefreshListLayout.setVisibility(View.GONE);
if (mAdapter instanceof FileListListAdapter) {
((FileListListAdapter) mAdapter).setGridMode(true);
}
mGridView.setAdapter(mAdapter);
mRefreshGridLayout.setVisibility(View.VISIBLE);
mCurrentListView = mGridView;
setListAdapter(mAdapter);
}
}
public void switchToListView() {
if (mCurrentListView == mGridView) {
if (isGridEnabled()) {
mGridView.setAdapter(null);
mRefreshGridLayout.setVisibility(View.GONE);
if (mAdapter instanceof FileListListAdapter) {
((FileListListAdapter) mAdapter).setGridMode(false);
}
mListView.setAdapter(mAdapter);
mRefreshListLayout.setVisibility(View.VISIBLE);
mCurrentListView = mListView;
setListAdapter(mAdapter);
}
}
public boolean isGridView(){
if (mAdapter instanceof FileListListAdapter) {
return ((FileListListAdapter) mAdapter).isGridMode();
}
return false;
public boolean isGridEnabled(){
return (mCurrentListView == mGridView);
}
@ -177,17 +154,6 @@ public class ExtendedListFragment extends Fragment
mGridFooterView = inflater.inflate(R.layout.list_footer, null, false);
if (savedInstanceState != null) {
int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
if (mCurrentListView == mListView) {
Log_OC.v(TAG, "Setting and centering around list position " + referencePosition);
mListView.setAndCenterSelection(referencePosition);
} else {
Log_OC.v(TAG, "Setting grid position " + referencePosition);
mGridView.setSelection(referencePosition);
}
}
// Pull-down to refresh layout
mRefreshListLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_list);
mRefreshGridLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_grid);
@ -201,13 +167,26 @@ public class ExtendedListFragment extends Fragment
mListView.setEmptyView(mRefreshEmptyLayout);
mGridView.setEmptyView(mRefreshEmptyLayout);
mCurrentListView = mListView; // list as default
mFabMain = (FloatingActionsMenu) v.findViewById(R.id.fab_main);
mFabUpload = (FloatingActionButton) v.findViewById(R.id.fab_upload);
mFabMkdir = (FloatingActionButton) v.findViewById(R.id.fab_mkdir);
mFabUploadFromApp = (FloatingActionButton) v.findViewById(R.id.fab_upload_from_app);
mCurrentListView = mListView; // list by default
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean(KEY_IS_GRID_VISIBLE, false)) {
switchToGridView();
}
int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
if (isGridEnabled()) {
Log_OC.v(TAG, "Setting grid position " + referencePosition);
mGridView.setSelection(referencePosition);
} else {
Log_OC.v(TAG, "Setting and centering around list position " + referencePosition);
mListView.setAndCenterSelection(referencePosition);
}
}
return v;
}
@ -238,6 +217,7 @@ public class ExtendedListFragment extends Fragment
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log_OC.d(TAG, "onSaveInstanceState()");
savedInstanceState.putBoolean(KEY_IS_GRID_VISIBLE, isGridEnabled());
savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);

View file

@ -184,7 +184,6 @@ public class OCFileListFragment extends ExtendedListFragment {
getActivity(),
mContainerActivity
);
mAdapter.restoreSelectionState(savedInstanceState);
setListAdapter(mAdapter);
mHideFab = (args != null) && args.getBoolean(ARG_HIDE_FAB, false);
@ -349,7 +348,7 @@ public class OCFileListFragment extends ExtendedListFragment {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
mAdapter.updateSelection(id, checked);
getListView().invalidateViews();
mode.invalidate();
}
@ -376,7 +375,7 @@ public class OCFileListFragment extends ExtendedListFragment {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
List<OCFile> checkedFiles = mAdapter.getCheckedItems();
List<OCFile> checkedFiles = mAdapter.getCheckedItems(getListView());
final int checkedCount = checkedFiles.size();
String title = getResources().getQuantityString(
R.plurals.items_selected_count,
@ -384,15 +383,13 @@ public class OCFileListFragment extends ExtendedListFragment {
checkedCount
);
mode.setTitle(title);
if (checkedCount > 0) {
FileMenuFilter mf = new FileMenuFilter(
checkedFiles,
((FileActivity) getActivity()).getAccount(),
mContainerActivity,
getActivity()
);
mf.filter(menu);
}
FileMenuFilter mf = new FileMenuFilter(
checkedFiles,
((FileActivity) getActivity()).getAccount(),
mContainerActivity,
getActivity()
);
mf.filter(menu);
return true;
}
@ -404,8 +401,6 @@ public class OCFileListFragment extends ExtendedListFragment {
@Override
public void onDestroyActionMode(ActionMode mode) {
mActiveActionMode = null;
//getListView().clearChoices();
mAdapter.clearSelection();
// reset to previous color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@ -427,7 +422,6 @@ public class OCFileListFragment extends ExtendedListFragment {
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(KEY_FILE, mFile);
mAdapter.saveSelectionState(outState);
}
@Override
@ -444,7 +438,7 @@ public class OCFileListFragment extends ExtendedListFragment {
* return Count of folder levels browsed up.
*/
public int onBrowseUp() {
OCFile parentDir = null;
OCFile parentDir;
int moveCount = 0;
if (mFile != null) {
@ -529,7 +523,7 @@ public class OCFileListFragment extends ExtendedListFragment {
* @return 'true' if the menu selection started any action, 'false' otherwise.
*/
public boolean onFileActionChosen(int menuId) {
final ArrayList<OCFile> checkedFiles = mAdapter.getCheckedItems();
final ArrayList<OCFile> checkedFiles = mAdapter.getCheckedItems(getListView());
if (checkedFiles.size() <= 0) return false;
if (checkedFiles.size() == 1) {
@ -626,11 +620,6 @@ public class OCFileListFragment extends ExtendedListFragment {
// listDirectory(null, onlyOnDevice);
}
public void refreshDirectory(){
// TODO Enable when "On Device" is recovered ?
listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
}
/**
* Lists the given directory on the view. When the input parameter is null,
* it will either refresh the last known directory. list the root
@ -673,7 +662,7 @@ public class OCFileListFragment extends ExtendedListFragment {
private void updateLayout() {
if (!mJustFolders) {
int filesCount = 0, foldersCount = 0, imagesCount = 0;
int filesCount = 0, foldersCount = 0;
int count = mAdapter.getCount();
OCFile file;
for (int i=0; i < count ; i++) {
@ -683,10 +672,6 @@ public class OCFileListFragment extends ExtendedListFragment {
} else {
if (!file.isHidden()) {
filesCount++;
if (file.isImage()) {
imagesCount++;
}
}
}
}
@ -767,13 +752,13 @@ public class OCFileListFragment extends ExtendedListFragment {
/**
* Determines if user set folder to grid or list view. If folder is not set itself,
* it finds a parent that is set (at least root is set).
* @param file
* @return
* @param file Folder to check.
* @return 'true' is folder should be shown in grid mode, 'false' if list mode is preferred.
*/
public boolean isGridViewPreferred(OCFile file){
if (file != null) {
OCFile fileToTest = file;
OCFile parentDir = null;
OCFile parentDir;
String parentPath = null;
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();