Avoid potention OOME when choosing a wallpaper in Home.

Change-Id: I0b9ffc69fa710a65fa370efa2020601f7a14e9d7
This commit is contained in:
Romain Guy
2009-09-16 16:54:21 -07:00
parent 2ca51dc5f0
commit e82140fe20
@@ -49,7 +49,7 @@ public class WallpaperChooser extends Activity implements AdapterView.OnItemSele
private ArrayList<Integer> mThumbs;
private ArrayList<Integer> mImages;
private AsyncTask<Integer,Void,Bitmap> mLoader;
private WallpaperLoader mLoader;
@Override
public void onCreate(Bundle icicle) {
@@ -115,9 +115,9 @@ public class WallpaperChooser extends Activity implements AdapterView.OnItemSele
public void onItemSelected(AdapterView parent, View v, int position, long id) {
if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
mLoader.cancel(true);
mLoader.cancel();
}
mLoader = new WallpaperLoader().execute(position);
mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
}
/*
@@ -183,16 +183,29 @@ public class WallpaperChooser extends Activity implements AdapterView.OnItemSele
}
class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
BitmapFactory.Options mOptions;
WallpaperLoader() {
mOptions = new BitmapFactory.Options();
mOptions.inDither = false;
mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
protected Bitmap doInBackground(Integer... params) {
if (isCancelled()) return null;
return BitmapFactory.decodeResource(getResources(), mImages.get(params[0]), null);
try {
return BitmapFactory.decodeResource(getResources(),
mImages.get(params[0]), mOptions);
} catch (OutOfMemoryError e) {
return null;
}
}
@Override
protected void onPostExecute(Bitmap b) {
if (b == null) return;
if (!isCancelled()) {
if (!isCancelled() && !mOptions.mCancel) {
// Help the GC
if (mBitmap != null) {
mBitmap.recycle();
@@ -214,5 +227,10 @@ public class WallpaperChooser extends Activity implements AdapterView.OnItemSele
b.recycle();
}
}
void cancel() {
mOptions.requestCancelDecode();
super.cancel(true);
}
}
}