OCFileListFragment.java 22 KB

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