OCFileListFragment.java 16 KB

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