1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.fragment
;
20 import java
.lang
.ref
.WeakReference
;
22 import android
.accounts
.Account
;
23 import android
.content
.Intent
;
24 import android
.os
.Bundle
;
25 import android
.view
.LayoutInflater
;
26 import android
.view
.View
;
27 import android
.view
.View
.OnClickListener
;
28 import android
.view
.ViewGroup
;
29 import android
.widget
.CheckBox
;
30 import android
.widget
.ImageView
;
31 import android
.widget
.ProgressBar
;
32 import android
.widget
.TextView
;
34 import com
.actionbarsherlock
.view
.Menu
;
35 import com
.actionbarsherlock
.view
.MenuInflater
;
36 import com
.actionbarsherlock
.view
.MenuItem
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.FileMenuFilter
;
41 import com
.owncloud
.android
.files
.services
.FileObserverService
;
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
.ui
.activity
.FileActivity
;
46 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
47 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
48 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
49 import com
.owncloud
.android
.utils
.DisplayUtils
;
50 import com
.owncloud
.android
.utils
.Log_OC
;
54 * This Fragment is used to display the details about a file.
56 * @author Bartek Przybylski
57 * @author David A. Velasco
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";
73 * Creates an empty details fragment.
75 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
77 public FileDetailFragment() {
80 mLayout
= R
.layout
.file_details_empty
;
81 mProgressListener
= null
;
85 * Creates a details fragment.
87 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
89 * @param fileToDetail An {@link OCFile} to show in the fragment
90 * @param ocAccount An ownCloud account; needed to start downloads
92 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
95 mLayout
= R
.layout
.file_details_empty
;
96 mProgressListener
= null
;
101 public void onActivityCreated(Bundle savedInstanceState
) {
102 super.onCreate(savedInstanceState
);
103 setHasOptionsMenu(true
);
108 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
109 Bundle savedInstanceState
) {
111 if (savedInstanceState
!= null
) {
112 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
113 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
116 if(getFile() != null
&& mAccount
!= null
) {
117 mLayout
= R
.layout
.file_details_fragment
;
121 view
= 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(),
183 getSherlockActivity()
188 // additional restriction for this fragment
189 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
191 item
.setVisible(false
);
192 item
.setEnabled(false
);
201 public boolean onOptionsItemSelected(MenuItem item
) {
202 switch (item
.getItemId()) {
203 case R
.id
.action_share_file
: {
204 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
207 case R
.id
.action_unshare_file
: {
208 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
211 case R
.id
.action_open_file_with
: {
212 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
215 case R
.id
.action_remove_file
: {
216 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
217 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
220 case R
.id
.action_rename_file
: {
221 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
222 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
225 case R
.id
.action_cancel_download
:
226 case R
.id
.action_cancel_upload
: {
227 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
230 case R
.id
.action_download_file
:
231 case R
.id
.action_sync_file
: {
232 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
235 case R
.id
.action_send_file
: {
237 if (!getFile().isDown()) { // Download the file
238 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
239 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
242 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
252 public void onClick(View v
) {
254 case R
.id
.fdKeepInSync
: {
258 case R
.id
.fdCancelBtn
: {
259 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
263 Log_OC
.e(TAG
, "Incorrect view clicked!");
268 private void toggleKeepInSync() {
269 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
270 OCFile file
= getFile();
271 file
.setKeepInSync(cb
.isChecked());
272 mContainerActivity
.getStorageManager().saveFile(file
);
274 /// register the OCFile instance in the observer service to monitor local updates;
275 /// if necessary, the file is download
276 Intent intent
= new Intent(getActivity().getApplicationContext(),
277 FileObserverService
.class);
278 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
280 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
281 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
282 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
283 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
284 getActivity().startService(intent
);
286 if (file
.keepInSync()) {
287 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
292 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
294 * @return True when the fragment was created with the empty layout.
296 public boolean isEmpty() {
297 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
302 * Use this method to signal this Activity that it shall update its view.
304 * @param file : An {@link OCFile}
306 public void updateFileDetails(OCFile file
, Account ocAccount
) {
308 mAccount
= ocAccount
;
309 updateFileDetails(false
, false
);
313 * Updates the view with all relevant details about that file.
315 * TODO Remove parameter when the transferring state of files is kept in database.
317 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
318 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
319 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
321 * @param refresh If 'true', try to refresh the whole file from the database
323 public void updateFileDetails(boolean transferring
, boolean refresh
) {
325 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
326 if (refresh
&& storageManager
!= null
) {
327 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
329 OCFile file
= getFile();
332 setFilename(file
.getFileName());
333 setFiletype(file
.getMimetype(), file
.getFileName());
334 setFilesize(file
.getFileLength());
335 if(ocVersionSupportsTimeCreated()){
336 setTimeCreated(file
.getCreationTimestamp());
339 setTimeModified(file
.getModificationTimestamp());
341 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
342 cb
.setChecked(file
.keepInSync());
344 // configure UI for depending upon local state of the file
345 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
346 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
347 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
348 setButtonsForTransferring();
350 } else if (file
.isDown()) {
355 // TODO load default preview image; when the local file is removed, the preview remains there
356 setButtonsForRemote();
359 getView().invalidate();
363 * Checks if the fragment is ready to show details of a OCFile
365 * @return 'True' when the fragment is ready to show details of a file
367 private boolean readyToShow() {
368 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
373 * Updates the filename in view
374 * @param filename to set
376 private void setFilename(String filename
) {
377 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
379 tv
.setText(filename
);
383 * Updates the MIME type in view
384 * @param mimetype to set
386 private void setFiletype(String mimetype
, String filename
) {
387 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
389 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
390 tv
.setText(printableMimetype
);
392 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
394 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
, filename
));
399 * Updates the file size in view
400 * @param filesize in bytes to set
402 private void setFilesize(long filesize
) {
403 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
405 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
409 * Updates the time that the file was created in view
410 * @param milliseconds Unix time to set
412 private void setTimeCreated(long milliseconds
){
413 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
414 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
416 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
417 tv
.setVisibility(View
.VISIBLE
);
418 tvLabel
.setVisibility(View
.VISIBLE
);
423 * Updates the time that the file was last modified
424 * @param milliseconds Unix time to set
426 private void setTimeModified(long milliseconds
){
427 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
429 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
434 * Enables or disables buttons for a file being downloaded
436 private void setButtonsForTransferring() {
438 // let's protect the user from himself ;)
439 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
441 // show the progress bar for the transfer
442 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
443 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
444 progressText
.setVisibility(View
.VISIBLE
);
445 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
446 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
447 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
448 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
449 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
450 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
456 * Enables or disables buttons for a file locally available
458 private void setButtonsForDown() {
460 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
462 // hides the progress bar
463 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
464 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
465 progressText
.setVisibility(View
.GONE
);
470 * Enables or disables buttons for a file not locally available
472 private void setButtonsForRemote() {
474 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
476 // hides the progress bar
477 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
478 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
479 progressText
.setVisibility(View
.GONE
);
485 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
486 * the time that the file was created. There is a chance that this will
487 * be fixed in future versions. Use this method to check if this version of
488 * ownCloud has this fix.
489 * @return True, if ownCloud the ownCloud version is supporting creation time
491 private boolean ocVersionSupportsTimeCreated(){
492 /*if(mAccount != null){
493 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
494 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
495 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
496 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
504 public void listenForTransferProgress() {
505 if (mProgressListener
!= null
) {
506 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
507 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
509 if (mContainerActivity
.getFileUploaderBinder() != null
) {
510 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
516 public void leaveTransferProgress() {
517 if (mProgressListener
!= null
) {
518 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
519 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
521 if (mContainerActivity
.getFileUploaderBinder() != null
) {
522 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
530 * Helper class responsible for updating the progress bar shown for file uploading or downloading
532 * @author David A. Velasco
534 private class ProgressListener
implements OnDatatransferProgressListener
{
535 int mLastPercent
= 0;
536 WeakReference
<ProgressBar
> mProgressBar
= null
;
538 ProgressListener(ProgressBar progressBar
) {
539 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
543 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
544 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
545 if (percent
!= mLastPercent
) {
546 ProgressBar pb
= mProgressBar
.get();
548 pb
.setProgress(percent
);
552 mLastPercent
= percent
;