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";
74 * Creates an empty details fragment.
76 * It's necessary to keep a public constructor without parameters; the system uses it when tries
77 * to reinstantiate a fragment automatically.
79 public FileDetailFragment() {
82 mLayout
= R
.layout
.file_details_empty
;
83 mProgressListener
= null
;
87 * Creates a details fragment.
89 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
91 * @param fileToDetail An {@link OCFile} to show in the fragment
92 * @param ocAccount An ownCloud account; needed to start downloads
94 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
97 mLayout
= R
.layout
.file_details_empty
;
98 mProgressListener
= null
;
103 public void onActivityCreated(Bundle savedInstanceState
) {
104 super.onCreate(savedInstanceState
);
105 setHasOptionsMenu(true
);
110 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
111 Bundle savedInstanceState
) {
113 if (savedInstanceState
!= null
) {
114 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
115 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
118 if(getFile() != null
&& mAccount
!= null
) {
119 mLayout
= R
.layout
.file_details_fragment
;
122 mView
= inflater
.inflate(mLayout
, null
);
124 if (mLayout
== R
.layout
.file_details_fragment
) {
125 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
126 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
127 mProgressListener
= new ProgressListener(progressBar
);
128 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
131 updateFileDetails(false
, false
);
136 public void onSaveInstanceState(Bundle outState
) {
137 super.onSaveInstanceState(outState
);
138 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
139 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
143 public void onStart() {
145 listenForTransferProgress();
149 public void onStop() {
150 leaveTransferProgress();
156 public View
getView() {
157 return super.getView() == null ? mView
: super.getView();
165 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
166 super.onCreateOptionsMenu(menu
, inflater
);
167 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
175 public void onPrepareOptionsMenu (Menu menu
) {
176 super.onPrepareOptionsMenu(menu
);
178 if (mContainerActivity
.getStorageManager() != null
) {
179 FileMenuFilter mf
= new FileMenuFilter(
181 mContainerActivity
.getStorageManager().getAccount(),
188 // additional restriction for this fragment
189 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
191 item
.setVisible(false
);
192 item
.setEnabled(false
);
195 // additional restriction for this fragment
196 item
= menu
.findItem(R
.id
.action_move
);
198 item
.setVisible(false
);
199 item
.setEnabled(false
);
208 public boolean onOptionsItemSelected(MenuItem item
) {
209 switch (item
.getItemId()) {
210 case R
.id
.action_share_file
: {
211 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
214 case R
.id
.action_unshare_file
: {
215 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
218 case R
.id
.action_open_file_with
: {
219 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
222 case R
.id
.action_remove_file
: {
223 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
224 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
227 case R
.id
.action_rename_file
: {
228 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
229 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
232 case R
.id
.action_cancel_download
:
233 case R
.id
.action_cancel_upload
: {
234 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
237 case R
.id
.action_download_file
:
238 case R
.id
.action_sync_file
: {
239 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
242 case R
.id
.action_send_file
: {
244 if (!getFile().isDown()) { // Download the file
245 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
246 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
249 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
259 public void onClick(View v
) {
261 case R
.id
.fdKeepInSync
: {
265 case R
.id
.fdCancelBtn
: {
266 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
270 Log_OC
.e(TAG
, "Incorrect view clicked!");
275 private void toggleKeepInSync() {
276 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
277 OCFile file
= getFile();
278 file
.setKeepInSync(cb
.isChecked());
279 mContainerActivity
.getStorageManager().saveFile(file
);
281 /// register the OCFile instance in the observer service to monitor local updates
282 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
287 getActivity().startService(observedFileIntent
);
289 /// immediate content synchronization
290 if (file
.keepInSync()) {
291 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
296 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
298 * @return True when the fragment was created with the empty layout.
300 public boolean isEmpty() {
301 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
306 * Use this method to signal this Activity that it shall update its view.
308 * @param file : An {@link OCFile}
310 public void updateFileDetails(OCFile file
, Account ocAccount
) {
312 mAccount
= ocAccount
;
313 updateFileDetails(false
, false
);
317 * Updates the view with all relevant details about that file.
319 * TODO Remove parameter when the transferring state of files is kept in database.
321 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
322 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
323 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
325 * @param refresh If 'true', try to refresh the whole file from the database
327 public void updateFileDetails(boolean transferring
, boolean refresh
) {
329 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
330 if (refresh
&& storageManager
!= null
) {
331 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
333 OCFile file
= getFile();
336 setFilename(file
.getFileName());
337 setFiletype(file
.getMimetype(), file
.getFileName());
338 setFilesize(file
.getFileLength());
340 setTimeModified(file
.getModificationTimestamp());
342 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
343 cb
.setChecked(file
.keepInSync());
345 // configure UI for depending upon local state of the file
346 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
347 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
349 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
350 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
352 setButtonsForTransferring();
354 } else if (file
.isDown()) {
359 // TODO load default preview image; when the local file is removed, the preview
361 setButtonsForRemote();
364 getView().invalidate();
368 * Checks if the fragment is ready to show details of a OCFile
370 * @return 'True' when the fragment is ready to show details of a file
372 private boolean readyToShow() {
373 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
378 * Updates the filename in view
379 * @param filename to set
381 private void setFilename(String filename
) {
382 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
384 tv
.setText(filename
);
388 * Updates the MIME type in view
389 * @param mimetype MIME type to set
390 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
392 private void setFiletype(String mimetype
, String filename
) {
393 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
395 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
396 tv
.setText(printableMimetype
);
398 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
400 iv
.setImageResource(DisplayUtils
.getFileTypeIconId(mimetype
, filename
));
405 * Updates the file size in view
406 * @param filesize in bytes to set
408 private void setFilesize(long filesize
) {
409 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
411 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
415 * Updates the time that the file was last modified
416 * @param milliseconds Unix time to set
418 private void setTimeModified(long milliseconds
){
419 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
421 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
426 * Enables or disables buttons for a file being downloaded
428 private void setButtonsForTransferring() {
430 // let's protect the user from himself ;)
431 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
433 // show the progress bar for the transfer
434 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
435 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
436 progressText
.setVisibility(View
.VISIBLE
);
437 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
438 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
439 //if (getFile().isDownloading()) {
440 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
441 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
442 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
443 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
449 * Enables or disables buttons for a file locally available
451 private void setButtonsForDown() {
453 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
455 // hides the progress bar
456 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
457 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
458 progressText
.setVisibility(View
.GONE
);
463 * Enables or disables buttons for a file not locally available
465 private void setButtonsForRemote() {
467 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
469 // hides the progress bar
470 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
471 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
472 progressText
.setVisibility(View
.GONE
);
477 public void listenForTransferProgress() {
478 if (mProgressListener
!= null
) {
479 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
480 mContainerActivity
.getFileDownloaderBinder().
481 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
483 if (mContainerActivity
.getFileUploaderBinder() != null
) {
484 mContainerActivity
.getFileUploaderBinder().
485 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
491 public void leaveTransferProgress() {
492 if (mProgressListener
!= null
) {
493 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
494 mContainerActivity
.getFileDownloaderBinder().
495 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
497 if (mContainerActivity
.getFileUploaderBinder() != null
) {
498 mContainerActivity
.getFileUploaderBinder().
499 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
507 * Helper class responsible for updating the progress bar shown for file uploading or
510 private class ProgressListener
implements OnDatatransferProgressListener
{
511 int mLastPercent
= 0;
512 WeakReference
<ProgressBar
> mProgressBar
= null
;
514 ProgressListener(ProgressBar progressBar
) {
515 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
519 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
520 long totalToTransfer
, String filename
) {
521 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
522 if (percent
!= mLastPercent
) {
523 ProgressBar pb
= mProgressBar
.get();
525 pb
.setProgress(percent
);
529 mLastPercent
= percent
;