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
.R
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.files
.FileMenuFilter
;
46 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
47 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
48 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
49 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
50 import com
.owncloud
.android
.utils
.BitmapUtils
;
51 import com
.owncloud
.android
.utils
.DisplayUtils
;
53 import third_parties
.michaelOrtiz
.TouchImageViewCustom
;
57 * This fragment shows a preview of a downloaded image.
59 * Trying to get an instance with a NULL {@link OCFile} will produce an
60 * {@link IllegalStateException}.
62 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on
65 public class PreviewImageFragment
extends FileFragment
{
67 public static final String EXTRA_FILE
= "FILE";
69 private static final String ARG_FILE
= "FILE";
70 private static final String ARG_IGNORE_FIRST
= "IGNORE_FIRST";
72 private TouchImageViewCustom mImageView
;
73 private TextView mMessageView
;
74 private ProgressBar mProgressWheel
;
76 public Bitmap mBitmap
= null
;
78 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
80 private boolean mIgnoreFirstSavedState
;
82 private LoadBitmapTask mLoadBitmapTask
= null
;
86 * Public factory method to create a new fragment that previews an image.
88 * Android strongly recommends keep the empty constructor of fragments as the only public
90 * use {@link #setArguments(Bundle)} to set the needed arguments.
92 * This method hides to client objects the need of doing the construction in two steps.
94 * @param imageFile An {@link OCFile} to preview as an image in the fragment
95 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of
96 * {@link FragmentStatePagerAdapter}
97 * ; TODO better solution
99 public static PreviewImageFragment
newInstance(OCFile imageFile
, boolean ignoreFirstSavedState
){
100 PreviewImageFragment frag
= new PreviewImageFragment();
101 Bundle args
= new Bundle();
102 args
.putParcelable(ARG_FILE
, imageFile
);
103 args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
);
104 frag
.setArguments(args
);
111 * Creates an empty fragment for image previews.
113 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
114 * (for instance, when the device is turned a aside).
116 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
119 public PreviewImageFragment() {
120 mIgnoreFirstSavedState
= false
;
128 public void onCreate(Bundle savedInstanceState
) {
129 super.onCreate(savedInstanceState
);
130 Bundle args
= getArguments();
131 setFile((OCFile
)args
.getParcelable(ARG_FILE
));
132 // TODO better in super, but needs to check ALL the class extending FileFragment;
135 mIgnoreFirstSavedState
= args
.getBoolean(ARG_IGNORE_FIRST
);
136 setHasOptionsMenu(true
);
144 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
145 Bundle savedInstanceState
) {
146 super.onCreateView(inflater
, container
, savedInstanceState
);
147 View view
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
148 mImageView
= (TouchImageViewCustom
) view
.findViewById(R
.id
.image
);
149 mImageView
.setVisibility(View
.GONE
);
150 mImageView
.setOnClickListener(new OnClickListener() {
152 public void onClick(View v
) {
153 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
157 mMessageView
= (TextView
)view
.findViewById(R
.id
.message
);
158 mMessageView
.setVisibility(View
.GONE
);
159 mProgressWheel
= (ProgressBar
)view
.findViewById(R
.id
.progressWheel
);
160 mProgressWheel
.setVisibility(View
.VISIBLE
);
168 public void onActivityCreated(Bundle savedInstanceState
) {
169 super.onActivityCreated(savedInstanceState
);
170 if (savedInstanceState
!= null
) {
171 if (!mIgnoreFirstSavedState
) {
172 OCFile file
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
175 mIgnoreFirstSavedState
= false
;
178 if (getFile() == null
) {
179 throw new IllegalStateException("Instanced with a NULL OCFile");
181 if (!getFile().isDown()) {
182 throw new IllegalStateException("There is no local file to preview");
191 public void onSaveInstanceState(Bundle outState
) {
192 super.onSaveInstanceState(outState
);
193 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
198 public void onStart() {
200 if (getFile() != null
) {
201 mLoadBitmapTask
= new LoadBitmapTask(mImageView
, mMessageView
, mProgressWheel
);
202 //mLoadBitmapTask.execute(new String[]{getFile().getStoragePath()});
203 mLoadBitmapTask
.execute(getFile().getStoragePath());
209 public void onStop() {
210 Log_OC
.d(TAG
, "onStop starts");
211 if (mLoadBitmapTask
!= null
) {
212 mLoadBitmapTask
.cancel(true
);
213 mLoadBitmapTask
= null
;
222 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
223 super.onCreateOptionsMenu(menu
, inflater
);
224 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
231 public void onPrepareOptionsMenu(Menu menu
) {
232 super.onPrepareOptionsMenu(menu
);
234 if (mContainerActivity
.getStorageManager() != null
) {
236 setFile(mContainerActivity
.getStorageManager().getFileById(getFile().getFileId()));
238 FileMenuFilter mf
= new FileMenuFilter(
240 mContainerActivity
.getStorageManager().getAccount(),
247 // additional restriction for this fragment
248 // TODO allow renaming in PreviewImageFragment
249 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
251 item
.setVisible(false
);
252 item
.setEnabled(false
);
255 // additional restriction for this fragment
256 // TODO allow refresh file in PreviewImageFragment
257 item
= menu
.findItem(R
.id
.action_sync_file
);
259 item
.setVisible(false
);
260 item
.setEnabled(false
);
263 // additional restriction for this fragment
264 item
= menu
.findItem(R
.id
.action_move
);
266 item
.setVisible(false
);
267 item
.setEnabled(false
);
278 public boolean onOptionsItemSelected(MenuItem item
) {
279 switch (item
.getItemId()) {
280 case R
.id
.action_share_file
: {
281 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
284 case R
.id
.action_unshare_file
: {
285 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
288 case R
.id
.action_open_file_with
: {
292 case R
.id
.action_remove_file
: {
293 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
294 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
297 case R
.id
.action_see_details
: {
301 case R
.id
.action_send_file
: {
302 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
305 case R
.id
.action_sync_file
: {
306 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
316 private void seeDetails() {
317 mContainerActivity
.showDetails(getFile());
322 public void onResume() {
328 public void onPause() {
333 public void onDestroy() {
334 if (mBitmap
!= null
) {
337 // putting this in onStop() is just the same; the fragment is always destroyed by
338 // {@link FragmentStatePagerAdapter} when the fragment in swiped further than the
339 // valid offscreen distance, and onStop() is never called before than that
346 * Opens the previewed image with an external application.
348 private void openFile() {
349 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
354 private class LoadBitmapTask
extends AsyncTask
<String
, Void
, Bitmap
> {
357 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
359 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
360 * memory before the load finishes.
362 private final WeakReference
<ImageViewCustom
> mImageViewRef
;
365 * Weak reference to the target {@link TextView} where error messages will be written.
367 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
368 * memory before the load finishes.
370 private final WeakReference
<TextView
> mMessageViewRef
;
374 * Weak reference to the target {@link ProgressBar} shown while the load is in progress.
376 * Using a weak reference will avoid memory leaks if the target ImageView is retired from
377 * memory before the load finishes.
379 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
383 * Error message to show when a load fails
385 private int mErrorMessageId
;
391 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
393 public LoadBitmapTask(ImageViewCustom imageView
, TextView messageView
,
394 ProgressBar progressWheel
) {
395 mImageViewRef
= new WeakReference
<ImageViewCustom
>(imageView
);
396 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
397 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
402 protected Bitmap
doInBackground(String
... params
) {
403 Bitmap result
= null
;
404 if (params
.length
!= 1) return null
;
405 String storagePath
= params
[0];
408 int maxDownScale
= 3; // could be a parameter passed to doInBackground(...)
409 Point screenSize
= DisplayUtils
.getScreenSize(getActivity());
410 int minWidth
= screenSize
.x
;
411 int minHeight
= screenSize
.y
;
412 for (int i
= 0; i
< maxDownScale
&& result
== null
; i
++) {
413 if (isCancelled()) return null
;
415 result
= BitmapUtils
.decodeSampledBitmapFromFile(storagePath
, minWidth
,
418 if (isCancelled()) return result
;
420 if (result
== null
) {
421 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
422 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
425 // Rotate image, obeying exif tag.
426 result
= BitmapUtils
.rotateImage(result
, storagePath
);
429 } catch (OutOfMemoryError e
) {
430 mErrorMessageId
= R
.string
.common_error_out_memory
;
431 if (i
< maxDownScale
- 1) {
432 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+
434 minWidth
= minWidth
/ 2;
435 minHeight
= minHeight
/ 2;
438 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+
441 if (result
!= null
) {
448 } catch (NoSuchFieldError e
) {
449 mErrorMessageId
= R
.string
.common_error_unknown
;
450 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file "
453 } catch (Throwable t
) {
454 mErrorMessageId
= R
.string
.common_error_unknown
;
455 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
463 protected void onCancelled(Bitmap result
) {
464 if (result
!= null
) {
470 protected void onPostExecute(Bitmap result
) {
472 if (result
!= null
) {
473 showLoadedImage(result
);
477 if (result
!= null
&& mBitmap
!= result
) {
478 // unused bitmap, release it! (just in case)
483 @SuppressLint("InlinedApi")
484 private void showLoadedImage(Bitmap result
) {
485 final ImageViewCustom imageView
= mImageViewRef
.get();
486 if (imageView
!= null
) {
487 Log_OC
.d(TAG
, "Showing image with resolution " + result
.getWidth() + "x" +
489 imageView
.setImageBitmap(result
);
490 imageView
.setVisibility(View
.VISIBLE
);
491 mBitmap
= result
; // needs to be kept for recycling when not useful
494 final TextView messageView
= mMessageViewRef
.get();
495 if (messageView
!= null
) {
496 messageView
.setVisibility(View
.GONE
);
497 } // else , silently finish, the fragment was destroyed
500 private void showErrorMessage() {
501 final ImageView imageView
= mImageViewRef
.get();
502 if (imageView
!= null
) {
503 // shows the default error icon
504 imageView
.setVisibility(View
.VISIBLE
);
505 } // else , silently finish, the fragment was destroyed
507 final TextView messageView
= mMessageViewRef
.get();
508 if (messageView
!= null
) {
509 messageView
.setText(mErrorMessageId
);
510 messageView
.setVisibility(View
.VISIBLE
);
511 } // else , silently finish, the fragment was destroyed
514 private void hideProgressWheel() {
515 final ProgressBar progressWheel
= mProgressWheelRef
.get();
516 if (progressWheel
!= null
) {
517 progressWheel
.setVisibility(View
.GONE
);
524 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment}
527 * @param file File to test if can be previewed.
528 * @return 'True' if the file can be handled by the fragment.
530 public static boolean canBePreviewed(OCFile file
) {
531 return (file
!= null
&& file
.isImage());
536 * Finishes the preview
538 private void finish() {
539 Activity container
= getActivity();
543 public TouchImageViewCustom
getImageView() {