package com.owncloud.android.ui.fragment;
import java.io.File;
-import java.util.ArrayList;
import android.app.Activity;
+import android.content.Intent;
import android.os.Bundle;
+import android.support.v4.widget.SwipeRefreshLayout;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.owncloud.android.files.FileMenuFilter;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.activity.FileDisplayActivity;
+import com.owncloud.android.ui.activity.MoveActivity;
import com.owncloud.android.ui.adapter.FileListListAdapter;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
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 EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
+ private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
+ OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
+
+ public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
+ public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
+
+ private static final String KEY_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 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}
Log_OC.e(TAG, "onAttach");
try {
mContainerActivity = (FileFragment.ContainerActivity) activity;
+
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement " +
FileFragment.ContainerActivity.class.getSimpleName());
}
+ try {
+ setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) activity);
+
+ } catch (ClassCastException e) {
+ throw new ClassCastException(activity.toString() + " must implement " +
+ SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
+ }
}
@Override
public void onDetach() {
+ setOnRefreshListener(null);
mContainerActivity = null;
super.onDetach();
}
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;
-
+ mFile = savedInstanceState.getParcelable(KEY_FILE);
}
- mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity);
-
+ Bundle args = getArguments();
+ boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
+ mAdapter = new FileListListAdapter(
+ justFolders,
+ getSherlockActivity(),
+ mContainerActivity
+ );
setListAdapter(mAdapter);
registerForContextMenu(getListView());
@Override
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());
+ outState.putParcelable(KEY_FILE, mFile);
}
/**
* 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.
*/
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);
+ onRefresh();
// restore index and top position
restoreIndexAndTopPosition();
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);
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);
* {@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);
- AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
- OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
-
- if (mContainerActivity.getStorageManager() != null) {
- FileMenuFilter mf = new FileMenuFilter(
- targetFile,
- mContainerActivity.getStorageManager().getAccount(),
- mContainerActivity,
- getSherlockActivity()
- );
- mf.filter(menu);
- }
-
- /// additional restrictions for this fragment
- // TODO allow in the future 'open with' for previewable files
- MenuItem item = menu.findItem(R.id.action_open_file_with);
- if (item != null) {
- item.setVisible(false);
- item.setEnabled(false);
- }
- /// TODO break this direct dependency on FileDisplayActivity... if possible
- FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
- if (frag != null && frag instanceof FileDetailFragment &&
- frag.getFile().getFileId() == targetFile.getFileId()) {
- item = menu.findItem(R.id.action_see_details);
+ Bundle args = getArguments();
+ boolean allowContextualActions =
+ (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
+ if (allowContextualActions) {
+ MenuInflater inflater = getSherlockActivity().getMenuInflater();
+ inflater.inflate(R.menu.file_actions_menu, menu);
+ AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
+ OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
+
+ if (mContainerActivity.getStorageManager() != null) {
+ FileMenuFilter mf = new FileMenuFilter(
+ targetFile,
+ mContainerActivity.getStorageManager().getAccount(),
+ mContainerActivity,
+ getSherlockActivity()
+ );
+ mf.filter(menu);
+ }
+
+ /// additional restrictions for this fragment
+ // TODO allow in the future 'open with' for previewable files
+ MenuItem item = menu.findItem(R.id.action_open_file_with);
if (item != null) {
item.setVisible(false);
item.setEnabled(false);
}
+ /// TODO break this direct dependency on FileDisplayActivity... if possible
+ FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
+ if (frag != null && frag instanceof FileDetailFragment &&
+ frag.getFile().getFileId() == targetFile.getFileId()) {
+ item = menu.findItem(R.id.action_see_details);
+ if (item != null) {
+ item.setVisible(false);
+ item.setEnabled(false);
+ }
+ }
}
-
-
}
}
return true;
}
+ case R.id.action_move: {
+ Intent action = new Intent(getActivity(), MoveActivity.class);
+
+ // Pass mTargetFile that contains info of selected file/folder
+ action.putExtra(MoveActivity.EXTRA_TARGET_FILE, mTargetFile);
+ getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
+ return true;
+ }
default:
return super.onContextItemSelected(item);
}
}
}
-
- @Override
- public void onRefresh() {
- super.onRefresh();
-
- if (mFile != null) {
- // Refresh mFile
- mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
-
- listDirectory(mFile);
-
- ((FileDisplayActivity)mContainerActivity).startSyncFolderOperation(mFile);
- }
- }
-
-
-
}