OCFileListFragment.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 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 java.util.List;
  22. import com.owncloud.android.AccountUtils;
  23. import com.owncloud.android.Log_OC;
  24. import com.owncloud.android.R;
  25. import com.owncloud.android.datamodel.DataStorageManager;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.files.FileHandler;
  28. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  29. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  30. import com.owncloud.android.operations.OnRemoteOperationListener;
  31. import com.owncloud.android.operations.RemoteOperation;
  32. import com.owncloud.android.operations.RemoveFileOperation;
  33. import com.owncloud.android.operations.RenameFileOperation;
  34. import com.owncloud.android.operations.SynchronizeFileOperation;
  35. import com.owncloud.android.ui.activity.FileDisplayActivity;
  36. import com.owncloud.android.ui.activity.TransferServiceGetter;
  37. import com.owncloud.android.ui.adapter.FileListListAdapter;
  38. import com.owncloud.android.ui.dialog.EditNameDialog;
  39. import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
  40. import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  41. import com.owncloud.android.ui.preview.PreviewImageFragment;
  42. import com.owncloud.android.ui.preview.PreviewMediaFragment;
  43. import android.accounts.Account;
  44. import android.app.Activity;
  45. import android.os.Bundle;
  46. import android.os.Handler;
  47. import android.view.ContextMenu;
  48. import android.view.MenuInflater;
  49. import android.view.MenuItem;
  50. import android.view.View;
  51. import android.widget.AdapterView;
  52. import android.widget.AdapterView.AdapterContextMenuInfo;
  53. /**
  54. * A Fragment that lists all files and folders in a given path.
  55. *
  56. * @author Bartek Przybylski
  57. *
  58. */
  59. public class OCFileListFragment extends ExtendedListFragment implements EditNameDialogListener, ConfirmationDialogFragmentListener {
  60. private static final String TAG = OCFileListFragment.class.getSimpleName();
  61. private static final String SAVED_LIST_POSITION = "LIST_POSITION";
  62. private OCFileListFragment.ContainerActivity mContainerActivity;
  63. private OCFile mFile = null;
  64. private FileListListAdapter mAdapter;
  65. private Handler mHandler;
  66. private OCFile mTargetFile;
  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 = (ContainerActivity) activity;
  76. } catch (ClassCastException e) {
  77. throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getSimpleName());
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. @Override
  84. public void onActivityCreated(Bundle savedInstanceState) {
  85. super.onActivityCreated(savedInstanceState);
  86. Log_OC.e(TAG, "onActivityCreated() start");
  87. mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity);
  88. setListAdapter(mAdapter);
  89. registerForContextMenu(getListView());
  90. getListView().setOnCreateContextMenuListener(this);
  91. mHandler = new Handler();
  92. }
  93. /**
  94. * Call this, when the user presses the up button
  95. */
  96. public void onBrowseUp() {
  97. OCFile parentDir = null;
  98. if(mFile != null){
  99. DataStorageManager storageManager = mContainerActivity.getStorageManager();
  100. parentDir = storageManager.getFileById(mFile.getParentId());
  101. mFile = parentDir;
  102. }
  103. listDirectory(parentDir);
  104. }
  105. @Override
  106. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  107. OCFile file = (OCFile) mAdapter.getItem(position);
  108. if (file != null) {
  109. if (file.isDirectory()) {
  110. // update state and view of this fragment
  111. mFile = file;
  112. listDirectory(file);
  113. // then, notify parent activity to let it update its state and view, and other fragments
  114. mContainerActivity.onBrowsedDownTo(file);
  115. } else { /// Click on a file
  116. if (PreviewImageFragment.canBePreviewed(file)) {
  117. // preview image - it handles the download, if needed
  118. mContainerActivity.startImagePreview(file);
  119. } else if (file.isDown()) {
  120. if (PreviewMediaFragment.canBePreviewed(file)) {
  121. // media preview
  122. mContainerActivity.startMediaPreview(file, 0, true);
  123. } else {
  124. // open with
  125. mContainerActivity.openFile(file);
  126. }
  127. } else {
  128. // automatic download, preview on finish
  129. mContainerActivity.startDownloadForPreview(file);
  130. }
  131. }
  132. } else {
  133. Log_OC.d(TAG, "Null object in ListAdapter!!");
  134. }
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. @Override
  140. public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  141. super.onCreateContextMenu(menu, v, menuInfo);
  142. MenuInflater inflater = getActivity().getMenuInflater();
  143. inflater.inflate(R.menu.file_actions_menu, menu);
  144. AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
  145. OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
  146. List<Integer> toHide = new ArrayList<Integer>();
  147. List<Integer> toDisable = new ArrayList<Integer>();
  148. MenuItem item = null;
  149. if (targetFile.isDirectory()) {
  150. // contextual menu for folders
  151. toHide.add(R.id.action_open_file_with);
  152. toHide.add(R.id.action_download_file);
  153. toHide.add(R.id.action_cancel_download);
  154. toHide.add(R.id.action_cancel_upload);
  155. toHide.add(R.id.action_sync_file);
  156. toHide.add(R.id.action_see_details);
  157. if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ||
  158. mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ) {
  159. toDisable.add(R.id.action_rename_file);
  160. toDisable.add(R.id.action_remove_file);
  161. }
  162. } else {
  163. // contextual menu for regular files
  164. if (targetFile.isDown()) {
  165. toHide.add(R.id.action_cancel_download);
  166. toHide.add(R.id.action_cancel_upload);
  167. toHide.add(R.id.action_download_file);
  168. } else {
  169. toHide.add(R.id.action_open_file_with);
  170. toHide.add(R.id.action_sync_file);
  171. }
  172. if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
  173. toHide.add(R.id.action_download_file);
  174. toHide.add(R.id.action_cancel_upload);
  175. toDisable.add(R.id.action_open_file_with);
  176. toDisable.add(R.id.action_rename_file);
  177. toDisable.add(R.id.action_remove_file);
  178. } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
  179. toHide.add(R.id.action_download_file);
  180. toHide.add(R.id.action_cancel_download);
  181. toDisable.add(R.id.action_open_file_with);
  182. toDisable.add(R.id.action_rename_file);
  183. toDisable.add(R.id.action_remove_file);
  184. } else {
  185. toHide.add(R.id.action_cancel_download);
  186. toHide.add(R.id.action_cancel_upload);
  187. }
  188. }
  189. for (int i : toHide) {
  190. item = menu.findItem(i);
  191. if (item != null) {
  192. item.setVisible(false);
  193. item.setEnabled(false);
  194. }
  195. }
  196. for (int i : toDisable) {
  197. item = menu.findItem(i);
  198. if (item != null) {
  199. item.setEnabled(false);
  200. }
  201. }
  202. }
  203. /**
  204. * {@inhericDoc}
  205. */
  206. @Override
  207. public boolean onContextItemSelected (MenuItem item) {
  208. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  209. mTargetFile = (OCFile) mAdapter.getItem(info.position);
  210. switch (item.getItemId()) {
  211. case R.id.action_rename_file: {
  212. String fileName = mTargetFile.getFileName();
  213. int extensionStart = mTargetFile.isDirectory() ? -1 : fileName.lastIndexOf(".");
  214. int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
  215. EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
  216. dialog.show(getFragmentManager(), EditNameDialog.TAG);
  217. return true;
  218. }
  219. case R.id.action_remove_file: {
  220. int messageStringId = R.string.confirmation_remove_alert;
  221. int posBtnStringId = R.string.confirmation_remove_remote;
  222. int neuBtnStringId = -1;
  223. if (mTargetFile.isDirectory()) {
  224. messageStringId = R.string.confirmation_remove_folder_alert;
  225. posBtnStringId = R.string.confirmation_remove_remote_and_local;
  226. neuBtnStringId = R.string.confirmation_remove_folder_local;
  227. } else if (mTargetFile.isDown()) {
  228. posBtnStringId = R.string.confirmation_remove_remote_and_local;
  229. neuBtnStringId = R.string.confirmation_remove_local;
  230. }
  231. ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
  232. messageStringId,
  233. new String[]{mTargetFile.getFileName()},
  234. posBtnStringId,
  235. neuBtnStringId,
  236. R.string.common_cancel);
  237. confDialog.setOnConfirmationListener(this);
  238. confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
  239. return true;
  240. }
  241. case R.id.action_open_file_with: {
  242. mContainerActivity.openFile(mTargetFile);
  243. return true;
  244. }
  245. case R.id.action_download_file:
  246. case R.id.action_sync_file: {
  247. Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
  248. RemoteOperation operation = new SynchronizeFileOperation(mTargetFile, null, mContainerActivity.getStorageManager(), account, true, false, getSherlockActivity());
  249. operation.execute(account, getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
  250. getSherlockActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
  251. return true;
  252. }
  253. case R.id.action_cancel_download: {
  254. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  255. Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
  256. if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
  257. downloaderBinder.cancel(account, mTargetFile);
  258. listDirectory();
  259. mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
  260. }
  261. return true;
  262. }
  263. case R.id.action_cancel_upload: {
  264. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  265. Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
  266. if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
  267. uploaderBinder.cancel(account, mTargetFile);
  268. listDirectory();
  269. mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
  270. }
  271. return true;
  272. }
  273. case R.id.action_see_details: {
  274. ((FileFragment.ContainerActivity)getActivity()).showDetails(mTargetFile);
  275. return true;
  276. }
  277. default:
  278. return super.onContextItemSelected(item);
  279. }
  280. }
  281. /**
  282. * Use this to query the {@link OCFile} that is currently
  283. * being displayed by this fragment
  284. * @return The currently viewed OCFile
  285. */
  286. public OCFile getCurrentFile(){
  287. return mFile;
  288. }
  289. /**
  290. * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
  291. */
  292. public void listDirectory(){
  293. listDirectory(null);
  294. }
  295. /**
  296. * Lists the given directory on the view. When the input parameter is null,
  297. * it will either refresh the last known directory. list the root
  298. * if there never was a directory.
  299. *
  300. * @param directory File to be listed
  301. */
  302. public void listDirectory(OCFile directory) {
  303. DataStorageManager storageManager = mContainerActivity.getStorageManager();
  304. if (storageManager != null) {
  305. // Check input parameters for null
  306. if(directory == null){
  307. if(mFile != null){
  308. directory = mFile;
  309. } else {
  310. directory = storageManager.getFileByPath("/");
  311. if (directory == null) return; // no files, wait for sync
  312. }
  313. }
  314. // If that's not a directory -> List its parent
  315. if(!directory.isDirectory()){
  316. Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
  317. directory = storageManager.getFileById(directory.getParentId());
  318. }
  319. mAdapter.swapDirectory(directory, storageManager);
  320. if (mFile == null || !mFile.equals(directory)) {
  321. mList.setSelectionFromTop(0, 0);
  322. }
  323. mFile = directory;
  324. }
  325. }
  326. /**
  327. * Interface to implement by any Activity that includes some instance of FileListFragment
  328. *
  329. * @author David A. Velasco
  330. */
  331. public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler {
  332. /**
  333. * Callback method invoked when a the user browsed into a different folder through the list of files
  334. *
  335. * @param file
  336. */
  337. public void onBrowsedDownTo(OCFile folder);
  338. public void startDownloadForPreview(OCFile file);
  339. public void startMediaPreview(OCFile file, int i, boolean b);
  340. public void startImagePreview(OCFile file);
  341. /**
  342. * Getter for the current DataStorageManager in the container activity
  343. */
  344. public DataStorageManager getStorageManager();
  345. /**
  346. * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
  347. *
  348. * @return Directory to list firstly. Can be NULL.
  349. */
  350. public OCFile getInitialDirectory();
  351. /**
  352. * Callback method invoked when a the 'transfer state' of a file changes.
  353. *
  354. * This happens when a download or upload is started or ended for a file.
  355. *
  356. * This method is necessary by now to update the user interface of the double-pane layout in tablets
  357. * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
  358. * won't provide the needed response before the method where this is called finishes.
  359. *
  360. * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
  361. *
  362. * @param file OCFile which state changed.
  363. * @param downloading Flag signaling if the file is now downloading.
  364. * @param uploading Flag signaling if the file is now uploading.
  365. */
  366. public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
  367. }
  368. @Override
  369. public void onDismiss(EditNameDialog dialog) {
  370. if (dialog.getResult()) {
  371. String newFilename = dialog.getNewFilename();
  372. Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
  373. RemoteOperation operation = new RenameFileOperation(mTargetFile,
  374. AccountUtils.getCurrentOwnCloudAccount(getActivity()),
  375. newFilename,
  376. mContainerActivity.getStorageManager());
  377. operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
  378. getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
  379. }
  380. }
  381. @Override
  382. public void onConfirmation(String callerTag) {
  383. if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
  384. if (mContainerActivity.getStorageManager().getFileById(mTargetFile.getFileId()) != null) {
  385. RemoteOperation operation = new RemoveFileOperation( mTargetFile,
  386. true,
  387. mContainerActivity.getStorageManager());
  388. operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
  389. getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
  390. }
  391. }
  392. }
  393. @Override
  394. public void onNeutral(String callerTag) {
  395. File f = null;
  396. if (mTargetFile.isDirectory()) {
  397. // TODO run in a secondary thread?
  398. mContainerActivity.getStorageManager().removeDirectory(mTargetFile, false, true);
  399. } else if (mTargetFile.isDown() && (f = new File(mTargetFile.getStoragePath())).exists()) {
  400. f.delete();
  401. mTargetFile.setStoragePath(null);
  402. mContainerActivity.getStorageManager().saveFile(mTargetFile);
  403. }
  404. listDirectory();
  405. mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
  406. }
  407. @Override
  408. public void onCancel(String callerTag) {
  409. Log_OC.d(TAG, "REMOVAL CANCELED");
  410. }
  411. }