OCFileListFragment.java 17 KB

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