Merge remote-tracking branch 'remotes/upstream/master' into beta
[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 android.accounts.Account;
25 import android.graphics.Bitmap;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.view.ViewGroup;
34 import android.widget.CheckBox;
35 import android.widget.ImageView;
36 import android.widget.ProgressBar;
37 import android.widget.TextView;
38
39 import com.owncloud.android.MainApp;
40 import com.owncloud.android.R;
41 import com.owncloud.android.datamodel.FileDataStorageManager;
42 import com.owncloud.android.datamodel.OCFile;
43 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
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.ui.activity.FileActivity;
50 import com.owncloud.android.ui.activity.FileDisplayActivity;
51 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
52 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
53 import com.owncloud.android.utils.DisplayUtils;
54 import com.owncloud.android.utils.MimetypeIconUtil;
55
56 import java.lang.ref.WeakReference;
57
58
59 /**
60 * This Fragment is used to display the details about a file.
61 */
62 public class FileDetailFragment extends FileFragment implements OnClickListener {
63
64 private int mLayout;
65 private View mView;
66 private Account mAccount;
67
68 public ProgressListener mProgressListener;
69
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";
73
74 private static final String ARG_FILE = "FILE";
75 private static final String ARG_ACCOUNT = "ACCOUNT";
76
77
78 /**
79 * Public factory method to create new FileDetailFragment instances.
80 *
81 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
82 *
83 * @param fileToDetail An {@link OCFile} to show in the fragment
84 * @param account An ownCloud account; needed to start downloads
85 * @return New fragment with arguments set
86 */
87 public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
88 FileDetailFragment frag = new FileDetailFragment();
89 Bundle args = new Bundle();
90 args.putParcelable(ARG_FILE, fileToDetail);
91 args.putParcelable(ARG_ACCOUNT, account);
92 frag.setArguments(args);
93 return frag;
94 }
95
96 /**
97 * Creates an empty details fragment.
98 *
99 * It's necessary to keep a public constructor without parameters; the system uses it when tries
100 * to reinstantiate a fragment automatically.
101 */
102 public FileDetailFragment() {
103 super();
104 mAccount = null;
105 mLayout = R.layout.file_details_empty;
106 mProgressListener = null;
107 }
108
109
110 @Override
111 public void onActivityCreated(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113 setHasOptionsMenu(true);
114 }
115
116
117 @Override
118 public View onCreateView(LayoutInflater inflater, ViewGroup container,
119 Bundle savedInstanceState) {
120
121 setFile((OCFile) getArguments().getParcelable(ARG_FILE));
122 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
123
124 if (savedInstanceState != null) {
125 setFile((OCFile) savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
126 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
127 }
128
129 if (getFile() != null && mAccount != null) {
130 mLayout = R.layout.file_details_fragment;
131 }
132
133 mView = inflater.inflate(mLayout, null);
134
135 if (mLayout == R.layout.file_details_fragment) {
136 mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
137 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
138 DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
139 mProgressListener = new ProgressListener(progressBar);
140 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
141 }
142
143 updateFileDetails(false, false);
144 return mView;
145 }
146
147 @Override
148 public void onSaveInstanceState(Bundle outState) {
149 super.onSaveInstanceState(outState);
150 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
151 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
152 }
153
154 @Override
155 public void onStart() {
156 super.onStart();
157 listenForTransferProgress();
158 }
159
160 @Override
161 public void onStop() {
162 leaveTransferProgress();
163 super.onStop();
164 }
165
166
167 @Override
168 public View getView() {
169 return super.getView() == null ? mView : super.getView();
170 }
171
172
173 /**
174 * {@inheritDoc}
175 */
176 @Override
177 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
178 super.onCreateOptionsMenu(menu, inflater);
179 inflater.inflate(R.menu.file_actions_menu, menu);
180 }
181
182
183 /**
184 * {@inheritDoc}
185 */
186 @Override
187 public void onPrepareOptionsMenu(Menu menu) {
188 super.onPrepareOptionsMenu(menu);
189
190 if (mContainerActivity.getStorageManager() != null) {
191 FileMenuFilter mf = new FileMenuFilter(
192 getFile(),
193 mContainerActivity.getStorageManager().getAccount(),
194 mContainerActivity,
195 getActivity()
196 );
197 mf.filter(menu);
198 }
199
200 // additional restriction for this fragment
201 MenuItem item = menu.findItem(R.id.action_see_details);
202 if (item != null) {
203 item.setVisible(false);
204 item.setEnabled(false);
205 }
206
207 // additional restriction for this fragment
208 item = menu.findItem(R.id.action_move);
209 if (item != null) {
210 item.setVisible(false);
211 item.setEnabled(false);
212 }
213
214 // additional restriction for this fragment
215 item = menu.findItem(R.id.action_copy);
216 if (item != null) {
217 item.setVisible(false);
218 item.setEnabled(false);
219 }
220
221 item = menu.findItem(R.id.action_switch_view);
222 if (item != null){
223 item.setVisible(false);
224 item.setEnabled(false);
225 }
226
227 item = menu.findItem(R.id.action_sync_account);
228 if (item != null) {
229 item.setVisible(false);
230 item.setEnabled(false);
231 }
232
233 item = menu.findItem(R.id.action_sort);
234 if (item != null) {
235 item.setVisible(false);
236 item.setEnabled(false);
237 }
238 }
239
240
241 /**
242 * {@inheritDoc}
243 */
244 @Override
245 public boolean onOptionsItemSelected(MenuItem item) {
246 switch (item.getItemId()) {
247 case R.id.action_share_file: {
248 mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
249 return true;
250 }
251 case R.id.action_open_file_with: {
252 mContainerActivity.getFileOperationsHelper().openFile(getFile());
253 return true;
254 }
255 case R.id.action_remove_file: {
256 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
257 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
258 return true;
259 }
260 case R.id.action_rename_file: {
261 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
262 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
263 return true;
264 }
265 case R.id.action_cancel_sync: {
266 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
267 return true;
268 }
269 case R.id.action_download_file:
270 case R.id.action_sync_file: {
271 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
272 return true;
273 }
274 case R.id.action_send_file: {
275 // Obtain the file
276 if (!getFile().isDown()) { // Download the file
277 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
278 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
279 }
280 else {
281 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
282 }
283 return true;
284 }
285 case R.id.action_favorite_file:{
286 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
287 return true;
288 }
289 case R.id.action_unfavorite_file:{
290 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
291 return true;
292 }
293 default:
294 return false;
295 }
296 }
297
298 @Override
299 public void onClick(View v) {
300 switch (v.getId()) {
301 case R.id.fdFavorite: {
302 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
303 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
304 break;
305 }
306 case R.id.fdCancelBtn: {
307 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
308 break;
309 }
310 default:
311 Log_OC.e(TAG, "Incorrect view clicked!");
312 }
313 }
314
315 /**
316 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
317 *
318 * @return True when the fragment was created with the empty layout.
319 */
320 public boolean isEmpty() {
321 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
322 }
323
324
325 /**
326 * Use this method to signal this Activity that it shall update its view.
327 *
328 * @param file : An {@link OCFile}
329 */
330 public void updateFileDetails(OCFile file, Account ocAccount) {
331 setFile(file);
332 mAccount = ocAccount;
333 updateFileDetails(false, false);
334 }
335
336 /**
337 * Updates the view with all relevant details about that file.
338 * <p/>
339 * TODO Remove parameter when the transferring state of files is kept in database.
340 *
341 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
342 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
343 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
344 * @param refresh If 'true', try to refresh the whole file from the database
345 */
346 public void updateFileDetails(boolean transferring, boolean refresh) {
347 if (readyToShow()) {
348 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
349 if (refresh && storageManager != null) {
350 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
351 }
352 OCFile file = getFile();
353
354 // set file details
355 setFilename(file.getFileName());
356 setFiletype(file);
357 setFilesize(file.getFileLength());
358
359 setTimeModified(file.getModificationTimestamp());
360
361 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
362 cb.setChecked(file.isFavorite());
363
364 // configure UI for depending upon local state of the file
365 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
366 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
367 if (transferring ||
368 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
369 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
370 ) {
371 setButtonsForTransferring();
372
373 } else if (file.isDown()) {
374
375 setButtonsForDown();
376
377 } else {
378 // TODO load default preview image; when the local file is removed, the preview
379 // remains there
380 setButtonsForRemote();
381 }
382 }
383 getView().invalidate();
384 }
385
386 /**
387 * Checks if the fragment is ready to show details of a OCFile
388 *
389 * @return 'True' when the fragment is ready to show details of a file
390 */
391 private boolean readyToShow() {
392 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
393 }
394
395
396 /**
397 * Updates the filename in view
398 *
399 * @param filename to set
400 */
401 private void setFilename(String filename) {
402 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
403 if (tv != null) {
404 tv.setText(filename);
405 }
406 }
407
408 /**
409 * Updates the MIME type in view
410 * @param file : An {@link OCFile}
411 */
412 private void setFiletype(OCFile file) {
413 String mimetype = file.getMimetype();
414 TextView tv = (TextView) getView().findViewById(R.id.fdType);
415 if (tv != null) {
416 // mimetype MIME type to set
417 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
418 tv.setText(printableMimetype);
419 }
420
421 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
422
423 if (iv != null) {
424 Bitmap thumbnail;
425 iv.setTag(file.getFileId());
426
427 if (file.isImage()) {
428 String tagId = String.valueOf(file.getRemoteId());
429 thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
430
431 if (thumbnail != null && !file.needsUpdateThumbnail()) {
432 iv.setImageBitmap(thumbnail);
433 } else {
434 // generate new Thumbnail
435 if (ThumbnailsCacheManager.cancelPotentialWork(file, iv)) {
436 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
437 new ThumbnailsCacheManager.ThumbnailGenerationTask(
438 iv, mContainerActivity.getStorageManager(), mAccount
439 );
440 if (thumbnail == null) {
441 thumbnail = ThumbnailsCacheManager.mDefaultImg;
442 }
443 final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
444 new ThumbnailsCacheManager.AsyncDrawable(
445 MainApp.getAppContext().getResources(),
446 thumbnail,
447 task
448 );
449 iv.setImageDrawable(asyncDrawable);
450 task.execute(file);
451 }
452 }
453 } else {
454 // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
455 String filename = file.getFileName();
456 iv.setImageResource(MimetypeIconUtil.getFileTypeIconId(mimetype, filename));
457 }
458 }
459 }
460
461 /**
462 * Updates the file size in view
463 *
464 * @param filesize in bytes to set
465 */
466 private void setFilesize(long filesize) {
467 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
468 if (tv != null) {
469 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
470 }
471 }
472
473 /**
474 * Updates the time that the file was last modified
475 *
476 * @param milliseconds Unix time to set
477 */
478 private void setTimeModified(long milliseconds) {
479 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
480 if (tv != null) {
481 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
482 }
483 }
484
485 /**
486 * Enables or disables buttons for a file being downloaded
487 */
488 private void setButtonsForTransferring() {
489 if (!isEmpty()) {
490 // let's protect the user from himself ;)
491 getView().findViewById(R.id.fdFavorite).setEnabled(false);
492
493 // show the progress bar for the transfer
494 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
495 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
496 progressText.setVisibility(View.VISIBLE);
497 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
498 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
499 //if (getFile().isDownloading()) {
500 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
501 progressText.setText(R.string.downloader_download_in_progress_ticker);
502 }
503 else {
504 if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
505 progressText.setText(R.string.uploader_upload_in_progress_ticker);
506 }
507 }
508 }
509 }
510
511 /**
512 * Enables or disables buttons for a file locally available
513 */
514 private void setButtonsForDown() {
515 if (!isEmpty()) {
516 getView().findViewById(R.id.fdFavorite).setEnabled(true);
517
518 // hides the progress bar
519 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
520 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
521 progressText.setVisibility(View.GONE);
522 }
523 }
524
525 /**
526 * Enables or disables buttons for a file not locally available
527 */
528 private void setButtonsForRemote() {
529 if (!isEmpty()) {
530 getView().findViewById(R.id.fdFavorite).setEnabled(true);
531
532 // hides the progress bar
533 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
534 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
535 progressText.setVisibility(View.GONE);
536 }
537 }
538
539
540 public void listenForTransferProgress() {
541 if (mProgressListener != null) {
542 if (mContainerActivity.getFileDownloaderBinder() != null) {
543 mContainerActivity.getFileDownloaderBinder().
544 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
545 }
546 if (mContainerActivity.getFileUploaderBinder() != null) {
547 mContainerActivity.getFileUploaderBinder().
548 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
549 }
550 }
551 }
552
553
554 public void leaveTransferProgress() {
555 if (mProgressListener != null) {
556 if (mContainerActivity.getFileDownloaderBinder() != null) {
557 mContainerActivity.getFileDownloaderBinder().
558 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
559 }
560 if (mContainerActivity.getFileUploaderBinder() != null) {
561 mContainerActivity.getFileUploaderBinder().
562 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
563 }
564 }
565 }
566
567
568 /**
569 * Helper class responsible for updating the progress bar shown for file uploading or
570 * downloading
571 */
572 private class ProgressListener implements OnDatatransferProgressListener {
573 int mLastPercent = 0;
574 WeakReference<ProgressBar> mProgressBar = null;
575
576 ProgressListener(ProgressBar progressBar) {
577 mProgressBar = new WeakReference<ProgressBar>(progressBar);
578 }
579
580 @Override
581 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
582 long totalToTransfer, String filename) {
583 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
584 if (percent != mLastPercent) {
585 ProgressBar pb = mProgressBar.get();
586 if (pb != null) {
587 pb.setProgress(percent);
588 pb.postInvalidate();
589 }
590 }
591 mLastPercent = percent;
592 }
593
594 }
595
596 }