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
.ConfirmationDialogFragment
;
48 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
49 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
50 import com
.owncloud
.android
.utils
.DisplayUtils
;
51 import com
.owncloud
.android
.utils
.Log_OC
;
55 * This Fragment is used to display the details about a file.
57 * @author Bartek Przybylski
58 * @author David A. Velasco
60 public class FileDetailFragment
extends FileFragment
implements
62 ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
, EditNameDialogListener
{
66 private Account mAccount
;
68 public ProgressListener mProgressListener
;
70 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
71 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
72 public static final String FTAG_RENAME_FILE
= "RENAME_FILE_FRAGMENT";
76 * Creates an empty details fragment.
78 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
80 public FileDetailFragment() {
83 mLayout
= R
.layout
.file_details_empty
;
84 mProgressListener
= null
;
88 * Creates a details fragment.
90 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
92 * @param fileToDetail An {@link OCFile} to show in the fragment
93 * @param ocAccount An ownCloud account; needed to start downloads
95 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
98 mLayout
= R
.layout
.file_details_empty
;
99 mProgressListener
= null
;
104 public void onCreate(Bundle savedInstanceState
) {
105 super.onCreate(savedInstanceState
);
106 setHasOptionsMenu(true
);
111 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
112 Bundle savedInstanceState
) {
114 if (savedInstanceState
!= null
) {
115 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
116 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
119 if(getFile() != null
&& mAccount
!= null
) {
120 mLayout
= R
.layout
.file_details_fragment
;
124 view
= inflater
.inflate(mLayout
, null
);
127 if (mLayout
== R
.layout
.file_details_fragment
) {
128 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
129 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
130 mProgressListener
= new ProgressListener(progressBar
);
131 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
134 updateFileDetails(false
, false
);
139 public void onSaveInstanceState(Bundle outState
) {
140 super.onSaveInstanceState(outState
);
141 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
142 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
146 public void onStart() {
148 listenForTransferProgress();
152 public void onStop() {
154 leaveTransferProgress();
159 public View
getView() {
160 return super.getView() == null ? mView
: super.getView();
168 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
169 super.onCreateOptionsMenu(menu
, inflater
);
170 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
178 public void onPrepareOptionsMenu (Menu menu
) {
179 super.onPrepareOptionsMenu(menu
);
181 FileMenuFilter mf
= new FileMenuFilter(
183 mContainerActivity
.getStorageManager().getAccount(),
185 getSherlockActivity()
189 // additional restriction for this fragment
190 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
192 item
.setVisible(false
);
193 item
.setEnabled(false
);
202 public boolean onOptionsItemSelected(MenuItem item
) {
203 switch (item
.getItemId()) {
204 case R
.id
.action_share_file
: {
205 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
208 case R
.id
.action_unshare_file
: {
209 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
212 case R
.id
.action_open_file_with
: {
213 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
216 case R
.id
.action_remove_file
: {
217 showDialogToRemoveFile();
220 case R
.id
.action_rename_file
: {
221 showDialogToRenameFile();
224 case R
.id
.action_cancel_download
:
225 case R
.id
.action_cancel_upload
: {
226 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
229 case R
.id
.action_download_file
:
230 case R
.id
.action_sync_file
: {
231 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
234 case R
.id
.action_send_file
: {
236 if (!getFile().isDown()) { // Download the file
237 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
238 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
241 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
251 public void onClick(View v
) {
253 case R
.id
.fdKeepInSync
: {
257 case R
.id
.fdCancelBtn
: {
258 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
262 Log_OC
.e(TAG
, "Incorrect view clicked!");
267 private void toggleKeepInSync() {
268 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
269 OCFile file
= getFile();
270 file
.setKeepInSync(cb
.isChecked());
271 mContainerActivity
.getStorageManager().saveFile(file
);
273 /// register the OCFile instance in the observer service to monitor local updates;
274 /// if necessary, the file is download
275 Intent intent
= new Intent(getActivity().getApplicationContext(),
276 FileObserverService
.class);
277 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
279 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
280 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
281 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
282 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
283 getActivity().startService(intent
);
285 if (file
.keepInSync()) {
286 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
290 private void showDialogToRemoveFile() {
291 OCFile file
= getFile();
292 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
293 R
.string
.confirmation_remove_alert
,
294 new String
[]{file
.getFileName()},
295 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
296 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
297 R
.string
.common_cancel
);
298 confDialog
.setOnConfirmationListener(this);
299 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
303 private void showDialogToRenameFile() {
304 OCFile file
= getFile();
305 String fileName
= file
.getFileName();
306 int extensionStart
= file
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
307 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
308 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
309 dialog
.show(getFragmentManager(), FTAG_RENAME_FILE
);
314 public void onConfirmation(String callerTag
) {
315 OCFile file
= getFile();
316 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
317 if (mContainerActivity
.getStorageManager().getFileById(file
.getFileId()) != null
) {
318 mContainerActivity
.getFileOperationsHelper().removeFile(file
, true
);
324 public void onNeutral(String callerTag
) {
325 OCFile file
= getFile();
326 mContainerActivity
.getStorageManager().removeFile(file
, false
, true
); // TODO perform in background task / new thread
327 if (file
.getStoragePath() != null
) {
328 file
.setStoragePath(null
);
329 updateFileDetails(file
, mAccount
);
334 public void onCancel(String callerTag
) {
335 Log_OC
.d(TAG
, "REMOVAL CANCELED");
340 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
342 * @return True when the fragment was created with the empty layout.
344 public boolean isEmpty() {
345 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
350 * Use this method to signal this Activity that it shall update its view.
352 * @param file : An {@link OCFile}
354 public void updateFileDetails(OCFile file
, Account ocAccount
) {
356 mAccount
= ocAccount
;
357 updateFileDetails(false
, false
);
361 * Updates the view with all relevant details about that file.
363 * TODO Remove parameter when the transferring state of files is kept in database.
365 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
366 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
367 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
369 * @param refresh If 'true', try to refresh the whole file from the database
371 public void updateFileDetails(boolean transferring
, boolean refresh
) {
373 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
374 if (refresh
&& storageManager
!= null
) {
375 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
377 OCFile file
= getFile();
380 setFilename(file
.getFileName());
381 setFiletype(file
.getMimetype());
382 setFilesize(file
.getFileLength());
383 if(ocVersionSupportsTimeCreated()){
384 setTimeCreated(file
.getCreationTimestamp());
387 setTimeModified(file
.getModificationTimestamp());
389 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
390 cb
.setChecked(file
.keepInSync());
392 // configure UI for depending upon local state of the file
393 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
394 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
395 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
396 setButtonsForTransferring();
398 } else if (file
.isDown()) {
403 // TODO load default preview image; when the local file is removed, the preview remains there
404 setButtonsForRemote();
407 getView().invalidate();
411 * Checks if the fragment is ready to show details of a OCFile
413 * @return 'True' when the fragment is ready to show details of a file
415 private boolean readyToShow() {
416 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
421 * Updates the filename in view
422 * @param filename to set
424 private void setFilename(String filename
) {
425 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
427 tv
.setText(filename
);
431 * Updates the MIME type in view
432 * @param mimetype to set
434 private void setFiletype(String mimetype
) {
435 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
437 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
438 tv
.setText(printableMimetype
);
440 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
442 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
447 * Updates the file size in view
448 * @param filesize in bytes to set
450 private void setFilesize(long filesize
) {
451 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
453 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
457 * Updates the time that the file was created in view
458 * @param milliseconds Unix time to set
460 private void setTimeCreated(long milliseconds
){
461 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
462 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
464 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
465 tv
.setVisibility(View
.VISIBLE
);
466 tvLabel
.setVisibility(View
.VISIBLE
);
471 * Updates the time that the file was last modified
472 * @param milliseconds Unix time to set
474 private void setTimeModified(long milliseconds
){
475 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
477 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
482 * Enables or disables buttons for a file being downloaded
484 private void setButtonsForTransferring() {
486 // let's protect the user from himself ;)
487 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
489 // show the progress bar for the transfer
490 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
491 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
492 progressText
.setVisibility(View
.VISIBLE
);
493 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
494 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
495 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
496 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
497 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
498 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
504 * Enables or disables buttons for a file locally available
506 private void setButtonsForDown() {
508 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
510 // hides the progress bar
511 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
512 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
513 progressText
.setVisibility(View
.GONE
);
518 * Enables or disables buttons for a file not locally available
520 private void setButtonsForRemote() {
522 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
524 // hides the progress bar
525 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
526 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
527 progressText
.setVisibility(View
.GONE
);
533 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
534 * the time that the file was created. There is a chance that this will
535 * be fixed in future versions. Use this method to check if this version of
536 * ownCloud has this fix.
537 * @return True, if ownCloud the ownCloud version is supporting creation time
539 private boolean ocVersionSupportsTimeCreated(){
540 /*if(mAccount != null){
541 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
542 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
543 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
544 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
552 public void onDismiss(EditNameDialog dialog
) {
553 if (dialog
.getResult()) {
554 String newFilename
= dialog
.getNewFilename();
555 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
556 mContainerActivity
.getFileOperationsHelper().renameFile(getFile(), newFilename
);
561 public void listenForTransferProgress() {
562 if (mProgressListener
!= null
) {
563 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
564 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
566 if (mContainerActivity
.getFileUploaderBinder() != null
) {
567 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
573 public void leaveTransferProgress() {
574 if (mProgressListener
!= null
) {
575 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
576 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
578 if (mContainerActivity
.getFileUploaderBinder() != null
) {
579 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
587 * Helper class responsible for updating the progress bar shown for file uploading or downloading
589 * @author David A. Velasco
591 private class ProgressListener
implements OnDatatransferProgressListener
{
592 int mLastPercent
= 0;
593 WeakReference
<ProgressBar
> mProgressBar
= null
;
595 ProgressListener(ProgressBar progressBar
) {
596 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
600 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
601 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
602 if (percent
!= mLastPercent
) {
603 ProgressBar pb
= mProgressBar
.get();
605 pb
.setProgress(percent
);
609 mLastPercent
= percent
;