OCFileListFragment.java 17 KB

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