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
;
21 import android
.accounts
.Account
;
22 import android
.annotation
.SuppressLint
;
23 import android
.app
.Activity
;
24 import android
.graphics
.Bitmap
;
25 import android
.graphics
.BitmapFactory
;
26 import android
.graphics
.BitmapFactory
.Options
;
27 import android
.graphics
.Point
;
28 import android
.os
.AsyncTask
;
29 import android
.os
.Bundle
;
30 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
31 import android
.view
.Display
;
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
.ortiz
.touch
.TouchImageView
;
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 TouchImageView 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
= (TouchImageView
) mView
.findViewById(R
.id
.image
);
128 mImageView
.setVisibility(View
.GONE
);
129 mImageView
.setOnClickListener(new OnClickListener() {
131 public void onClick(View v
) {
132 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
136 mMessageView
= (TextView
)mView
.findViewById(R
.id
.message
);
137 mMessageView
.setVisibility(View
.GONE
);
138 mProgressWheel
= (ProgressBar
)mView
.findViewById(R
.id
.progressWheel
);
139 mProgressWheel
.setVisibility(View
.VISIBLE
);
147 public void onActivityCreated(Bundle savedInstanceState
) {
148 super.onActivityCreated(savedInstanceState
);
149 if (savedInstanceState
!= null
) {
150 if (!mIgnoreFirstSavedState
) {
151 OCFile file
= (OCFile
)savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
153 mAccount
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
);
155 mIgnoreFirstSavedState
= false
;
158 if (getFile() == null
) {
159 throw new IllegalStateException("Instanced with a NULL OCFile");
161 if (mAccount
== null
) {
162 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
164 if (!getFile().isDown()) {
165 throw new IllegalStateException("There is no local file to preview");
174 public void onSaveInstanceState(Bundle outState
) {
175 super.onSaveInstanceState(outState
);
176 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
177 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
182 public void onStart() {
184 if (getFile() != null
) {
185 BitmapLoader bl
= new BitmapLoader(mImageView
, mMessageView
, mProgressWheel
);
186 bl
.execute(new String
[]{getFile().getStoragePath()});
195 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
196 super.onCreateOptionsMenu(menu
, inflater
);
197 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
204 public void onPrepareOptionsMenu(Menu menu
) {
205 super.onPrepareOptionsMenu(menu
);
207 if (mContainerActivity
.getStorageManager() != null
) {
209 setFile(mContainerActivity
.getStorageManager().getFileById(getFile().getFileId()));
211 FileMenuFilter mf
= new FileMenuFilter(
213 mContainerActivity
.getStorageManager().getAccount(),
215 getSherlockActivity()
220 // additional restriction for this fragment
221 // TODO allow renaming in PreviewImageFragment
222 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
224 item
.setVisible(false
);
225 item
.setEnabled(false
);
228 // additional restriction for this fragment
229 // TODO allow refresh file in PreviewImageFragment
230 item
= menu
.findItem(R
.id
.action_sync_file
);
232 item
.setVisible(false
);
233 item
.setEnabled(false
);
244 public boolean onOptionsItemSelected(MenuItem item
) {
245 switch (item
.getItemId()) {
246 case R
.id
.action_share_file
: {
247 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
250 case R
.id
.action_unshare_file
: {
251 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
254 case R
.id
.action_open_file_with
: {
258 case R
.id
.action_remove_file
: {
259 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
260 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
263 case R
.id
.action_see_details
: {
267 case R
.id
.action_send_file
: {
268 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
271 case R
.id
.action_sync_file
: {
272 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
282 private void seeDetails() {
283 mContainerActivity
.showDetails(getFile());
288 public void onResume() {
294 public void onPause() {
299 public void onDestroy() {
300 if (mBitmap
!= null
) {
308 * Opens the previewed image with an external application.
310 private void openFile() {
311 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
316 private class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
319 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
321 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
323 private final WeakReference
<ImageView
> mImageViewRef
;
326 * Weak reference to the target {@link TextView} where error messages will be written.
328 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
330 private final WeakReference
<TextView
> mMessageViewRef
;
334 * Weak reference to the target {@link Progressbar} shown while the load is in progress.
336 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
338 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
342 * Error message to show when a load fails
344 private int mErrorMessageId
;
350 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
352 public BitmapLoader(ImageView imageView
, TextView messageView
, ProgressBar progressWheel
) {
353 mImageViewRef
= new WeakReference
<ImageView
>(imageView
);
354 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
355 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
359 @SuppressWarnings("deprecation")
360 @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
362 protected Bitmap
doInBackground(String
... params
) {
363 Bitmap result
= null
;
364 if (params
.length
!= 1) return result
;
365 String storagePath
= params
[0];
367 // set desired options that will affect the size of the bitmap
368 BitmapFactory
.Options options
= new Options();
369 options
.inScaled
= true
;
370 options
.inPurgeable
= true
;
371 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
372 options
.inPreferQualityOverSpeed
= false
;
374 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
375 options
.inMutable
= false
;
377 // make a false load of the bitmap - just to be able to read outWidth, outHeight and outMimeType
378 options
.inJustDecodeBounds
= true
;
379 BitmapFactory
.decodeFile(storagePath
, options
);
381 int width
= options
.outWidth
;
382 int height
= options
.outHeight
;
385 Display display
= getActivity().getWindowManager().getDefaultDisplay();
386 Point size
= new Point();
389 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
390 display
.getSize(size
);
391 screenWidth
= size
.x
;
392 screenHeight
= size
.y
;
394 screenWidth
= display
.getWidth();
395 screenHeight
= display
.getHeight();
398 if (width
> screenWidth
) {
399 // second try to scale down the image , this time depending upon the screen size
400 scale
= (int) Math
.floor((float)width
/ screenWidth
);
402 if (height
> screenHeight
) {
403 scale
= Math
.max(scale
, (int) Math
.floor((float)height
/ screenHeight
));
405 options
.inSampleSize
= scale
;
407 // really load the bitmap
408 options
.inJustDecodeBounds
= false
; // the next decodeFile call will be real
409 result
= BitmapFactory
.decodeFile(storagePath
, options
);
410 //Log_OC.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
412 if (result
== null
) {
413 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
414 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
417 } catch (OutOfMemoryError e
) {
418 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
419 Log_OC
.e(TAG
, "Out of memory occured for file " + storagePath
, e
);
421 } catch (NoSuchFieldError e
) {
422 mErrorMessageId
= R
.string
.common_error_unknown
;
423 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file " + storagePath
, e
);
425 } catch (Throwable t
) {
426 mErrorMessageId
= R
.string
.common_error_unknown
;
427 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
434 protected void onPostExecute(Bitmap result
) {
436 if (result
!= null
) {
437 showLoadedImage(result
);
443 private void showLoadedImage(Bitmap result
) {
444 if (mImageViewRef
!= null
) {
445 final ImageView imageView
= mImageViewRef
.get();
446 if (imageView
!= null
) {
447 imageView
.setImageBitmap(result
);
448 imageView
.setVisibility(View
.VISIBLE
);
450 } // else , silently finish, the fragment was destroyed
452 if (mMessageViewRef
!= null
) {
453 final TextView messageView
= mMessageViewRef
.get();
454 if (messageView
!= null
) {
455 messageView
.setVisibility(View
.GONE
);
456 } // else , silently finish, the fragment was destroyed
460 private void showErrorMessage() {
461 if (mImageViewRef
!= null
) {
462 final ImageView imageView
= mImageViewRef
.get();
463 if (imageView
!= null
) {
464 // shows the default error icon
465 imageView
.setVisibility(View
.VISIBLE
);
466 } // else , silently finish, the fragment was destroyed
468 if (mMessageViewRef
!= null
) {
469 final TextView messageView
= mMessageViewRef
.get();
470 if (messageView
!= null
) {
471 messageView
.setText(mErrorMessageId
);
472 messageView
.setVisibility(View
.VISIBLE
);
473 } // else , silently finish, the fragment was destroyed
477 private void hideProgressWheel() {
478 if (mProgressWheelRef
!= null
) {
479 final ProgressBar progressWheel
= mProgressWheelRef
.get();
480 if (progressWheel
!= null
) {
481 progressWheel
.setVisibility(View
.GONE
);
489 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
491 * @param file File to test if can be previewed.
492 * @return 'True' if the file can be handled by the fragment.
494 public static boolean canBePreviewed(OCFile file
) {
495 return (file
!= null
&& file
.isImage());
500 * Finishes the preview
502 private void finish() {
503 Activity container
= getActivity();
507 public TouchImageView
getImageView() {