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
.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
;
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
.files
.FileMenuFilter
;
42 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
43 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
44 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
45 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
46 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
47 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
48 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
49 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
50 import com
.owncloud
.android
.utils
.DisplayUtils
;
51 import com
.owncloud
.android
.utils
.MimetypeIconUtil
;
53 import java
.lang
.ref
.WeakReference
;
57 * This Fragment is used to display the details about a file.
59 public class FileDetailFragment
extends FileFragment
implements OnClickListener
{
63 private Account mAccount
;
65 public ProgressListener mProgressListener
;
67 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
68 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
69 public static final String FTAG_RENAME_FILE
= "RENAME_FILE_FRAGMENT";
71 private static final String ARG_FILE
= "FILE";
72 private static final String ARG_ACCOUNT
= "ACCOUNT";
76 * Public factory method to create new FileDetailFragment instances.
78 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
80 * @param fileToDetail An {@link OCFile} to show in the fragment
81 * @param account An ownCloud account; needed to start downloads
82 * @return New fragment with arguments set
84 public static FileDetailFragment
newInstance(OCFile fileToDetail
, Account account
) {
85 FileDetailFragment frag
= new FileDetailFragment();
86 Bundle args
= new Bundle();
87 args
.putParcelable(ARG_FILE
, fileToDetail
);
88 args
.putParcelable(ARG_ACCOUNT
, account
);
89 frag
.setArguments(args
);
94 * Creates an empty details fragment.
96 * It's necessary to keep a public constructor without parameters; the system uses it when tries
97 * to reinstantiate a fragment automatically.
99 public FileDetailFragment() {
102 mLayout
= R
.layout
.file_details_empty
;
103 mProgressListener
= null
;
108 public void onActivityCreated(Bundle savedInstanceState
) {
109 super.onCreate(savedInstanceState
);
110 setHasOptionsMenu(true
);
115 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
116 Bundle savedInstanceState
) {
118 setFile((OCFile
) getArguments().getParcelable(ARG_FILE
));
119 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
121 if (savedInstanceState
!= null
) {
122 setFile((OCFile
) savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
123 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
126 if (getFile() != null
&& mAccount
!= null
) {
127 mLayout
= R
.layout
.file_details_fragment
;
130 mView
= inflater
.inflate(mLayout
, null
);
132 if (mLayout
== R
.layout
.file_details_fragment
) {
133 mView
.findViewById(R
.id
.fdFavorite
).setOnClickListener(this);
134 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
135 DisplayUtils
.colorPreLollipopHorizontalProgressBar(progressBar
);
136 mProgressListener
= new ProgressListener(progressBar
);
137 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
140 updateFileDetails(false
, false
);
145 public void onSaveInstanceState(Bundle outState
) {
146 super.onSaveInstanceState(outState
);
147 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
148 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
152 public void onStart() {
154 listenForTransferProgress();
158 public void onStop() {
159 leaveTransferProgress();
165 public View
getView() {
166 return super.getView() == null ? mView
: super.getView();
174 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
175 super.onCreateOptionsMenu(menu
, inflater
);
176 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
184 public void onPrepareOptionsMenu(Menu menu
) {
185 super.onPrepareOptionsMenu(menu
);
187 if (mContainerActivity
.getStorageManager() != null
) {
188 FileMenuFilter mf
= new FileMenuFilter(
190 mContainerActivity
.getStorageManager().getAccount(),
197 // additional restriction for this fragment
198 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
200 item
.setVisible(false
);
201 item
.setEnabled(false
);
204 // additional restriction for this fragment
205 item
= menu
.findItem(R
.id
.action_move
);
207 item
.setVisible(false
);
208 item
.setEnabled(false
);
211 // additional restriction for this fragment
212 item
= menu
.findItem(R
.id
.action_copy
);
214 item
.setVisible(false
);
215 item
.setEnabled(false
);
224 public boolean onOptionsItemSelected(MenuItem item
) {
225 switch (item
.getItemId()) {
226 case R
.id
.action_share_file
: {
227 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
230 case R
.id
.action_unshare_file
: {
231 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
234 case R
.id
.action_open_file_with
: {
235 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
238 case R
.id
.action_remove_file
: {
239 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
240 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
243 case R
.id
.action_rename_file
: {
244 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
245 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
248 case R
.id
.action_cancel_download
:
249 case R
.id
.action_cancel_upload
: {
250 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(getFile());
253 case R
.id
.action_download_file
:
254 case R
.id
.action_sync_file
: {
255 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
258 case R
.id
.action_send_file
: {
260 if (!getFile().isDown()) { // Download the file
261 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
262 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(getFile());
266 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
270 case R
.id
.action_favorite_file
:{
271 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
274 case R
.id
.action_unfavorite_file
:{
275 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
284 public void onClick(View v
) {
286 case R
.id
.fdFavorite
: {
287 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdFavorite
);
288 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(),cb
.isChecked());
291 case R
.id
.fdCancelBtn
: {
292 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(getFile());
296 Log_OC
.e(TAG
, "Incorrect view clicked!");
302 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
304 * @return True when the fragment was created with the empty layout.
306 public boolean isEmpty() {
307 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
312 * Use this method to signal this Activity that it shall update its view.
314 * @param file : An {@link OCFile}
316 public void updateFileDetails(OCFile file
, Account ocAccount
) {
318 mAccount
= ocAccount
;
319 updateFileDetails(false
, false
);
323 * Updates the view with all relevant details about that file.
325 * TODO Remove parameter when the transferring state of files is kept in database.
327 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
328 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
329 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
330 * @param refresh If 'true', try to refresh the whole file from the database
332 public void updateFileDetails(boolean transferring
, boolean refresh
) {
334 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
335 if (refresh
&& storageManager
!= null
) {
336 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
338 OCFile file
= getFile();
341 setFilename(file
.getFileName());
342 setFiletype(file
.getMimetype(), file
.getFileName());
343 setFilesize(file
.getFileLength());
345 setTimeModified(file
.getModificationTimestamp());
347 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdFavorite
);
348 cb
.setChecked(file
.isFavorite());
350 // configure UI for depending upon local state of the file
351 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
352 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
354 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
355 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
357 setButtonsForTransferring();
359 } else if (file
.isDown()) {
364 // TODO load default preview image; when the local file is removed, the preview
366 setButtonsForRemote();
369 getView().invalidate();
373 * Checks if the fragment is ready to show details of a OCFile
375 * @return 'True' when the fragment is ready to show details of a file
377 private boolean readyToShow() {
378 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
383 * Updates the filename in view
385 * @param filename to set
387 private void setFilename(String filename
) {
388 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
390 tv
.setText(filename
);
395 * Updates the MIME type in view
396 * @param mimetype MIME type to set
397 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
399 private void setFiletype(String mimetype
, String filename
) {
400 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
402 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);
403 tv
.setText(printableMimetype
);
405 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
407 iv
.setImageResource(MimetypeIconUtil
.getFileTypeIconId(mimetype
, filename
));
412 * Updates the file size in view
414 * @param filesize in bytes to set
416 private void setFilesize(long filesize
) {
417 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
419 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
424 * Updates the time that the file was last modified
426 * @param milliseconds Unix time to set
428 private void setTimeModified(long milliseconds
) {
429 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
431 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
436 * Enables or disables buttons for a file being downloaded
438 private void setButtonsForTransferring() {
440 // let's protect the user from himself ;)
441 getView().findViewById(R
.id
.fdFavorite
).setEnabled(false
);
443 // show the progress bar for the transfer
444 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
445 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
446 progressText
.setVisibility(View
.VISIBLE
);
447 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
448 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
449 //if (getFile().isDownloading()) {
450 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
451 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
454 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
455 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
462 * Enables or disables buttons for a file locally available
464 private void setButtonsForDown() {
466 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
468 // hides the progress bar
469 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
470 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
471 progressText
.setVisibility(View
.GONE
);
476 * Enables or disables buttons for a file not locally available
478 private void setButtonsForRemote() {
480 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
482 // hides the progress bar
483 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
484 TextView progressText
= (TextView
) getView().findViewById(R
.id
.fdProgressText
);
485 progressText
.setVisibility(View
.GONE
);
490 public void listenForTransferProgress() {
491 if (mProgressListener
!= null
) {
492 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
493 mContainerActivity
.getFileDownloaderBinder().
494 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
496 if (mContainerActivity
.getFileUploaderBinder() != null
) {
497 mContainerActivity
.getFileUploaderBinder().
498 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
504 public void leaveTransferProgress() {
505 if (mProgressListener
!= null
) {
506 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
507 mContainerActivity
.getFileDownloaderBinder().
508 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
510 if (mContainerActivity
.getFileUploaderBinder() != null
) {
511 mContainerActivity
.getFileUploaderBinder().
512 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
519 * Helper class responsible for updating the progress bar shown for file uploading or
522 private class ProgressListener
implements OnDatatransferProgressListener
{
523 int mLastPercent
= 0;
524 WeakReference
<ProgressBar
> mProgressBar
= null
;
526 ProgressListener(ProgressBar progressBar
) {
527 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
531 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
532 long totalToTransfer
, String filename
) {
533 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
534 if (percent
!= mLastPercent
) {
535 ProgressBar pb
= mProgressBar
.get();
537 pb
.setProgress(percent
);
541 mLastPercent
= percent
;