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
.io
.BufferedInputStream
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.FilterInputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.lang
.ref
.WeakReference
;
27 import android
.accounts
.Account
;
28 import android
.annotation
.SuppressLint
;
29 import android
.app
.Activity
;
30 import android
.graphics
.Bitmap
;
31 import android
.graphics
.BitmapFactory
;
32 import android
.os
.AsyncTask
;
33 import android
.os
.Bundle
;
34 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
35 import android
.view
.LayoutInflater
;
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
.actionbarsherlock
.view
.Menu
;
44 import com
.actionbarsherlock
.view
.MenuInflater
;
45 import com
.actionbarsherlock
.view
.MenuItem
;
46 import com
.owncloud
.android
.R
;
47 import com
.owncloud
.android
.datamodel
.OCFile
;
48 import com
.owncloud
.android
.files
.FileMenuFilter
;
49 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
50 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
51 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
52 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
53 import com
.owncloud
.android
.utils
.TouchImageViewCustom
;
58 * This fragment shows a preview of a downloaded image.
60 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
62 * If the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
64 * @author David A. Velasco
66 public class PreviewImageFragment
extends FileFragment
{
68 public static final String EXTRA_FILE
= "FILE";
69 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
72 private Account mAccount
;
73 private TouchImageViewCustom mImageView
;
74 private TextView mMessageView
;
75 private ProgressBar mProgressWheel
;
77 public Bitmap mBitmap
= null
;
79 private static final String TAG
= PreviewImageFragment
.class.getSimpleName();
81 private boolean mIgnoreFirstSavedState
;
85 * Creates a fragment to preview an image.
87 * When 'imageFile' or 'ocAccount' are null
89 * @param imageFile An {@link OCFile} to preview as an image in the fragment
90 * @param ocAccount An ownCloud account; needed to start downloads
91 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
93 public PreviewImageFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
96 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
101 * Creates an empty fragment for image previews.
103 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
105 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
107 public PreviewImageFragment() {
110 mIgnoreFirstSavedState
= false
;
118 public void onCreate(Bundle savedInstanceState
) {
119 super.onCreate(savedInstanceState
);
120 setHasOptionsMenu(true
);
128 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
129 Bundle savedInstanceState
) {
130 super.onCreateView(inflater
, container
, savedInstanceState
);
131 mView
= inflater
.inflate(R
.layout
.preview_image_fragment
, container
, false
);
132 mImageView
= (TouchImageViewCustom
) mView
.findViewById(R
.id
.image
);
133 mImageView
.setVisibility(View
.GONE
);
134 mImageView
.setOnClickListener(new OnClickListener() {
136 public void onClick(View v
) {
137 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
141 mMessageView
= (TextView
)mView
.findViewById(R
.id
.message
);
142 mMessageView
.setVisibility(View
.GONE
);
143 mProgressWheel
= (ProgressBar
)mView
.findViewById(R
.id
.progressWheel
);
144 mProgressWheel
.setVisibility(View
.VISIBLE
);
152 public void onActivityCreated(Bundle savedInstanceState
) {
153 super.onActivityCreated(savedInstanceState
);
154 if (savedInstanceState
!= null
) {
155 if (!mIgnoreFirstSavedState
) {
156 OCFile file
= (OCFile
)savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_FILE
);
158 mAccount
= savedInstanceState
.getParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
);
160 mIgnoreFirstSavedState
= false
;
163 if (getFile() == null
) {
164 throw new IllegalStateException("Instanced with a NULL OCFile");
166 if (mAccount
== null
) {
167 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
169 if (!getFile().isDown()) {
170 throw new IllegalStateException("There is no local file to preview");
179 public void onSaveInstanceState(Bundle outState
) {
180 super.onSaveInstanceState(outState
);
181 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
182 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
187 public void onStart() {
189 if (getFile() != null
) {
190 BitmapLoader bl
= new BitmapLoader(mImageView
, mMessageView
, mProgressWheel
);
191 bl
.execute(new String
[]{getFile().getStoragePath()});
200 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
201 super.onCreateOptionsMenu(menu
, inflater
);
202 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
209 public void onPrepareOptionsMenu(Menu menu
) {
210 super.onPrepareOptionsMenu(menu
);
212 if (mContainerActivity
.getStorageManager() != null
) {
214 setFile(mContainerActivity
.getStorageManager().getFileById(getFile().getFileId()));
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
);
233 // additional restriction for this fragment
234 // TODO allow refresh file in PreviewImageFragment
235 item
= menu
.findItem(R
.id
.action_sync_file
);
237 item
.setVisible(false
);
238 item
.setEnabled(false
);
241 // additional restriction for this fragment
242 item
= menu
.findItem(R
.id
.action_move
);
244 item
.setVisible(false
);
245 item
.setEnabled(false
);
256 public boolean onOptionsItemSelected(MenuItem item
) {
257 switch (item
.getItemId()) {
258 case R
.id
.action_share_file
: {
259 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
262 case R
.id
.action_unshare_file
: {
263 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
266 case R
.id
.action_open_file_with
: {
270 case R
.id
.action_remove_file
: {
271 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
272 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
275 case R
.id
.action_see_details
: {
279 case R
.id
.action_send_file
: {
280 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
283 case R
.id
.action_sync_file
: {
284 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
294 private void seeDetails() {
295 mContainerActivity
.showDetails(getFile());
300 public void onResume() {
306 public void onPause() {
311 public void onDestroy() {
312 if (mBitmap
!= null
) {
321 * Opens the previewed image with an external application.
323 private void openFile() {
324 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
329 private class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
332 * Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
334 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
336 private final WeakReference
<ImageViewCustom
> mImageViewRef
;
339 * Weak reference to the target {@link TextView} where error messages will be written.
341 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
343 private final WeakReference
<TextView
> mMessageViewRef
;
347 * Weak reference to the target {@link Progressbar} shown while the load is in progress.
349 * Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes.
351 private final WeakReference
<ProgressBar
> mProgressWheelRef
;
355 * Error message to show when a load fails
357 private int mErrorMessageId
;
363 * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
365 public BitmapLoader(ImageViewCustom imageView
, TextView messageView
, ProgressBar progressWheel
) {
366 mImageViewRef
= new WeakReference
<ImageViewCustom
>(imageView
);
367 mMessageViewRef
= new WeakReference
<TextView
>(messageView
);
368 mProgressWheelRef
= new WeakReference
<ProgressBar
>(progressWheel
);
373 protected Bitmap
doInBackground(String
... params
) {
374 Bitmap result
= null
;
375 if (params
.length
!= 1) return result
;
376 String storagePath
= params
[0];
380 File picture
= new File(storagePath
);
382 if (picture
!= null
) {
383 //Decode file into a bitmap in real size for being able to make zoom on the image
384 result
= BitmapFactory
.decodeStream(new FlushedInputStream
385 (new BufferedInputStream(new FileInputStream(picture
))));
388 if (result
== null
) {
389 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
390 Log_OC
.e(TAG
, "File could not be loaded as a bitmap: " + storagePath
);
393 } catch (OutOfMemoryError e
) {
394 mErrorMessageId
= R
.string
.preview_image_error_unknown_format
;
395 Log_OC
.e(TAG
, "Out of memory occured for file " + storagePath
, e
);
397 } catch (NoSuchFieldError e
) {
398 mErrorMessageId
= R
.string
.common_error_unknown
;
399 Log_OC
.e(TAG
, "Error from access to unexisting field despite protection; file " + storagePath
, e
);
401 } catch (Throwable t
) {
402 mErrorMessageId
= R
.string
.common_error_unknown
;
403 Log_OC
.e(TAG
, "Unexpected error loading " + getFile().getStoragePath(), t
);
410 protected void onPostExecute(Bitmap result
) {
412 if (result
!= null
) {
413 showLoadedImage(result
);
419 @SuppressLint("InlinedApi")
420 private void showLoadedImage(Bitmap result
) {
421 if (mImageViewRef
!= null
) {
422 final ImageViewCustom imageView
= mImageViewRef
.get();
423 if (imageView
!= null
) {
424 imageView
.setBitmap(result
);
425 imageView
.setImageBitmap(result
);
426 imageView
.setVisibility(View
.VISIBLE
);
428 } // else , silently finish, the fragment was destroyed
430 if (mMessageViewRef
!= null
) {
431 final TextView messageView
= mMessageViewRef
.get();
432 if (messageView
!= null
) {
433 messageView
.setVisibility(View
.GONE
);
434 } // else , silently finish, the fragment was destroyed
438 private void showErrorMessage() {
439 if (mImageViewRef
!= null
) {
440 final ImageView imageView
= mImageViewRef
.get();
441 if (imageView
!= null
) {
442 // shows the default error icon
443 imageView
.setVisibility(View
.VISIBLE
);
444 } // else , silently finish, the fragment was destroyed
446 if (mMessageViewRef
!= null
) {
447 final TextView messageView
= mMessageViewRef
.get();
448 if (messageView
!= null
) {
449 messageView
.setText(mErrorMessageId
);
450 messageView
.setVisibility(View
.VISIBLE
);
451 } // else , silently finish, the fragment was destroyed
455 private void hideProgressWheel() {
456 if (mProgressWheelRef
!= null
) {
457 final ProgressBar progressWheel
= mProgressWheelRef
.get();
458 if (progressWheel
!= null
) {
459 progressWheel
.setVisibility(View
.GONE
);
467 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewImageFragment} to be previewed.
469 * @param file File to test if can be previewed.
470 * @return 'True' if the file can be handled by the fragment.
472 public static boolean canBePreviewed(OCFile file
) {
473 return (file
!= null
&& file
.isImage());
478 * Finishes the preview
480 private void finish() {
481 Activity container
= getActivity();
485 public TouchImageViewCustom
getImageView() {
489 static class FlushedInputStream
extends FilterInputStream
{
490 public FlushedInputStream(InputStream inputStream
) {
495 public long skip(long n
) throws IOException
{
496 long totalBytesSkipped
= 0L;
497 while (totalBytesSkipped
< n
) {
498 long bytesSkipped
= in.skip(n
- totalBytesSkipped
);
499 if (bytesSkipped
== 0L) {
500 int byteValue
= read();
502 break; // we reached EOF
504 bytesSkipped
= 1; // we read one byte
507 totalBytesSkipped
+= bytesSkipped
;
509 return totalBytesSkipped
;