From 2d8de5845be5f49d3767b8354f0d5479a0715abe Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Wed, 4 Sep 2013 14:42:52 +0200 Subject: Create a separate wallpaper crop activity Change-Id: Ifb0a7dcb5a6e9ddd4f4bbccc793d9b4db1256cf2 --- AndroidManifest.xml | 15 +- res/layout/wallpaper_cropper.xml | 39 +++ res/values/strings.xml | 23 -- .../android/launcher3/WallpaperCropActivity.java | 239 +++++++++++++- .../android/launcher3/WallpaperPickerActivity.java | 359 ++++++--------------- 5 files changed, 381 insertions(+), 294 deletions(-) create mode 100644 res/layout/wallpaper_cropper.xml diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 477c4e1d3..a91de6572 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -110,11 +110,24 @@ android:finishOnCloseSystemDialogs="true" android:process=":wallpaper_chooser"> - + + + + + + + + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index b860d42aa..b43f51b6c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -28,14 +28,10 @@ Android Core Apps - - Choose wallpaper from Set wallpaper Gallery - - Wallpapers App isn\'t installed. @@ -88,8 +84,6 @@ s --> Shortcuts Widgets - - Wallpapers No more room on your Home screens. @@ -142,23 +136,6 @@ s --> be uninstalled. [CHAR_LIMIT=30] --> Uninstall update - - - - Add - - Manage apps - - Wallpaper - - Search - - Notifications - - System settings - - Help - diff --git a/src/com/android/launcher3/WallpaperCropActivity.java b/src/com/android/launcher3/WallpaperCropActivity.java index 3468f2fcf..6832b0c2c 100644 --- a/src/com/android/launcher3/WallpaperCropActivity.java +++ b/src/com/android/launcher3/WallpaperCropActivity.java @@ -16,9 +16,11 @@ package com.android.launcher3; +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.Resources; import android.graphics.Bitmap; @@ -33,9 +35,14 @@ import android.graphics.Rect; import android.graphics.RectF; import android.net.Uri; import android.os.AsyncTask; +import android.os.Bundle; import android.util.Log; +import android.view.Display; +import android.view.View; +import android.view.WindowManager; import com.android.gallery3d.common.Utils; +import com.android.photos.BitmapRegionTileSource; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; @@ -44,7 +51,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -// LAUNCHER crop activity! public class WallpaperCropActivity extends Activity { private static final String LOGTAG = "Launcher3.CropActivity"; @@ -59,7 +65,201 @@ public class WallpaperCropActivity extends Activity { * array instead of a Bitmap instance to avoid overhead. */ public static final int MAX_BMAP_IN_INTENT = 750000; + private static final float WALLPAPER_SCREENS_SPAN = 2f; + protected CropView mCropView; + protected Uri mUri; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + init(); + } + + protected void init() { + setContentView(R.layout.wallpaper_cropper); + + mCropView = (CropView) findViewById(R.id.cropView); + + Intent cropIntent = this.getIntent(); + final Uri imageUri = cropIntent.getData(); + + mCropView.setTileSource(new BitmapRegionTileSource(this, imageUri, 1024, 0), null); + mCropView.setTouchEnabled(true); + // Action bar + // Show the custom action bar view + final ActionBar actionBar = getActionBar(); + actionBar.setCustomView(R.layout.actionbar_set_wallpaper); + actionBar.getCustomView().setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean finishActivityWhenDone = true; + cropImageAndSetWallpaper(imageUri, finishActivityWhenDone); + } + }); + } + + // As a ratio of screen height, the total distance we want the parallax effect to span + // horizontally + private static float wallpaperTravelToScreenWidthRatio(int width, int height) { + float aspectRatio = width / (float) height; + + // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width + // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width + // We will use these two data points to extrapolate how much the wallpaper parallax effect + // to span (ie travel) at any aspect ratio: + + final float ASPECT_RATIO_LANDSCAPE = 16/10f; + final float ASPECT_RATIO_PORTRAIT = 10/16f; + final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; + final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; + + // To find out the desired width at different aspect ratios, we use the following two + // formulas, where the coefficient on x is the aspect ratio (width/height): + // (16/10)x + y = 1.5 + // (10/16)x + y = 1.2 + // We solve for x and y and end up with a final formula: + final float x = + (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / + (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); + final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; + return x * aspectRatio + y; + } + + static protected Point getDefaultWallpaperSize(Resources res, WindowManager windowManager) { + Point minDims = new Point(); + Point maxDims = new Point(); + windowManager.getDefaultDisplay().getCurrentSizeRange(minDims, maxDims); + + int maxDim = Math.max(maxDims.x, maxDims.y); + final int minDim = Math.min(minDims.x, minDims.y); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { + Point realSize = new Point(); + windowManager.getDefaultDisplay().getRealSize(realSize); + maxDim = Math.max(realSize.x, realSize.y); + } + + // We need to ensure that there is enough extra space in the wallpaper + // for the intended + // parallax effects + final int defaultWidth, defaultHeight; + if (LauncherAppState.isScreenLarge(res)) { + defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); + defaultHeight = maxDim; + } else { + defaultWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim); + defaultHeight = maxDim; + } + return new Point(defaultWidth, defaultHeight); + } + + protected void cropImageAndSetWallpaper(Resources res, + int resId, final boolean finishActivityWhenDone) { + // crop this image and scale it down to the default wallpaper size for + // this device + Point inSize = mCropView.getSourceDimensions(); + Point outSize = getDefaultWallpaperSize(getResources(), + getWindowManager()); + RectF crop = getMaxCropRect( + inSize.x, inSize.y, outSize.x, outSize.y); + Runnable onEndCrop = new Runnable() { + public void run() { + // Passing 0, 0 will cause launcher to revert to using the + // default wallpaper size + updateWallpaperDimensions(0, 0); + if (finishActivityWhenDone) { + setResult(Activity.RESULT_OK); + finish(); + } + } + }; + BitmapCropTask cropTask = new BitmapCropTask(res, resId, + crop, outSize.x, outSize.y, + true, false, onEndCrop); + cropTask.execute(); + } + + protected void cropImageAndSetWallpaper(Uri uri, final boolean finishActivityWhenDone) { + // Get the crop + Point inSize = mCropView.getSourceDimensions(); + + Point minDims = new Point(); + Point maxDims = new Point(); + Display d = getWindowManager().getDefaultDisplay(); + d.getCurrentSizeRange(minDims, maxDims); + + Point displaySize = new Point(); + d.getSize(displaySize); + + int maxDim = Math.max(maxDims.x, maxDims.y); + final int minDim = Math.min(minDims.x, minDims.y); + int defaultWidth; + if (LauncherAppState.isScreenLarge(getResources())) { + defaultWidth = (int) (maxDim * + wallpaperTravelToScreenWidthRatio(maxDim, minDim)); + } else { + defaultWidth = Math.max((int) + (minDim * WALLPAPER_SCREENS_SPAN), maxDim); + } + + boolean isPortrait = displaySize.x < displaySize.y; + int portraitHeight; + if (isPortrait) { + portraitHeight = mCropView.getHeight(); + } else { + // TODO: how to actually get the proper portrait height? + // This is not quite right: + portraitHeight = Math.max(maxDims.x, maxDims.y); + } + if (android.os.Build.VERSION.SDK_INT >= + android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { + Point realSize = new Point(); + d.getRealSize(realSize); + portraitHeight = Math.max(realSize.x, realSize.y); + } + // Get the crop + RectF cropRect = mCropView.getCrop(); + float cropScale = mCropView.getWidth() / (float) cropRect.width(); + + // ADJUST CROP WIDTH + // Extend the crop all the way to the right, for parallax + float extraSpaceToRight = inSize.x - cropRect.right; + // Cap the amount of extra width + float maxExtraSpace = defaultWidth / cropScale - cropRect.width(); + extraSpaceToRight = Math.min(extraSpaceToRight, maxExtraSpace); + + cropRect.right += extraSpaceToRight; + + // ADJUST CROP HEIGHT + if (isPortrait) { + cropRect.bottom = cropRect.top + portraitHeight / cropScale; + } else { // LANDSCAPE + float extraPortraitHeight = + portraitHeight / cropScale - cropRect.height(); + float expandHeight = + Math.min(Math.min(inSize.y - cropRect.bottom, cropRect.top), + extraPortraitHeight / 2); + cropRect.top -= expandHeight; + cropRect.bottom += expandHeight; + } + final int outWidth = (int) Math.round(cropRect.width() * cropScale); + final int outHeight = (int) Math.round(cropRect.height() * cropScale); + + Runnable onEndCrop = new Runnable() { + public void run() { + updateWallpaperDimensions(outWidth, outHeight); + if (finishActivityWhenDone) { + setResult(Activity.RESULT_OK); + finish(); + } + } + }; + BitmapCropTask cropTask = new BitmapCropTask(uri, + cropRect, outWidth, outHeight, true, false, onEndCrop); + cropTask.execute(); + } protected class BitmapCropTask extends AsyncTask { Uri mInUri = null; @@ -294,10 +494,45 @@ public class WallpaperCropActivity extends Activity { } editor.commit(); - WallpaperPickerActivity.suggestWallpaperDimension(getResources(), + suggestWallpaperDimension(getResources(), sp, getWindowManager(), WallpaperManager.getInstance(this)); } + static public void suggestWallpaperDimension(Resources res, + final SharedPreferences sharedPrefs, + WindowManager windowManager, + final WallpaperManager wallpaperManager) { + final Point defaultWallpaperSize = + WallpaperCropActivity.getDefaultWallpaperSize(res, windowManager); + + new Thread("suggestWallpaperDimension") { + public void run() { + // If we have saved a wallpaper width/height, use that instead + int savedWidth = sharedPrefs.getInt(WALLPAPER_WIDTH_KEY, defaultWallpaperSize.x); + int savedHeight = sharedPrefs.getInt(WALLPAPER_HEIGHT_KEY, defaultWallpaperSize.y); + wallpaperManager.suggestDesiredDimensions(savedWidth, savedHeight); + } + }.start(); + } + + + protected static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight) { + RectF cropRect = new RectF(); + // Get a crop rect that will fit this + if (inWidth / (float) inHeight > outWidth / (float) outHeight) { + cropRect.top = 0; + cropRect.bottom = inHeight; + cropRect.left = (inWidth - (outWidth / (float) outHeight) * inHeight) / 2; + cropRect.right = inWidth - cropRect.left; + } else { + cropRect.left = 0; + cropRect.right = inWidth; + cropRect.top = (inHeight - (outHeight / (float) outWidth) * inWidth) / 2; + cropRect.bottom = inHeight - cropRect.top; + } + return cropRect; + } + protected static CompressFormat convertExtensionToCompressFormat(String extension) { return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG; } diff --git a/src/com/android/launcher3/WallpaperPickerActivity.java b/src/com/android/launcher3/WallpaperPickerActivity.java index b7226f61b..674c597d7 100644 --- a/src/com/android/launcher3/WallpaperPickerActivity.java +++ b/src/com/android/launcher3/WallpaperPickerActivity.java @@ -18,10 +18,8 @@ package com.android.launcher3; import android.app.ActionBar; import android.app.Activity; -import android.app.WallpaperManager; import android.content.ComponentName; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -36,7 +34,6 @@ import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.util.Pair; -import android.view.Display; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -64,7 +61,6 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { private static final int IMAGE_PICK = 5; private static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6; - private static final float WALLPAPER_SCREENS_SPAN = 2f; private ArrayList mThumbs; private ArrayList mImages; @@ -73,6 +69,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { private View mSelectedThumb; private CropView mCropView; private boolean mIgnoreNextTap; + private OnClickListener mThumbnailOnClickListener; private static class ThumbnailMetaData { public boolean mLaunchesGallery; @@ -80,106 +77,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { public int mWallpaperResId; } - private OnClickListener mThumbnailOnClickListener = new OnClickListener() { - public void onClick(View v) { - if (mSelectedThumb != null) { - mSelectedThumb.setSelected(false); - } - - ThumbnailMetaData meta = (ThumbnailMetaData) v.getTag(); - - if (!meta.mLaunchesGallery) { - mSelectedThumb = v; - v.setSelected(true); - } - - if (meta.mLaunchesGallery) { - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("image/*"); - Utilities.startActivityForResultSafely( - WallpaperPickerActivity.this, intent, IMAGE_PICK); - } else if (meta.mGalleryImageUri != null) { - mCropView.setTileSource(new BitmapRegionTileSource(WallpaperPickerActivity.this, - meta.mGalleryImageUri, 1024, 0), null); - mCropView.setTouchEnabled(true); - } else { - BitmapRegionTileSource source = new BitmapRegionTileSource(mWallpaperResources, - WallpaperPickerActivity.this, meta.mWallpaperResId, 1024, 0); - mCropView.setTileSource(source, null); - Point wallpaperSize = getDefaultWallpaperSize(getResources(), getWindowManager()); - RectF crop = getMaxCropRect(source.getImageWidth(), source.getImageHeight(), - wallpaperSize.x, wallpaperSize.y); - mCropView.setScale(wallpaperSize.x / crop.width()); - mCropView.setTouchEnabled(false); - } - } - }; - - private RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight) { - RectF cropRect = new RectF(); - // Get a crop rect that will fit this - if (inWidth / (float) inHeight > outWidth / (float) outHeight) { - cropRect.top = 0; - cropRect.bottom = inHeight; - cropRect.left = (inWidth - (outWidth / (float) outHeight) * inHeight) / 2; - cropRect.right = inWidth - cropRect.left; - } else { - cropRect.left = 0; - cropRect.right = inWidth; - cropRect.top = (inHeight - (outHeight / (float) outWidth) * inWidth) / 2; - cropRect.bottom = inHeight - cropRect.top; - } - return cropRect; - } - - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) { - Uri uri = data.getData(); - - // Add a tile for the image picked from Gallery - LinearLayout wallpapers = (LinearLayout) findViewById(R.id.wallpaper_list); - FrameLayout pickedImageThumbnail = (FrameLayout) getLayoutInflater(). - inflate(R.layout.wallpaper_picker_item, wallpapers, false); - setWallpaperItemPaddingToZero(pickedImageThumbnail); - - // Load the thumbnail - ImageView image = (ImageView) pickedImageThumbnail.findViewById(R.id.wallpaper_image); - - Resources res = getResources(); - int width = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth); - int height = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight); - - BitmapCropTask cropTask = - new BitmapCropTask(uri, null, width, height, false, true, null); - Point bounds = cropTask.getImageBounds(); - - RectF cropRect = getMaxCropRect(bounds.x, bounds.y, width, height); - cropTask.setCropBounds(cropRect); - - if (cropTask.cropBitmap()) { - image.setImageBitmap(cropTask.getCroppedBitmap()); - Drawable thumbDrawable = image.getDrawable(); - thumbDrawable.setDither(true); - } else { - Log.e(TAG, "Error loading thumbnail for uri=" + uri); - } - wallpapers.addView(pickedImageThumbnail, 0); - - ThumbnailMetaData meta = new ThumbnailMetaData(); - meta.mGalleryImageUri = uri; - pickedImageThumbnail.setTag(meta); - pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener); - mThumbnailOnClickListener.onClick(pickedImageThumbnail); - } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) { - // No result code is returned; just return - setResult(RESULT_OK); - finish(); - } - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + // called by onCreate; this is subclassed to overwrite WallpaperCropActivity + protected void init() { setContentView(R.layout.wallpaper_picker); mCropView = (CropView) findViewById(R.id.cropView); @@ -212,6 +111,43 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } }); + mThumbnailOnClickListener = new OnClickListener() { + public void onClick(View v) { + if (mSelectedThumb != null) { + mSelectedThumb.setSelected(false); + } + + ThumbnailMetaData meta = (ThumbnailMetaData) v.getTag(); + + if (!meta.mLaunchesGallery) { + mSelectedThumb = v; + v.setSelected(true); + } + + if (meta.mLaunchesGallery) { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("image/*"); + Utilities.startActivityForResultSafely( + WallpaperPickerActivity.this, intent, IMAGE_PICK); + } else if (meta.mGalleryImageUri != null) { + mCropView.setTileSource(new BitmapRegionTileSource(WallpaperPickerActivity.this, + meta.mGalleryImageUri, 1024, 0), null); + mCropView.setTouchEnabled(true); + } else { + BitmapRegionTileSource source = new BitmapRegionTileSource(mWallpaperResources, + WallpaperPickerActivity.this, meta.mWallpaperResId, 1024, 0); + mCropView.setTileSource(source, null); + Point wallpaperSize = WallpaperCropActivity.getDefaultWallpaperSize( + getResources(), getWindowManager()); + RectF crop = WallpaperCropActivity.getMaxCropRect( + source.getImageWidth(), source.getImageHeight(), + wallpaperSize.x, wallpaperSize.y); + mCropView.setScale(wallpaperSize.x / crop.width()); + mCropView.setTouchEnabled(false); + } + } + }; + // Populate the built-in wallpapers findWallpapers(); @@ -256,105 +192,63 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { if (meta.mLaunchesGallery) { // shouldn't be selected, but do nothing } else if (meta.mGalleryImageUri != null) { - // Get the crop - Point inSize = mCropView.getSourceDimensions(); - - Point minDims = new Point(); - Point maxDims = new Point(); - Display d = getWindowManager().getDefaultDisplay(); - d.getCurrentSizeRange(minDims, maxDims); - - Point displaySize = new Point(); - d.getSize(displaySize); - - int maxDim = Math.max(maxDims.x, maxDims.y); - final int minDim = Math.min(minDims.x, minDims.y); - int defaultWidth; - if (LauncherAppState.isScreenLarge(getResources())) { - defaultWidth = (int) (maxDim * - wallpaperTravelToScreenWidthRatio(maxDim, minDim)); - } else { - defaultWidth = Math.max((int) - (minDim * WALLPAPER_SCREENS_SPAN), maxDim); - } - - boolean isPortrait = displaySize.x < displaySize.y; - int portraitHeight; - if (isPortrait) { - portraitHeight = mCropView.getHeight(); - } else { - // TODO: how to actually get the proper portrait height? - // This is not quite right: - portraitHeight = Math.max(maxDims.x, maxDims.y); - } - if (android.os.Build.VERSION.SDK_INT >= - android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { - Point realSize = new Point(); - d.getRealSize(realSize); - portraitHeight = Math.max(realSize.x, realSize.y); - } - // Get the crop - RectF cropRect = mCropView.getCrop(); - float cropScale = mCropView.getWidth() / (float) cropRect.width(); - - // ADJUST CROP WIDTH - // Extend the crop all the way to the right, for parallax - float extraSpaceToRight = inSize.x - cropRect.right; - // Cap the amount of extra width - float maxExtraSpace = defaultWidth / cropScale - cropRect.width(); - extraSpaceToRight = Math.min(extraSpaceToRight, maxExtraSpace); - - cropRect.right += extraSpaceToRight; - - // ADJUST CROP HEIGHT - if (isPortrait) { - cropRect.bottom = cropRect.top + portraitHeight / cropScale; - } else { // LANDSCAPE - float extraPortraitHeight = - portraitHeight / cropScale - cropRect.height(); - float expandHeight = - Math.min(Math.min(inSize.y - cropRect.bottom, cropRect.top), - extraPortraitHeight / 2); - cropRect.top -= expandHeight; - cropRect.bottom += expandHeight; - } - final int outWidth = (int) Math.round(cropRect.width() * cropScale); - final int outHeight = (int) Math.round(cropRect.height() * cropScale); - - Runnable onEndCrop = new Runnable() { - public void run() { - updateWallpaperDimensions(outWidth, outHeight); - } - }; - BitmapCropTask cropTask = new BitmapCropTask(meta.mGalleryImageUri, - cropRect, outWidth, outHeight, true, false, onEndCrop); - cropTask.execute(); + boolean finishActivityWhenDone = true; + cropImageAndSetWallpaper(meta.mGalleryImageUri, finishActivityWhenDone); } else if (meta.mWallpaperResId != 0) { - // crop this image and scale it down to the default wallpaper size for - // this device - Point inSize = mCropView.getSourceDimensions(); - Point outSize = getDefaultWallpaperSize(getResources(), - getWindowManager()); - RectF crop = getMaxCropRect( - inSize.x, inSize.y, outSize.x, outSize.y); - Runnable onEndCrop = new Runnable() { - public void run() { - // Passing 0, 0 will cause launcher to revert to using the - // default wallpaper size - updateWallpaperDimensions(0, 0); - } - }; - BitmapCropTask cropTask = new BitmapCropTask(mWallpaperResources, - meta.mWallpaperResId, crop, outSize.x, outSize.y, - true, false, onEndCrop); - cropTask.execute(); - setResult(Activity.RESULT_OK); - finish(); + boolean finishActivityWhenDone = true; + cropImageAndSetWallpaper(mWallpaperResources, + meta.mWallpaperResId, finishActivityWhenDone); } } }); } + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) { + Uri uri = data.getData(); + + // Add a tile for the image picked from Gallery + LinearLayout wallpapers = (LinearLayout) findViewById(R.id.wallpaper_list); + FrameLayout pickedImageThumbnail = (FrameLayout) getLayoutInflater(). + inflate(R.layout.wallpaper_picker_item, wallpapers, false); + setWallpaperItemPaddingToZero(pickedImageThumbnail); + + // Load the thumbnail + ImageView image = (ImageView) pickedImageThumbnail.findViewById(R.id.wallpaper_image); + + Resources res = getResources(); + int width = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth); + int height = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight); + + BitmapCropTask cropTask = + new BitmapCropTask(uri, null, width, height, false, true, null); + Point bounds = cropTask.getImageBounds(); + + RectF cropRect = WallpaperCropActivity.getMaxCropRect( + bounds.x, bounds.y, width, height); + cropTask.setCropBounds(cropRect); + + if (cropTask.cropBitmap()) { + image.setImageBitmap(cropTask.getCroppedBitmap()); + Drawable thumbDrawable = image.getDrawable(); + thumbDrawable.setDither(true); + } else { + Log.e(TAG, "Error loading thumbnail for uri=" + uri); + } + wallpapers.addView(pickedImageThumbnail, 0); + + ThumbnailMetaData meta = new ThumbnailMetaData(); + meta.mGalleryImageUri = uri; + pickedImageThumbnail.setTag(meta); + pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener); + mThumbnailOnClickListener.onClick(pickedImageThumbnail); + } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) { + // No result code is returned; just return + setResult(RESULT_OK); + finish(); + } + } + private static void setWallpaperItemPaddingToZero(FrameLayout frameLayout) { frameLayout.setPadding(0, 0, 0, 0); frameLayout.setForeground(new ZeroPaddingDrawable(frameLayout.getForeground())); @@ -466,77 +360,6 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - // As a ratio of screen height, the total distance we want the parallax effect to span - // horizontally - private static float wallpaperTravelToScreenWidthRatio(int width, int height) { - float aspectRatio = width / (float) height; - - // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width - // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width - // We will use these two data points to extrapolate how much the wallpaper parallax effect - // to span (ie travel) at any aspect ratio: - - final float ASPECT_RATIO_LANDSCAPE = 16/10f; - final float ASPECT_RATIO_PORTRAIT = 10/16f; - final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; - final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; - - // To find out the desired width at different aspect ratios, we use the following two - // formulas, where the coefficient on x is the aspect ratio (width/height): - // (16/10)x + y = 1.5 - // (10/16)x + y = 1.2 - // We solve for x and y and end up with a final formula: - final float x = - (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / - (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); - final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; - return x * aspectRatio + y; - } - - static private Point getDefaultWallpaperSize(Resources res, WindowManager windowManager) { - Point minDims = new Point(); - Point maxDims = new Point(); - windowManager.getDefaultDisplay().getCurrentSizeRange(minDims, maxDims); - - int maxDim = Math.max(maxDims.x, maxDims.y); - final int minDim = Math.min(minDims.x, minDims.y); - - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { - Point realSize = new Point(); - windowManager.getDefaultDisplay().getRealSize(realSize); - maxDim = Math.max(realSize.x, realSize.y); - } - - // We need to ensure that there is enough extra space in the wallpaper - // for the intended - // parallax effects - final int defaultWidth, defaultHeight; - if (LauncherAppState.isScreenLarge(res)) { - defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); - defaultHeight = maxDim; - } else { - defaultWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim); - defaultHeight = maxDim; - } - return new Point(defaultWidth, defaultHeight); - } - - static public void suggestWallpaperDimension(Resources res, - final SharedPreferences sharedPrefs, - WindowManager windowManager, - final WallpaperManager wallpaperManager) { - final Point defaultWallpaperSize = getDefaultWallpaperSize(res, windowManager); - - new Thread("suggestWallpaperDimension") { - public void run() { - // If we have saved a wallpaper width/height, use that instead - int savedWidth = sharedPrefs.getInt(WALLPAPER_WIDTH_KEY, defaultWallpaperSize.x); - int savedHeight = sharedPrefs.getInt(WALLPAPER_HEIGHT_KEY, defaultWallpaperSize.y); - wallpaperManager.suggestDesiredDimensions(savedWidth, savedHeight); - } - }.start(); - } - static class ZeroPaddingDrawable extends LevelListDrawable { public ZeroPaddingDrawable(Drawable d) { super(); -- cgit v1.2.3