Some clean-up and refactoring in fragments listing files

This commit is contained in:
David A. Velasco 2014-08-19 11:13:08 +02:00
parent f63996d115
commit b3bc1aeaf2
5 changed files with 149 additions and 200 deletions

View file

@ -1,3 +1,20 @@
/* ownCloud Android client application
* Copyright (C) 2012-2014 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/>.
*
*/
package com.owncloud.android.ui.activity;
import java.io.IOException;

View file

@ -18,6 +18,8 @@
package com.owncloud.android.ui.fragment;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
@ -42,6 +44,11 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
private static final String TAG = ExtendedListFragment.class.getSimpleName();
private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION";
private static final String KEY_INDEXES = "INDEXES";
private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
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";
protected ExtendedListView mList;
@ -49,6 +56,13 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
private SwipeRefreshLayout mRefreshEmptyLayout;
private TextView mEmptyListMessage;
// Save the state of the scroll in browsing
private ArrayList<Integer> mIndexes;
private ArrayList<Integer> mFirstPositions;
private ArrayList<Integer> mTops;
private int mHeightCell = 0;
public void setListAdapter(ListAdapter listAdapter) {
mList.setAdapter(listAdapter);
mList.invalidate();
@ -62,7 +76,6 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log_OC.e(TAG, "onCreateView");
//mList = new ExtendedListView(getActivity());
View v = inflater.inflate(R.layout.list_fragment, null);
mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
@ -90,11 +103,39 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
}
/**
* {@inheritDoc}
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
} else {
mIndexes = new ArrayList<Integer>();
mFirstPositions = new ArrayList<Integer>();
mTops = new ArrayList<Integer>();
mHeightCell = 0;
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log_OC.e(TAG, "onSaveInstanceState()");
savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
}
@ -126,6 +167,60 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
}
}
/*
* Restore index and position
*/
protected void restoreIndexAndTopPosition() {
if (mIndexes.size() > 0) {
// needs to be checked; not every browse-up had a browse-down before
int index = mIndexes.remove(mIndexes.size() - 1);
int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
int top = mTops.remove(mTops.size() - 1);
mList.setSelectionFromTop(firstPosition, top);
// Move the scroll if the selection is not visible
int indexPosition = mHeightCell*index;
int height = mList.getHeight();
if (indexPosition > height) {
if (android.os.Build.VERSION.SDK_INT >= 11)
{
mList.smoothScrollToPosition(index);
}
else if (android.os.Build.VERSION.SDK_INT >= 8)
{
mList.setSelectionFromTop(index, 0);
}
}
}
}
/*
* Save index and top position
*/
protected void saveIndexAndTopPosition(int index) {
mIndexes.add(index);
int firstPosition = mList.getFirstVisiblePosition();
mFirstPositions.add(firstPosition);
View view = mList.getChildAt(0);
int top = (view == null) ? 0 : view.getTop() ;
mTops.add(top);
// Save the height of a cell
mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
}
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
// to be @overriden

View file

