2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.ui
.fragment
;
24 import android
.accounts
.Account
;
25 import android
.graphics
.Bitmap
;
26 import android
.os
.Bundle
;
27 import android
.view
.LayoutInflater
;
28 import android
.view
.Menu
;
29 import android
.view
.MenuInflater
;
30 import android
.view
.MenuItem
;
31 import android
.view
.View
;
32 import android
.view
.View
.OnClickListener
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.CheckBox
;
35 import android
.widget
.ImageView
;
36 import android
.widget
.ProgressBar
;
37 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
;
56 import java
.lang
.ref
.WeakReference
;
60 * This Fragment is used to display the details about a file.
62 public class FileDetailFragment
extends FileFragment
implements OnClickListener
{
66 private Account mAccount
;
68 public ProgressListener mProgressListener
;
70 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
71 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
72 public static final String FTAG_RENAME_FILE
= "RENAME_FILE_FRAGMENT";
74 private static final String ARG_FILE
= "FILE";
75 private static final String ARG_ACCOUNT
= "ACCOUNT";
79 * Public factory method to create new FileDetailFragment instances.
81 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
83 * @param fileToDetail An {@link OCFile} to show in the fragment
84 * @param account An ownCloud account; needed to start downloads
85 * @return New fragment with arguments set
87 public static FileDetailFragment
newInstance(OCFile fileToDetail
, Account account
) {
88 FileDetailFragment frag
= new FileDetailFragment();
89 Bundle args
= new Bundle();
90 args
.putParcelable(ARG_FILE
, fileToDetail
);
91 args
.putParcelable(ARG_ACCOUNT
, account
);
92 frag
.setArguments(args
);
97 * Creates an empty details fragment.
99 * It's necessary to keep a public constructor without parameters; the system uses it when tries
100 * to reinstantiate a fragment automatically.
102 public FileDetailFragment() {
105 mLayout
= R
.layout
.file_details_empty
;
106 mProgressListener
= null
;
111 public void onActivityCreated(Bundle savedInstanceState
) {
112 super.onCreate(savedInstanceState
);
113 setHasOptionsMenu(true
);
118 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
119 Bundle savedInstanceState
) {
121 setFile((OCFile
) getArguments().getParcelable(ARG_FILE
));
122 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
124 if (savedInstanceState
!= null
) {
125 setFile((OCFile
) savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
126 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
129 if (getFile() != null
&& mAccount
!= null
) {
130 mLayout
= R
.layout
.file_details_fragment
;
133 mView
= inflater
.inflate(mLayout
, null
);
135 if (mLayout
== R
.layout
.file_details_fragment
) {
136 mView
.findViewById(R
.id
.fdFavorite
).setOnClickListener(this);
137 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
138 DisplayUtils
.colorPreLollipopHorizontalProgressBar(progressBar
);
139 mProgressListener
= new ProgressListener(progressBar
);
140 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
143 updateFileDetails(false
, false
);
148 public void onSaveInstanceState(Bundle outState
) {
149 super.onSaveInstanceState(outState
);
150 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
151 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
155 public void onStart() {
157 listenForTransferProgress();
161 public void onStop() {
162 leaveTransferProgress();
168 public View
getView() {
169 return super.getView() == null ? mView
: super.getView();
177 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
178 super.onCreateOptionsMenu(menu
, inflater
);
179 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
187 public void onPrepareOptionsMenu(Menu menu
) {
188 super.onPrepareOptionsMenu(menu
);
190 if (mContainerActivity
.getStorageManager() != null
) {
191 FileMenuFilter mf
= new FileMenuFilter(
193 mContainerActivity
.getStorageManager().getAccount(),
200 // additional restriction for this fragment
201 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
203 item
.setVisible(false
);
204 item
.setEnabled(false
);
207 // additional restriction for this fragment
208 item
= menu
.findItem(R
.id
.action_move
);
210 item
.setVisible(false
);
211 item
.setEnabled(false
);
214 // additional restriction for this fragment
215 item
= menu
.findItem(R
.id
.action_copy
);
217 item
.setVisible(false
);
218 item
.setEnabled(false
);
221 item
= menu
.findItem(R
.id
.action_switch_view
);
223 item
.setVisible(false
);
224 item
.setEnabled(false
);
227 item
= menu
.findItem(R
.id
.action_sync_account
);
229 item
.setVisible(false
);
230 item
.setEnabled(false
);
233 item
= menu
.findItem(R
.id
.action_sort
);
235 item
.setVisible(false
);
236 item
.setEnabled(false
);
245 public boolean onOptionsItemSelected(MenuItem item
) {
246 switch (item
.getItemId()) {
247 case R
.id
.action_share_file
: {
248 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
251 case R
.id
.action_share_with_users
: {
252 mContainerActivity
.getFileOperationsHelper().showShareFile(getFile());
255 case R
.id
.action_unshare_file
: {
256 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
260 case R
.id
.action_open_file_with
: {
261 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
264 case R
.id
.action_remove_file
: {
265 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
266 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
269 case R
.id
.action_rename_file
: {
270 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
271 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
274 case R
.id
.action_cancel_sync
: {
275 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
278 case R
.id
.action_download_file
:
279 case R
.id
.action_sync_file
: {
280 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
283 case R
.id
.action_send_file
: {
285 if (!getFile().isDown()) { // Download the file
286 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
287 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(getFile());
290 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
294 case R
.id
.action_favorite_file
:{
295 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
298 case R
.id
.action_unfavorite_file
:{
299 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
308 public void onClick(View v
) {
310 case R
.id
.fdFavorite
: {
311 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdFavorite
);
312 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(),cb
.isChecked());
315 case R
.id
.fdCancelBtn
: {
316 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(getFile());
320 Log_OC
.e(TAG
, "Incorrect view clicked!");
325 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
327 * @return True when the fragment was created with the empty layout.
329 public boolean isEmpty() {
330 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
335 * Use this method to signal this Activity that it shall update its view.
337 * @param file : An {@link OCFile}
339 public void updateFileDetails(OCFile file
, Account ocAccount
) {
341 mAccount
= ocAccount
;
342 updateFileDetails(false
, false
);
346 * Updates the view with all relevant details about that file.
348 * TODO Remove parameter when the transferring state of files is kept in database.
350 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
351 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
352 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
353 * @param refresh If 'true', try to refresh the whole file from the database
355 public void updateFileDetails(boolean transferring
, boolean refresh
) {
357 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
358 if (refresh
&& storageManager
!= null
) {
359 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
361 OCFile file
= getFile();
364 setFilename(file
.getFileName());
366 setFilesize(file
.getFileLength());
368 setTimeModified(file
.getModificationTimestamp());
370 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdFavorite
);
371 cb
.setChecked(file
.isFavorite());
373 // configure UI for depending upon local state of the file
374 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
375 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
377 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
378 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
380 setButtonsForTransferring();
382 } else if (file
.isDown()) {
387 // TODO load default preview image; when the local file is removed, the preview
389 setButtonsForRemote();
392 getView().invalidate();
396 * Checks if the fragment is ready to show details of a OCFile
398 * @return 'True' when the fragment is ready to show details of a file
400 private boolean readyToShow() {
401 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
406 * Updates the filename in view
408 * @param filename to set
410 private void setFilename(String filename
) {
411 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
413 tv
.setText(filename
);
418 * Updates the MIME type in view
419 * @param file : An {@link OCFile}
421 private void setFiletype(OCFile file
) {
422 String mimetype
= file
.getMimetype();
423 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
425 // mimetype MIME type to set
426 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);
427 tv
.setText(printableMimetype
);
430 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
434 iv
.setTag(file
.getFileId());
436 if (file
.isImage()) {
437 String tagId
= String
.valueOf(file
.getRemoteId());
438 thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(tagId
);
440 if (thumbnail
!= null
&& !file
.needsUpdateThumbnail()) {
441 iv
.setImageBitmap(thumbnail
);
443 // generate new Thumbnail
444 if (ThumbnailsCacheManager
.cancelPotentialWork(file
, iv
)) {
445 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
446 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
447 iv
, mContainerActivity
.getStorageManager(), mAccount
449 if (thumbnail
== null
) {
450 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
452 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
453 new ThumbnailsCacheManager
.AsyncDrawable(
454 MainApp
.getAppContext().getResources(),
458 iv
.setImageDrawable(asyncDrawable
);
463 // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
464 String filename
= file
.getFileName();
465 iv
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mimetype
, filename
));
471 * Updates the file size in view
473 * @param filesize in bytes to set
475 private void setFilesize(long filesize
) {
476 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
478 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
483 * Updates the time that the file was last modified
485 * @param milliseconds Unix time to set
487 private void setTimeModified(long milliseconds
) {
488 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
490 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
495 * Enables or disables buttons for a file being downloaded
497 private void setButtonsForTransferring() {
499 // let's protect the user from himself ;)
500 getView().findViewById(R
.id
.fdFavorite
).setEnabled(false
);
502 // show the progress bar for the transfer
503 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
504 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
505 progressText
.setVisibility(View
.VISIBLE
);
506 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
507 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
508 //if (getFile().isDownloading()) {
509 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
510 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
513 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
514 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
521 * Enables or disables buttons for a file locally available
523 private void setButtonsForDown() {
525 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
527 // hides the progress bar
528 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
529 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
530 progressText
.setVisibility(View
.GONE
);
535 * Enables or disables buttons for a file not locally available
537 private void setButtonsForRemote() {
539 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
541 // hides the progress bar
542 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
543 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
544 progressText
.setVisibility(View
.GONE
);
549 public void listenForTransferProgress() {
550 if (mProgressListener
!= null
) {
551 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
552 mContainerActivity
.getFileDownloaderBinder().
553 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
555 if (mContainerActivity
.getFileUploaderBinder() != null
) {
556 mContainerActivity
.getFileUploaderBinder().
557 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
563 public void leaveTransferProgress() {
564 if (mProgressListener
!= null
) {
565 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
566 mContainerActivity
.getFileDownloaderBinder().
567 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
569 if (mContainerActivity
.getFileUploaderBinder() != null
) {
570 mContainerActivity
.getFileUploaderBinder().
571 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
578 * Helper class responsible for updating the progress bar shown for file uploading or
581 private class ProgressListener
implements OnDatatransferProgressListener
{
582 int mLastPercent
= 0;
583 WeakReference
<ProgressBar
> mProgressBar
= null
;
585 ProgressListener(ProgressBar progressBar
) {
586 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
590 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
591 long totalToTransfer
, String filename
) {
592 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
593 if (percent
!= mLastPercent
) {
594 ProgressBar pb
= mProgressBar
.get();
596 pb
.setProgress(percent
);
600 mLastPercent
= percent
;