OCFileListFragment.java 17 KB

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