FileDetailFragment.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2011 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.ui.fragment;
  23. import android.accounts.Account;
  24. import android.graphics.Bitmap;
  25. import android.os.Bundle;
  26. import android.view.LayoutInflater;
  27. import android.view.Menu;
  28. import android.view.MenuInflater;
  29. import android.view.MenuItem;
  30. import android.view.View;
  31. import android.view.View.OnClickListener;
  32. import android.view.ViewGroup;
  33. import android.widget.CheckBox;
  34. import android.widget.ImageView;
  35. import android.widget.ProgressBar;
  36. import android.widget.TextView;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.datamodel.FileDataStorageManager;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  42. import com.owncloud.android.files.FileMenuFilter;
  43. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  44. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  45. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.ui.activity.FileActivity;
  48. import com.owncloud.android.ui.activity.FileDisplayActivity;
  49. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  50. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  51. import com.owncloud.android.utils.DisplayUtils;
  52. import com.owncloud.android.utils.MimeTypeUtil;
  53. import java.lang.ref.WeakReference;
  54. /**
  55. * This Fragment is used to display the details about a file.
  56. */
  57. public class FileDetailFragment extends FileFragment implements OnClickListener {
  58. private int mLayout;
  59. private View mView;
  60. private Account mAccount;
  61. public ProgressListener mProgressListener;
  62. private static final String TAG = FileDetailFragment.class.getSimpleName();
  63. public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  64. public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  65. private static final String ARG_FILE = "FILE";
  66. private static final String ARG_ACCOUNT = "ACCOUNT";
  67. private static final String SCREEN_NAME = "File details";
  68. /**
  69. * Public factory method to create new FileDetailFragment instances.
  70. *
  71. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  72. *
  73. * @param fileToDetail An {@link OCFile} to show in the fragment
  74. * @param account An ownCloud account; needed to start downloads
  75. * @return New fragment with arguments set
  76. */
  77. public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
  78. FileDetailFragment frag = new FileDetailFragment();
  79. Bundle args = new Bundle();
  80. args.putParcelable(ARG_FILE, fileToDetail);
  81. args.putParcelable(ARG_ACCOUNT, account);
  82. frag.setArguments(args);
  83. return frag;
  84. }
  85. /**
  86. * Creates an empty details fragment.
  87. *
  88. * It's necessary to keep a public constructor without parameters; the system uses it when tries
  89. * to reinstantiate a fragment automatically.
  90. */
  91. public FileDetailFragment() {
  92. super();
  93. mAccount = null;
  94. mLayout = R.layout.file_details_empty;
  95. mProgressListener = null;
  96. }
  97. @Override
  98. public void onActivityCreated(Bundle savedInstanceState) {
  99. super.onCreate(savedInstanceState);
  100. setHasOptionsMenu(true);
  101. }
  102. @Override
  103. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  104. Bundle savedInstanceState) {
  105. setFile((OCFile) getArguments().getParcelable(ARG_FILE));
  106. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  107. if (savedInstanceState != null) {
  108. setFile((OCFile) savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
  109. mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  110. }
  111. if (getFile() != null && mAccount != null) {
  112. mLayout = R.layout.file_details_fragment;
  113. }
  114. mView = inflater.inflate(mLayout, null);
  115. if (mLayout == R.layout.file_details_fragment) {
  116. mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
  117. ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
  118. DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
  119. mProgressListener = new ProgressListener(progressBar);
  120. mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
  121. }
  122. updateFileDetails(false, false);
  123. return mView;
  124. }
  125. @Override
  126. public void onSaveInstanceState(Bundle outState) {
  127. super.onSaveInstanceState(outState);
  128. outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
  129. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  130. }
  131. @Override
  132. public void onStart() {
  133. super.onStart();
  134. listenForTransferProgress();
  135. }
  136. @Override
  137. public void onStop() {
  138. leaveTransferProgress();
  139. super.onStop();
  140. }
  141. @Override
  142. public View getView() {
  143. return super.getView() == null ? mView : super.getView();
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. @Override
  149. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  150. super.onCreateOptionsMenu(menu, inflater);
  151. inflater.inflate(R.menu.file_actions_menu, menu);
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. @Override
  157. public void onPrepareOptionsMenu(Menu menu) {
  158. super.onPrepareOptionsMenu(menu);
  159. if (mContainerActivity.getStorageManager() != null) {
  160. FileMenuFilter mf = new FileMenuFilter(
  161. getFile(),
  162. mContainerActivity.getStorageManager().getAccount(),
  163. mContainerActivity,
  164. getActivity()
  165. );
  166. mf.filter(menu);
  167. }
  168. // additional restriction for this fragment
  169. MenuItem item = menu.findItem(R.id.action_see_details);
  170. if (item != null) {
  171. item.setVisible(false);
  172. item.setEnabled(false);
  173. }
  174. // additional restriction for this fragment
  175. item = menu.findItem(R.id.action_move);
  176. if (item != null) {
  177. item.setVisible(false);
  178. item.setEnabled(false);
  179. }
  180. // additional restriction for this fragment
  181. item = menu.findItem(R.id.action_copy);
  182. if (item != null) {
  183. item.setVisible(false);
  184. item.setEnabled(false);
  185. }
  186. Boolean dualPane = getResources().getBoolean(R.bool.large_land_layout);
  187. item = menu.findItem(R.id.action_switch_view);
  188. if (item != null && !dualPane){
  189. item.setVisible(false);
  190. item.setEnabled(false);
  191. }
  192. item = menu.findItem(R.id.action_sync_account);
  193. if (item != null && !dualPane) {
  194. item.setVisible(false);
  195. item.setEnabled(false);
  196. }
  197. item = menu.findItem(R.id.action_sort);
  198. if (item != null && !dualPane) {
  199. item.setVisible(false);
  200. item.setEnabled(false);
  201. }
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. @Override
  207. public boolean onOptionsItemSelected(MenuItem item) {
  208. switch (item.getItemId()) {
  209. case R.id.action_share_file: {
  210. mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
  211. return true;
  212. }
  213. case R.id.action_open_file_with: {
  214. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  215. return true;
  216. }
  217. case R.id.action_remove_file: {
  218. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  219. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  220. return true;
  221. }
  222. case R.id.action_rename_file: {
  223. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  224. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  225. return true;
  226. }
  227. case R.id.action_cancel_sync: {
  228. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  229. return true;
  230. }
  231. case R.id.action_download_file:
  232. case R.id.action_sync_file: {
  233. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  234. return true;
  235. }
  236. case R.id.action_send_file: {
  237. // Obtain the file
  238. if (!getFile().isDown()) { // Download the file
  239. Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
  240. ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
  241. }
  242. else {
  243. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  244. }
  245. return true;
  246. }
  247. case R.id.action_favorite_file:{
  248. mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
  249. return true;
  250. }
  251. case R.id.action_unfavorite_file:{
  252. mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
  253. return true;
  254. }
  255. default:
  256. return super.onOptionsItemSelected(item);
  257. }
  258. }
  259. @Override
  260. public void onClick(View v) {
  261. switch (v.getId()) {
  262. case R.id.fdFavorite: {
  263. CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
  264. mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
  265. break;
  266. }
  267. case R.id.fdCancelBtn: {
  268. ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
  269. break;
  270. }
  271. default:
  272. Log_OC.e(TAG, "Incorrect view clicked!");
  273. }
  274. }
  275. /**
  276. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  277. *
  278. * @return True when the fragment was created with the empty layout.
  279. */
  280. public boolean isEmpty() {
  281. return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
  282. }
  283. /**
  284. * Use this method to signal this Activity that it shall update its view.
  285. *
  286. * @param file : An {@link OCFile}
  287. */
  288. public void updateFileDetails(OCFile file, Account ocAccount) {
  289. setFile(file);
  290. mAccount = ocAccount;
  291. updateFileDetails(false, false);
  292. }
  293. /**
  294. * Updates the view with all relevant details about that file.
  295. * <p/>
  296. * TODO Remove parameter when the transferring state of files is kept in database.
  297. *
  298. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  299. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  300. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  301. * @param refresh If 'true', try to refresh the whole file from the database
  302. */
  303. public void updateFileDetails(boolean transferring, boolean refresh) {
  304. if (readyToShow()) {
  305. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  306. if (refresh && storageManager != null) {
  307. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  308. }
  309. OCFile file = getFile();
  310. // set file details
  311. setFilename(file.getFileName());
  312. setFiletype(file);
  313. setFilesize(file.getFileLength());
  314. setTimeModified(file.getModificationTimestamp());
  315. CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
  316. cb.setChecked(file.isFavorite());
  317. // configure UI for depending upon local state of the file
  318. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  319. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  320. if (transferring ||
  321. (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
  322. (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
  323. ) {
  324. setButtonsForTransferring();
  325. } else if (file.isDown()) {
  326. setButtonsForDown();
  327. } else {
  328. // TODO load default preview image; when the local file is removed, the preview
  329. // remains there
  330. setButtonsForRemote();
  331. }
  332. }
  333. getView().invalidate();
  334. }
  335. /**
  336. * Checks if the fragment is ready to show details of a OCFile
  337. *
  338. * @return 'True' when the fragment is ready to show details of a file
  339. */
  340. private boolean readyToShow() {
  341. return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
  342. }
  343. /**
  344. * Updates the filename in view
  345. *
  346. * @param filename to set
  347. */
  348. private void setFilename(String filename) {
  349. TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
  350. if (tv != null) {
  351. tv.setText(filename);
  352. }
  353. }
  354. /**
  355. * Updates the MIME type in view
  356. * @param file : An {@link OCFile}
  357. */
  358. private void setFiletype(OCFile file) {
  359. String mimetype = file.getMimetype();
  360. TextView tv = (TextView) getView().findViewById(R.id.fdType);
  361. if (tv != null) {
  362. // mimetype MIME type to set
  363. String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
  364. tv.setText(printableMimetype);
  365. }
  366. ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
  367. if (iv != null) {
  368. Bitmap thumbnail;
  369. iv.setTag(file.getFileId());
  370. if (MimeTypeUtil.isImage(file)) {
  371. String tagId = String.valueOf(file.getRemoteId());
  372. thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  373. if (thumbnail != null && !file.needsUpdateThumbnail()) {
  374. iv.setImageBitmap(thumbnail);
  375. } else {
  376. // generate new Thumbnail
  377. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, iv)) {
  378. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  379. new ThumbnailsCacheManager.ThumbnailGenerationTask(
  380. iv, mContainerActivity.getStorageManager(), mAccount
  381. );
  382. if (thumbnail == null) {
  383. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  384. }
  385. final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
  386. new ThumbnailsCacheManager.AsyncThumbnailDrawable(
  387. MainApp.getAppContext().getResources(),
  388. thumbnail,
  389. task
  390. );
  391. iv.setImageDrawable(asyncDrawable);
  392. task.execute(file);
  393. }
  394. }
  395. } else {
  396. // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
  397. String filename = file.getFileName();
  398. iv.setImageResource(MimeTypeUtil.getFileTypeIconId(mimetype, filename));
  399. }
  400. }
  401. }
  402. /**
  403. * Updates the file size in view
  404. *
  405. * @param filesize in bytes to set
  406. */
  407. private void setFilesize(long filesize) {
  408. TextView tv = (TextView) getView().findViewById(R.id.fdSize);
  409. if (tv != null) {
  410. tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
  411. }
  412. }
  413. /**
  414. * Updates the time that the file was last modified
  415. *
  416. * @param milliseconds Unix time to set
  417. */
  418. private void setTimeModified(long milliseconds) {
  419. TextView tv = (TextView) getView().findViewById(R.id.fdModified);
  420. if (tv != null) {
  421. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  422. }
  423. }
  424. /**
  425. * Enables or disables buttons for a file being downloaded
  426. */
  427. private void setButtonsForTransferring() {
  428. if (!isEmpty()) {
  429. // let's protect the user from himself ;)
  430. getView().findViewById(R.id.fdFavorite).setEnabled(false);
  431. // show the progress bar for the transfer
  432. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
  433. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  434. progressText.setVisibility(View.VISIBLE);
  435. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  436. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  437. //if (getFile().isDownloading()) {
  438. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
  439. progressText.setText(R.string.downloader_download_in_progress_ticker);
  440. }
  441. else {
  442. if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
  443. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * Enables or disables buttons for a file locally available
  450. */
  451. private void setButtonsForDown() {
  452. if (!isEmpty()) {
  453. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  454. // hides the progress bar
  455. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  456. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  457. progressText.setVisibility(View.GONE);
  458. }
  459. }
  460. /**
  461. * Enables or disables buttons for a file not locally available
  462. */
  463. private void setButtonsForRemote() {
  464. if (!isEmpty()) {
  465. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  466. // hides the progress bar
  467. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  468. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  469. progressText.setVisibility(View.GONE);
  470. }
  471. }
  472. public void listenForTransferProgress() {
  473. if (mProgressListener != null) {
  474. if (mContainerActivity.getFileDownloaderBinder() != null) {
  475. mContainerActivity.getFileDownloaderBinder().
  476. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  477. }
  478. if (mContainerActivity.getFileUploaderBinder() != null) {
  479. mContainerActivity.getFileUploaderBinder().
  480. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  481. }
  482. } else {
  483. Log_OC.d(TAG, "mProgressListener == null");
  484. }
  485. }
  486. public void leaveTransferProgress() {
  487. if (mProgressListener != null) {
  488. if (mContainerActivity.getFileDownloaderBinder() != null) {
  489. mContainerActivity.getFileDownloaderBinder().
  490. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  491. }
  492. if (mContainerActivity.getFileUploaderBinder() != null) {
  493. mContainerActivity.getFileUploaderBinder().
  494. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  495. }
  496. }
  497. }
  498. /**
  499. * Helper class responsible for updating the progress bar shown for file uploading or
  500. * downloading
  501. */
  502. private class ProgressListener implements OnDatatransferProgressListener {
  503. int mLastPercent = 0;
  504. WeakReference<ProgressBar> mProgressBar = null;
  505. ProgressListener(ProgressBar progressBar) {
  506. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  507. }
  508. @Override
  509. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  510. long totalToTransfer, String filename) {
  511. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  512. if (percent != mLastPercent) {
  513. ProgressBar pb = mProgressBar.get();
  514. if (pb != null) {
  515. pb.setProgress(percent);
  516. pb.postInvalidate();
  517. }
  518. }
  519. mLastPercent = percent;
  520. }
  521. }
  522. }