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
.View
;
34 import android
.view
.View
.OnClickListener
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.ImageView
;
37 import android
.widget
.ProgressBar
;
38 import android
.widget
.TextView
;
40 import com
.actionbarsherlock
.view
.Menu
;
41 import com
.actionbarsherlock
.view
.MenuInflater
;
42 import com
.actionbarsherlock
.view
.MenuItem
;
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 {@link IllegalStateException}.
61 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
63 public class PreviewImageFragment
extends FileFragment
{
65 public static final String EXTRA_FILE
= "FILE";
67 private static final String ARG_FILE
= "FILE";
68 private static final String ARG_IGNORE_FIRST
= "IGNORE_FIRST";
70 private TouchImageViewCustom mImageView
;
71 private TextView mMessageView
;
72 private ProgressBar mProgressWheel
;
74 public Bitmap mBitmap
= null
;
76 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
78 private boolean mIgnoreFirstSavedState
;
80 private LoadBitmapTask mLoadBitmapTask
= null
;
84 * Public factory method to create a new fragment that previews an image.
86 * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
87 * use {@link #setArguments(Bundle)} to set the needed arguments.
89 * This method hides to client objects the need of doing the construction in two steps.
91 * @param imageFile An {@link OCFile} to preview as an image in the fragment
92 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
93 * ; TODO better solution
95 public static PreviewImageFragment
newInstance(OCFile imageFile
, boolean ignoreFirstSavedState
) {
96 PreviewImageFragment frag
= new PreviewImageFragment();
97 Bundle args
= new Bundle();
98 args
.putParcelable(ARG_FILE
, imageFile
);
99 args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
);
100 frag
.setArguments(args
);
107 * Creates an empty fragment for image previews.
109 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
110 * (for instance, when the device is turned a aside).
112 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
114 public PreviewImageFragment() {
115 mIgnoreFirstSavedState
= false
;
123 public void onCreate(Bundle savedInstanceState
) {
124 super.onCreate(savedInstanceState
);
125 Bundle args
= getArguments();
126 setFile((OCFile
)args
.getParcelable(ARG_FILE
));
127 // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
129 mIgnoreFirstSavedState
= args
.getBoolean(ARG_IGNORE_FIRST
);
130 setHasOptionsMenu(true
);
138 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
139 Bundle savedInstanceState
) {
140 super.onCreateView(inflater
, container
, savedInstanceState
);
141 View view
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
142 mImageView
= (TouchImageViewCustom
) view
.findViewById(R
.id
.image
);
143 mImageView
.setVisibility(View
.GONE
);
144 mImageView
.setOnClickListener(new OnClickListener() {
146 public void onClick(View v
) {
147 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
151 mMessageView
= (TextView
)view
.findViewById(R
.id
.message
);
152 mMessageView
.setVisibility(View
.GONE
);
153 mProgressWheel
= (ProgressBar
)view
.findViewById(R
.id
.progressWheel
);
154 mProgressWheel
.setVisibility(View
.VISIBLE
);
162 public void onActivityCreated(Bundle savedInstanceState
) {
163 super.onActivityCreated(savedInstanceState
);
164 if (savedInstanceState
!= null
) {
165 if (!mIgnoreFirstSavedState
) {
166 OCFile file
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
169 mIgnoreFirstSavedState
= false
;
172 if (getFile() == null
) {
173 throw new IllegalStateException("Instanced with a NULL OCFile");
175 if (!getFile().isDown()) {
176 throw new IllegalStateException("There is no local file to preview");
185 public void onSaveInstanceState(Bundle outState
) {
186 super.onSaveInstanceState(outState
);
187 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
192 public void onStart() {
194 if (getFile() != null
) {
195 mLoadBitmapTask
= new LoadBitmapTask(mImageView
, mMessageView
, mProgressWheel
);
196 //mLoadBitmapTask.execute(new String[]{getFile().getStoragePath()});
197 mLoadBitmapTask
.execute(getFile().getStoragePath());
203 public void onStop() {
204 Log_OC
.d(TAG
, "onStop starts");
205 if (mLoadBitmapTask
!= null
) {
206 mLoadBitmapTask
.cancel(true
);
207 mLoadBitmapTask
= null
;
216 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
217 super.onCreateOptionsMenu(menu
, inflater
);
218 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
225 public void onPrepareOptionsMenu(Menu menu
) {
226 super.onPrepareOptionsMenu(menu
);
228 if (mContainerActivity
.getStorageManager() != null
) {
230 setFile(mContainerActivity
.getStorageManager().getFileById(getFile().getFileId()));
232 FileMenuFilter mf
= new FileMenuFilter(
234 mContainerActivity
.getStorageManager().getAccount(),
236 getSherlockActivity()
241 // additional restriction for this fragment
242 // TODO allow renaming in PreviewImageFragment
243 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
245 item
.setVisible(false
);
246 item
.setEnabled(false
);
249 // additional restriction for this fragment
250 // TODO allow refresh file in PreviewImageFragment
251 item
= menu
.findItem(R
.id
.action_sync_file
);
253 item
.setVisible(false
);
254 item
.setEnabled(false
);
257 // additional restriction for this fragment
258 item
= menu
.findItem(R
.id
.action_move
);
260 item
.setVisible(false
);
261 item
.setEnabled(false
);
272 public boolean onOptionsItemSelected(MenuItem item
) {
273 switch (item
.getItemId()) {
274 case R
.id
.action_share_file
: {
275 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
278 case R
.id
.action_unshare_file
: {
279 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
282 case R
.id
.action_open_file_with
: {
286 case R
.id
.action_remove_file
: {
287 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
288 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
291 case R
.id
.action_see_details
: {
295 case R
.id
.action_send_file
: {
296 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
299 case R
.id
.action_sync_file
: {
300 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
310 private void seeDetails() {
311 mContainerActivity
.showDetails(getFile());
316 public void onResume() {
322 public void onPause() {
327 public void onDestroy() {
328 if (mBitmap
!= null
) {
331 // putting this in onStop() is just the same; the fragment is always destroyed by
332 // {@link FragmentStatePagerAdapter} when the fragment in swiped further than the valid offscreen
333 // distance, and onStop() is never called before than that
340 * Opens the previewed image with an external application.
342 private void openFile() {
343 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
348 private class LoadBitmapTask
extends AsyncTask
<String
, Void
, Bitmap
> {
351 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
353 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before
356 private final WeakReference
<ImageViewCustom
> mImageViewRef
;
359 * Weak reference to the target {@link TextView} where error messages will be written.
361 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the
364 private final WeakReference
<TextView
> mMessageViewRef
;
368 * Weak reference to the target {@link ProgressBar} shown while the load is in progress.
370 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
372 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
376 * Error message to show when a load fails
378 private int mErrorMessageId
;
384 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
386 public LoadBitmapTask(ImageViewCustom imageView
, TextView messageView
, ProgressBar progressWheel
) {
387 mImageViewRef
= new WeakReference
<ImageViewCustom
>(imageView
);
388 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
389 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
394 protected Bitmap
doInBackground(String
... params
) {
395 Bitmap result
= null
;
396 if (params
.length
!= 1) return null
;
397 String storagePath
= params
[0];
400 int maxDownScale
= 3; // could be a parameter passed to doInBackground(...)
401 Point screenSize
= DisplayUtils
.getScreenSize(getActivity());
402 int minWidth
= screenSize
.x
;
403 int minHeight
= screenSize
.y
;
404 for (int i
= 0; i
< maxDownScale
&& result
== null
; i
++) {
405 if (isCancelled()) return null
;
407 result
= BitmapUtils
.decodeSampledBitmapFromFile(storagePath
, minWidth
, minHeight
);
409 if (isCancelled()) return result
;
411 if (result
== null
) {
412 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
413 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
416 // Rotate image, obeying exif tag.
417 result
= BitmapUtils
.rotateImage(result
, storagePath
);
420 } catch (OutOfMemoryError e
) {
421 mErrorMessageId
= R
.string
.common_error_out_memory
;
422 if (i
< maxDownScale
- 1) {
423 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+ " ; scaling down");
424 minWidth
= minWidth
/ 2;
425 minHeight
= minHeight
/ 2;
428 Log_OC
.w(TAG
, "Out of memory rendering file " + storagePath
+ " ; failing");
430 if (result
!= null
) {
437 } catch (NoSuchFieldError e
) {
438 mErrorMessageId
= R
.string
.common_error_unknown
;
439 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file "
442 } catch (Throwable t
) {
443 mErrorMessageId
= R
.string
.common_error_unknown
;
444 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
452 protected void onCancelled(Bitmap result
) {
453 if (result
!= null
) {
459 protected void onPostExecute(Bitmap result
) {
461 if (result
!= null
) {
462 showLoadedImage(result
);
466 if (result
!= null
&& mBitmap
!= result
) {
467 // unused bitmap, release it! (just in case)
472 @SuppressLint("InlinedApi")
473 private void showLoadedImage(Bitmap result
) {
474 final ImageViewCustom imageView
= mImageViewRef
.get();
475 if (imageView
!= null
) {
476 Log_OC
.d(TAG
, "Showing image with resolution " + result
.getWidth() + "x" + result
.getHeight());
477 imageView
.setImageBitmap(result
);
478 imageView
.setVisibility(View
.VISIBLE
);
479 mBitmap
= result
; // needs to be kept for recycling when not useful
482 final TextView messageView
= mMessageViewRef
.get();
483 if (messageView
!= null
) {
484 messageView
.setVisibility(View
.GONE
);
485 } // else , silently finish, the fragment was destroyed
488 private void showErrorMessage() {
489 final ImageView imageView
= mImageViewRef
.get();
490 if (imageView
!= null
) {
491 // shows the default error icon
492 imageView
.setVisibility(View
.VISIBLE
);
493 } // else , silently finish, the fragment was destroyed
495 final TextView messageView
= mMessageViewRef
.get();
496 if (messageView
!= null
) {
497 messageView
.setText(mErrorMessageId
);
498 messageView
.setVisibility(View
.VISIBLE
);
499 } // else , silently finish, the fragment was destroyed
502 private void hideProgressWheel() {
503 final ProgressBar progressWheel
= mProgressWheelRef
.get();
504 if (progressWheel
!= null
) {
505 progressWheel
.setVisibility(View
.GONE
);
512 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
514 * @param file File to test if can be previewed.
515 * @return 'True' if the file can be handled by the fragment.
517 public static boolean canBePreviewed(OCFile file
) {
518 return (file
!= null
&& file
.isImage());
523 * Finishes the preview
525 private void finish() {
526 Activity container
= getActivity();
530 public TouchImageViewCustom
getImageView() {