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 java
.lang
.ref
.WeakReference
;
26 import android
.accounts
.Account
;
27 import android
.content
.Intent
;
28 import android
.os
.Bundle
;
29 import android
.view
.LayoutInflater
;
30 import android
.view
.Menu
;
31 import android
.view
.MenuInflater
;
32 import android
.view
.MenuItem
;
33 import android
.view
.View
;
34 import android
.view
.View
.OnClickListener
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.CheckBox
;
37 import android
.widget
.ImageView
;
38 import android
.widget
.ProgressBar
;
39 import android
.widget
.TextView
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
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
.services
.observer
.FileObserverService
;
50 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
51 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
52 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
53 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
54 import com
.owncloud
.android
.utils
.DisplayUtils
;
58 * This Fragment is used to display the details about a file.
60 public class FileDetailFragment
extends FileFragment
implements OnClickListener
{
64 private Account mAccount
;
66 public ProgressListener mProgressListener
;
68 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
69 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
70 public static final String FTAG_RENAME_FILE
= "RENAME_FILE_FRAGMENT";
72 private static final String ARG_FILE
= "FILE";
73 private static final String ARG_ACCOUNT
= "ACCOUNT";
77 * Public factory method to create new FileDetailFragment instances.
79 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
81 * @param fileToDetail An {@link OCFile} to show in the fragment
82 * @param account An ownCloud account; needed to start downloads
83 * @return New fragment with arguments set
85 public static FileDetailFragment
newInstance(OCFile fileToDetail
, Account account
) {
86 FileDetailFragment frag
= new FileDetailFragment();
87 Bundle args
= new Bundle();
88 args
.putParcelable(ARG_FILE
, fileToDetail
);
89 args
.putParcelable(ARG_ACCOUNT
, account
);
90 frag
.setArguments(args
);
95 * Creates an empty details fragment.
97 * It's necessary to keep a public constructor without parameters; the system uses it when tries
98 * to reinstantiate a fragment automatically.
100 public FileDetailFragment() {
103 mLayout
= R
.layout
.file_details_empty
;
104 mProgressListener
= null
;
109 public void onActivityCreated(Bundle savedInstanceState
) {
110 super.onCreate(savedInstanceState
);
111 setHasOptionsMenu(true
);
116 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
117 Bundle savedInstanceState
) {
119 setFile((OCFile
) getArguments().getParcelable(ARG_FILE
));
120 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
122 if (savedInstanceState
!= null
) {
123 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
124 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
127 if(getFile() != null
&& mAccount
!= null
) {
128 mLayout
= R
.layout
.file_details_fragment
;
131 mView
= inflater
.inflate(mLayout
, null
);
133 if (mLayout
== R
.layout
.file_details_fragment
) {
134 mView
.findViewById(R
.id
.fdFavorite
).setOnClickListener(this);
135 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
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
);
217 public boolean onOptionsItemSelected(MenuItem item
) {
218 switch (item
.getItemId()) {
219 case R
.id
.action_share_file
: {
220 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
223 case R
.id
.action_unshare_file
: {
224 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
227 case R
.id
.action_open_file_with
: {
228 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
231 case R
.id
.action_remove_file
: {
232 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
233 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
236 case R
.id
.action_rename_file
: {
237 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
238 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
241 case R
.id
.action_cancel_download
:
242 case R
.id
.action_cancel_upload
: {
243 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
246 case R
.id
.action_download_file
:
247 case R
.id
.action_sync_file
: {
248 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
251 case R
.id
.action_send_file
: {
253 if (!getFile().isDown()) { // Download the file
254 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
255 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
258 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
262 case R
.id
.action_favorite_file
:{
263 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
266 case R
.id
.action_unfavorite_file
:{
267 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
276 public void onClick(View v
) {
278 case R
.id
.fdFavorite
: {
279 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdFavorite
);
280 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(),cb
.isChecked());
283 case R
.id
.fdCancelBtn
: {
284 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
288 Log_OC
.e(TAG
, "Incorrect view clicked!");
294 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
296 * @return True when the fragment was created with the empty layout.
298 public boolean isEmpty() {
299 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
304 * Use this method to signal this Activity that it shall update its view.
306 * @param file : An {@link OCFile}
308 public void updateFileDetails(OCFile file
, Account ocAccount
) {
310 mAccount
= ocAccount
;
311 updateFileDetails(false
, false
);
315 * Updates the view with all relevant details about that file.
317 * TODO Remove parameter when the transferring state of files is kept in database.
319 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
320 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
321 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
323 * @param refresh If 'true', try to refresh the whole file from the database
325 public void updateFileDetails(boolean transferring
, boolean refresh
) {
327 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
328 if (refresh
&& storageManager
!= null
) {
329 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
331 OCFile file
= getFile();
334 setFilename(file
.getFileName());
335 setFiletype(file
.getMimetype(), file
.getFileName());
336 setFilesize(file
.getFileLength());
338 setTimeModified(file
.getModificationTimestamp());
340 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdFavorite
);
341 cb
.setChecked(file
.isFavorite());
343 // configure UI for depending upon local state of the file
344 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
345 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
347 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
348 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
350 setButtonsForTransferring();
352 } else if (file
.isDown()) {
357 // TODO load default preview image; when the local file is removed, the preview
359 setButtonsForRemote();
362 getView().invalidate();
366 * Checks if the fragment is ready to show details of a OCFile
368 * @return 'True' when the fragment is ready to show details of a file
370 private boolean readyToShow() {
371 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
376 * Updates the filename in view
377 * @param filename to set
379 private void setFilename(String filename
) {
380 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
382 tv
.setText(filename
);
386 * Updates the MIME type in view
387 * @param mimetype MIME type to set
388 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
390 private void setFiletype(String mimetype
, String filename
) {
391 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
393 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);
394 tv
.setText(printableMimetype
);
396 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
398 iv
.setImageResource(DisplayUtils
.getFileTypeIconId(mimetype
, filename
));
403 * Updates the file size in view
404 * @param filesize in bytes to set
406 private void setFilesize(long filesize
) {
407 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
409 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
413 * Updates the time that the file was last modified
414 * @param milliseconds Unix time to set
416 private void setTimeModified(long milliseconds
){
417 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
419 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
424 * Enables or disables buttons for a file being downloaded
426 private void setButtonsForTransferring() {
428 // let's protect the user from himself ;)
429 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 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
441 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
447 * Enables or disables buttons for a file locally available
449 private void setButtonsForDown() {
451 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
453 // hides the progress bar
454 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
455 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
456 progressText
.setVisibility(View
.GONE
);
461 * Enables or disables buttons for a file not locally available
463 private void setButtonsForRemote() {
465 getView().findViewById(R
.id
.fdFavorite
).setEnabled(true
);
467 // hides the progress bar
468 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
469 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
470 progressText
.setVisibility(View
.GONE
);
475 public void listenForTransferProgress() {
476 if (mProgressListener
!= null
) {
477 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
478 mContainerActivity
.getFileDownloaderBinder().
479 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
481 if (mContainerActivity
.getFileUploaderBinder() != null
) {
482 mContainerActivity
.getFileUploaderBinder().
483 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
489 public void leaveTransferProgress() {
490 if (mProgressListener
!= null
) {
491 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
492 mContainerActivity
.getFileDownloaderBinder().
493 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
495 if (mContainerActivity
.getFileUploaderBinder() != null
) {
496 mContainerActivity
.getFileUploaderBinder().
497 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
505 * Helper class responsible for updating the progress bar shown for file uploading or
508 private class ProgressListener
implements OnDatatransferProgressListener
{
509 int mLastPercent
= 0;
510 WeakReference
<ProgressBar
> mProgressBar
= null
;
512 ProgressListener(ProgressBar progressBar
) {
513 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
517 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
518 long totalToTransfer
, String filename
) {
519 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
520 if (percent
!= mLastPercent
) {
521 ProgressBar pb
= mProgressBar
.get();
523 pb
.setProgress(percent
);
527 mLastPercent
= percent
;