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 DisplayUtils
.colorPreLollipopHorizontalProgressBar(progressBar
);
137 mProgressListener
= new ProgressListener(progressBar
);
138 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
141 updateFileDetails(false
, false
);
146 public void onSaveInstanceState(Bundle outState
) {
147 super.onSaveInstanceState(outState
);
148 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
149 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
153 public void onStart() {
155 listenForTransferProgress();
159 public void onStop() {
160 leaveTransferProgress();
166 public View
getView() {
167 return super.getView() == null ? mView
: super.getView();
175 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
176 super.onCreateOptionsMenu(menu
, inflater
);
177 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
185 public void onPrepareOptionsMenu (Menu menu
) {
186 super.onPrepareOptionsMenu(menu
);
188 if (mContainerActivity
.getStorageManager() != null
) {
189 FileMenuFilter mf
= new FileMenuFilter(
191 mContainerActivity
.getStorageManager().getAccount(),
198 // additional restriction for this fragment
199 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
201 item
.setVisible(false
);
202 item
.setEnabled(false
);
205 // additional restriction for this fragment
206 item
= menu
.findItem(R
.id
.action_move
);
208 item
.setVisible(false
);
209 item
.setEnabled(false
);
218 public boolean onOptionsItemSelected(MenuItem item
) {
219 switch (item
.getItemId()) {
220 case R
.id
.action_share_file
: {
221 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
224 case R
.id
.action_unshare_file
: {
225 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
228 case R
.id
.action_open_file_with
: {
229 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
232 case R
.id
.action_remove_file
: {
233 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
234 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
237 case R
.id
.action_rename_file
: {
238 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
239 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
242 case R
.id
.action_cancel_download
:
243 case R
.id
.action_cancel_upload
: {
244 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
247 case R
.id
.action_download_file
:
248 case R
.id
.action_sync_file
: {
249 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
252 case R
.id
.action_send_file
: {
254 if (!getFile().isDown()) { // Download the file
255 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
256 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
259 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
263 case R
.id
.action_favorite_file
:{
264 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
267 case R
.id
.action_unfavorite_file
:{
268 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
277 public void onClick(View v
) {
279 case R
.id
.fdFavorite
: {
280 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdFavorite
);
281 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(),cb
.isChecked());
284 case R
.id
.fdCancelBtn
: {
285 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
289 Log_OC
.e(TAG
, "Incorrect view clicked!");
295 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
297 * @return True when the fragment was created with the empty layout.
299 public boolean isEmpty() {
300 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
305 * Use this method to signal this Activity that it shall update its view.
307 * @param file : An {@link OCFile}
309 public void updateFileDetails(OCFile file
, Account ocAccount
) {
311 mAccount
= ocAccount
;
312 updateFileDetails(false
, false
);
316 * Updates the view with all relevant details about that file.
318 * TODO Remove parameter when the transferring state of files is kept in database.
320 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
321 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
322 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
324 * @param refresh If 'true', try to refresh the whole file from the database
326 public void updateFileDetails(boolean transferring
, boolean refresh
) {
328 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
329 if (refresh
&& storageManager
!= null
) {
330 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
332 OCFile file
= getFile();
335 setFilename(file
.getFileName());
336 setFiletype(file
.getMimetype(), file
.getFileName());
337 setFilesize(file
.getFileLength());
339 setTimeModified(file
.getModificationTimestamp());
341 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdFavorite
);
342 cb
.setChecked(file
.isFavorite());
344 // configure UI for depending upon local state of the file
345 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
346 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
348 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
349 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
351 setButtonsForTransferring();
353 } else if (file
.isDown()) {
358 // TODO load default preview image; when the local file is removed, the preview
360 setButtonsForRemote();
363 getView().invalidate();
367 * Checks if the fragment is ready to show details of a OCFile
369 * @return 'True' when the fragment is ready to show details of a file
371 private boolean readyToShow() {
372 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
377 * Updates the filename in view
378 * @param filename to set
380 private void setFilename(String filename
) {
381 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
383 tv
.setText(filename
);
387 * Updates the MIME type in view
388 * @param mimetype MIME type to set
389 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
391 private void setFiletype(String mimetype
, String filename
) {
392 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
394 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);
395 tv
.setText(printableMimetype
);
397 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
399 iv
.setImageResource(DisplayUtils
.getFileTypeIconId(mimetype
, filename
));
404 * Updates the file size in view
405 * @param filesize in bytes to set
407 private void setFilesize(long filesize
) {
408 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
410 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
414 * Updates the time that the file was last modified
415 * @param milliseconds Unix time to set
417 private void setTimeModified(long milliseconds
){
418 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
420 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
425 * Enables or disables buttons for a file being downloaded
427 private void setButtonsForTransferring() {
429 // let's protect the user from himself ;)
430 getView().findViewById(R
.id
.fdFavorite
).setEnabled(false
);
432 // show the progress bar for the transfer
433 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
434 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
435 progressText
.setVisibility(View
.VISIBLE
);
436 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
437 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
438 //if (getFile().isDownloading()) {
439 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
440 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
441 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
442 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
448 * Enables or disables buttons for a file locally available
450 private void setButtonsForDown() {
452 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
);
462 * Enables or disables buttons for a file not locally available
464 private void setButtonsForRemote() {
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 public void listenForTransferProgress() {
477 if (mProgressListener
!= null
) {
478 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
479 mContainerActivity
.getFileDownloaderBinder().
480 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
482 if (mContainerActivity
.getFileUploaderBinder() != null
) {
483 mContainerActivity
.getFileUploaderBinder().
484 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
490 public void leaveTransferProgress() {
491 if (mProgressListener
!= null
) {
492 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
493 mContainerActivity
.getFileDownloaderBinder().
494 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
496 if (mContainerActivity
.getFileUploaderBinder() != null
) {
497 mContainerActivity
.getFileUploaderBinder().
498 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
506 * Helper class responsible for updating the progress bar shown for file uploading or
509 private class ProgressListener
implements OnDatatransferProgressListener
{
510 int mLastPercent
= 0;
511 WeakReference
<ProgressBar
> mProgressBar
= null
;
513 ProgressListener(ProgressBar progressBar
) {
514 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
518 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
519 long totalToTransfer
, String filename
) {
520 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
521 if (percent
!= mLastPercent
) {
522 ProgressBar pb
= mProgressBar
.get();
524 pb
.setProgress(percent
);
528 mLastPercent
= percent
;