1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.preview
;
19 import java
.lang
.ref
.WeakReference
;
22 import android
.accounts
.Account
;
23 import android
.annotation
.SuppressLint
;
24 import android
.app
.Activity
;
25 import android
.graphics
.Bitmap
;
26 import android
.graphics
.BitmapFactory
;
27 import android
.graphics
.BitmapFactory
.Options
;
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
.Display
;
33 import android
.view
.LayoutInflater
;
34 import android
.view
.View
;
35 import android
.view
.View
.OnTouchListener
;
36 import android
.view
.ViewGroup
;
37 import android
.widget
.ImageView
;
38 import android
.widget
.ProgressBar
;
39 import android
.widget
.TextView
;
41 import com
.actionbarsherlock
.view
.Menu
;
42 import com
.actionbarsherlock
.view
.MenuInflater
;
43 import com
.actionbarsherlock
.view
.MenuItem
;
44 import com
.owncloud
.android
.R
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.files
.FileMenuFilter
;
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
.Log_OC
;
54 * This fragment shows a preview of a downloaded image.
56 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
58 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
60 * @author David A. Velasco
62 public class PreviewImageFragment
extends FileFragment
{
63 public static final String EXTRA_FILE
= "FILE";
64 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
67 private Account mAccount
;
68 private ImageView mImageView
;
69 private TextView mMessageView
;
70 private ProgressBar mProgressWheel
;
72 public Bitmap mBitmap
= null
;
74 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
76 private boolean mIgnoreFirstSavedState
;
80 * Creates a fragment to preview an image.
82 * When 'imageFile' or 'ocAccount' are null
84 * @param imageFile An {@link OCFile} to preview as an image in the fragment
85 * @param ocAccount An ownCloud account; needed to start downloads
86 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
88 public PreviewImageFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
91 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
96 * Creates an empty fragment for image previews.
98 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
100 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
102 public PreviewImageFragment() {
105 mIgnoreFirstSavedState
= false
;
113 public void onCreate(Bundle savedInstanceState
) {
114 super.onCreate(savedInstanceState
);
115 setHasOptionsMenu(true
);
123 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
124 Bundle savedInstanceState
) {
125 super.onCreateView(inflater
, container
, savedInstanceState
);
126 mView
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
127 mImageView
= (ImageView
)mView
.findViewById(R
.id
.image
);
128 mImageView
.setVisibility(View
.GONE
);
129 mView
.setOnTouchListener((OnTouchListener
)getActivity());
130 mMessageView
= (TextView
)mView
.findViewById(R
.id
.message
);
131 mMessageView
.setVisibility(View
.GONE
);
132 mProgressWheel
= (ProgressBar
)mView
.findViewById(R
.id
.progressWheel
);
133 mProgressWheel
.setVisibility(View
.VISIBLE
);
142 public void onAttach(Activity activity
) {
143 super.onAttach(activity
);
144 if (!(activity
instanceof OnTouchListener
)) {
145 throw new ClassCastException(activity
.toString() +
146 " must implement " + OnTouchListener
.class.getSimpleName());
155 public void onActivityCreated(Bundle savedInstanceState
) {
156 super.onActivityCreated(savedInstanceState
);
157 if (savedInstanceState
!= null
) {
158 if (!mIgnoreFirstSavedState
) {
159 OCFile file
= (OCFile
)savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
161 mAccount
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
);
163 mIgnoreFirstSavedState
= false
;
166 if (getFile() == null
) {
167 throw new IllegalStateException("Instanced with a NULL OCFile");
169 if (mAccount
== null
) {
170 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
172 if (!getFile().isDown()) {
173 throw new IllegalStateException("There is no local file to preview");
182 public void onSaveInstanceState(Bundle outState
) {
183 super.onSaveInstanceState(outState
);
184 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
185 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
190 public void onStart() {
192 if (getFile() != null
) {
193 BitmapLoader bl
= new BitmapLoader(mImageView
, mMessageView
, mProgressWheel
);
194 bl
.execute(new String
[]{getFile().getStoragePath()});
203 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
204 super.onCreateOptionsMenu(menu
, inflater
);
205 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
212 public void onPrepareOptionsMenu(Menu menu
) {
213 super.onPrepareOptionsMenu(menu
);
215 if (mContainerActivity
.getStorageManager() != null
) {
216 FileMenuFilter mf
= new FileMenuFilter(
218 mContainerActivity
.getStorageManager().getAccount(),
220 getSherlockActivity()
225 // additional restriction for this fragment
226 // TODO allow renaming in PreviewImageFragment
227 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
229 item
.setVisible(false
);
230 item
.setEnabled(false
);
240 public boolean onOptionsItemSelected(MenuItem item
) {
241 switch (item
.getItemId()) {
242 case R
.id
.action_share_file
: {
243 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
246 case R
.id
.action_unshare_file
: {
247 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
250 case R
.id
.action_open_file_with
: {
254 case R
.id
.action_remove_file
: {
255 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
256 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
259 case R
.id
.action_see_details
: {
263 case R
.id
.action_send_file
: {
264 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
267 case R
.id
.action_sync_file
: {
268 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
278 private void seeDetails() {
279 mContainerActivity
.showDetails(getFile());
284 public void onResume() {
290 public void onPause() {
296 public void onDestroy() {
298 if (mBitmap
!= null
) {
305 * Opens the previewed image with an external application.
307 private void openFile() {
308 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
313 private class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
316 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
318 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
320 private final WeakReference
<ImageView
> mImageViewRef
;
323 * Weak reference to the target {@link TextView} where error messages will be written.
325 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
327 private final WeakReference
<TextView
> mMessageViewRef
;
331 * Weak reference to the target {@link Progressbar} shown while the load is in progress.
333 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
335 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
339 * Error message to show when a load fails
341 private int mErrorMessageId
;
347 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
349 public BitmapLoader(ImageView imageView
, TextView messageView
, ProgressBar progressWheel
) {
350 mImageViewRef
= new WeakReference
<ImageView
>(imageView
);
351 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
352 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
356 @SuppressWarnings("deprecation")
357 @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
359 protected Bitmap
doInBackground(String
... params
) {
360 Bitmap result
= null
;
361 if (params
.length
!= 1) return result
;
362 String storagePath
= params
[0];
364 // set desired options that will affect the size of the bitmap
365 BitmapFactory
.Options options
= new Options();
366 options
.inScaled
= true
;
367 options
.inPurgeable
= true
;
368 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
369 options
.inPreferQualityOverSpeed
= false
;
371 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
372 options
.inMutable
= false
;
374 // make a false load of the bitmap - just to be able to read outWidth, outHeight and outMimeType
375 options
.inJustDecodeBounds
= true
;
376 BitmapFactory
.decodeFile(storagePath
, options
);
378 int width
= options
.outWidth
;
379 int height
= options
.outHeight
;
382 Display display
= getActivity().getWindowManager().getDefaultDisplay();
383 Point size
= new Point();
386 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
387 display
.getSize(size
);
388 screenWidth
= size
.x
;
389 screenHeight
= size
.y
;
391 screenWidth
= display
.getWidth();
392 screenHeight
= display
.getHeight();
395 if (width
> screenWidth
) {
396 // second try to scale down the image , this time depending upon the screen size
397 scale
= (int) Math
.floor((float)width
/ screenWidth
);
399 if (height
> screenHeight
) {
400 scale
= Math
.max(scale
, (int) Math
.floor((float)height
/ screenHeight
));
402 options
.inSampleSize
= scale
;
404 // really load the bitmap
405 options
.inJustDecodeBounds
= false
; // the next decodeFile call will be real
406 result
= BitmapFactory
.decodeFile(storagePath
, options
);
407 //Log_OC.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
409 if (result
== null
) {
410 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
411 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
414 } catch (OutOfMemoryError e
) {
415 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
416 Log_OC
.e(TAG
, "Out of memory occured for file " + storagePath
, e
);
418 } catch (NoSuchFieldError e
) {
419 mErrorMessageId
= R
.string
.common_error_unknown
;
420 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file " + storagePath
, e
);
422 } catch (Throwable t
) {
423 mErrorMessageId
= R
.string
.common_error_unknown
;
424 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
431 protected void onPostExecute(Bitmap result
) {
433 if (result
!= null
) {
434 showLoadedImage(result
);
440 private void showLoadedImage(Bitmap result
) {
441 if (mImageViewRef
!= null
) {
442 final ImageView imageView
= mImageViewRef
.get();
443 if (imageView
!= null
) {
444 imageView
.setImageBitmap(result
);
445 imageView
.setVisibility(View
.VISIBLE
);
447 } // else , silently finish, the fragment was destroyed
449 if (mMessageViewRef
!= null
) {
450 final TextView messageView
= mMessageViewRef
.get();
451 if (messageView
!= null
) {
452 messageView
.setVisibility(View
.GONE
);
453 } // else , silently finish, the fragment was destroyed
457 private void showErrorMessage() {
458 if (mImageViewRef
!= null
) {
459 final ImageView imageView
= mImageViewRef
.get();
460 if (imageView
!= null
) {
461 // shows the default error icon
462 imageView
.setVisibility(View
.VISIBLE
);
463 } // else , silently finish, the fragment was destroyed
465 if (mMessageViewRef
!= null
) {
466 final TextView messageView
= mMessageViewRef
.get();
467 if (messageView
!= null
) {
468 messageView
.setText(mErrorMessageId
);
469 messageView
.setVisibility(View
.VISIBLE
);
470 } // else , silently finish, the fragment was destroyed
474 private void hideProgressWheel() {
475 if (mProgressWheelRef
!= null
) {
476 final ProgressBar progressWheel
= mProgressWheelRef
.get();
477 if (progressWheel
!= null
) {
478 progressWheel
.setVisibility(View
.GONE
);
486 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
488 * @param file File to test if can be previewed.
489 * @return 'True' if the file can be handled by the fragment.
491 public static boolean canBePreviewed(OCFile file
) {
492 return (file
!= null
&& file
.isImage());
497 * Finishes the preview
499 private void finish() {
500 Activity container
= getActivity();