OCFileListFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2014 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.fragment;
  19. import java.io.File;
  20. import android.app.Activity;
  21. import android.content.Intent;
  22. import android.os.Bundle;
  23. import android.support.v4.widget.SwipeRefreshLayout;
  24. import android.view.ContextMenu;
  25. import android.view.MenuInflater;
  26. import android.view.MenuItem;
  27. import android.view.View;
  28. import android.widget.AdapterView;
  29. import android.widget.AdapterView.AdapterContextMenuInfo;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.datamodel.FileDataStorageManager;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.files.FileMenuFilter;
  34. import com.owncloud.android.lib.common.utils.Log_OC;
  35. import com.owncloud.android.ui.activity.FileDisplayActivity;
  36. import com.owncloud.android.ui.activity.FolderPickerActivity;
  37. import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
  38. import com.owncloud.android.ui.adapter.FileListListAdapter;
  39. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  40. import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
  41. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  42. import com.owncloud.android.ui.preview.PreviewImageFragment;
  43. import com.owncloud.android.ui.preview.PreviewMediaFragment;
  44. /**
  45. * A Fragment that lists all files and folders in a given path.
  46. *
  47. * TODO refactorize to get rid of direct dependency on FileDisplayActivity
  48. *
  49. * @author Bartek Przybylski
  50. * @author masensio
  51. * @author David A. Velasco
  52. */
  53. public class OCFileListFragment extends ExtendedListFragment {
  54. private static final String TAG = OCFileListFragment.class.getSimpleName();
  55. private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
  56. OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
  57. public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
  58. public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
  59. private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
  60. private FileFragment.ContainerActivity mContainerActivity;
  61. private OCFile mFile = null;
  62. private FileListListAdapter mAdapter;
  63. private OCFile mTargetFile;
  64. /**
  65. * {@inheritDoc}
  66. */
  67. @Override
  68. public void onAttach(Activity activity) {
  69. super.onAttach(activity);
  70. Log_OC.e(TAG, "onAttach");
  71. try {
  72. mContainerActivity = (FileFragment.ContainerActivity) activity;
  73. } catch (ClassCastException e) {
  74. throw new ClassCastException(activity.toString() + " must implement " +
  75. FileFragment.ContainerActivity.class.getSimpleName());
  76. }
  77. try {
  78. setOnRefreshListener((OnEnforceableRefreshListener) activity);
  79. } catch (ClassCastException e) {
  80. throw new ClassCastException(activity.toString() + " must implement " +
  81. SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
  82. }
  83. }
  84. @Override
  85. public void onDetach() {
  86. setOnRefreshListener(null);
  87. mContainerActivity = null;
  88. super.onDetach();
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. @Override
  94. public void onActivityCreated(Bundle savedInstanceState) {
  95. super.onActivityCreated(savedInstanceState);
  96. Log_OC.e(TAG, "onActivityCreated() start");
  97. if (savedInstanceState != null) {
  98. mFile = savedInstanceState.getParcelable(KEY_FILE);
  99. }
  100. Bundle args = getArguments();
  101. boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
  102. mAdapter = new FileListListAdapter(
  103. justFolders,
  104. getSherlockActivity(),
  105. mContainerActivity
  106. );
  107. setListAdapter(mAdapter);
  108. registerForContextMenu(getListView());
  109. getListView().setOnCreateContextMenuListener(this);
  110. }
  111. /**
  112. * Saves the current listed folder.
  113. */
  114. @Override
  115. public void onSaveInstanceState (Bundle outState) {
  116. super.onSaveInstanceState(outState);
  117. outState.putParcelable(KEY_FILE, mFile);
  118. }
  119. /**
  120. * Call this, when the user presses the up button.
  121. *
  122. * Tries to move up the current folder one level. If the parent folder was removed from the
  123. * database, it continues browsing up until finding an existing folders.
  124. *
  125. * return Count of folder levels browsed up.
  126. */
  127. public int onBrowseUp() {
  128. OCFile parentDir = null;
  129. int moveCount = 0;
  130. if(mFile != null){
  131. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  132. String parentPath = null;
  133. if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
  134. parentPath = new File(mFile.getRemotePath()).getParent();
  135. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
  136. parentPath + OCFile.PATH_SEPARATOR;
  137. parentDir = storageManager.getFileByPath(parentPath);
  138. moveCount++;
  139. } else {
  140. parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
  141. }
  142. while (parentDir == null) {
  143. parentPath = new File(parentPath).getParent();
  144. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
  145. parentPath + OCFile.PATH_SEPARATOR;
  146. parentDir = storageManager.getFileByPath(parentPath);
  147. moveCount++;
  148. } // exit is granted because storageManager.getFileByPath("/") never returns null
  149. mFile = parentDir;
  150. listDirectory(mFile);
  151. onRefresh(false);
  152. // restore index and top position
  153. restoreIndexAndTopPosition();
  154. } // else - should never happen now
  155. return moveCount;
  156. }
  157. @Override
  158. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  159. OCFile file = (OCFile) mAdapter.getItem(position);
  160. if (file != null) {
  161. if (file.isFolder()) {
  162. // update state and view of this fragment
  163. listDirectory(file);
  164. // then, notify parent activity to let it update its state and view
  165. mContainerActivity.onBrowsedDownTo(file);
  166. // save index and top position
  167. saveIndexAndTopPosition(position);
  168. } else { /// Click on a file
  169. if (PreviewImageFragment.canBePreviewed(file)) {
  170. // preview image - it handles the download, if needed
  171. ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
  172. } else if (file.isDown()) {
  173. if (PreviewMediaFragment.canBePreviewed(file)) {
  174. // media preview
  175. ((FileDisplayActivity)mContainerActivity).startMediaPreview(file, 0, true);
  176. } else {
  177. mContainerActivity.getFileOperationsHelper().openFile(file);
  178. }
  179. } else {
  180. // automatic download, preview on finish
  181. ((FileDisplayActivity)mContainerActivity).startDownloadForPreview(file);
  182. }
  183. }
  184. } else {
  185. Log_OC.d(TAG, "Null object in ListAdapter!!");
  186. }
  187. }
  188. /**
  189. * {@inheritDoc}
  190. */
  191. @Override
  192. public void onCreateContextMenu (
  193. ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  194. super.onCreateContextMenu(menu, v, menuInfo);
  195. Bundle args = getArguments();
  196. boolean allowContextualActions =
  197. (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
  198. if (allowContextualActions) {
  199. MenuInflater inflater = getSherlockActivity().getMenuInflater();
  200. inflater.inflate(R.menu.file_actions_menu, menu);
  201. AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
  202. OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
  203. if (mContainerActivity.getStorageManager() != null) {
  204. FileMenuFilter mf = new FileMenuFilter(
  205. targetFile,
  206. mContainerActivity.getStorageManager().getAccount(),
  207. mContainerActivity,
  208. getSherlockActivity()
  209. );
  210. mf.filter(menu);
  211. }
  212. /// additional restrictions for this fragment
  213. // TODO allow in the future 'open with' for previewable files
  214. MenuItem item = menu.findItem(R.id.action_open_file_with);
  215. if (item != null) {
  216. item.setVisible(false);
  217. item.setEnabled(false);
  218. }
  219. /// TODO break this direct dependency on FileDisplayActivity... if possible
  220. FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
  221. if (frag != null && frag instanceof FileDetailFragment &&
  222. frag.getFile().getFileId() == targetFile.getFileId()) {
  223. item = menu.findItem(R.id.action_see_details);
  224. if (item != null) {
  225. item.setVisible(false);
  226. item.setEnabled(false);
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * {@inhericDoc}
  233. */
  234. @Override
  235. public boolean onContextItemSelected (MenuItem item) {
  236. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  237. mTargetFile = (OCFile) mAdapter.getItem(info.position);
  238. switch (item.getItemId()) {
  239. case R.id.action_share_file: {
  240. mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile);
  241. return true;
  242. }
  243. case R.id.action_unshare_file: {
  244. mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile);
  245. return true;
  246. }
  247. case R.id.action_rename_file: {
  248. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
  249. dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
  250. return true;
  251. }
  252. case R.id.action_remove_file: {
  253. RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
  254. dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
  255. return true;
  256. }
  257. case R.id.action_download_file:
  258. case R.id.action_sync_file: {
  259. mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
  260. return true;
  261. }
  262. case R.id.action_cancel_download:
  263. case R.id.action_cancel_upload: {
  264. ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
  265. return true;
  266. }
  267. case R.id.action_see_details: {
  268. mContainerActivity.showDetails(mTargetFile);
  269. return true;
  270. }
  271. case R.id.action_send_file: {
  272. // Obtain the file
  273. if (!mTargetFile.isDown()) { // Download the file
  274. Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
  275. ((FileDisplayActivity)mContainerActivity).startDownloadForSending(mTargetFile);
  276. } else {
  277. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
  278. }
  279. return true;
  280. }
  281. case R.id.action_move: {
  282. Intent action = new Intent(getActivity(), FolderPickerActivity.class);
  283. // Pass mTargetFile that contains info of selected file/folder
  284. action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
  285. getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
  286. return true;
  287. }
  288. default:
  289. return super.onContextItemSelected(item);
  290. }
  291. }
  292. /**
  293. * Use this to query the {@link OCFile} that is currently
  294. * being displayed by this fragment
  295. * @return The currently viewed OCFile
  296. */
  297. public OCFile getCurrentFile(){
  298. return mFile;
  299. }
  300. /**
  301. * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
  302. */
  303. public void listDirectory(){
  304. listDirectory(null);
  305. }
  306. /**
  307. * Lists the given directory on the view. When the input parameter is null,
  308. * it will either refresh the last known directory. list the root
  309. * if there never was a directory.
  310. *
  311. * @param directory File to be listed
  312. */
  313. public void listDirectory(OCFile directory) {
  314. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  315. if (storageManager != null) {
  316. // Check input parameters for null
  317. if(directory == null){
  318. if(mFile != null){
  319. directory = mFile;
  320. } else {
  321. directory = storageManager.getFileByPath("/");
  322. if (directory == null) return; // no files, wait for sync
  323. }
  324. }
  325. // If that's not a directory -> List its parent
  326. if(!directory.isFolder()){
  327. Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
  328. directory = storageManager.getFileById(directory.getParentId());
  329. }
  330. mAdapter.swapDirectory(directory, storageManager);
  331. if (mFile == null || !mFile.equals(directory)) {
  332. mList.setSelectionFromTop(0, 0);
  333. }
  334. mFile = directory;
  335. }
  336. }
  337. public void sortByName(boolean descending) {
  338. mAdapter.setSortOrder(FileListListAdapter.SORT_NAME, descending);
  339. }
  340. public void sortByDate(boolean descending) {
  341. mAdapter.setSortOrder(FileListListAdapter.SORT_DATE, descending);
  342. }
  343. public void sortBySize(boolean descending) {
  344. mAdapter.setSortOrder(FileListListAdapter.SORT_SIZE, descending);
  345. }
  346. }