@ -90,7 +90,7 @@ public class LocalFileListFragment extends ExtendedListFragment {
public void onActivityCreated(Bundle savedInstanceState) {
Log_OC.i(TAG, "onActivityCreated() start");
super.onCreate(savedInstanceState);
super.onActivityCreated(savedInstanceState);
mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
setListAdapter(mAdapter);
@ -111,6 +111,8 @@ public class LocalFileListFragment extends ExtendedListFragment {
listDirectory(file);
// notify the click to container Activity
mContainerActivity.onDirectoryClick(file);
// save index and top position
saveIndexAndTopPosition(position);
} else { /// Click on a file
ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
@ -140,6 +142,9 @@ public class LocalFileListFragment extends ExtendedListFragment {
parentDir = mDirectory.getParentFile(); // can be null
}
listDirectory(parentDir);
// restore index and top position
restoreIndexAndTopPosition();
}

View file

@ -1,5 +1,4 @@
/* ownCloud Android client application
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2012-2014 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
@ -15,10 +14,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui.fragment;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
@ -32,35 +31,23 @@ import com.owncloud.android.ui.adapter.FolderListListAdapter;
import com.owncloud.android.utils.Log_OC;
/**
* A Fragment that lists all folders in a given path.
* A Fragment that shows all the folders in a given path, and allows browsing through them.
*
* TODO refactorize to get rid of direct dependency on MoveActivity
*
*/
public class MoveFileListFragment extends ExtendedListFragment {
private static final String TAG = MoveFileListFragment.class.getSimpleName();
private static final String MY_PACKAGE = MoveFileListFragment.class.getPackage() != null ? MoveFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
private static final String MY_PACKAGE = MoveFileListFragment.class.getPackage() != null ?
MoveFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
private static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
private static final String KEY_INDEXES = "INDEXES";
private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
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 FileFragment.ContainerActivity mContainerActivity;
private OCFile mFile = null;
private FolderListListAdapter mAdapter;
// Save the state of the scroll in browsing
private ArrayList<Integer> mIndexes;
private ArrayList<Integer> mFirstPositions;
private ArrayList<Integer> mTops;
private int mHeightCell = 0;
/**
* {@inheritDoc}
@ -92,31 +79,17 @@ public class MoveFileListFragment extends ExtendedListFragment {
super.onActivityCreated(savedInstanceState);
Log_OC.e(TAG, "onActivityCreated() start");
mAdapter = new FolderListListAdapter(getSherlockActivity(), mContainerActivity);
if (savedInstanceState != null) {
mFile = savedInstanceState.getParcelable(EXTRA_FILE);
mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
} else {
mIndexes = new ArrayList<Integer>();
mFirstPositions = new ArrayList<Integer>();
mTops = new ArrayList<Integer>();
mHeightCell = 0;
}
mAdapter = new FolderListListAdapter(getSherlockActivity(), mContainerActivity);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
getListView().setOnCreateContextMenuListener(this);
}
}
/**
* Saves the current listed folder.
@ -125,18 +98,13 @@ public class MoveFileListFragment extends ExtendedListFragment {
public void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(EXTRA_FILE, mFile);
outState.putIntegerArrayList(KEY_INDEXES, mIndexes);
outState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
outState.putIntegerArrayList(KEY_TOPS, mTops);
outState.putInt(KEY_HEIGHT_CELL, mHeightCell);
outState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
}
/**
* Call this, when the user presses the up button.
*
* Tries to move up the current folder one level. If the parent folder was removed from the database,
* it continues browsing up until finding an existing folders.
* Tries to move up the current folder one level. If the parent folder was removed from the
* database, it continues browsing up until finding an existing folders.
*
* return Count of folder levels browsed up.
*/
@ -150,22 +118,22 @@ public class MoveFileListFragment extends ExtendedListFragment {
String parentPath = null;
if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
parentPath = new File(mFile.getRemotePath()).getParent();
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
parentPath + OCFile.PATH_SEPARATOR;
parentDir = storageManager.getFileByPath(parentPath);
moveCount++;
} else {
parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null; keep the path in root folder
parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
}
while (parentDir == null) {
parentPath = new File(parentPath).getParent();
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
parentPath + OCFile.PATH_SEPARATOR;
parentDir = storageManager.getFileByPath(parentPath);
moveCount++;
} // exit is granted because storageManager.getFileByPath("/") never returns null
mFile = parentDir;
}
if (mFile != null) {
mFile = parentDir;
listDirectory(mFile);
((MoveActivity)mContainerActivity).startSyncFolderOperation(mFile);
@ -178,58 +146,6 @@ public class MoveFileListFragment extends ExtendedListFragment {
return moveCount;
}
/*
* Restore index and position
*/
private void restoreIndexAndTopPosition() {
if (mIndexes.size() > 0) {
// needs to be checked; not every browse-up had a browse-down before
int index = mIndexes.remove(mIndexes.size() - 1);
int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
int top = mTops.remove(mTops.size() - 1);
mList.setSelectionFromTop(firstPosition, top);
// Move the scroll if the selection is not visible
int indexPosition = mHeightCell*index;
int height = mList.getHeight();
if (indexPosition > height) {
if (android.os.Build.VERSION.SDK_INT >= 11)
{
mList.smoothScrollToPosition(index);
}
else if (android.os.Build.VERSION.SDK_INT >= 8)
{
mList.setSelectionFromTop(index, 0);
}
}
}
}
/*
* Save index and top position
*/
private void saveIndexAndTopPosition(int index) {
mIndexes.add(index);
int firstPosition = mList.getFirstVisiblePosition();
mFirstPositions.add(firstPosition);
View view = mList.getChildAt(0);
int top = (view == null) ? 0 : view.getTop() ;
mTops.add(top);
// Save the height of a cell
mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
}
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
OCFile file = (OCFile) mAdapter.getItem(position);
@ -237,7 +153,7 @@ public class MoveFileListFragment extends ExtendedListFragment {
if (file.isFolder()) {
// update state and view of this fragment
listDirectory(file);
// then, notify parent activity to let it update its state and view, and other fragments
// then, notify parent activity to let it update its state and view
mContainerActivity.onBrowsedDownTo(file);
// save index and top position
saveIndexAndTopPosition(position);

View file

@ -18,7 +18,6 @@
package com.owncloud.android.ui.fragment;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
@ -57,15 +56,10 @@ public class OCFileListFragment extends ExtendedListFragment {
private static final String TAG = OCFileListFragment.class.getSimpleName();
private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ? OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
private static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
private static final String KEY_INDEXES = "INDEXES";
private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
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 FileFragment.ContainerActivity mContainerActivity;
private OCFile mFile = null;
@ -73,13 +67,6 @@ public class OCFileListFragment extends ExtendedListFragment {
private OCFile mTargetFile;
// Save the state of the scroll in browsing
private ArrayList<Integer> mIndexes;
private ArrayList<Integer> mFirstPositions;
private ArrayList<Integer> mTops;
private int mHeightCell = 0;
/**
* {@inheritDoc}
*/
@ -110,26 +97,11 @@ public class OCFileListFragment extends ExtendedListFragment {
super.onActivityCreated(savedInstanceState);
Log_OC.e(TAG, "onActivityCreated() start");
mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity);
if (savedInstanceState != null) {
mFile = savedInstanceState.getParcelable(EXTRA_FILE);
mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
} else {
mIndexes = new ArrayList<Integer>();
mFirstPositions = new ArrayList<Integer>();
mTops = new ArrayList<Integer>();
mHeightCell = 0;
}
mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
@ -143,18 +115,13 @@ public class OCFileListFragment extends ExtendedListFragment {
public void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(EXTRA_FILE, mFile);
outState.putIntegerArrayList(KEY_INDEXES, mIndexes);
outState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
outState.putIntegerArrayList(KEY_TOPS, mTops);
outState.putInt(KEY_HEIGHT_CELL, mHeightCell);
outState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
}
/**
* Call this, when the user presses the up button.
*
* Tries to move up the current folder one level. If the parent folder was removed from the database,
* it continues browsing up until finding an existing folders.
* Tries to move up the current folder one level. If the parent folder was removed from the
* database, it continues browsing up until finding an existing folders.
*
* return Count of folder levels browsed up.
*/
@ -168,22 +135,22 @@ public class OCFileListFragment extends ExtendedListFragment {
String parentPath = null;
if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
parentPath = new File(mFile.getRemotePath()).getParent();
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
parentPath + OCFile.PATH_SEPARATOR;
parentDir = storageManager.getFileByPath(parentPath);
moveCount++;
} else {
parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null; keep the path in root folder
parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
}
while (parentDir == null) {
parentPath = new File(parentPath).getParent();
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
parentPath + OCFile.PATH_SEPARATOR;
parentDir = storageManager.getFileByPath(parentPath);
moveCount++;
} // exit is granted because storageManager.getFileByPath("/") never returns null
mFile = parentDir;
}
if (mFile != null) {
mFile = parentDir;
listDirectory(mFile);
((FileDisplayActivity)mContainerActivity).startSyncFolderOperation(mFile);
@ -196,58 +163,6 @@ public class OCFileListFragment extends ExtendedListFragment {
return moveCount;
}
/*
* Restore index and position
*/
private void restoreIndexAndTopPosition() {
if (mIndexes.size() > 0) {
// needs to be checked; not every browse-up had a browse-down before
int index = mIndexes.remove(mIndexes.size() - 1);
int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
int top = mTops.remove(mTops.size() - 1);
mList.setSelectionFromTop(firstPosition, top);
// Move the scroll if the selection is not visible
int indexPosition = mHeightCell*index;
int height = mList.getHeight();
if (indexPosition > height) {
if (android.os.Build.VERSION.SDK_INT >= 11)
{
mList.smoothScrollToPosition(index);
}
else if (android.os.Build.VERSION.SDK_INT >= 8)
{
mList.setSelectionFromTop(index, 0);
}
}
}
}
/*
* Save index and top position
*/
private void saveIndexAndTopPosition(int index) {
mIndexes.add(index);
int firstPosition = mList.getFirstVisiblePosition();
mFirstPositions.add(firstPosition);
View view = mList.getChildAt(0);
int top = (view == null) ? 0 : view.getTop() ;
mTops.add(top);
// Save the height of a cell
mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
}
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
OCFile file = (OCFile) mAdapter.getItem(position);
@ -255,7 +170,7 @@ public class OCFileListFragment extends ExtendedListFragment {
if (file.isFolder()) {
// update state and view of this fragment
listDirectory(file);
// then, notify parent activity to let it update its state and view, and other fragments
// then, notify parent activity to let it update its state and view
mContainerActivity.onBrowsedDownTo(file);
// save index and top position
saveIndexAndTopPosition(position);
@ -290,7 +205,8 @@ public class OCFileListFragment extends ExtendedListFragment {
* {@inheritDoc}
*/
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
public void onCreateContextMenu (
ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getSherlockActivity().getMenuInflater();
inflater.inflate(R.menu.file_actions_menu, menu);