*/
package com.owncloud.android.ui.preview;
+import org.apache.commons.httpclient.methods.PostMethod;
+
import android.accounts.Account;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
+import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
@Override
protected void onResume() {
super.onResume();
+ Log.e(TAG, "ACTIVITY, ONRESUME");
mDownloadFinishReceiver = new DownloadFinishReceiver();
IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
registerReceiver(mDownloadFinishReceiver, filter);
}
-
@Override
protected void onPostResume() {
+ Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
super.onPostResume();
}
getSupportActionBar().setTitle(currentFile.getFileName());
if (!currentFile.isDown()) {
requestForDownload(currentFile);
+ /*} else {
+ FileFragment fragment = mPreviewImagePagerAdapter.getFragmentAt(mViewPager.getCurrentItem());
+ if (fragment instanceof PreviewImageFragment) {
+ ((PreviewImageFragment)fragment).showError();
+ }*/
}
}
}
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
+import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
*/
public class PreviewImageFragment extends SherlockFragment implements FileFragment,
OnRemoteOperationListener,
- ConfirmationDialogFragment.ConfirmationDialogFragmentListener{
+ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
public static final String EXTRA_FILE = "FILE";
public static final String EXTRA_ACCOUNT = "ACCOUNT";
private Account mAccount;
private FileDataStorageManager mStorageManager;
private ImageView mImageView;
+ private TextView mMessageView;
+
public Bitmap mBitmap = null;
private Handler mHandler;
private static final String TAG = PreviewImageFragment.class.getSimpleName();
private boolean mIgnoreFirstSavedState;
+
/**
* Creates a fragment to preview an image.
super.onCreateView(inflater, container, savedInstanceState);
mView = inflater.inflate(R.layout.preview_image_fragment, container, false);
mImageView = (ImageView)mView.findViewById(R.id.image);
- mView.setOnTouchListener((OnTouchListener)getActivity()); // WATCH OUT
+ mView.setOnTouchListener((OnTouchListener)getActivity()); // WATCH OUT THAT CAST
+ mMessageView = (TextView)mView.findViewById(R.id.message);
+ mMessageView.setVisibility(View.GONE);
return mView;
}
public void onStart() {
super.onStart();
if (mFile != null) {
- BitmapLoader bl = new BitmapLoader(mImageView);
+ BitmapLoader bl = new BitmapLoader(mImageView, mMessageView);
bl.execute(new String[]{mFile.getStoragePath()});
}
}
@Override
public void onResume() {
super.onResume();
+ Log.e(TAG, "FRAGMENT, ONRESUME");
/*
mDownloadFinishReceiver = new DownloadFinishReceiver();
IntentFilter filter = new IntentFilter(
* 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;
+
+ /**
+ * 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;
+
+
+ private Throwable mThrowable;
/**
*
* @param imageView Target {@link ImageView} where the bitmap will be loaded into.
*/
- public BitmapLoader(ImageView imageView) {
+ public BitmapLoader(ImageView imageView, TextView messageView) {
mImageViewRef = new WeakReference<ImageView>(imageView);
+ mMessageViewRef = new WeakReference<TextView>(messageView);
+ mThrowable = null;
}
int width = options.outWidth;
int height = options.outHeight;
int scale = 1;
+
+ /*
if (width >= 2048 || height >= 2048) {
// try to scale down the image to save memory
scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));
options.inSampleSize = scale;
}
+ */
+
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
- int screenwidth;
+ int screenWidth;
+ int screenHeight;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
- screenwidth = size.x;
+ screenWidth = size.x;
+ screenHeight = size.y;
} else {
- screenwidth = display.getWidth();
+ screenWidth = display.getWidth();
+ screenHeight = display.getHeight();
}
- Log.d(TAG, "image width: " + width + ", screen width: " + screenwidth);
-
- if (width > screenwidth) {
+ if (width > screenWidth) {
// second try to scale down the image , this time depending upon the screen size; WTF...
- scale = (int) Math.ceil((float)width / screenwidth);
+ scale = (int) Math.ceil((float)width / screenWidth);
options.inSampleSize = scale;
}
+ if (height > screenHeight) {
+ scale = Math.max(scale, (int) Math.ceil((float)height / screenHeight));
+ }
+
// really load the bitmap
options.inJustDecodeBounds = false; // the next decodeFile call will be real
result = BitmapFactory.decodeFile(storagePath, options);
- Log.e(TAG, "loaded width: " + options.outWidth + ", loaded height: " + options.outHeight);
+ //Log.d(TAG, "Image loaded - width: " + options.outWidth + ", loaded height: " + options.outHeight);
- } catch (OutOfMemoryError e) {
- result = null;
- Log.e(TAG, "Out of memory occured for file with size " + storagePath);
-
- } catch (NoSuchFieldError e) {
- result = null;
- Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);
-
} catch (Throwable t) {
result = null;
+ mThrowable = t; // error processing is delayed to #onPostExecute(Bitmap)
Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);
}
return result;
@Override
protected void onPostExecute(Bitmap result) {
- if (result != null && mImageViewRef != null) {
+ if (mImageViewRef != null && result != null) {
final ImageView imageView = mImageViewRef.get();
- imageView.setImageBitmap(result);
- mBitmap = result;
+ if (imageView != null) {
+ imageView.setImageBitmap(result);
+ mBitmap = result;
+ if (mMessageViewRef != null) {
+ final TextView messageView = mMessageViewRef.get();
+ if (messageView != null) {
+ messageView.setVisibility(View.GONE);
+ }
+ }
+ } // else , silently finish, the fragment was destroyed
+
+ } else if (mMessageViewRef != null && result == null) {
+ // error
+ int messageId;
+ if (mThrowable == null) {
+ messageId = R.string.preview_image_error_unknown_format;
+ Log.e(TAG, "File could not be loaded as a bitmap: " + mFile.getStoragePath());
+
+ } else if (mThrowable instanceof OutOfMemoryError) {
+ messageId = R.string.preview_image_error_unknown_format;
+ Log.e(TAG, "Out of memory occured for file " + mFile.getStoragePath(), mThrowable);
+
+ } else if (mThrowable instanceof NoSuchFieldError) {
+ messageId = R.string.common_error_unknown;
+ Log.e(TAG, "Error from access to unexisting field despite protection; file " + mFile.getStoragePath(), mThrowable);
+
+ } else {
+ messageId = R.string.common_error_unknown;
+ Log.e(TAG, "Unexpected error loading " + mFile.getStoragePath(), mThrowable);
+ }
+ final TextView messageView = mMessageViewRef.get();
+ if (messageView != null) {
+ messageView.setText(messageId);
+ messageView.setVisibility(View.VISIBLE);
+ } // else , silently finish, the fragment was destroyed
+
}
+
}
}
return (file != null && file.isImage());
}
+
/**
* {@inheritDoc}
*/
container.finish();
}
-
+
}