FileDetailFragment.java 22 KB

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