2013-02-25 15:24:14 +04:00
/ * ownCloud Android client application
* Copyright ( C ) 2012 - 2013 ownCloud Inc .
*
* This program is free software : you can redistribute it and / or modify
2013-04-17 14:26:13 +04:00
* it under the terms of the GNU General Public License version 2 ,
* as published by the Free Software Foundation .
2013-02-25 15:24:14 +04:00
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*
* /
2013-02-26 15:33:28 +04:00
package com.owncloud.android.ui.preview ;
2013-02-25 15:24:14 +04:00
import java.lang.ref.WeakReference ;
import java.util.ArrayList ;
import java.util.List ;
import android.accounts.Account ;
import android.annotation.SuppressLint ;
import android.app.Activity ;
import android.content.ActivityNotFoundException ;
import android.content.Intent ;
import android.graphics.Bitmap ;
import android.graphics.BitmapFactory ;
import android.graphics.BitmapFactory.Options ;
import android.graphics.Point ;
import android.net.Uri ;
import android.os.AsyncTask ;
import android.os.Bundle ;
import android.os.Handler ;
2013-02-27 17:08:58 +04:00
import android.support.v4.app.FragmentStatePagerAdapter ;
2013-02-25 15:24:14 +04:00
import android.view.Display ;
import android.view.LayoutInflater ;
import android.view.View ;
2013-02-28 20:09:00 +04:00
import android.view.View.OnTouchListener ;
2013-02-25 15:24:14 +04:00
import android.view.ViewGroup ;
import android.webkit.MimeTypeMap ;
import android.widget.ImageView ;
2013-03-01 13:49:35 +04:00
import android.widget.ProgressBar ;
2013-03-01 12:54:49 +04:00
import android.widget.TextView ;
2013-02-25 15:24:14 +04:00
import android.widget.Toast ;
import com.actionbarsherlock.view.Menu ;
import com.actionbarsherlock.view.MenuInflater ;
import com.actionbarsherlock.view.MenuItem ;
2013-10-29 17:10:42 +04:00
import com.owncloud.android.R ;
2013-02-25 15:24:14 +04:00
import com.owncloud.android.datamodel.FileDataStorageManager ;
import com.owncloud.android.datamodel.OCFile ;
2014-01-20 14:59:41 +04:00
import com.owncloud.android.lib.network.webdav.WebdavUtils ;
import com.owncloud.android.lib.operations.common.OnRemoteOperationListener ;
import com.owncloud.android.lib.operations.common.RemoteOperation ;
import com.owncloud.android.lib.operations.common.RemoteOperationResult ;
2013-02-25 15:24:14 +04:00
import com.owncloud.android.operations.RemoveFileOperation ;
2014-02-05 13:53:47 +04:00
import com.owncloud.android.ui.activity.FileActivity ;
2013-02-26 15:33:28 +04:00
import com.owncloud.android.ui.fragment.ConfirmationDialogFragment ;
import com.owncloud.android.ui.fragment.FileFragment ;
2013-11-13 14:17:30 +04:00
import com.owncloud.android.utils.Log_OC ;
2013-02-25 15:24:14 +04:00
/ * *
* This fragment shows a preview of a downloaded image .
*
* Trying to get an instance with NULL { @link OCFile } or ownCloud { @link Account } values will produce an { @link IllegalStateException } .
*
* If the { @link OCFile } passed is not downloaded , an { @link IllegalStateException } is generated on instantiation too .
*
* @author David A . Velasco
* /
2013-05-30 19:53:21 +04:00
public class PreviewImageFragment extends FileFragment implements OnRemoteOperationListener ,
2013-03-01 12:54:49 +04:00
ConfirmationDialogFragment . ConfirmationDialogFragmentListener {
2013-02-25 15:24:14 +04:00
public static final String EXTRA_FILE = " FILE " ;
public static final String EXTRA_ACCOUNT = " ACCOUNT " ;
private View mView ;
private Account mAccount ;
private FileDataStorageManager mStorageManager ;
private ImageView mImageView ;
2013-03-01 12:54:49 +04:00
private TextView mMessageView ;
2013-03-01 13:49:35 +04:00
private ProgressBar mProgressWheel ;
2013-03-01 12:54:49 +04:00
2013-02-25 15:24:14 +04:00
public Bitmap mBitmap = null ;
private Handler mHandler ;
private RemoteOperation mLastRemoteOperation ;
private static final String TAG = PreviewImageFragment . class . getSimpleName ( ) ;
2013-02-27 17:08:58 +04:00
private boolean mIgnoreFirstSavedState ;
2013-03-01 12:54:49 +04:00
2013-02-25 15:24:14 +04:00
/ * *
* Creates a fragment to preview an image .
*
* When ' imageFile ' or ' ocAccount ' are null
*
2013-02-27 17:08:58 +04:00
* @param imageFile An { @link OCFile } to preview as an image in the fragment
* @param ocAccount An ownCloud account ; needed to start downloads
* @param ignoreFirstSavedState Flag to work around an unexpected behaviour of { @link FragmentStatePagerAdapter } ; TODO better solution
2013-02-25 15:24:14 +04:00
* /
2013-02-27 17:08:58 +04:00
public PreviewImageFragment ( OCFile fileToDetail , Account ocAccount , boolean ignoreFirstSavedState ) {
2013-05-30 19:53:21 +04:00
super ( fileToDetail ) ;
2013-02-25 15:24:14 +04:00
mAccount = ocAccount ;
2013-02-27 17:08:58 +04:00
mStorageManager = null ; // we need a context to init this; the container activity is not available yet at this moment
mIgnoreFirstSavedState = ignoreFirstSavedState ;
2013-02-25 15:24:14 +04:00
}
/ * *
* Creates an empty fragment for image previews .
*
* MUST BE KEPT : the system uses it when tries to reinstantiate a fragment automatically ( for instance , when the device is turned a aside ) .
*
* DO NOT CALL IT : an { @link OCFile } and { @link Account } must be provided for a successful construction
* /
public PreviewImageFragment ( ) {
2013-05-30 19:53:21 +04:00
super ( ) ;
2013-02-25 15:24:14 +04:00
mAccount = null ;
mStorageManager = null ;
2013-02-27 17:08:58 +04:00
mIgnoreFirstSavedState = false ;
2013-02-25 15:24:14 +04:00
}
/ * *
* { @inheritDoc }
* /
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
mHandler = new Handler ( ) ;
setHasOptionsMenu ( true ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
super . onCreateView ( inflater , container , savedInstanceState ) ;
mView = inflater . inflate ( R . layout . preview_image_fragment , container , false ) ;
mImageView = ( ImageView ) mView . findViewById ( R . id . image ) ;
2013-03-01 13:49:35 +04:00
mImageView . setVisibility ( View . GONE ) ;
2013-03-01 12:54:49 +04:00
mView . setOnTouchListener ( ( OnTouchListener ) getActivity ( ) ) ; // WATCH OUT THAT CAST
mMessageView = ( TextView ) mView . findViewById ( R . id . message ) ;
mMessageView . setVisibility ( View . GONE ) ;
2013-03-01 13:49:35 +04:00
mProgressWheel = ( ProgressBar ) mView . findViewById ( R . id . progressWheel ) ;
mProgressWheel . setVisibility ( View . VISIBLE ) ;
2013-02-25 15:24:14 +04:00
return mView ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void onAttach ( Activity activity ) {
super . onAttach ( activity ) ;
if ( ! ( activity instanceof FileFragment . ContainerActivity ) )
throw new ClassCastException ( activity . toString ( ) + " must implement " + FileFragment . ContainerActivity . class . getSimpleName ( ) ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void onActivityCreated ( Bundle savedInstanceState ) {
super . onActivityCreated ( savedInstanceState ) ;
mStorageManager = new FileDataStorageManager ( mAccount , getActivity ( ) . getApplicationContext ( ) . getContentResolver ( ) ) ;
if ( savedInstanceState ! = null ) {
2013-02-27 17:08:58 +04:00
if ( ! mIgnoreFirstSavedState ) {
2014-02-10 14:27:49 +04:00
OCFile file = ( OCFile ) savedInstanceState . getParcelable ( PreviewImageFragment . EXTRA_FILE ) ;
2013-02-27 17:08:58 +04:00
mAccount = savedInstanceState . getParcelable ( PreviewImageFragment . EXTRA_ACCOUNT ) ;
2014-02-10 14:27:49 +04:00
// Update the file
if ( mAccount ! = null ) {
mStorageManager = new FileDataStorageManager ( mAccount , getActivity ( ) . getApplicationContext ( ) . getContentResolver ( ) ) ;
OCFile updatedFile = mStorageManager . getFileByPath ( file . getRemotePath ( ) ) ;
if ( updatedFile ! = null ) {
setFile ( updatedFile ) ;
} else {
setFile ( file ) ;
}
} else {
setFile ( file ) ;
}
2013-02-27 17:08:58 +04:00
} else {
mIgnoreFirstSavedState = false ;
}
2013-02-25 15:24:14 +04:00
}
2013-05-30 19:53:21 +04:00
if ( getFile ( ) = = null ) {
2013-02-25 15:24:14 +04:00
throw new IllegalStateException ( " Instanced with a NULL OCFile " ) ;
}
if ( mAccount = = null ) {
throw new IllegalStateException ( " Instanced with a NULL ownCloud Account " ) ;
}
2013-05-30 19:53:21 +04:00
if ( ! getFile ( ) . isDown ( ) ) {
2013-02-25 15:24:14 +04:00
throw new IllegalStateException ( " There is no local file to preview " ) ;
}
}
/ * *
* { @inheritDoc }
* /
@Override
public void onSaveInstanceState ( Bundle outState ) {
super . onSaveInstanceState ( outState ) ;
2013-05-30 19:53:21 +04:00
outState . putParcelable ( PreviewImageFragment . EXTRA_FILE , getFile ( ) ) ;
2013-02-25 15:24:14 +04:00
outState . putParcelable ( PreviewImageFragment . EXTRA_ACCOUNT , mAccount ) ;
}
@Override
public void onStart ( ) {
super . onStart ( ) ;
2013-05-30 19:53:21 +04:00
if ( getFile ( ) ! = null ) {
2013-03-01 13:49:35 +04:00
BitmapLoader bl = new BitmapLoader ( mImageView , mMessageView , mProgressWheel ) ;
2013-05-30 19:53:21 +04:00
bl . execute ( new String [ ] { getFile ( ) . getStoragePath ( ) } ) ;
2013-02-25 15:24:14 +04:00
}
}
/ * *
* { @inheritDoc }
* /
@Override
public void onCreateOptionsMenu ( Menu menu , MenuInflater inflater ) {
super . onCreateOptionsMenu ( menu , inflater ) ;
inflater . inflate ( R . menu . file_actions_menu , menu ) ;
List < Integer > toHide = new ArrayList < Integer > ( ) ;
MenuItem item = null ;
toHide . add ( R . id . action_cancel_download ) ;
toHide . add ( R . id . action_cancel_upload ) ;
toHide . add ( R . id . action_download_file ) ;
toHide . add ( R . id . action_rename_file ) ; // by now
2014-02-04 15:31:46 +04:00
// Options shareLink
if ( ! getFile ( ) . isShareByLink ( ) ) {
toHide . add ( R . id . action_unshare_file ) ;
}
2013-02-25 15:24:14 +04:00
for ( int i : toHide ) {
item = menu . findItem ( i ) ;
if ( item ! = null ) {
item . setVisible ( false ) ;
item . setEnabled ( false ) ;
}
}
}
2014-02-05 20:32:29 +04:00
/ * *
* { @inheritDoc }
* /
@Override
public void onPrepareOptionsMenu ( Menu menu ) {
super . onPrepareOptionsMenu ( menu ) ;
2014-02-07 12:11:08 +04:00
MenuItem item = menu . findItem ( R . id . action_unshare_file ) ;
2014-02-05 20:32:29 +04:00
// Options shareLink
2014-02-12 21:38:18 +04:00
OCFile file = ( ( FileActivity ) getSherlockActivity ( ) ) . getFile ( ) ;
if ( ! file . isShareByLink ( ) ) {
2014-02-05 20:32:29 +04:00
item . setVisible ( false ) ;
item . setEnabled ( false ) ;
2014-02-07 12:11:08 +04:00
} else {
item . setVisible ( true ) ;
item . setEnabled ( true ) ;
2014-02-05 20:32:29 +04:00
}
2014-02-07 12:11:08 +04:00
2014-02-05 20:32:29 +04:00
}
2013-02-25 15:24:14 +04:00
/ * *
* { @inheritDoc }
* /
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {
switch ( item . getItemId ( ) ) {
2014-01-31 13:43:25 +04:00
case R . id . action_share_file : {
2014-02-05 13:53:47 +04:00
FileActivity act = ( FileActivity ) getSherlockActivity ( ) ;
act . getFileOperationsHelper ( ) . shareFileWithLink ( getFile ( ) , act ) ;
2014-01-31 13:43:25 +04:00
return true ;
}
2014-02-05 20:32:29 +04:00
case R . id . action_unshare_file : {
FileActivity act = ( FileActivity ) getSherlockActivity ( ) ;
act . getFileOperationsHelper ( ) . unshareFileWithLink ( getFile ( ) , act ) ;
return true ;
}
2013-02-25 15:24:14 +04:00
case R . id . action_open_file_with : {
openFile ( ) ;
return true ;
}
case R . id . action_remove_file : {
removeFile ( ) ;
return true ;
}
case R . id . action_see_details : {
seeDetails ( ) ;
return true ;
}
2014-02-14 15:13:58 +04:00
case R . id . action_send_file : {
2014-02-17 15:44:08 +04:00
FileActivity act = ( FileActivity ) getSherlockActivity ( ) ;
act . getFileOperationsHelper ( ) . sendDownloadedFile ( getFile ( ) , act ) ;
2013-11-24 13:00:04 +04:00
return true ;
}
2013-02-25 15:24:14 +04:00
default :
return false ;
}
}
2013-11-24 13:00:04 +04:00
2014-02-03 14:22:39 +04:00
2013-02-25 15:24:14 +04:00
private void seeDetails ( ) {
2013-05-30 19:53:21 +04:00
( ( FileFragment . ContainerActivity ) getActivity ( ) ) . showDetails ( getFile ( ) ) ;
2013-02-25 15:24:14 +04:00
}
@Override
public void onResume ( ) {
super . onResume ( ) ;
}
@Override
public void onPause ( ) {
super . onPause ( ) ;
}
@Override
public void onDestroy ( ) {
super . onDestroy ( ) ;
if ( mBitmap ! = null ) {
mBitmap . recycle ( ) ;
}
}
/ * *
* Opens the previewed image with an external application .
*
* TODO - improve this ; instead of prioritize the actions available for the MIME type in the server ,
* we should get a list of available apps for MIME tpye in the server and join it with the list of
* available apps for the MIME type known from the file extension , to let the user choose
* /
private void openFile ( ) {
2013-05-30 19:53:21 +04:00
OCFile file = getFile ( ) ;
String storagePath = file . getStoragePath ( ) ;
2013-02-25 15:24:14 +04:00
String encodedStoragePath = WebdavUtils . encodePath ( storagePath ) ;
try {
Intent i = new Intent ( Intent . ACTION_VIEW ) ;
2013-05-30 19:53:21 +04:00
i . setDataAndType ( Uri . parse ( " file:// " + encodedStoragePath ) , file . getMimetype ( ) ) ;
2013-02-25 15:24:14 +04:00
i . setFlags ( Intent . FLAG_GRANT_READ_URI_PERMISSION | Intent . FLAG_GRANT_WRITE_URI_PERMISSION ) ;
startActivity ( i ) ;
} catch ( Throwable t ) {
2013-05-30 19:53:21 +04:00
Log_OC . e ( TAG , " Fail when trying to open with the mimeType provided from the ownCloud server: " + file . getMimetype ( ) ) ;
2013-02-25 15:24:14 +04:00
boolean toastIt = true ;
String mimeType = " " ;
try {
Intent i = new Intent ( Intent . ACTION_VIEW ) ;
mimeType = MimeTypeMap . getSingleton ( ) . getMimeTypeFromExtension ( storagePath . substring ( storagePath . lastIndexOf ( '.' ) + 1 ) ) ;
2013-05-30 19:53:21 +04:00
if ( mimeType = = null | | ! mimeType . equals ( file . getMimetype ( ) ) ) {
2013-02-25 15:24:14 +04:00
if ( mimeType ! = null ) {
i . setDataAndType ( Uri . parse ( " file:// " + encodedStoragePath ) , mimeType ) ;
} else {
// desperate try
i . setDataAndType ( Uri . parse ( " file:// " + encodedStoragePath ) , " *-/* " ) ;
}
i . setFlags ( Intent . FLAG_GRANT_READ_URI_PERMISSION | Intent . FLAG_GRANT_WRITE_URI_PERMISSION ) ;
startActivity ( i ) ;
toastIt = false ;
}
} catch ( IndexOutOfBoundsException e ) {
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " Trying to find out MIME type of a file without extension: " + storagePath ) ;
2013-02-25 15:24:14 +04:00
} catch ( ActivityNotFoundException e ) {
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension " ) ;
2013-02-25 15:24:14 +04:00
} catch ( Throwable th ) {
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " Unexpected problem when opening: " + storagePath , th ) ;
2013-02-25 15:24:14 +04:00
} finally {
if ( toastIt ) {
2013-05-30 19:53:21 +04:00
Toast . makeText ( getActivity ( ) , " There is no application to handle file " + file . getFileName ( ) , Toast . LENGTH_SHORT ) . show ( ) ;
2013-02-25 15:24:14 +04:00
}
}
}
finish ( ) ;
}
/ * *
* Starts a the removal of the previewed file .
*
* Shows a confirmation dialog . The action continues in { @link # onConfirmation ( String ) } , { @link # onNeutral ( String ) } or { @link # onCancel ( String ) } ,
* depending upon the user selection in the dialog .
* /
private void removeFile ( ) {
ConfirmationDialogFragment confDialog = ConfirmationDialogFragment . newInstance (
R . string . confirmation_remove_alert ,
2013-05-30 19:53:21 +04:00
new String [ ] { getFile ( ) . getFileName ( ) } ,
2013-02-25 15:24:14 +04:00
R . string . confirmation_remove_remote_and_local ,
R . string . confirmation_remove_local ,
R . string . common_cancel ) ;
confDialog . setOnConfirmationListener ( this ) ;
confDialog . show ( getFragmentManager ( ) , ConfirmationDialogFragment . FTAG_CONFIRMATION ) ;
}
/ * *
* Performs the removal of the previewed file , both locally and in the server .
* /
@Override
public void onConfirmation ( String callerTag ) {
2013-05-30 19:53:21 +04:00
if ( mStorageManager . getFileById ( getFile ( ) . getFileId ( ) ) ! = null ) { // check that the file is still there;
mLastRemoteOperation = new RemoveFileOperation ( getFile ( ) , // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
2013-02-25 15:24:14 +04:00
true ,
mStorageManager ) ;
2013-04-25 21:39:22 +04:00
mLastRemoteOperation . execute ( mAccount , getSherlockActivity ( ) , this , mHandler , getSherlockActivity ( ) ) ;
2013-02-25 15:24:14 +04:00
2013-09-19 15:29:25 +04:00
( ( PreviewImageActivity ) getActivity ( ) ) . showLoadingDialog ( ) ;
2013-02-25 15:24:14 +04:00
}
}
/ * *
* Removes the file from local storage
* /
@Override
public void onNeutral ( String callerTag ) {
2013-05-30 19:53:21 +04:00
OCFile file = getFile ( ) ;
2013-11-27 12:44:54 +04:00
mStorageManager . removeFile ( file , false , true ) ; // TODO perform in background task / new thread
finish ( ) ;
2013-02-25 15:24:14 +04:00
}
/ * *
* User cancelled the removal action .
* /
@Override
public void onCancel ( String callerTag ) {
// nothing to do here
}
private class BitmapLoader extends AsyncTask < String , Void , Bitmap > {
/ * *
* Weak reference to the target { @link ImageView } where the bitmap will be loaded into .
*
* Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes .
* /
private final WeakReference < ImageView > mImageViewRef ;
2013-03-01 12:54:49 +04:00
/ * *
* Weak reference to the target { @link TextView } where error messages will be written .
*
* Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes .
* /
private final WeakReference < TextView > mMessageViewRef ;
2013-03-01 13:49:35 +04:00
/ * *
* Weak reference to the target { @link Progressbar } shown while the load is in progress .
*
* Using a weak reference will avoid memory leaks if the target ImageView is retired from memory before the load finishes .
* /
private final WeakReference < ProgressBar > mProgressWheelRef ;
/ * *
* Error message to show when a load fails
* /
private int mErrorMessageId ;
2013-02-25 15:24:14 +04:00
/ * *
* Constructor .
*
* @param imageView Target { @link ImageView } where the bitmap will be loaded into .
* /
2013-03-01 13:49:35 +04:00
public BitmapLoader ( ImageView imageView , TextView messageView , ProgressBar progressWheel ) {
2013-02-25 15:24:14 +04:00
mImageViewRef = new WeakReference < ImageView > ( imageView ) ;
2013-03-01 12:54:49 +04:00
mMessageViewRef = new WeakReference < TextView > ( messageView ) ;
2013-03-01 13:49:35 +04:00
mProgressWheelRef = new WeakReference < ProgressBar > ( progressWheel ) ;
2013-02-25 15:24:14 +04:00
}
@SuppressWarnings ( " deprecation " )
@SuppressLint ( { " NewApi " , " NewApi " , " NewApi " } ) // to avoid Lint errors since Android SDK r20
@Override
protected Bitmap doInBackground ( String . . . params ) {
Bitmap result = null ;
if ( params . length ! = 1 ) return result ;
String storagePath = params [ 0 ] ;
try {
// set desired options that will affect the size of the bitmap
BitmapFactory . Options options = new Options ( ) ;
options . inScaled = true ;
options . inPurgeable = true ;
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . GINGERBREAD_MR1 ) {
options . inPreferQualityOverSpeed = false ;
}
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . HONEYCOMB ) {
options . inMutable = false ;
}
// make a false load of the bitmap - just to be able to read outWidth, outHeight and outMimeType
options . inJustDecodeBounds = true ;
BitmapFactory . decodeFile ( storagePath , options ) ;
int width = options . outWidth ;
int height = options . outHeight ;
int scale = 1 ;
2013-03-01 12:54:49 +04:00
2013-02-25 15:24:14 +04:00
Display display = getActivity ( ) . getWindowManager ( ) . getDefaultDisplay ( ) ;
Point size = new Point ( ) ;
2013-03-01 12:54:49 +04:00
int screenWidth ;
int screenHeight ;
2013-02-25 15:24:14 +04:00
if ( android . os . Build . VERSION . SDK_INT > = android . os . Build . VERSION_CODES . HONEYCOMB_MR2 ) {
display . getSize ( size ) ;
2013-03-01 12:54:49 +04:00
screenWidth = size . x ;
screenHeight = size . y ;
2013-02-25 15:24:14 +04:00
} else {
2013-03-01 12:54:49 +04:00
screenWidth = display . getWidth ( ) ;
screenHeight = display . getHeight ( ) ;
2013-02-25 15:24:14 +04:00
}
2013-03-01 12:54:49 +04:00
if ( width > screenWidth ) {
2013-03-01 14:17:12 +04:00
// second try to scale down the image , this time depending upon the screen size
scale = ( int ) Math . floor ( ( float ) width / screenWidth ) ;
2013-02-25 15:24:14 +04:00
}
2013-03-01 12:54:49 +04:00
if ( height > screenHeight ) {
2013-03-01 14:17:12 +04:00
scale = Math . max ( scale , ( int ) Math . floor ( ( float ) height / screenHeight ) ) ;
2013-03-01 12:54:49 +04:00
}
2013-03-01 14:17:12 +04:00
options . inSampleSize = scale ;
2013-02-25 15:24:14 +04:00
// really load the bitmap
options . inJustDecodeBounds = false ; // the next decodeFile call will be real
result = BitmapFactory . decodeFile ( storagePath , options ) ;
2013-04-25 21:39:22 +04:00
//Log_OC.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
2013-02-25 15:24:14 +04:00
2013-03-01 13:49:35 +04:00
if ( result = = null ) {
mErrorMessageId = R . string . preview_image_error_unknown_format ;
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " File could not be loaded as a bitmap: " + storagePath ) ;
2013-03-01 13:49:35 +04:00
}
} catch ( OutOfMemoryError e ) {
mErrorMessageId = R . string . preview_image_error_unknown_format ;
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " Out of memory occured for file " + storagePath , e ) ;
2013-03-01 13:49:35 +04:00
} catch ( NoSuchFieldError e ) {
mErrorMessageId = R . string . common_error_unknown ;
2013-04-25 21:39:22 +04:00
Log_OC . e ( TAG , " Error from access to unexisting field despite protection; file " + storagePath , e ) ;
2013-03-01 13:49:35 +04:00
2013-02-25 15:24:14 +04:00
} catch ( Throwable t ) {
2013-03-01 13:49:35 +04:00
mErrorMessageId = R . string . common_error_unknown ;
2013-05-30 19:53:21 +04:00
Log_OC . e ( TAG , " Unexpected error loading " + getFile ( ) . getStoragePath ( ) , t ) ;
2013-03-01 13:49:35 +04:00
2013-02-25 15:24:14 +04:00
}
return result ;
}
@Override
protected void onPostExecute ( Bitmap result ) {
2013-03-01 13:49:35 +04:00
hideProgressWheel ( ) ;
if ( result ! = null ) {
showLoadedImage ( result ) ;
} else {
showErrorMessage ( ) ;
}
}
private void showLoadedImage ( Bitmap result ) {
if ( mImageViewRef ! = null ) {
2013-02-25 15:24:14 +04:00
final ImageView imageView = mImageViewRef . get ( ) ;
2013-03-01 12:54:49 +04:00
if ( imageView ! = null ) {
imageView . setImageBitmap ( result ) ;
2013-03-01 13:49:35 +04:00
imageView . setVisibility ( View . VISIBLE ) ;
2013-03-01 12:54:49 +04:00
mBitmap = result ;
} // else , silently finish, the fragment was destroyed
2013-03-01 13:49:35 +04:00
}
if ( mMessageViewRef ! = null ) {
final TextView messageView = mMessageViewRef . get ( ) ;
if ( messageView ! = null ) {
messageView . setVisibility ( View . GONE ) ;
} // else , silently finish, the fragment was destroyed
}
}
private void showErrorMessage ( ) {
if ( mImageViewRef ! = null ) {
final ImageView imageView = mImageViewRef . get ( ) ;
if ( imageView ! = null ) {
// shows the default error icon
imageView . setVisibility ( View . VISIBLE ) ;
} // else , silently finish, the fragment was destroyed
}
if ( mMessageViewRef ! = null ) {
2013-03-01 12:54:49 +04:00
final TextView messageView = mMessageViewRef . get ( ) ;
if ( messageView ! = null ) {
2013-03-01 13:49:35 +04:00
messageView . setText ( mErrorMessageId ) ;
2013-03-01 12:54:49 +04:00
messageView . setVisibility ( View . VISIBLE ) ;
} // else , silently finish, the fragment was destroyed
2013-02-25 15:24:14 +04:00
}
2013-03-01 13:49:35 +04:00
}
private void hideProgressWheel ( ) {
if ( mProgressWheelRef ! = null ) {
final ProgressBar progressWheel = mProgressWheelRef . get ( ) ;
if ( progressWheel ! = null ) {
progressWheel . setVisibility ( View . GONE ) ;
}
}
2013-02-25 15:24:14 +04:00
}
}
/ * *
* Helper method to test if an { @link OCFile } can be passed to a { @link PreviewImageFragment } to be previewed .
*
* @param file File to test if can be previewed .
* @return ' True ' if the file can be handled by the fragment .
* /
public static boolean canBePreviewed ( OCFile file ) {
return ( file ! = null & & file . isImage ( ) ) ;
}
2013-03-01 12:54:49 +04:00
2013-02-25 15:24:14 +04:00
/ * *
* { @inheritDoc }
* /
@Override
public void onRemoteOperationFinish ( RemoteOperation operation , RemoteOperationResult result ) {
if ( operation . equals ( mLastRemoteOperation ) & & operation instanceof RemoveFileOperation ) {
onRemoveFileOperationFinish ( ( RemoveFileOperation ) operation , result ) ;
}
}
private void onRemoveFileOperationFinish ( RemoveFileOperation operation , RemoteOperationResult result ) {
2013-09-19 15:29:25 +04:00
( ( PreviewImageActivity ) getActivity ( ) ) . dismissLoadingDialog ( ) ;
2013-02-25 15:24:14 +04:00
if ( result . isSuccess ( ) ) {
Toast msg = Toast . makeText ( getActivity ( ) . getApplicationContext ( ) , R . string . remove_success_msg , Toast . LENGTH_LONG ) ;
msg . show ( ) ;
finish ( ) ;
} else {
Toast msg = Toast . makeText ( getActivity ( ) , R . string . remove_fail_msg , Toast . LENGTH_LONG ) ;
msg . show ( ) ;
if ( result . isSslRecoverableException ( ) ) {
// TODO show the SSL warning dialog
}
}
}
/ * *
* Finishes the preview
* /
private void finish ( ) {
Activity container = getActivity ( ) ;
container . finish ( ) ;
}
2013-03-01 12:54:49 +04:00
2013-02-25 15:24:14 +04:00
}