1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 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
.activity
.FileActivity
;
48 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
49 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
50 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
51 import com
.owncloud
.android
.utils
.Log_OC
;
55 * This fragment shows a preview of a downloaded image.
57 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
59 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
61 * @author David A. Velasco
63 public class PreviewImageFragment
extends FileFragment
{
64 public static final String EXTRA_FILE
= "FILE";
65 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
68 private Account mAccount
;
69 private ImageView mImageView
;
70 private TextView mMessageView
;
71 private ProgressBar mProgressWheel
;
73 public Bitmap mBitmap
= null
;
75 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
77 private boolean mIgnoreFirstSavedState
;
81 * Creates a fragment to preview an image.
83 * When 'imageFile' or 'ocAccount' are null
85 * @param imageFile An {@link OCFile} to preview as an image in the fragment
86 * @param ocAccount An ownCloud account; needed to start downloads
87 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
89 public PreviewImageFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
92 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
97 * Creates an empty fragment for image previews.
99 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
101 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
103 public PreviewImageFragment() {
106 mIgnoreFirstSavedState
= false
;
114 public void onCreate(Bundle savedInstanceState
) {
115 super.onCreate(savedInstanceState
);
116 setHasOptionsMenu(true
);
124 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
125 Bundle savedInstanceState
) {
126 super.onCreateView(inflater
, container
, savedInstanceState
);
127 mView
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
128 mImageView
= (ImageView
)mView
.findViewById(R
.id
.image
);
129 mImageView
.setVisibility(View
.GONE
);
130 mView
.setOnTouchListener((OnTouchListener
)getActivity());
131 mMessageView
= (TextView
)mView
.findViewById(R
.id
.message
);
132 mMessageView
.setVisibility(View
.GONE
);
133 mProgressWheel
= (ProgressBar
)mView
.findViewById(R
.id
.progressWheel
);
134 mProgressWheel
.setVisibility(View
.VISIBLE
);
143 public void onAttach(Activity activity
) {
144 super.onAttach(activity
);
145 if (!(activity
instanceof OnTouchListener
)) {
146 throw new ClassCastException(activity
.toString() +
147 " must implement " + OnTouchListener
.class.getSimpleName());
156 public void onActivityCreated(Bundle savedInstanceState
) {
157 super.onActivityCreated(savedInstanceState
);
158 if (savedInstanceState
!= null
) {
159 if (!mIgnoreFirstSavedState
) {
160 OCFile file
= (OCFile
)savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
161 mAccount
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
);
164 if (mAccount
!= null
) {
165 OCFile updatedFile
= ((FileActivity
) getSherlockActivity()).
166 getStorageManager().getFileByPath(file
.getRemotePath());
167 if (updatedFile
!= null
) {
168 setFile(updatedFile
);
176 mIgnoreFirstSavedState
= false
;
179 if (getFile() == null
) {
180 throw new IllegalStateException("Instanced with a NULL OCFile");
182 if (mAccount
== null
) {
183 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
185 if (!getFile().isDown()) {
186 throw new IllegalStateException("There is no local file to preview");
195 public void onSaveInstanceState(Bundle outState
) {
196 super.onSaveInstanceState(outState
);
197 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
198 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
203 public void onStart() {
205 if (getFile() != null
) {
206 BitmapLoader bl
= new BitmapLoader(mImageView
, mMessageView
, mProgressWheel
);
207 bl
.execute(new String
[]{getFile().getStoragePath()});
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
) {
229 FileMenuFilter mf
= new FileMenuFilter(
231 mContainerActivity
.getStorageManager().getAccount(),
233 getSherlockActivity()
238 // additional restriction for this fragment
239 // TODO allow renaming in PreviewImageFragment
240 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
242 item
.setVisible(false
);
243 item
.setEnabled(false
);
253 public boolean onOptionsItemSelected(MenuItem item
) {
254 switch (item
.getItemId()) {
255 case R
.id
.action_share_file
: {
256 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
259 case R
.id
.action_unshare_file
: {
260 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
263 case R
.id
.action_open_file_with
: {
267 case R
.id
.action_remove_file
: {
268 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
269 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
272 case R
.id
.action_see_details
: {
276 case R
.id
.action_send_file
: {
277 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
280 case R
.id
.action_sync_file
: {
281 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
291 private void seeDetails() {
292 mContainerActivity
.showDetails(getFile());
297 public void onResume() {
303 public void onPause() {
309 public void onDestroy() {
311 if (mBitmap
!= null
) {
318 * Opens the previewed image with an external application.
320 private void openFile() {
321 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
326 private class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
329 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
331 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
333 private final WeakReference
<ImageView
> mImageViewRef
;
336 * Weak reference to the target {@link TextView} where error messages will be written.
338 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
340 private final WeakReference
<TextView
> mMessageViewRef
;
344 * Weak reference to the target {@link Progressbar} shown while the load is in progress.
346 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
348 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
352 * Error message to show when a load fails
354 private int mErrorMessageId
;
360 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
362 public BitmapLoader(ImageView imageView
, TextView messageView
, ProgressBar progressWheel
) {
363 mImageViewRef
= new WeakReference
<ImageView
>(imageView
);
364 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
365 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
369 @SuppressWarnings("deprecation")
370 @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
372 protected Bitmap
doInBackground(String
... params
) {
373 Bitmap result
= null
;
374 if (params
.length
!= 1) return result
;
375 String storagePath
= params
[0];
377 // set desired options that will affect the size of the bitmap
378 BitmapFactory
.Options options
= new Options();
379 options
.inScaled
= true
;
380 options
.inPurgeable
= true
;
381 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
382 options
.inPreferQualityOverSpeed
= false
;
384 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
385 options
.inMutable
= false
;
387 // make a false load of the bitmap - just to be able to read outWidth, outHeight and outMimeType
388 options
.inJustDecodeBounds
= true
;
389 BitmapFactory
.decodeFile(storagePath
, options
);
391 int width
= options
.outWidth
;
392 int height
= options
.outHeight
;
395 Display display
= getActivity().getWindowManager().getDefaultDisplay();
396 Point size
= new Point();
399 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
400 display
.getSize(size
);
401 screenWidth
= size
.x
;
402 screenHeight
= size
.y
;
404 screenWidth
= display
.getWidth();
405 screenHeight
= display
.getHeight();
408 if (width
> screenWidth
) {
409 // second try to scale down the image , this time depending upon the screen size
410 scale
= (int) Math
.floor((float)width
/ screenWidth
);
412 if (height
> screenHeight
) {
413 scale
= Math
.max(scale
, (int) Math
.floor((float)height
/ screenHeight
));
415 options
.inSampleSize
= scale
;
417 // really load the bitmap
418 options
.inJustDecodeBounds
= false
; // the next decodeFile call will be real
419 result
= BitmapFactory
.decodeFile(storagePath
, options
);
420 //Log_OC.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
422 if (result
== null
) {
423 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
424 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
427 } catch (OutOfMemoryError e
) {
428 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
429 Log_OC
.e(TAG
, "Out of memory occured for file " + storagePath
, e
);
431 } catch (NoSuchFieldError e
) {
432 mErrorMessageId
= R
.string
.common_error_unknown
;
433 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file " + storagePath
, e
);
435 } catch (Throwable t
) {
436 mErrorMessageId
= R
.string
.common_error_unknown
;
437 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
444 protected void onPostExecute(Bitmap result
) {
446 if (result
!= null
) {
447 showLoadedImage(result
);
453 private void showLoadedImage(Bitmap result
) {
454 if (mImageViewRef
!= null
) {
455 final ImageView imageView
= mImageViewRef
.get();
456 if (imageView
!= null
) {
457 imageView
.setImageBitmap(result
);
458 imageView
.setVisibility(View
.VISIBLE
);
460 } // else , silently finish, the fragment was destroyed
462 if (mMessageViewRef
!= null
) {
463 final TextView messageView
= mMessageViewRef
.get();
464 if (messageView
!= null
) {
465 messageView
.setVisibility(View
.GONE
);
466 } // else , silently finish, the fragment was destroyed
470 private void showErrorMessage() {
471 if (mImageViewRef
!= null
) {
472 final ImageView imageView
= mImageViewRef
.get();
473 if (imageView
!= null
) {
474 // shows the default error icon
475 imageView
.setVisibility(View
.VISIBLE
);
476 } // else , silently finish, the fragment was destroyed
478 if (mMessageViewRef
!= null
) {
479 final TextView messageView
= mMessageViewRef
.get();
480 if (messageView
!= null
) {
481 messageView
.setText(mErrorMessageId
);
482 messageView
.setVisibility(View
.VISIBLE
);
483 } // else , silently finish, the fragment was destroyed
487 private void hideProgressWheel() {
488 if (mProgressWheelRef
!= null
) {
489 final ProgressBar progressWheel
= mProgressWheelRef
.get();
490 if (progressWheel
!= null
) {
491 progressWheel
.setVisibility(View
.GONE
);
499 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
501 * @param file File to test if can be previewed.
502 * @return 'True' if the file can be handled by the fragment.
504 public static boolean canBePreviewed(OCFile file
) {
505 return (file
!= null
&& file
.isImage());
510 * Finishes the preview
512 private void finish() {
513 Activity container
= getActivity();