2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.preview
;
22 import java
.lang
.ref
.WeakReference
;
24 import android
.accounts
.Account
;
25 import android
.annotation
.SuppressLint
;
26 import android
.app
.Activity
;
27 import android
.graphics
.Bitmap
;
28 import android
.graphics
.Point
;
29 import android
.os
.AsyncTask
;
30 import android
.os
.Bundle
;
31 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.Menu
;
34 import android
.view
.MenuInflater
;
35 import android
.view
.MenuItem
;
36 import android
.view
.View
;
37 import android
.view
.View
.OnClickListener
;
38 import android
.view
.ViewGroup
;
39 import android
.widget
.ImageView
;
40 import android
.widget
.ProgressBar
;
41 import android
.widget
.TextView
;
43 import com
.owncloud
.android
.MainApp
;
44 import com
.owncloud
.android
.R
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
47 import com
.owncloud
.android
.files
.FileMenuFilter
;
48 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
49 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
50 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
51 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
52 import com
.owncloud
.android
.utils
.BitmapUtils
;
53 import com
.owncloud
.android
.utils
.DisplayUtils
;
55 import third_parties
.michaelOrtiz
.TouchImageViewCustom
;
59 * This fragment shows a preview of a downloaded image.
61 * Trying to get an instance with a NULL {@link OCFile} will produce an
62 * {@link IllegalStateException}.
64 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on
67 public class PreviewImageFragment
extends FileFragment
{
69 public static final String EXTRA_FILE
= "FILE";
71 private static final String ARG_FILE
= "FILE";
72 private static final String ARG_IGNORE_FIRST
= "IGNORE_FIRST";
74 private TouchImageViewCustom mImageView
;
75 private TextView mMessageView
;
76 private ProgressBar mProgressWheel
;
78 private Boolean mShowResizedImage
= false
;
80 public Bitmap mBitmap
= null
;
82 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
84 private boolean mIgnoreFirstSavedState
;
86 private LoadBitmapTask mLoadBitmapTask
= null
;
90 * Public factory method to create a new fragment that previews an image.
92 * Android strongly recommends keep the empty constructor of fragments as the only public
94 * use {@link #setArguments(Bundle)} to set the needed arguments.
96 * This method hides to client objects the need of doing the construction in two steps.
98 * @param imageFile An {@link OCFile} to preview as an image in the fragment
99 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of
100 * {@link FragmentStatePagerAdapter}
101 * ; TODO better solution
103 public static PreviewImageFragment
newInstance(OCFile imageFile
, boolean ignoreFirstSavedState
,
104 boolean showResizedImage
){
105 PreviewImageFragment frag
= new PreviewImageFragment();
106 frag
.mShowResizedImage
= showResizedImage
;
107 Bundle args
= new Bundle();
108 args
.putParcelable(ARG_FILE
, imageFile
);
109 args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
);
110 frag
.setArguments(args
);
117 * Creates an empty fragment for image previews.
119 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
120 * (for instance, when the device is turned a aside).
122 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
125 public PreviewImageFragment() {
126 mIgnoreFirstSavedState
= false
;
134 public void onCreate(Bundle savedInstanceState
) {
135 super.onCreate(savedInstanceState
);
136 Bundle args
= getArguments();
137 setFile((OCFile
)args
.getParcelable(ARG_FILE
));
138 // TODO better in super, but needs to check ALL the class extending FileFragment;
141 mIgnoreFirstSavedState
= args
.getBoolean(ARG_IGNORE_FIRST
);
142 setHasOptionsMenu(true
);
150 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
151 Bundle savedInstanceState
) {
152 super.onCreateView(inflater
, container
, savedInstanceState
);
153 View view
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
154 mImageView
= (TouchImageViewCustom
) view
.findViewById(R
.id
.image
);
155 mImageView
.setVisibility(View
.GONE
);
156 mImageView
.setOnClickListener(new OnClickListener() {
158 public void onClick(View v
) {
159 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
163 mMessageView
= (TextView
)view
.findViewById(R
.id
.message
);
164 mMessageView
.setVisibility(View
.GONE
);
165 mProgressWheel
= (ProgressBar
)view
.findViewById(R
.id
.progressWheel
);
166 mProgressWheel
.setVisibility(View
.VISIBLE
);
174 public void onActivityCreated(Bundle savedInstanceState
) {
175 super.onActivityCreated(savedInstanceState
);
176 if (savedInstanceState
!= null
) {
177 if (!mIgnoreFirstSavedState
) {
178 OCFile file
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
181 mIgnoreFirstSavedState
= false
;
184 if (getFile() == null
) {
185 throw new IllegalStateException("Instanced with a NULL OCFile");
194 public void onSaveInstanceState(Bundle outState
) {
195 super.onSaveInstanceState(outState
);
196 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
201 public void onStart() {
203 if (getFile() != null
) {
204 mImageView
.setTag(getFile().getFileId());
206 if (mShowResizedImage
){
207 Bitmap thumbnail
= ThumbnailsCacheManager
.getBitmapFromDiskCache(
208 String
.valueOf("r" + getFile().getRemoteId())
211 if (thumbnail
!= null
&& !getFile().needsUpdateThumbnail()){
212 mProgressWheel
.setVisibility(View
.GONE
);
213 mImageView
.setImageBitmap(thumbnail
);
214 mImageView
.setVisibility(View
.VISIBLE
);
217 // generate new Thumbnail
218 if (ThumbnailsCacheManager
.cancelPotentialWork(getFile(), mImageView
)) {
219 final ThumbnailsCacheManager
.ThumbnailGenerationTask task
=
220 new ThumbnailsCacheManager
.ThumbnailGenerationTask(
221 mImageView
, mContainerActivity
.getStorageManager(),
222 mContainerActivity
.getStorageManager().getAccount(),
224 if (thumbnail
== null
) {
225 thumbnail
= ThumbnailsCacheManager
.mDefaultImg
;
227 final ThumbnailsCacheManager
.AsyncDrawable asyncDrawable
=
228 new ThumbnailsCacheManager
.AsyncDrawable(
229 MainApp
.getAppContext().getResources(),
233 mImageView
.setImageDrawable(asyncDrawable
);
234 task
.execute(getFile(), false
);
238 mLoadBitmapTask
= new LoadBitmapTask(mImageView
, mMessageView
, mProgressWheel
);
239 mLoadBitmapTask
.execute(getFile().getStoragePath());
246 public void onStop() {
247 Log_OC
.d(TAG
, "onStop starts");
248 if (mLoadBitmapTask
!= null
) {
249 mLoadBitmapTask
.cancel(true
);
250 mLoadBitmapTask
= null
;
259 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
260 super.onCreateOptionsMenu(menu
, inflater
);
261 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
268 public void onPrepareOptionsMenu(Menu menu
) {
269 super.onPrepareOptionsMenu(menu
);
271 if (mContainerActivity
.getStorageManager() != null
) {
273 setFile(mContainerActivity
.getStorageManager().getFileById(getFile().getFileId()));
275 FileMenuFilter mf
= new FileMenuFilter(
277 mContainerActivity
.getStorageManager().getAccount(),
284 // additional restriction for this fragment
285 // TODO allow renaming in PreviewImageFragment
286 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
288 item
.setVisible(false
);
289 item
.setEnabled(false
);
292 // additional restriction for this fragment
293 // TODO allow refresh file in PreviewImageFragment
294 item
= menu
.findItem(R
.id
.action_sync_file
);
296 item
.setVisible(false
);
297 item
.setEnabled(false
);
300 // additional restriction for this fragment
301 item
= menu
.findItem(R
.id
.action_move
);
303 item
.setVisible(false
);
304 item
.setEnabled(false
);
315 public boolean onOptionsItemSelected(MenuItem item
) {
316 switch (item
.getItemId()) {
317 case R
.id
.action_share_file
: {
318 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
321 case R
.id
.action_unshare_file
: {
322 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
325 case R
.id
.action_open_file_with
: {
329 case R
.id
.action_remove_file
: {
330 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
331 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
334 case R
.id
.action_see_details
: {
338 case R
.id
.action_send_file
: {
339 if (getFile().isImage() && !getFile().isDown()){
340 mContainerActivity
.getFileOperationsHelper().sendCachedImage(getFile());
343 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
347 case R
.id
.action_sync_file
: {
348 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
358 private void seeDetails() {
359 mContainerActivity
.showDetails(getFile());
364 public void onResume() {
370 public void onPause() {
375 public void onDestroy() {
376 if (mBitmap
!= null
) {
379 // putting this in onStop() is just the same; the fragment is always destroyed by
380 // {@link FragmentStatePagerAdapter} when the fragment in swiped further than the
381 // valid offscreen distance, and onStop() is never called before than that
388 * Opens the previewed image with an external application.
390 private void openFile() {
391 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
396 private class LoadBitmapTask
extends AsyncTask
<String
, Void
, Bitmap
> {
399 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
401 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
402 * memory before the load finishes.
404 private final WeakReference
<ImageViewCustom
> mImageViewRef
;
407 * Weak reference to the target {@link TextView} where error messages will be written.
409 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
410 * memory before the load finishes.
412 private final WeakReference
<TextView
> mMessageViewRef
;
416 * Weak reference to the target {@link ProgressBar} shown while the load is in progress.
418 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
419 * memory before the load finishes.
421 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
425 * Error message to show when a load fails
427 private int mErrorMessageId
;
433 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
435 public LoadBitmapTask(ImageViewCustom imageView
, TextView messageView
,
436 ProgressBar progressWheel
) {
437 mImageViewRef
= new WeakReference
<ImageViewCustom
>(imageView
);
438 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
439 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
444 protected Bitmap
doInBackground(String
... params
) {
445 Bitmap result
= null
;
446 if (params
.length
!= 1) return null
;
447 String storagePath
= params
[0];
450 int maxDownScale
= 3; // could be a parameter passed to doInBackground(...)
451 Point screenSize
= DisplayUtils
.getScreenSize(getActivity());
452 int minWidth
= screenSize
.x
;
453 int minHeight
= screenSize
.y
;
454 for (int i
= 0; i
< maxDownScale
&& result
== null
; i
++) {
455 if (isCancelled()) return null
;
457 result
= BitmapUtils
.decodeSampledBitmapFromFile(storagePath
, minWidth
,
460 if (isCancelled()) return result
;
462 if (result
== null
) {
463 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
464 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
467 // Rotate image, obeying exif tag.
468 result
= BitmapUtils
.rotateImage(result
, storagePath
);
471 } catch (OutOfMemoryError e
) {
472 mErrorMessageId
= R
.string
.common_error_out_memory
;
473 if (i
< maxDownScale
- 1) {
474 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+
476 minWidth
= minWidth
/ 2;
477 minHeight
= minHeight
/ 2;
480 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+
483 if (result
!= null
) {
490 } catch (NoSuchFieldError e
) {
491 mErrorMessageId
= R
.string
.common_error_unknown
;
492 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file "
495 } catch (Throwable t
) {
496 mErrorMessageId
= R
.string
.common_error_unknown
;
497 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
505 protected void onCancelled(Bitmap result
) {
506 if (result
!= null
) {
512 protected void onPostExecute(Bitmap result
) {
514 if (result
!= null
) {
515 showLoadedImage(result
);
519 if (result
!= null
&& mBitmap
!= result
) {
520 // unused bitmap, release it! (just in case)
525 @SuppressLint("InlinedApi")
526 private void showLoadedImage(Bitmap result
) {
527 final ImageViewCustom imageView
= mImageViewRef
.get();
528 if (imageView
!= null
) {
529 Log_OC
.d(TAG
, "Showing image with resolution " + result
.getWidth() + "x" +
531 imageView
.setImageBitmap(result
);
532 imageView
.setVisibility(View
.VISIBLE
);
533 mBitmap
= result
; // needs to be kept for recycling when not useful
536 final TextView messageView
= mMessageViewRef
.get();
537 if (messageView
!= null
) {
538 messageView
.setVisibility(View
.GONE
);
539 } // else , silently finish, the fragment was destroyed
542 private void showErrorMessage() {
543 final ImageView imageView
= mImageViewRef
.get();
544 if (imageView
!= null
) {
545 // shows the default error icon
546 imageView
.setVisibility(View
.VISIBLE
);
547 } // else , silently finish, the fragment was destroyed
549 final TextView messageView
= mMessageViewRef
.get();
550 if (messageView
!= null
) {
551 messageView
.setText(mErrorMessageId
);
552 messageView
.setVisibility(View
.VISIBLE
);
553 } // else , silently finish, the fragment was destroyed
556 private void hideProgressWheel() {
557 final ProgressBar progressWheel
= mProgressWheelRef
.get();
558 if (progressWheel
!= null
) {
559 progressWheel
.setVisibility(View
.GONE
);
566 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment}
569 * @param file File to test if can be previewed.
570 * @return 'True' if the file can be handled by the fragment.
572 public static boolean canBePreviewed(OCFile file
) {
573 return (file
!= null
&& file
.isImage());
578 * Finishes the preview
580 private void finish() {
581 Activity container
= getActivity();
585 public TouchImageViewCustom
getImageView() {