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
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
43 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
44 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
45 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
46 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
47 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
48 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
49 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
50 import com
.owncloud
.android
.utils
.DisplayUtils
;
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(),
182 getSherlockActivity()
187 // additional restriction for this fragment
188 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
190 item
.setVisible(false
);
191 item
.setEnabled(false
);
194 // additional restriction for this fragment
195 item
= menu
.findItem(R
.id
.action_move
);
197 item
.setVisible(false
);
198 item
.setEnabled(false
);
207 public boolean onOptionsItemSelected(MenuItem item
) {
208 switch (item
.getItemId()) {
209 case R
.id
.action_share_file
: {
210 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
213 case R
.id
.action_unshare_file
: {
214 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
217 case R
.id
.action_open_file_with
: {
218 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
221 case R
.id
.action_remove_file
: {
222 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
223 dialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
226 case R
.id
.action_rename_file
: {
227 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(getFile());
228 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
231 case R
.id
.action_cancel_download
:
232 case R
.id
.action_cancel_upload
: {
233 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
236 case R
.id
.action_download_file
:
237 case R
.id
.action_sync_file
: {
238 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
241 case R
.id
.action_send_file
: {
243 if (!getFile().isDown()) { // Download the file
244 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
245 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
248 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
258 public void onClick(View v
) {
260 case R
.id
.fdKeepInSync
: {
264 case R
.id
.fdCancelBtn
: {
265 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
269 Log_OC
.e(TAG
, "Incorrect view clicked!");
274 private void toggleKeepInSync() {
275 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
276 OCFile file
= getFile();
277 file
.setKeepInSync(cb
.isChecked());
278 mContainerActivity
.getStorageManager().saveFile(file
);
280 /// register the OCFile instance in the observer service to monitor local updates
281 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
286 getActivity().startService(observedFileIntent
);
288 /// immediate content synchronization
289 if (file
.keepInSync()) {
290 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
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());
338 if(ocVersionSupportsTimeCreated()){
339 setTimeCreated(file
.getCreationTimestamp());
342 setTimeModified(file
.getModificationTimestamp());
344 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
345 cb
.setChecked(file
.keepInSync());
347 // configure UI for depending upon local state of the file
348 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
349 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
350 if (transferring
|| file
.isDownloading() || file
.isUploading()) {
351 setButtonsForTransferring();
353 } else if (file
.isDown()) {
358 // TODO load default preview image; when the local file is removed, the preview remains there
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 to set
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 created in view
414 * @param milliseconds Unix time to set
416 private void setTimeCreated(long milliseconds
){
417 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
418 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
420 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
421 tv
.setVisibility(View
.VISIBLE
);
422 tvLabel
.setVisibility(View
.VISIBLE
);
427 * Updates the time that the file was last modified
428 * @param milliseconds Unix time to set
430 private void setTimeModified(long milliseconds
){
431 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
433 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
438 * Enables or disables buttons for a file being downloaded
440 private void setButtonsForTransferring() {
442 // let's protect the user from himself ;)
443 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
445 // show the progress bar for the transfer
446 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
447 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
448 progressText
.setVisibility(View
.VISIBLE
);
449 if (getFile().isDownloading()) {
450 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
451 } else if (getFile().isUploading()) {
452 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
458 * Enables or disables buttons for a file locally available
460 private void setButtonsForDown() {
462 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
464 // hides the progress bar
465 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
466 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
467 progressText
.setVisibility(View
.GONE
);
472 * Enables or disables buttons for a file not locally available
474 private void setButtonsForRemote() {
476 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
478 // hides the progress bar
479 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
480 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
481 progressText
.setVisibility(View
.GONE
);
487 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
488 * the time that the file was created. There is a chance that this will
489 * be fixed in future versions. Use this method to check if this version of
490 * ownCloud has this fix.
491 * @return True, if ownCloud the ownCloud version is supporting creation time
493 private boolean ocVersionSupportsTimeCreated(){
494 /*if(mAccount != null){
495 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
496 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
497 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
498 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
506 public void listenForTransferProgress() {
507 if (mProgressListener
!= null
) {
508 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
509 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
511 if (mContainerActivity
.getFileUploaderBinder() != null
) {
512 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
518 public void leaveTransferProgress() {
519 if (mProgressListener
!= null
) {
520 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
521 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
523 if (mContainerActivity
.getFileUploaderBinder() != null
) {
524 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
532 * Helper class responsible for updating the progress bar shown for file uploading or downloading
534 * @author David A. Velasco
536 private class ProgressListener
implements OnDatatransferProgressListener
{
537 int mLastPercent
= 0;
538 WeakReference
<ProgressBar
> mProgressBar
= null
;
540 ProgressListener(ProgressBar progressBar
) {
541 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
545 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
546 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
547 if (percent
!= mLastPercent
) {
548 ProgressBar pb
= mProgressBar
.get();
550 pb
.setProgress(percent
);
554 mLastPercent
= percent
;