diff options
Diffstat (limited to 'WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java')
-rw-r--r-- | WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java | 217 |
1 files changed, 99 insertions, 118 deletions
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java index b717b597c..4be6f17ac 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java @@ -20,15 +20,14 @@ import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperManager; -import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.Configuration; +import android.content.pm.ActivityInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.RectF; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -58,24 +57,10 @@ import java.util.WeakHashMap; public class WallpaperCropActivity extends BaseActivity implements Handler.Callback { private static final String LOGTAG = "Launcher3.CropActivity"; - protected static final String WALLPAPER_WIDTH_KEY = WallpaperUtils.WALLPAPER_WIDTH_KEY; - protected static final String WALLPAPER_HEIGHT_KEY = WallpaperUtils.WALLPAPER_HEIGHT_KEY; - - /** - * 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; - public static final float WALLPAPER_SCREENS_SPAN = WallpaperUtils.WALLPAPER_SCREENS_SPAN; - private static final int MSG_LOAD_IMAGE = 1; protected CropView mCropView; protected View mProgressView; - protected Uri mUri; protected View mSetWallpaperButton; private HandlerThread mLoaderThread; @@ -96,7 +81,7 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb init(); if (!enableRotation()) { - setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT); + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @@ -123,12 +108,9 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb new View.OnClickListener() { @Override public void onClick(View v) { - boolean finishActivityWhenDone = true; // Never fade on finish because we return to the app that started us (e.g. // Photos), not the home screen. - boolean shouldFadeOutOnFinish = false; - cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone, - shouldFadeOutOnFinish); + cropImageAndSetWallpaper(imageUri, null, false /* shouldFadeOutOnFinish */); } }); mSetWallpaperButton = findViewById(R.id.set_wallpaper_button); @@ -169,52 +151,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); } @@ -226,7 +227,7 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) - protected boolean isActivityDestroyed() { + public boolean isActivityDestroyed() { return Utilities.ATLEAST_JB_MR1 && isDestroyed(); } @@ -274,14 +275,16 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb mProgressView.setVisibility(View.GONE); } + @TargetApi(Build.VERSION_CODES.KITKAT) public final void setCropViewTileSource(BitmapSource bitmapSource, boolean touchEnabled, - boolean moveToLeft, CropViewScaleAndOffsetProvider scaleProvider, Runnable postExecute) { + boolean moveToLeft, CropViewScaleAndOffsetProvider scaleAndOffsetProvider, + Runnable postExecute) { final LoadRequest req = new LoadRequest(); req.moveToLeft = moveToLeft; req.src = bitmapSource; req.touchEnabled = touchEnabled; req.postExecute = postExecute; - req.scaleAndOffsetProvider = scaleProvider; + req.scaleAndOffsetProvider = scaleAndOffsetProvider; mCurrentLoadRequest = req; // Remove any pending requests @@ -305,62 +308,39 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb return getResources().getBoolean(R.bool.allow_rotation); } - protected void setWallpaper(Uri uri, final boolean finishActivityWhenDone, - final boolean shouldFadeOutOnFinish) { + public void setWallpaper(Uri uri, boolean shouldFadeOutOnFinish) { int rotation = BitmapUtils.getRotationFromExif(getContext(), uri); BitmapCropTask cropTask = new BitmapCropTask( getContext(), uri, null, rotation, 0, 0, true, false, null); - final Point bounds = cropTask.getImageBounds(); - BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() { - public void run(boolean cropSucceeded) { - updateWallpaperDimensions(bounds.x, bounds.y); - if (finishActivityWhenDone) { - setResult(Activity.RESULT_OK); - finish(); - if (cropSucceeded && shouldFadeOutOnFinish) { - overridePendingTransition(0, R.anim.fade_out); - } - } - } - }; - cropTask.setOnEndRunnable(onEndCrop); + BitmapCropTask.OnEndCropHandler onEndCrop = new CropAndFinishHandler( + cropTask.getImageBounds(), shouldFadeOutOnFinish); + cropTask.setOnEndCropHandler(onEndCrop); cropTask.setNoCrop(true); cropTask.execute(); } - protected void cropImageAndSetWallpaper(Resources res, int resId, - final boolean finishActivityWhenDone, final boolean shouldFadeOutOnFinish) { + public void cropImageAndSetWallpaper(Resources res, int resId, boolean shouldFadeOutOnFinish) { // crop this image and scale it down to the default wallpaper size for // this device - int rotation = BitmapUtils.getRotationFromExif(res, resId); + int rotation = BitmapUtils.getRotationFromExif(res, resId, this); Point inSize = mCropView.getSourceDimensions(); Point outSize = WallpaperUtils.getDefaultWallpaperSize(getResources(), getWindowManager()); RectF crop = Utils.getMaxCropRect( inSize.x, inSize.y, outSize.x, outSize.y, false); - BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() { - public void run(boolean cropSucceeded) { - // Passing 0, 0 will cause launcher to revert to using the - // default wallpaper size - updateWallpaperDimensions(0, 0); - if (finishActivityWhenDone) { - setResult(Activity.RESULT_OK); - finish(); - if (cropSucceeded && shouldFadeOutOnFinish) { - overridePendingTransition(0, R.anim.fade_out); - } - } - } - }; + // Passing 0, 0 will cause launcher to revert to using the + // default wallpaper size + CropAndFinishHandler onEndCrop = new CropAndFinishHandler(new Point(0, 0), + shouldFadeOutOnFinish); BitmapCropTask cropTask = new BitmapCropTask(getContext(), res, resId, crop, rotation, outSize.x, outSize.y, true, false, onEndCrop); cropTask.execute(); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) - protected void cropImageAndSetWallpaper(Uri uri, + public void cropImageAndSetWallpaper(Uri uri, BitmapCropTask.OnBitmapCroppedHandler onBitmapCroppedHandler, - final boolean finishActivityWhenDone, final boolean shouldFadeOutOnFinish) { + boolean shouldFadeOutOnFinish) { // Give some feedback so user knows something is happening. mProgressView.setVisibility(View.VISIBLE); @@ -436,21 +416,12 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb cropRect.top -= expandHeight; cropRect.bottom += expandHeight; } + final int outWidth = (int) Math.round(cropRect.width() * cropScale); final int outHeight = (int) Math.round(cropRect.height() * cropScale); + CropAndFinishHandler onEndCrop = new CropAndFinishHandler(new Point(outWidth, outHeight), + shouldFadeOutOnFinish); - BitmapCropTask.OnEndCropHandler onEndCrop = new BitmapCropTask.OnEndCropHandler() { - public void run(boolean cropSucceeded) { - updateWallpaperDimensions(outWidth, outHeight); - if (finishActivityWhenDone) { - setResult(Activity.RESULT_OK); - finish(); - } - if (cropSucceeded && shouldFadeOutOnFinish) { - overridePendingTransition(0, R.anim.fade_out); - } - } - }; BitmapCropTask cropTask = new BitmapCropTask(getContext(), uri, cropRect, cropRotation, outWidth, outHeight, true, false, onEndCrop); if (onBitmapCroppedHandler != null) { @@ -459,20 +430,30 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb cropTask.execute(); } - protected void updateWallpaperDimensions(int width, int height) { - String spKey = LauncherFiles.WALLPAPER_CROP_PREFERENCES_KEY; - SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); - SharedPreferences.Editor editor = sp.edit(); - if (width != 0 && height != 0) { - editor.putInt(WALLPAPER_WIDTH_KEY, width); - editor.putInt(WALLPAPER_HEIGHT_KEY, height); - } else { - editor.remove(WALLPAPER_WIDTH_KEY); - editor.remove(WALLPAPER_HEIGHT_KEY); + public class CropAndFinishHandler implements BitmapCropTask.OnEndCropHandler { + private final Point mBounds; + private boolean mShouldFadeOutOnFinish; + + /** + * @param shouldFadeOutOnFinish Whether the wallpaper picker should override the default + * exit animation to fade out instead. This should only be set to true if the wallpaper + * preview will exactly match the actual wallpaper on the page we are returning to. + */ + public CropAndFinishHandler(Point bounds, boolean shouldFadeOutOnFinish) { + mBounds = bounds; + mShouldFadeOutOnFinish = shouldFadeOutOnFinish; + } + + @Override + public void run(boolean cropSucceeded) { + WallpaperUtils.saveWallpaperDimensions(mBounds.x, mBounds.y, + WallpaperCropActivity.this); + setResult(Activity.RESULT_OK); + finish(); + if (cropSucceeded && mShouldFadeOutOnFinish) { + overridePendingTransition(0, R.anim.fade_out); + } } - editor.commit(); - WallpaperUtils.suggestWallpaperDimension(getResources(), - sp, getWindowManager(), WallpaperManager.getInstance(getContext()), true); } static class LoadRequest { @@ -485,7 +466,7 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb TileSource result; } - interface CropViewScaleAndOffsetProvider { + public interface CropViewScaleAndOffsetProvider { float getScale(Point wallpaperSize, RectF crop); float getParallaxOffset(); } |