Merge branch 'navigationDrawer_update' of github.com:owncloud/android into navigation...
[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.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.View.OnClickListener;
35 import android.view.ViewGroup;
36 import android.widget.CheckBox;
37 import android.widget.ImageView;
38 import android.widget.ProgressBar;
39 import android.widget.TextView;
40
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 getActivity()
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
364 // remains there
365 setButtonsForRemote();
366 }
367 }
368 getView().invalidate();
369 }
370
371 /**
372 * Checks if the fragment is ready to show details of a OCFile
373 *
374 * @return 'True' when the fragment is ready to show details of a file
375 */
376 private boolean readyToShow() {
377 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
378 }
379
380
381 /**
382 * Updates the filename in view
383 * @param filename to set
384 */
385 private void setFilename(String filename) {
386 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
387 if (tv != null)
388 tv.setText(filename);
389 }
390
391 /**
392 * Updates the MIME type in view
393 * @param mimetype to set
394 * @param filename
395 */
396 private void setFiletype(String mimetype, String filename) {
397 TextView tv = (TextView) getView().findViewById(R.id.fdType);
398 if (tv != null) {
399 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
400 tv.setText(printableMimetype);
401 }
402 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
403 if (iv != null) {
404 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
405 }
406 }
407
408 /**
409 * Updates the file size in view
410 * @param filesize in bytes to set
411 */
412 private void setFilesize(long filesize) {
413 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
414 if (tv != null)
415 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
416 }
417
418 /**
419 * Updates the time that the file was created in view
420 * @param milliseconds Unix time to set
421 */
422 private void setTimeCreated(long milliseconds){
423 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
424 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
425 if(tv != null){
426 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
427 tv.setVisibility(View.VISIBLE);
428 tvLabel.setVisibility(View.VISIBLE);
429 }
430 }
431
432 /**
433 * Updates the time that the file was last modified
434 * @param milliseconds Unix time to set
435 */
436 private void setTimeModified(long milliseconds){
437 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
438 if(tv != null){
439 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
440 }
441 }
442
443 /**
444 * Enables or disables buttons for a file being downloaded
445 */
446 private void setButtonsForTransferring() {
447 if (!isEmpty()) {
448 // let's protect the user from himself ;)
449 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
450
451 // show the progress bar for the transfer
452 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
453 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
454 progressText.setVisibility(View.VISIBLE);
455 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
456 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
457 //if (getFile().isDownloading()) {
458 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
459 progressText.setText(R.string.downloader_download_in_progress_ticker);
460 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
461 progressText.setText(R.string.uploader_upload_in_progress_ticker);
462 }
463 }
464 }
465
466 /**
467 * Enables or disables buttons for a file locally available
468 */
469 private void setButtonsForDown() {
470 if (!isEmpty()) {
471 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
472
473 // hides the progress bar
474 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
475 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
476 progressText.setVisibility(View.GONE);
477 }
478 }
479
480 /**
481 * Enables or disables buttons for a file not locally available
482 */
483 private void setButtonsForRemote() {
484 if (!isEmpty()) {
485 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
486
487 // hides the progress bar
488 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
489 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
490 progressText.setVisibility(View.GONE);
491 }
492 }
493
494
495 /**
496 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
497 * the time that the file was created. There is a chance that this will
498 * be fixed in future versions. Use this method to check if this version of
499 * ownCloud has this fix.
500 * @return True, if ownCloud the ownCloud version is supporting creation time
501 */
502 private boolean ocVersionSupportsTimeCreated(){
503 /*if(mAccount != null){
504 AccountManager accManager = (AccountManager) getActivity()
505 .getSystemService(Context.ACCOUNT_SERVICE);
506 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
507 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
508 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
509 return true;
510 }
511 }*/
512 return false;
513 }
514
515
516 public void listenForTransferProgress() {
517 if (mProgressListener != null) {
518 if (mContainerActivity.getFileDownloaderBinder() != null) {
519 mContainerActivity.getFileDownloaderBinder().
520 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
521 }
522 if (mContainerActivity.getFileUploaderBinder() != null) {
523 mContainerActivity.getFileUploaderBinder().
524 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
525 }
526 }
527 }
528
529
530 public void leaveTransferProgress() {
531 if (mProgressListener != null) {
532 if (mContainerActivity.getFileDownloaderBinder() != null) {
533 mContainerActivity.getFileDownloaderBinder().
534 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
535 }
536 if (mContainerActivity.getFileUploaderBinder() != null) {
537 mContainerActivity.getFileUploaderBinder().
538 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
539 }
540 }
541 }
542
543
544
545 /**
546 * Helper class responsible for updating the progress bar shown for file uploading or
547 * downloading
548 */
549 private class ProgressListener implements OnDatatransferProgressListener {
550 int mLastPercent = 0;
551 WeakReference<ProgressBar> mProgressBar = null;
552
553 ProgressListener(ProgressBar progressBar) {
554 mProgressBar = new WeakReference<ProgressBar>(progressBar);
555 }
556
557 @Override
558 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
559 long totalToTransfer, String filename) {
560 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
561 if (percent != mLastPercent) {
562 ProgressBar pb = mProgressBar.get();
563 if (pb != null) {
564 pb.setProgress(percent);
565 pb.postInvalidate();
566 }
567 }
568 mLastPercent = percent;
569 }
570
571 };
572
573 }