Merge branch 'develop' into update_propfind_for_server_8.1
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
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.
12 *
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.
17 *
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/>.
20 *
21 */
22 package com.owncloud.android.ui.fragment;
23
24 import java.lang.ref.WeakReference;
25
26 import android.accounts.Account;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.CheckBox;
34 import android.widget.ImageView;
35 import android.widget.ProgressBar;
36 import android.widget.TextView;
37
38 import com.actionbarsherlock.view.Menu;
39 import com.actionbarsherlock.view.MenuInflater;
40 import com.actionbarsherlock.view.MenuItem;
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;
55
56
57 /**
58 * This Fragment is used to display the details about a file.
59 */
60 public class FileDetailFragment extends FileFragment implements OnClickListener {
61
62 private int mLayout;
63 private View mView;
64 private Account mAccount;
65
66 public ProgressListener mProgressListener;
67
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";
71
72
73 /**
74 * Creates an empty details fragment.
75 *
76 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
77 */
78 public FileDetailFragment() {
79 super();
80 mAccount = null;
81 mLayout = R.layout.file_details_empty;
82 mProgressListener = null;
83 }
84
85 /**
86 * Creates a details fragment.
87 *
88 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
89 *
90 * @param fileToDetail An {@link OCFile} to show in the fragment
91 * @param ocAccount An ownCloud account; needed to start downloads
92 */
93 public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {
94 super(fileToDetail);
95 mAccount = ocAccount;
96 mLayout = R.layout.file_details_empty;
97 mProgressListener = null;
98 }
99
100
101 @Override
102 public void onActivityCreated(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
104 setHasOptionsMenu(true);
105 }
106
107
108 @Override
109 public View onCreateView(LayoutInflater inflater, ViewGroup container,
110 Bundle savedInstanceState) {
111
112 if (savedInstanceState != null) {
113 setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
114 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
115 }
116
117 if(getFile() != null && mAccount != null) {
118 mLayout = R.layout.file_details_fragment;
119 }
120
121 View view = null;
122 view = inflater.inflate(mLayout, null);
123 mView = view;
124
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);
130 }
131
132 updateFileDetails(false, false);
133 return view;
134 }
135
136 @Override
137 public void onSaveInstanceState(Bundle outState) {
138 super.onSaveInstanceState(outState);
139 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
140 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
141 }
142
143 @Override
144 public void onStart() {
145 super.onStart();
146 listenForTransferProgress();
147 }
148
149 @Override
150 public void onStop() {
151 leaveTransferProgress();
152 super.onStop();
153 }
154
155
156 @Override
157 public View getView() {
158 return super.getView() == null ? mView : super.getView();
159 }
160
161
162 /**
163 * {@inheritDoc}
164 */
165 @Override
166 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
167 super.onCreateOptionsMenu(menu, inflater);
168 inflater.inflate(R.menu.file_actions_menu, menu);
169 }
170
171
172 /**
173 * {@inheritDoc}
174 */
175 @Override
176 public void onPrepareOptionsMenu (Menu menu) {
177 super.onPrepareOptionsMenu(menu);
178
179 if (mContainerActivity.getStorageManager() != null) {
180 FileMenuFilter mf = new FileMenuFilter(
181 getFile(),
182 mContainerActivity.getStorageManager().getAccount(),
183 mContainerActivity,
184 getSherlockActivity()
185 );
186 mf.filter(menu);
187 }
188
189 // additional restriction for this fragment
190 MenuItem item = menu.findItem(R.id.action_see_details);
191 if (item != null) {
192 item.setVisible(false);
193 item.setEnabled(false);
194 }
195
196 // additional restriction for this fragment
197 item = menu.findItem(R.id.action_move);
198 if (item != null) {
199 item.setVisible(false);
200 item.setEnabled(false);
201 }
202 }
203
204
205 /**
206 * {@inheritDoc}
207 */
208 @Override
209 public boolean onOptionsItemSelected(MenuItem item) {
210 switch (item.getItemId()) {
211 case R.id.action_share_file: {
212 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
213 return true;
214 }
215 case R.id.action_unshare_file: {
216 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
217 return true;
218 }
219 case R.id.action_open_file_with: {
220 mContainerActivity.getFileOperationsHelper().openFile(getFile());
221 return true;
222 }
223 case R.id.action_remove_file: {
224 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
225 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
226 return true;
227 }
228 case R.id.action_rename_file: {
229 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
230 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
231 return true;
232 }
233 case R.id.action_cancel_download:
234 case R.id.action_cancel_upload: {
235 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
236 return true;
237 }
238 case R.id.action_download_file:
239 case R.id.action_sync_file: {
240 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
241 return true;
242 }
243 case R.id.action_send_file: {
244 // Obtain the file
245 if (!getFile().isDown()) { // Download the file
246 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
247 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
248
249 } else {
250 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
251 }
252 return true;
253 }
254 default:
255 return false;
256 }
257 }
258
259 @Override
260 public void onClick(View v) {
261 switch (v.getId()) {
262 case R.id.fdKeepInSync: {
263 toggleKeepInSync();
264 break;
265 }
266 case R.id.fdCancelBtn: {
267 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
268 break;
269 }
270 default:
271 Log_OC.e(TAG, "Incorrect view clicked!");
272 }
273 }
274
275
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);
281
282 /// register the OCFile instance in the observer service to monitor local updates
283 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
284 getActivity(),
285 file,
286 mAccount,
287 cb.isChecked());
288 getActivity().startService(observedFileIntent);
289
290 /// immediate content synchronization
291 if (file.keepInSync()) {
292 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
293 }
294 }
295
296 /**
297 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
298 *
299 * @return True when the fragment was created with the empty layout.
300 */
301 public boolean isEmpty() {
302 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
303 }
304
305
306 /**
307 * Use this method to signal this Activity that it shall update its view.
308 *
309 * @param file : An {@link OCFile}
310 */
311 public void updateFileDetails(OCFile file, Account ocAccount) {
312 setFile(file);
313 mAccount = ocAccount;
314 updateFileDetails(false, false);
315 }
316
317 /**
318 * Updates the view with all relevant details about that file.
319 *
320 * TODO Remove parameter when the transferring state of files is kept in database.
321 *
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.
325 *
326 * @param refresh If 'true', try to refresh the whole file from the database
327 */
328 public void updateFileDetails(boolean transferring, boolean refresh) {
329 if (readyToShow()) {
330 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
331 if (refresh && storageManager != null) {
332 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
333 }
334 OCFile file = getFile();
335
336 // set file details
337 setFilename(file.getFileName());
338 setFiletype(file.getMimetype(), file.getFileName());
339 setFilesize(file.getFileLength());
340 if(ocVersionSupportsTimeCreated()){
341 setTimeCreated(file.getCreationTimestamp());
342 }
343
344 setTimeModified(file.getModificationTimestamp());
345
346 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
347 cb.setChecked(file.keepInSync());
348
349 // configure UI for depending upon local state of the file
350 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
351 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
352 if (transferring ||
353 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
354 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
355 ) {
356 setButtonsForTransferring();
357
358 } else if (file.isDown()) {
359
360 setButtonsForDown();
361
362 } else {
363 // TODO load default preview image; when the local file is removed, the preview remains there
364 setButtonsForRemote();
365 }
366 }
367 getView().invalidate();
368 }
369
370 /**
371 * Checks if the fragment is ready to show details of a OCFile
372 *
373 * @return 'True' when the fragment is ready to show details of a file
374 */
375 private boolean readyToShow() {
376 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
377 }
378
379
380 /**
381 * Updates the filename in view
382 * @param filename to set
383 */
384 private void setFilename(String filename) {
385 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
386 if (tv != null)
387 tv.setText(filename);
388 }
389
390 /**
391 * Updates the MIME type in view
392 * @param mimetype to set
393 * @param filename
394 */
395 private void setFiletype(String mimetype, String filename) {
396 TextView tv = (TextView) getView().findViewById(R.id.fdType);
397 if (tv != null) {
398 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
399 tv.setText(printableMimetype);
400 }
401 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
402 if (iv != null) {
403 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
404 }
405 }
406
407 /**
408 * Updates the file size in view
409 * @param filesize in bytes to set
410 */
411 private void setFilesize(long filesize) {
412 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
413 if (tv != null)
414 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
415 }
416
417 /**
418 * Updates the time that the file was created in view
419 * @param milliseconds Unix time to set
420 */
421 private void setTimeCreated(long milliseconds){
422 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
423 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
424 if(tv != null){
425 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
426 tv.setVisibility(View.VISIBLE);
427 tvLabel.setVisibility(View.VISIBLE);
428 }
429 }
430
431 /**
432 * Updates the time that the file was last modified
433 * @param milliseconds Unix time to set
434 */
435 private void setTimeModified(long milliseconds){
436 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
437 if(tv != null){
438 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
439 }
440 }
441
442 /**
443 * Enables or disables buttons for a file being downloaded
444 */
445 private void setButtonsForTransferring() {
446 if (!isEmpty()) {
447 // let's protect the user from himself ;)
448 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
449
450 // show the progress bar for the transfer
451 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
452 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
453 progressText.setVisibility(View.VISIBLE);
454 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
455 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
456 //if (getFile().isDownloading()) {
457 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
458 progressText.setText(R.string.downloader_download_in_progress_ticker);
459 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
460 progressText.setText(R.string.uploader_upload_in_progress_ticker);
461 }
462 }
463 }
464
465 /**
466 * Enables or disables buttons for a file locally available
467 */
468 private void setButtonsForDown() {
469 if (!isEmpty()) {
470 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
471
472 // hides the progress bar
473 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
474 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
475 progressText.setVisibility(View.GONE);
476 }
477 }
478
479 /**
480 * Enables or disables buttons for a file not locally available
481 */
482 private void setButtonsForRemote() {
483 if (!isEmpty()) {
484 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
485
486 // hides the progress bar
487 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
488 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
489 progressText.setVisibility(View.GONE);
490 }
491 }
492
493
494 /**
495 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
496 * the time that the file was created. There is a chance that this will
497 * be fixed in future versions. Use this method to check if this version of
498 * ownCloud has this fix.
499 * @return True, if ownCloud the ownCloud version is supporting creation time
500 */
501 private boolean ocVersionSupportsTimeCreated(){
502 /*if(mAccount != null){
503 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
504 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
505 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
506 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
507 return true;
508 }
509 }*/
510 return false;
511 }
512
513
514 public void listenForTransferProgress() {
515 if (mProgressListener != null) {
516 if (mContainerActivity.getFileDownloaderBinder() != null) {
517 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
518 }
519 if (mContainerActivity.getFileUploaderBinder() != null) {
520 mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
521 }
522 }
523 }
524
525
526 public void leaveTransferProgress() {
527 if (mProgressListener != null) {
528 if (mContainerActivity.getFileDownloaderBinder() != null) {
529 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
530 }
531 if (mContainerActivity.getFileUploaderBinder() != null) {
532 mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
533 }
534 }
535 }
536
537
538
539 /**
540 * Helper class responsible for updating the progress bar shown for file uploading or downloading
541 */
542 private class ProgressListener implements OnDatatransferProgressListener {
543 int mLastPercent = 0;
544 WeakReference<ProgressBar> mProgressBar = null;
545
546 ProgressListener(ProgressBar progressBar) {
547 mProgressBar = new WeakReference<ProgressBar>(progressBar);
548 }
549
550 @Override
551 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
552 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
553 if (percent != mLastPercent) {
554 ProgressBar pb = mProgressBar.get();
555 if (pb != null) {
556 pb.setProgress(percent);
557 pb.postInvalidate();
558 }
559 }
560 mLastPercent = percent;
561 }
562
563 };
564
565 }