import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
+import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
private static final String ARG_FILE = "FILE";
private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
+ private static final String ARG_SHOW_RESIZED_IMAGE = "SHOW_RESIZED_IMAGE";
private TouchImageViewCustom mImageView;
private TextView mMessageView;
private Boolean mShowResizedImage = false;
public Bitmap mBitmap = null;
-
+
private static final String TAG = PreviewImageFragment.class.getSimpleName();
private boolean mIgnoreFirstSavedState;
-
+
private LoadBitmapTask mLoadBitmapTask = null;
Bundle args = new Bundle();
args.putParcelable(ARG_FILE, imageFile);
args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
+ args.putBoolean(ARG_SHOW_RESIZED_IMAGE, showResizedImage);
frag.setArguments(args);
return frag;
}
-
+
/**
* Creates an empty fragment for image previews.
public PreviewImageFragment() {
mIgnoreFirstSavedState = false;
}
-
-
+
+
/**
* {@inheritDoc}
*/
// not right now
mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
+ mShowResizedImage = args.getBoolean(ARG_SHOW_RESIZED_IMAGE);
setHasOptionsMenu(true);
}
-
+
/**
* {@inheritDoc}
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
+ Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.preview_image_fragment, container, false);
mImageView = (TouchImageViewCustom) view.findViewById(R.id.image);
throw new IllegalStateException("Instanced with a NULL OCFile");
}
}
-
+
/**
* {@inheritDoc}
super.onSaveInstanceState(outState);
outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
}
-
+
@Override
public void onStart() {
mImageView.setVisibility(View.VISIBLE);
mBitmap = thumbnail;
} else {
- // generate new Thumbnail
- if (ThumbnailsCacheManager.cancelPotentialWork(getFile(), mImageView)) {
+ // generate new Thumbnail
+ if (ThumbnailsCacheManager.cancelPotentialWork(getFile(), mImageView) &&
+ mContainerActivity.getStorageManager() != null) {
final ThumbnailsCacheManager.ThumbnailGenerationTask task =
new ThumbnailsCacheManager.ThumbnailGenerationTask(
mImageView, mContainerActivity.getStorageManager(),
}
} else {
mLoadBitmapTask = new LoadBitmapTask(mImageView, mMessageView, mProgressWheel);
- mLoadBitmapTask.execute(getFile().getStoragePath());
+ mLoadBitmapTask.execute(getFile());
}
}
}
-
-
+
+
@Override
public void onStop() {
Log_OC.d(TAG, "onStop starts");
}
super.onStop();
}
-
+
/**
* {@inheritDoc}
*/
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
-
+
if (mContainerActivity.getStorageManager() != null) {
// Update the file
setFile(mContainerActivity.getStorageManager().getFileById(getFile().getFileId()));
-
+
FileMenuFilter mf = new FileMenuFilter(
getFile(),
mContainerActivity.getStorageManager().getAccount(),
);
mf.filter(menu);
}
-
+
// additional restriction for this fragment
// TODO allow renaming in PreviewImageFragment
MenuItem item = menu.findItem(R.id.action_rename_file);
item.setVisible(false);
item.setEnabled(false);
}
-
+
// additional restriction for this fragment
// TODO allow refresh file in PreviewImageFragment
item = menu.findItem(R.id.action_sync_file);
item.setVisible(false);
item.setEnabled(false);
}
-
+
+ // additional restriction for this fragment
+ item = menu.findItem(R.id.action_copy);
+ if (item != null) {
+ item.setVisible(false);
+ item.setEnabled(false);
+ }
+
}
-
-
+
/**
* {@inheritDoc}
*/
mContainerActivity.getFileOperationsHelper().syncFile(getFile());
return true;
}
-
+ case R.id.action_favorite_file:{
+ mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
+ return true;
+ }
+ case R.id.action_unfavorite_file:{
+ mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
+ return true;
+ }
default:
return false;
}
}
-
+
private void seeDetails() {
- mContainerActivity.showDetails(getFile());
+ mContainerActivity.showDetails(getFile());
}
super.onDestroy();
}
-
+
/**
* Opens the previewed image with an external application.
*/
mContainerActivity.getFileOperationsHelper().openFile(getFile());
finish();
}
+
-
- private class LoadBitmapTask extends AsyncTask<String, Void, Bitmap> {
+ private class LoadBitmapTask extends AsyncTask<OCFile, Void, LoadImage> {
/**
* Weak reference to the target {@link ImageView} where the bitmap will be loaded into.
*/
private final WeakReference<TextView> mMessageViewRef;
-
+
/**
* Weak reference to the target {@link ProgressBar} shown while the load is in progress.
*
*/
private final WeakReference<ProgressBar> mProgressWheelRef;
-
+
/**
- * Error message to show when a load fails
+ * Error message to show when a load fails
*/
private int mErrorMessageId;
-
-
+
+
/**
* Constructor.
- *
- * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
+ *
+ * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
*/
public LoadBitmapTask(ImageViewCustom imageView, TextView messageView,
ProgressBar progressWheel) {
mMessageViewRef = new WeakReference<TextView>(messageView);
mProgressWheelRef = new WeakReference<ProgressBar>(progressWheel);
}
-
-
+
@Override
- protected Bitmap doInBackground(String... params) {
+ protected LoadImage doInBackground(OCFile... params) {
Bitmap result = null;
if (params.length != 1) return null;
- String storagePath = params[0];
+ OCFile ocFile = params[0];
+ String storagePath = ocFile.getStoragePath();
try {
int maxDownScale = 3; // could be a parameter passed to doInBackground(...)
result = BitmapUtils.decodeSampledBitmapFromFile(storagePath, minWidth,
minHeight);
- if (isCancelled()) return result;
+ if (isCancelled()) return new LoadImage(result, ocFile);
if (result == null) {
mErrorMessageId = R.string.preview_image_error_unknown_format;
} catch (NoSuchFieldError e) {
mErrorMessageId = R.string.common_error_unknown;
- Log_OC.e(TAG, "Error from access to unexisting field despite protection; file "
- + storagePath, e);
-
+ Log_OC.e(TAG, "Error from access to unexisting field despite protection; file "
+ + storagePath, e);
+
} catch (Throwable t) {
mErrorMessageId = R.string.common_error_unknown;
Log_OC.e(TAG, "Unexpected error loading " + getFile().getStoragePath(), t);
-
+
}
-
- return result;
+
+ return new LoadImage(result, ocFile);
}
-
+
@Override
- protected void onCancelled(Bitmap result) {
- if (result != null) {
- result.recycle();
+ protected void onCancelled(LoadImage result) {
+ if (result != null && result.bitmap != null) {
+ result.bitmap.recycle();
}
}
@Override
- protected void onPostExecute(Bitmap result) {
+ protected void onPostExecute(LoadImage result) {
hideProgressWheel();
- if (result != null) {
+ if (result.bitmap != null) {
showLoadedImage(result);
- } else {
+ }
+ else {
showErrorMessage();
}
- if (result != null && mBitmap != result) {
+ if (result.bitmap != null && mBitmap != result.bitmap) {
// unused bitmap, release it! (just in case)
- result.recycle();
+ result.bitmap.recycle();
}
}
-
+
@SuppressLint("InlinedApi")
- private void showLoadedImage(Bitmap result) {
+ private void showLoadedImage(LoadImage result) {
final ImageViewCustom imageView = mImageViewRef.get();
+ Bitmap bitmap = result.bitmap;
if (imageView != null) {
- Log_OC.d(TAG, "Showing image with resolution " + result.getWidth() + "x" +
- result.getHeight());
- imageView.setImageBitmap(result);
+ Log_OC.d(TAG, "Showing image with resolution " + bitmap.getWidth() + "x" +
+ bitmap.getHeight());
+
+ if (result.ocFile.getMimetype().equalsIgnoreCase("image/png")){
+ Drawable backrepeat = getResources().getDrawable(R.drawable.backrepeat);
+ imageView.setBackground(backrepeat);
+ }
+
+ if (result.ocFile.getMimetype().equalsIgnoreCase("image/gif")){
+ imageView.setGifImage(result.ocFile);
+ } else {
+ imageView.setImageBitmap(bitmap);
+ }
+
imageView.setVisibility(View.VISIBLE);
- mBitmap = result; // needs to be kept for recycling when not useful
+ mBitmap = bitmap; // needs to be kept for recycling when not useful
}
final TextView messageView = mMessageViewRef.get();
messageView.setVisibility(View.GONE);
} // else , silently finish, the fragment was destroyed
}
-
+
private void showErrorMessage() {
final ImageView imageView = mImageViewRef.get();
if (imageView != null) {
messageView.setVisibility(View.VISIBLE);
} // else , silently finish, the fragment was destroyed
}
-
+
private void hideProgressWheel() {
final ProgressBar progressWheel = mProgressWheelRef.get();
if (progressWheel != null) {
progressWheel.setVisibility(View.GONE);
}
}
-
+
}
/**
return (file != null && file.isImage());
}
-
+
/**
* Finishes the preview
*/
Activity container = getActivity();
container.finish();
}
-
+
public TouchImageViewCustom getImageView() {
return mImageView;
}
+ private class LoadImage {
+ private Bitmap bitmap;
+ private OCFile ocFile;
+
+ public LoadImage(Bitmap bitmap, OCFile ocFile){
+ this.bitmap = bitmap;
+ this.ocFile = ocFile;
+ }
+
+ }
+
}