OCFileListFragment.java 16 KB

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