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 to reinstantiate a fragment automatically.
78 public FileDetailFragment() {
81 mLayout
= R
.layout
.file_details_empty
;
82 mProgressListener
= null
;
86 * Creates a details fragment.
88 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
90 * @param fileToDetail An {@link OCFile} to show in the fragment
91 * @param ocAccount An ownCloud account; needed to start downloads
93 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
96 mLayout
= R
.layout
.file_details_empty
;
97 mProgressListener
= null
;
102 public void onActivityCreated(Bundle savedInstanceState
) {
103 super.onCreate(savedInstanceState
);
104 setHasOptionsMenu(true
);
109 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
110 Bundle savedInstanceState
) {
112 if (savedInstanceState
!= null
) {
113 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
114 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
117 if(getFile() != null
&& mAccount
!= null
) {
118 mLayout
= R
.layout
.file_details_fragment
;
122 view
= inflater
.inflate(mLayout
, null
);
125 if (mLayout
== R
.layout
.file_details_fragment
) {
126 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
127 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
128 mProgressListener
= new ProgressListener(progressBar
);
129 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
132 updateFileDetails(false
, false
);
137 public void onSaveInstanceState(Bundle outState
) {
138 super.onSaveInstanceState(outState
);
139 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
140 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
144 public void onStart() {
146 listenForTransferProgress();
150 public void onStop() {
151 leaveTransferProgress();
157 public View
getView() {
158 return super.getView() == null ? mView
: super.getView();
166 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
167 super.onCreateOptionsMenu(menu
, inflater
);
168 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
176 public void onPrepareOptionsMenu (Menu menu
) {
177 super.onPrepareOptionsMenu(menu
);
179 if (mContainerActivity
.getStorageManager() != null
) {
180 FileMenuFilter mf
= new FileMenuFilter(
182 mContainerActivity
.getStorageManager().getAccount(),
189 // additional restriction for this fragment
190 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
192 item
.setVisible(false
);
193 item
.setEnabled(false
);
196 // additional restriction for this fragment
197 item
= menu
.findItem(R
.id
.action_move
);
199 item
.setVisible(false
);
200 item
.setEnabled(false
);
209 public boolean onOptionsItemSelected(MenuItem item
) {
210 switch (item
.getItemId()) {
211 case R
.id
.action_share_file
: {
212 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
215 case R
.id
.action_unshare_file
: {
216 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
219 case R
.id
.action_open_file_with
: {
220 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
223 case R
.id
.action_remove_file
: {
224 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
225 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
228 case R
.id
.action_rename_file
: {
229 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
230 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
233 case R
.id
.action_cancel_download
:
234 case R
.id
.action_cancel_upload
: {
235 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
238 case R
.id
.action_download_file
:
239 case R
.id
.action_sync_file
: {
240 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
243 case R
.id
.action_send_file
: {
245 if (!getFile().isDown()) { // Download the file
246 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
247 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
250 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
260 public void onClick(View v
) {
262 case R
.id
.fdKeepInSync
: {
266 case R
.id
.fdCancelBtn
: {
267 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
271 Log_OC
.e(TAG
, "Incorrect view clicked!");
276 private void toggleKeepInSync() {
277 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
278 OCFile file
= getFile();
279 file
.setKeepInSync(cb
.isChecked());
280 mContainerActivity
.getStorageManager().saveFile(file
);
282 /// register the OCFile instance in the observer service to monitor local updates
283 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
288 getActivity().startService(observedFileIntent
);
290 /// immediate content synchronization
291 if (file
.keepInSync()) {
292 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
297 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
299 * @return True when the fragment was created with the empty layout.
301 public boolean isEmpty() {
302 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
307 * Use this method to signal this Activity that it shall update its view.
309 * @param file : An {@link OCFile}
311 public void updateFileDetails(OCFile file
, Account ocAccount
) {
313 mAccount
= ocAccount
;
314 updateFileDetails(false
, false
);
318 * Updates the view with all relevant details about that file.
320 * TODO Remove parameter when the transferring state of files is kept in database.
322 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
323 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
324 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
326 * @param refresh If 'true', try to refresh the whole file from the database
328 public void updateFileDetails(boolean transferring
, boolean refresh
) {
330 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
331 if (refresh
&& storageManager
!= null
) {
332 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
334 OCFile file
= getFile();
337 setFilename(file
.getFileName());
338 setFiletype(file
.getMimetype(), file
.getFileName());
339 setFilesize(file
.getFileLength());
340 if(ocVersionSupportsTimeCreated()){
341 setTimeCreated(file
.getCreationTimestamp());
344 setTimeModified(file
.getModificationTimestamp());
346 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
347 cb
.setChecked(file
.keepInSync());
349 // configure UI for depending upon local state of the file
350 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
351 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
353 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) ||
354 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))
356 setButtonsForTransferring();
358 } else if (file
.isDown()) {
363 // TODO load default preview image; when the local file is removed, the preview
365 setButtonsForRemote();
368 getView().invalidate();
372 * Checks if the fragment is ready to show details of a OCFile
374 * @return 'True' when the fragment is ready to show details of a file
376 private boolean readyToShow() {
377 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
382 * Updates the filename in view
383 * @param filename to set
385 private void setFilename(String filename
) {
386 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
388 tv
.setText(filename
);
392 * Updates the MIME type in view
393 * @param mimetype to set
396 private void setFiletype(String mimetype
, String filename
) {
397 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
399 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
400 tv
.setText(printableMimetype
);
402 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
404 iv
.setImageResource(DisplayUtils
.getFileTypeIconId(mimetype
, filename
));
409 * Updates the file size in view
410 * @param filesize in bytes to set
412 private void setFilesize(long filesize
) {
413 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
415 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
419 * Updates the time that the file was created in view
420 * @param milliseconds Unix time to set
422 private void setTimeCreated(long milliseconds
){
423 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
424 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
426 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
427 tv
.setVisibility(View
.VISIBLE
);
428 tvLabel
.setVisibility(View
.VISIBLE
);
433 * Updates the time that the file was last modified
434 * @param milliseconds Unix time to set
436 private void setTimeModified(long milliseconds
){
437 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
439 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
444 * Enables or disables buttons for a file being downloaded
446 private void setButtonsForTransferring() {
448 // let's protect the user from himself ;)
449 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
451 // show the progress bar for the transfer
452 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
453 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
454 progressText
.setVisibility(View
.VISIBLE
);
455 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
456 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
457 //if (getFile().isDownloading()) {
458 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
459 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
460 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
461 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
467 * Enables or disables buttons for a file locally available
469 private void setButtonsForDown() {
471 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
473 // hides the progress bar
474 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
475 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
476 progressText
.setVisibility(View
.GONE
);
481 * Enables or disables buttons for a file not locally available
483 private void setButtonsForRemote() {
485 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
487 // hides the progress bar
488 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
489 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
490 progressText
.setVisibility(View
.GONE
);
496 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
497 * the time that the file was created. There is a chance that this will
498 * be fixed in future versions. Use this method to check if this version of
499 * ownCloud has this fix.
500 * @return True, if ownCloud the ownCloud version is supporting creation time
502 private boolean ocVersionSupportsTimeCreated(){
503 /*if(mAccount != null){
504 AccountManager accManager = (AccountManager) getActivity()
505 .getSystemService(Context.ACCOUNT_SERVICE);
506 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
507 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
508 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
516 public void listenForTransferProgress() {
517 if (mProgressListener
!= null
) {
518 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
519 mContainerActivity
.getFileDownloaderBinder().
520 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
522 if (mContainerActivity
.getFileUploaderBinder() != null
) {
523 mContainerActivity
.getFileUploaderBinder().
524 addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
530 public void leaveTransferProgress() {
531 if (mProgressListener
!= null
) {
532 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
533 mContainerActivity
.getFileDownloaderBinder().
534 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
536 if (mContainerActivity
.getFileUploaderBinder() != null
) {
537 mContainerActivity
.getFileUploaderBinder().
538 removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
546 * Helper class responsible for updating the progress bar shown for file uploading or
549 private class ProgressListener
implements OnDatatransferProgressListener
{
550 int mLastPercent
= 0;
551 WeakReference
<ProgressBar
> mProgressBar
= null
;
553 ProgressListener(ProgressBar progressBar
) {
554 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
558 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
559 long totalToTransfer
, String filename
) {
560 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
561 if (percent
!= mLastPercent
) {
562 ProgressBar pb
= mProgressBar
.get();
564 pb
.setProgress(percent
);
568 mLastPercent
= percent
;