package com.android.camera;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.util.Log;
import java.io.FileDescriptor;
import java.util.WeakHashMap;
private static final String TAG = "BitmapManager";
private static enum State {CANCEL, ALLOW}
public State mState = State.ALLOW;
public BitmapFactory.Options mOptions;
public boolean mThumbRequesting;
@Override
String s;
if (mState == State.CANCEL) {
s = "Cancel";
} else if (mState == State.ALLOW) {
s = "Allow";
} else {
s = "?";
}
s = "thread state = " + s + ", options = " + mOptions;
return s;
}
}
private final WeakHashMap<Thread, ThreadStatus> mThreadStatus =
new WeakHashMap<Thread, ThreadStatus>();
private static BitmapManager sManager = null;
}
ThreadStatus status = mThreadStatus.get(t);
if (status == null) {
status = new ThreadStatus();
mThreadStatus.put(t, status);
}
return status;
}
BitmapFactory.Options options) {
getOrCreateThreadStatus(t).mOptions = options;
}
ThreadStatus status = mThreadStatus.get(t);
status.mOptions = null;
}
ThreadStatus status = mThreadStatus.get(t);
if (status == null) {
return true;
}
boolean result = (status.mState != State.CANCEL);
return result;
}
getOrCreateThreadStatus(t).mState = State.ALLOW;
}
ThreadStatus status = getOrCreateThreadStatus(t);
status.mState = State.CANCEL;
if (status.mOptions != null) {
status.mOptions.requestCancelDecode();
}
notifyAll();
try {
synchronized (status) {
while (status.mThumbRequesting) {
Images.Thumbnails.cancelThumbnailRequest(cr, t.getId());
Video.Thumbnails.cancelThumbnailRequest(cr, t.getId());
status.wait(200);
}
}
} catch (InterruptedException ex) {
}
}
public Bitmap
getThumbnail(ContentResolver cr,
long origId,
int kind,
BitmapFactory.Options options, boolean isVideo) {
Thread t = Thread.currentThread();
ThreadStatus status = getOrCreateThreadStatus(t);
if (!canThreadDecoding(t)) {
Log.d(TAG, "Thread " + t + " is not allowed to decode.");
return null;
}
try {
synchronized (status) {
status.mThumbRequesting = true;
}
if (isVideo) {
return Video.Thumbnails.getThumbnail(cr, t.getId(),
kind, null);
} else {
return Images.Thumbnails.getThumbnail(cr, t.getId(),
kind, null);
}
} finally {
synchronized (status) {
status.mThumbRequesting = false;
status.notifyAll();
}
}
}
public static synchronized BitmapManager
instance() {
if (sManager == null) {
sManager = new BitmapManager();
}
return sManager;
}
BitmapFactory.Options options) {
if (options.mCancel) {
return null;
}
Thread thread = Thread.currentThread();
if (!canThreadDecoding(thread)) {
Log.d(TAG, "Thread " + thread + " is not allowed to decode.");
return null;
}
setDecodingOptions(thread, options);
Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options);
removeDecodingOptions(thread);
return b;
}
}