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";
75 * Creates an empty details fragment.
77 * It's necessary to keep a public constructor without parameters; the system uses it when tries 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 onCreate(Bundle savedInstanceState
) {
104 super.onCreate(savedInstanceState
);
105 setHasOptionsMenu(true
);
110 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
111 Bundle savedInstanceState
) {
112 //super.onCreateView(inflater, container, 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
);
173 TODO Maybe should stay here? It's context (fragment) specific
175 MenuItem item = menu.findItem(R.id.action_see_details);
177 item.setVisible(false);
178 item.setEnabled(false);
182 item = menu.findItem(R.id.action_send_file);
183 boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
186 item.setVisible(true);
187 item.setEnabled(true);
189 item.setVisible(false);
190 item.setEnabled(false);
202 public void onPrepareOptionsMenu (Menu menu
) {
203 super.onPrepareOptionsMenu(menu
);
205 FileMenuFilter mf
= new FileMenuFilter();
206 mf
.setFile(getFile());
207 mf
.setComponentGetter(mContainerActivity
);
208 mf
.setAccount(mContainerActivity
.getStorageManager().getAccount());
209 mf
.setContext(getSherlockActivity());
210 mf
.setFragment(this);
219 public boolean onOptionsItemSelected(MenuItem item
) {
220 switch (item
.getItemId()) {
221 case R
.id
.action_share_file
: {
222 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
225 case R
.id
.action_unshare_file
: {
226 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
229 case R
.id
.action_open_file_with
: {
230 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
233 case R
.id
.action_remove_file
: {
234 showDialogToRemoveFile();
237 case R
.id
.action_rename_file
: {
238 showDialogToRenameFile();
241 case R
.id
.action_cancel_download
:
242 case R
.id
.action_cancel_upload
: {
243 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
246 case R
.id
.action_download_file
:
247 case R
.id
.action_sync_file
: {
248 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
251 case R
.id
.action_send_file
: {
253 if (!getFile().isDown()) { // Download the file
254 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
255 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(getFile());
258 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
268 public void onClick(View v
) {
270 case R
.id
.fdKeepInSync
: {
274 case R
.id
.fdCancelBtn
: {
275 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(getFile());
279 Log_OC
.e(TAG
, "Incorrect view clicked!");
284 private void toggleKeepInSync() {
285 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
286 OCFile file
= getFile();
287 file
.setKeepInSync(cb
.isChecked());
288 mContainerActivity
.getStorageManager().saveFile(file
);
290 /// register the OCFile instance in the observer service to monitor local updates;
291 /// if necessary, the file is download
292 Intent intent
= new Intent(getActivity().getApplicationContext(),
293 FileObserverService
.class);
294 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
296 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
297 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
298 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
299 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
300 getActivity().startService(intent
);
302 if (file
.keepInSync()) {
303 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
307 private void showDialogToRemoveFile() {
308 OCFile file
= getFile();
309 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
310 R
.string
.confirmation_remove_alert
,
311 new String
[]{file
.getFileName()},
312 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
313 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
314 R
.string
.common_cancel
);
315 confDialog
.setOnConfirmationListener(this);
316 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
320 private void showDialogToRenameFile() {
321 OCFile file
= getFile();
322 String fileName
= file
.getFileName();
323 int extensionStart
= file
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
324 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
325 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
326 dialog
.show(getFragmentManager(), "nameeditdialog");
331 public void onConfirmation(String callerTag
) {
332 OCFile file
= getFile();
333 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
334 if (mContainerActivity
.getStorageManager().getFileById(file
.getFileId()) != null
) {
335 mContainerActivity
.getFileOperationsHelper().removeFile(file
, true
);
341 public void onNeutral(String callerTag
) {
342 OCFile file
= getFile();
343 mContainerActivity
.getStorageManager().removeFile(file
, false
, true
); // TODO perform in background task / new thread
344 if (file
.getStoragePath() != null
) {
345 file
.setStoragePath(null
);
346 updateFileDetails(file
, mAccount
);
351 public void onCancel(String callerTag
) {
352 Log_OC
.d(TAG
, "REMOVAL CANCELED");
357 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
359 * @return True when the fragment was created with the empty layout.
361 public boolean isEmpty() {
362 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
367 * Use this method to signal this Activity that it shall update its view.
369 * @param file : An {@link OCFile}
371 public void updateFileDetails(OCFile file
, Account ocAccount
) {
373 mAccount
= ocAccount
;
374 updateFileDetails(false
, false
);
378 * Updates the view with all relevant details about that file.
380 * TODO Remove parameter when the transferring state of files is kept in database.
382 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
383 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
384 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
386 * @param refresh If 'true', try to refresh the whole file from the database
388 public void updateFileDetails(boolean transferring
, boolean refresh
) {
390 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
391 if (refresh
&& storageManager
!= null
) {
392 setFile(storageManager
.getFileByPath(getFile().getRemotePath()));
394 OCFile file
= getFile();
397 setFilename(file
.getFileName());
398 setFiletype(file
.getMimetype());
399 setFilesize(file
.getFileLength());
400 if(ocVersionSupportsTimeCreated()){
401 setTimeCreated(file
.getCreationTimestamp());
404 setTimeModified(file
.getModificationTimestamp());
406 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
407 cb
.setChecked(file
.keepInSync());
409 // configure UI for depending upon local state of the file
410 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
411 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
412 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
413 setButtonsForTransferring();
415 } else if (file
.isDown()) {
420 // TODO load default preview image; when the local file is removed, the preview remains there
421 setButtonsForRemote();
424 getView().invalidate();
428 * Checks if the fragment is ready to show details of a OCFile
430 * @return 'True' when the fragment is ready to show details of a file
432 private boolean readyToShow() {
433 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
438 * Updates the filename in view
439 * @param filename to set
441 private void setFilename(String filename
) {
442 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
444 tv
.setText(filename
);
448 * Updates the MIME type in view
449 * @param mimetype to set
451 private void setFiletype(String mimetype
) {
452 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
454 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
455 tv
.setText(printableMimetype
);
457 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
459 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
464 * Updates the file size in view
465 * @param filesize in bytes to set
467 private void setFilesize(long filesize
) {
468 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
470 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
474 * Updates the time that the file was created in view
475 * @param milliseconds Unix time to set
477 private void setTimeCreated(long milliseconds
){
478 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
479 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
481 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
482 tv
.setVisibility(View
.VISIBLE
);
483 tvLabel
.setVisibility(View
.VISIBLE
);
488 * Updates the time that the file was last modified
489 * @param milliseconds Unix time to set
491 private void setTimeModified(long milliseconds
){
492 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
494 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
499 * Enables or disables buttons for a file being downloaded
501 private void setButtonsForTransferring() {
503 // let's protect the user from himself ;)
504 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
506 // show the progress bar for the transfer
507 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
508 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
509 progressText
.setVisibility(View
.VISIBLE
);
510 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
511 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
512 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
513 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
514 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
515 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
521 * Enables or disables buttons for a file locally available
523 private void setButtonsForDown() {
525 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
527 // hides the progress bar
528 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
529 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
530 progressText
.setVisibility(View
.GONE
);
535 * Enables or disables buttons for a file not locally available
537 private void setButtonsForRemote() {
539 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
541 // hides the progress bar
542 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
543 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
544 progressText
.setVisibility(View
.GONE
);
550 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
551 * the time that the file was created. There is a chance that this will
552 * be fixed in future versions. Use this method to check if this version of
553 * ownCloud has this fix.
554 * @return True, if ownCloud the ownCloud version is supporting creation time
556 private boolean ocVersionSupportsTimeCreated(){
557 /*if(mAccount != null){
558 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
559 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
560 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
561 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
569 public void onDismiss(EditNameDialog dialog
) {
570 if (dialog
.getResult()) {
571 String newFilename
= dialog
.getNewFilename();
572 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
573 mContainerActivity
.getFileOperationsHelper().renameFile(getFile(), newFilename
);
578 public void listenForTransferProgress() {
579 if (mProgressListener
!= null
) {
580 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
581 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
583 if (mContainerActivity
.getFileUploaderBinder() != null
) {
584 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
590 public void leaveTransferProgress() {
591 if (mProgressListener
!= null
) {
592 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
593 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
595 if (mContainerActivity
.getFileUploaderBinder() != null
) {
596 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
604 * Helper class responsible for updating the progress bar shown for file uploading or downloading
606 * @author David A. Velasco
608 private class ProgressListener
implements OnDatatransferProgressListener
{
609 int mLastPercent
= 0;
610 WeakReference
<ProgressBar
> mProgressBar
= null
;
612 ProgressListener(ProgressBar progressBar
) {
613 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
617 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
618 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
619 if (percent
!= mLastPercent
) {
620 ProgressBar pb
= mProgressBar
.get();
622 pb
.setProgress(percent
);
626 mLastPercent
= percent
;