diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2015-09-01 10:14:04 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2015-09-01 10:59:47 -0700 |
commit | fd169ab17b2241ec8fb1c472a22fc8711a225268 (patch) | |
tree | 633e98a552523ddb7d6b34dd4d5b8c20687d5d34 /WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java | |
parent | b9d7ff7ecb1a5e53da2e7bcce0838556ca05f634 (diff) | |
download | android_packages_apps_Trebuchet-fd169ab17b2241ec8fb1c472a22fc8711a225268.tar.gz android_packages_apps_Trebuchet-fd169ab17b2241ec8fb1c472a22fc8711a225268.tar.bz2 android_packages_apps_Trebuchet-fd169ab17b2241ec8fb1c472a22fc8711a225268.zip |
Loading default wallpaper on the background thread
Bug: 23353784
Change-Id: Ia64cfd6b8065fb3d589e32af12e0e0bf5242a43a
Diffstat (limited to 'WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java')
-rw-r--r-- | WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java | 111 |
1 files changed, 53 insertions, 58 deletions
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java index 890d1ffe6..2cfc9bfc3 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java @@ -21,6 +21,7 @@ import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperManager; import android.content.Intent; +import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Bitmap; @@ -57,20 +58,10 @@ import java.util.WeakHashMap; public class WallpaperCropActivity extends BaseActivity implements Handler.Callback { private static final String LOGTAG = "Launcher3.CropActivity"; - /** - * The maximum bitmap size we allow to be returned through the intent. - * Intents have a maximum of 1MB in total size. However, the Bitmap seems to - * have some overhead to hit so that we go way below the limit here to make - * sure the intent stays below 1MB.We should consider just returning a byte - * array instead of a Bitmap instance to avoid overhead. - */ - public static final int MAX_BMAP_IN_INTENT = 750000; - private static final int MSG_LOAD_IMAGE = 1; protected CropView mCropView; protected View mProgressView; - protected Uri mUri; protected View mSetWallpaperButton; private HandlerThread mLoaderThread; @@ -91,7 +82,7 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb init(); if (!enableRotation()) { - setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT); + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @@ -159,52 +150,71 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb public boolean handleMessage(Message msg) { if (msg.what == MSG_LOAD_IMAGE) { final LoadRequest req = (LoadRequest) msg.obj; - try { - req.src.loadInBackground(new InBitmapProvider() { + final boolean loadSuccess; - @Override - public Bitmap forPixelCount(int count) { - Bitmap bitmapToReuse = null; - // Find the smallest bitmap that satisfies the pixel count limit - synchronized (mReusableBitmaps) { - int currentBitmapSize = Integer.MAX_VALUE; - for (Bitmap b : mReusableBitmaps) { - int bitmapSize = b.getWidth() * b.getHeight(); - if ((bitmapSize >= count) && (bitmapSize < currentBitmapSize)) { - bitmapToReuse = b; - currentBitmapSize = bitmapSize; + if (req.src == null) { + Drawable defaultWallpaper = WallpaperManager.getInstance(this) + .getBuiltInDrawable(mCropView.getWidth(), mCropView.getHeight(), + false, 0.5f, 0.5f); + + if (defaultWallpaper == null) { + loadSuccess = false; + Log.w(LOGTAG, "Null default wallpaper encountered."); + } else { + loadSuccess = true; + req.result = new DrawableTileSource(this, + defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE); + } + } else { + try { + req.src.loadInBackground(new InBitmapProvider() { + + @Override + public Bitmap forPixelCount(int count) { + Bitmap bitmapToReuse = null; + // Find the smallest bitmap that satisfies the pixel count limit + synchronized (mReusableBitmaps) { + int currentBitmapSize = Integer.MAX_VALUE; + for (Bitmap b : mReusableBitmaps) { + int bitmapSize = b.getWidth() * b.getHeight(); + if ((bitmapSize >= count) && (bitmapSize < currentBitmapSize)) { + bitmapToReuse = b; + currentBitmapSize = bitmapSize; + } } - } - if (bitmapToReuse != null) { - mReusableBitmaps.remove(bitmapToReuse); + if (bitmapToReuse != null) { + mReusableBitmaps.remove(bitmapToReuse); + } } + return bitmapToReuse; } - return bitmapToReuse; + }); + } catch (SecurityException securityException) { + if (isActivityDestroyed()) { + // Temporarily granted permissions are revoked when the activity + // finishes, potentially resulting in a SecurityException here. + // Even though {@link #isDestroyed} might also return true in different + // situations where the configuration changes, we are fine with + // catching these cases here as well. + return true; + } else { + // otherwise it had a different cause and we throw it further + throw securityException; } - }); - } catch (SecurityException securityException) { - if (isActivityDestroyed()) { - // Temporarily granted permissions are revoked when the activity - // finishes, potentially resulting in a SecurityException here. - // Even though {@link #isDestroyed} might also return true in different - // situations where the configuration changes, we are fine with - // catching these cases here as well. - return true; - } else { - // otherwise it had a different cause and we throw it further - throw securityException; } + + req.result = new BitmapRegionTileSource(getContext(), req.src, + mTempStorageForDecoding); + loadSuccess = req.src.getLoadingState() == BitmapSource.State.LOADED; } - req.result = new BitmapRegionTileSource(getContext(), req.src, mTempStorageForDecoding); runOnUiThread(new Runnable() { @Override public void run() { if (req == mCurrentLoadRequest) { - onLoadRequestComplete(req, - req.src.getLoadingState() == BitmapSource.State.LOADED); + onLoadRequestComplete(req, loadSuccess); } else { addReusableBitmap(req.result); } @@ -268,21 +278,6 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb req.postExecute = postExecute; req.scaleProvider = scaleProvider; mCurrentLoadRequest = req; - if (bitmapSource == null) { - // Load the default wallpaper - Drawable defaultWallpaper = WallpaperManager.getInstance(this) - .getBuiltInDrawable(mCropView.getWidth(), mCropView.getHeight(), - false, 0.5f, 0.5f); - if (defaultWallpaper == null) { - Log.w(LOGTAG, "Null default wallpaper encountered."); - mCropView.setTileSource(null, null); - return; - } - req.result = new DrawableTileSource(this, - defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE); - onLoadRequestComplete(req, true); - return; - } // Remove any pending requests mLoaderHandler.removeMessages(MSG_LOAD_IMAGE); |