[tx-robot] updated from transifex
[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
222
223 /**
224 * {@inheritDoc}
225 */
226 @Override
227 public boolean onOptionsItemSelected(MenuItem item) {
228 switch (item.getItemId()) {
229 case R.id.action_share_file: {
230 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
231 return true;
232 }
233 case R.id.action_unshare_file: {
234 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
235 return true;
236 }
237 case R.id.action_open_file_with: {
238 mContainerActivity.getFileOperationsHelper().openFile(getFile());
239 return true;
240 }
241 case R.id.action_remove_file: {
242 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
243 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
244 return true;
245 }
246 case R.id.action_rename_file: {
247 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
248 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
249 return true;
250 }
251 case R.id.action_cancel_download:
252 case R.id.action_cancel_upload: {
253 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
254 return true;
255 }
256 case R.id.action_download_file:
257 case R.id.action_sync_file: {
258 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
259 return true;
260 }
261 case R.id.action_send_file: {
262 // Obtain the file
263 if (!getFile().isDown()) { // Download the file
264 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
265 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
266
267 }
268 else {
269 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
270 }
271 return true;
272 }
273 case R.id.action_favorite_file:{
274 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
275 return true;
276 }
277 case R.id.action_unfavorite_file:{
278 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
279 return true;
280 }
281 default:
282 return false;
283 }
284 }
285
286 @Override
287 public void onClick(View v) {
288 switch (v.getId()) {
289 case R.id.fdFavorite: {
290 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
291 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
292 break;
293 }
294 case R.id.fdCancelBtn: {
295 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
296 break;
297 }
298 default:
299 Log_OC.e(TAG, "Incorrect view clicked!");
300 }
301 }
302
303
304 /**
305 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
306 *
307 * @return True when the fragment was created with the empty layout.
308 */
309 public boolean isEmpty() {
310 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
311 }
312
313
314 /**
315 * Use this method to signal this Activity that it shall update its view.
316 *
317 * @param file : An {@link OCFile}
318 */
319 public void updateFileDetails(OCFile file, Account ocAccount) {
320 setFile(file);
321 mAccount = ocAccount;
322 updateFileDetails(false, false);
323 }
324
325 /**
326 * Updates the view with all relevant details about that file.
327 * <p/>
328 * TODO Remove parameter when the transferring state of files is kept in database.
329 *
330 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
331 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
332 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
333 * @param refresh If 'true', try to refresh the whole file from the database
334 */
335 public void updateFileDetails(boolean transferring, boolean refresh) {
336 if (readyToShow()) {
337 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
338 if (refresh && storageManager != null) {
339 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
340 }
341 OCFile file = getFile();
342
343 // set file details
344 setFilename(file.getFileName());
345 setFiletype(file);
346 setFilesize(file.getFileLength());
347
348 setTimeModified(file.getModificationTimestamp());
349
350 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
351 cb.setChecked(file.isFavorite());
352
353 // configure UI for depending upon local state of the file
354 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
355 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
356 if (transferring ||
357 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
358 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
359 ) {
360 setButtonsForTransferring();
361
362 } else if (file.isDown()) {
363
364 setButtonsForDown();
365
366 } else {
367 // TODO load default preview image; when the local file is removed, the preview
368 // remains there
369 setButtonsForRemote();
370 }
371 }
372 getView().invalidate();
373 }
374
375 /**
376 * Checks if the fragment is ready to show details of a OCFile
377 *
378 * @return 'True' when the fragment is ready to show details of a file
379 */
380 private boolean readyToShow() {
381 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
382 }
383
384
385 /**
386 * Updates the filename in view
387 *
388 * @param filename to set
389 */
390 private void setFilename(String filename) {
391 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
392 if (tv != null) {
393 tv.setText(filename);
394 }
395 }
396
397 /**
398 * Updates the MIME type in view
399 * @param file : An {@link OCFile}
400 */
401 private void setFiletype(OCFile file) {
402 String mimetype = file.getMimetype();
403 TextView tv = (TextView) getView().findViewById(R.id.fdType);
404 if (tv != null) {
405 // mimetype MIME type to set
406 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
407 tv.setText(printableMimetype);
408 }
409
410 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
411
412 if (iv != null) {
413 Bitmap thumbnail;
414 iv.setTag(file.getFileId());
415
416 if (file.isImage()) {
417 String tagId = String.valueOf(file.getRemoteId());
418 thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
419
420 if (thumbnail != null && !file.needsUpdateThumbnail()) {
421 iv.setImageBitmap(thumbnail);
422 } else {
423 // generate new Thumbnail
424 if (ThumbnailsCacheManager.cancelPotentialWork(file, iv)) {
425 final ThumbnailsCacheManager.ThumbnailGenerationTask task =
426 new ThumbnailsCacheManager.ThumbnailGenerationTask(
427 iv, mContainerActivity.getStorageManager(), mAccount
428 );
429 if (thumbnail == null) {
430 thumbnail = ThumbnailsCacheManager.mDefaultImg;
431 }
432 final ThumbnailsCacheManager.AsyncDrawable asyncDrawable =
433 new ThumbnailsCacheManager.AsyncDrawable(
434 MainApp.getAppContext().getResources(),
435 thumbnail,
436 task
437 );
438 iv.setImageDrawable(asyncDrawable);
439 task.execute(file);
440 }
441 }
442 } else {
443 // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
444 String filename = file.getFileName();
445 iv.setImageResource(MimetypeIconUtil.getFileTypeIconId(mimetype, filename));
446 }
447 }
448 }
449
450 /**
451 * Updates the file size in view
452 *
453 * @param filesize in bytes to set
454 */
455 private void setFilesize(long filesize) {
456 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
457 if (tv != null) {
458 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
459 }
460 }
461
462 /**
463 * Updates the time that the file was last modified
464 *
465 * @param milliseconds Unix time to set
466 */
467 private void setTimeModified(long milliseconds) {
468 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
469 if (tv != null) {
470 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
471 }
472 }
473
474 /**
475 * Enables or disables buttons for a file being downloaded
476 */
477 private void setButtonsForTransferring() {
478 if (!isEmpty()) {
479 // let's protect the user from himself ;)
480 getView().findViewById(R.id.fdFavorite).setEnabled(false);
481
482 // show the progress bar for the transfer
483 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
484 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
485 progressText.setVisibility(View.VISIBLE);
486 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
487 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
488 //if (getFile().isDownloading()) {
489 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
490 progressText.setText(R.string.downloader_download_in_progress_ticker);
491 }
492 else {
493 if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
494 progressText.setText(R.string.uploader_upload_in_progress_ticker);
495 }
496 }
497 }
498 }
499
500 /**
501 * Enables or disables buttons for a file locally available
502 */
503 private void setButtonsForDown() {
504 if (!isEmpty()) {
505 getView().findViewById(R.id.fdFavorite).setEnabled(true);
506
507 // hides the progress bar
508 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
509 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
510 progressText.setVisibility(View.GONE);
511 }
512 }
513
514 /**
515 * Enables or disables buttons for a file not locally available
516 */
517 private void setButtonsForRemote() {
518 if (!isEmpty()) {
519 getView().findViewById(R.id.fdFavorite).setEnabled(true);
520
521 // hides the progress bar
522 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
523 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
524 progressText.setVisibility(View.GONE);
525 }
526 }
527
528
529 public void listenForTransferProgress() {
530 if (mProgressListener != null) {
531 if (mContainerActivity.getFileDownloaderBinder() != null) {
532 mContainerActivity.getFileDownloaderBinder().
533 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
534 }
535 if (mContainerActivity.getFileUploaderBinder() != null) {
536 mContainerActivity.getFileUploaderBinder().
537 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
538 }
539 }
540 }
541
542
543 public void leaveTransferProgress() {
544 if (mProgressListener != null) {
545 if (mContainerActivity.getFileDownloaderBinder() != null) {
546 mContainerActivity.getFileDownloaderBinder().
547 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
548 }
549 if (mContainerActivity.getFileUploaderBinder() != null) {
550 mContainerActivity.getFileUploaderBinder().
551 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
552 }
553 }
554 }
555
556
557 /**
558 * Helper class responsible for updating the progress bar shown for file uploading or
559 * downloading
560 */
561 private class ProgressListener implements OnDatatransferProgressListener {
562 int mLastPercent = 0;
563 WeakReference<ProgressBar> mProgressBar = null;
564
565 ProgressListener(ProgressBar progressBar) {
566 mProgressBar = new WeakReference<ProgressBar>(progressBar);
567 }
568
569 @Override
570 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
571 long totalToTransfer, String filename) {
572 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
573 if (percent != mLastPercent) {
574 ProgressBar pb = mProgressBar.get();
575 if (pb != null) {
576 pb.setProgress(percent);
577 pb.postInvalidate();
578 }
579 }
580 mLastPercent = percent;
581 }
582
583 }
584
585 }