Dispaly thumbnail when file is image
[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.graphics.Bitmap;
29 import android.os.Bundle;
30 import android.view.LayoutInflater;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.View.OnClickListener;
36 import android.view.ViewGroup;
37 import android.widget.CheckBox;
38 import android.widget.ImageView;
39 import android.widget.ProgressBar;
40 import android.widget.TextView;
41
42 import com.owncloud.android.R;
43 import com.owncloud.android.datamodel.FileDataStorageManager;
44 import com.owncloud.android.datamodel.OCFile;
45 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
46 import com.owncloud.android.files.FileMenuFilter;
47 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
48 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
49 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
50 import com.owncloud.android.lib.common.utils.Log_OC;
51 import com.owncloud.android.services.observer.FileObserverService;
52 import com.owncloud.android.ui.activity.FileActivity;
53 import com.owncloud.android.ui.activity.FileDisplayActivity;
54 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
55 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
56 import com.owncloud.android.utils.DisplayUtils;
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.fdKeepInSync).setOnClickListener(this);
137 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
138 mProgressListener = new ProgressListener(progressBar);
139 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
140 }
141
142 updateFileDetails(false, false);
143 return mView;
144 }
145
146 @Override
147 public void onSaveInstanceState(Bundle outState) {
148 super.onSaveInstanceState(outState);
149 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
150 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
151 }
152
153 @Override
154 public void onStart() {
155 super.onStart();
156 listenForTransferProgress();
157 }
158
159 @Override
160 public void onStop() {
161 leaveTransferProgress();
162 super.onStop();
163 }
164
165
166 @Override
167 public View getView() {
168 return super.getView() == null ? mView : super.getView();
169 }
170
171
172 /**
173 * {@inheritDoc}
174 */
175 @Override
176 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
177 super.onCreateOptionsMenu(menu, inflater);
178 inflater.inflate(R.menu.file_actions_menu, menu);
179 }
180
181
182 /**
183 * {@inheritDoc}
184 */
185 @Override
186 public void onPrepareOptionsMenu (Menu menu) {
187 super.onPrepareOptionsMenu(menu);
188
189 if (mContainerActivity.getStorageManager() != null) {
190 FileMenuFilter mf = new FileMenuFilter(
191 getFile(),
192 mContainerActivity.getStorageManager().getAccount(),
193 mContainerActivity,
194 getActivity()
195 );
196 mf.filter(menu);
197 }
198
199 // additional restriction for this fragment
200 MenuItem item = menu.findItem(R.id.action_see_details);
201 if (item != null) {
202 item.setVisible(false);
203 item.setEnabled(false);
204 }
205
206 // additional restriction for this fragment
207 item = menu.findItem(R.id.action_move);
208 if (item != null) {
209 item.setVisible(false);
210 item.setEnabled(false);
211 }
212 }
213
214
215 /**
216 * {@inheritDoc}
217 */
218 @Override
219 public boolean onOptionsItemSelected(MenuItem item) {
220 switch (item.getItemId()) {
221 case R.id.action_share_file: {
222 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
223 return true;
224 }
225 case R.id.action_unshare_file: {
226 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
227 return true;
228 }
229 case R.id.action_open_file_with: {
230 mContainerActivity.getFileOperationsHelper().openFile(getFile());
231 return true;
232 }
233 case R.id.action_remove_file: {
234 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
235 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
236 return true;
237 }
238 case R.id.action_rename_file: {
239 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
240 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
241 return true;
242 }
243 case R.id.action_cancel_download:
244 case R.id.action_cancel_upload: {
245 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
246 return true;
247 }
248 case R.id.action_download_file:
249 case R.id.action_sync_file: {
250 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
251 return true;
252 }
253 case R.id.action_send_file: {
254 // Obtain the file
255 if (!getFile().isDown()) { // Download the file
256 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
257 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
258
259 } else {
260 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
261 }
262 return true;
263 }
264 default:
265 return false;
266 }
267 }
268
269 @Override
270 public void onClick(View v) {
271 switch (v.getId()) {
272 case R.id.fdKeepInSync: {
273 toggleKeepInSync();
274 break;
275 }
276 case R.id.fdCancelBtn: {
277 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
278 break;
279 }
280 default:
281 Log_OC.e(TAG, "Incorrect view clicked!");
282 }
283 }
284
285
286 private void toggleKeepInSync() {
287 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
288 OCFile file = getFile();
289 file.setKeepInSync(cb.isChecked());
290 mContainerActivity.getStorageManager().saveFile(file);
291
292 /// register the OCFile instance in the observer service to monitor local updates
293 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
294 getActivity(),
295 file,
296 mAccount,
297 cb.isChecked());
298 getActivity().startService(observedFileIntent);
299
300 /// immediate content synchronization
301 if (file.keepInSync()) {
302 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
303 }
304 }
305
306 /**
307 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
308 *
309 * @return True when the fragment was created with the empty layout.
310 */
311 public boolean isEmpty() {
312 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
313 }
314
315
316 /**
317 * Use this method to signal this Activity that it shall update its view.
318 *
319 * @param file : An {@link OCFile}
320 */
321 public void updateFileDetails(OCFile file, Account ocAccount) {
322 setFile(file);
323 mAccount = ocAccount;
324 updateFileDetails(false, false);
325 }
326
327 /**
328 * Updates the view with all relevant details about that file.
329 *
330 * TODO Remove parameter when the transferring state of files is kept in database.
331 *
332 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
333 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
334 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
335 *
336 * @param refresh If 'true', try to refresh the whole file from the database
337 */
338 public void updateFileDetails(boolean transferring, boolean refresh) {
339 if (readyToShow()) {
340 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
341 if (refresh && storageManager != null) {
342 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
343 }
344 OCFile file = getFile();
345
346 // set file details
347 setFilename(file.getFileName());
348 setFiletype(file);
349 setFilesize(file.getFileLength());
350
351 setTimeModified(file.getModificationTimestamp());
352
353 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
354 cb.setChecked(file.keepInSync());
355
356 // configure UI for depending upon local state of the file
357 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
358 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
359 if (transferring ||
360 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
361 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
362 ) {
363 setButtonsForTransferring();
364
365 } else if (file.isDown()) {
366
367 setButtonsForDown();
368
369 } else {
370 // TODO load default preview image; when the local file is removed, the preview
371 // remains there
372 setButtonsForRemote();
373 }
374 }
375 getView().invalidate();
376 }
377
378 /**
379 * Checks if the fragment is ready to show details of a OCFile
380 *
381 * @return 'True' when the fragment is ready to show details of a file
382 */
383 private boolean readyToShow() {
384 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
385 }
386
387
388 /**
389 * Updates the filename in view
390 * @param filename to set
391 */
392 private void setFilename(String filename) {
393 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
394 if (tv != null)
395 tv.setText(filename);
396 }
397
398 /**
399 * Updates the MIME type in view
400 * @param file : An {@link OCFile}
401 */
402 private void setFiletype(OCFile file) {
403 String mimetype = file.getMimetype();
404 TextView tv = (TextView) getView().findViewById(R.id.fdType);
405 if (tv != null) {
406 // mimetype MIME type to set
407 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
408 tv.setText(printableMimetype);
409 }
410 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
411 if (iv != null) {
412 Bitmap thumbnail = null;
413 if (file.isImage()) {
414 String tagId = String.valueOf(file.getRemoteId());
415 thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
416 }
417 if (thumbnail != null) {
418 // Display thumbnail
419 iv.setImageBitmap(thumbnail);
420 } else {
421 // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
422 String filename = file.getFileName();
423 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
424 }
425 }
426 }
427
428 /**
429 * Updates the file size in view
430 * @param filesize in bytes to set
431 */
432 private void setFilesize(long filesize) {
433 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
434 if (tv != null)
435 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
436 }
437
438 /**
439 * Updates the time that the file was last modified
440 * @param milliseconds Unix time to set
441 */
442 private void setTimeModified(long milliseconds){
443 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
444 if(tv != null){
445 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
446 }
447 }
448
449 /**
450 * Enables or disables buttons for a file being downloaded
451 */
452 private void setButtonsForTransferring() {
453 if (!isEmpty()) {
454 // let's protect the user from himself ;)
455 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
456
457 // show the progress bar for the transfer
458 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
459 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
460 progressText.setVisibility(View.VISIBLE);
461 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
462 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
463 //if (getFile().isDownloading()) {
464 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
465 progressText.setText(R.string.downloader_download_in_progress_ticker);
466 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
467 progressText.setText(R.string.uploader_upload_in_progress_ticker);
468 }
469 }
470 }
471
472 /**
473 * Enables or disables buttons for a file locally available
474 */
475 private void setButtonsForDown() {
476 if (!isEmpty()) {
477 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
478
479 // hides the progress bar
480 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
481 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
482 progressText.setVisibility(View.GONE);
483 }
484 }
485
486 /**
487 * Enables or disables buttons for a file not locally available
488 */
489 private void setButtonsForRemote() {
490 if (!isEmpty()) {
491 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
492
493 // hides the progress bar
494 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
495 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
496 progressText.setVisibility(View.GONE);
497 }
498 }
499
500
501 public void listenForTransferProgress() {
502 if (mProgressListener != null) {
503 if (mContainerActivity.getFileDownloaderBinder() != null) {
504 mContainerActivity.getFileDownloaderBinder().
505 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
506 }
507 if (mContainerActivity.getFileUploaderBinder() != null) {
508 mContainerActivity.getFileUploaderBinder().
509 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
510 }
511 }
512 }
513
514
515 public void leaveTransferProgress() {
516 if (mProgressListener != null) {
517 if (mContainerActivity.getFileDownloaderBinder() != null) {
518 mContainerActivity.getFileDownloaderBinder().
519 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
520 }
521 if (mContainerActivity.getFileUploaderBinder() != null) {
522 mContainerActivity.getFileUploaderBinder().
523 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
524 }
525 }
526 }
527
528
529
530 /**
531 * Helper class responsible for updating the progress bar shown for file uploading or
532 * downloading
533 */
534 private class ProgressListener implements OnDatatransferProgressListener {
535 int mLastPercent = 0;
536 WeakReference<ProgressBar> mProgressBar = null;
537
538 ProgressListener(ProgressBar progressBar) {
539 mProgressBar = new WeakReference<ProgressBar>(progressBar);
540 }
541
542 @Override
543 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
544 long totalToTransfer, String filename) {
545 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
546 if (percent != mLastPercent) {
547 ProgressBar pb = mProgressBar.get();
548 if (pb != null) {
549 pb.setProgress(percent);
550 pb.postInvalidate();
551 }
552 }
553 mLastPercent = percent;
554 }
555
556 }
557
558 }