From 1e4e6ddb38d0b867d0564be834f3425ed53d46e1 Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Mon, 23 Sep 2013 23:02:05 +0100 Subject: Refactoring to better support mixed wallpaper sources Change-Id: I3b47a8f56d418c6f2e7b9fd469427c3056def8d8 --- .../launcher3/LiveWallpaperListAdapter.java | 65 ++-- .../android/launcher3/SavedWallpaperImages.java | 72 +++- .../ThirdPartyWallpaperPickerListAdapter.java | 17 +- .../android/launcher3/WallpaperCropActivity.java | 62 ++-- .../android/launcher3/WallpaperPickerActivity.java | 366 ++++++++++----------- 5 files changed, 316 insertions(+), 266 deletions(-) diff --git a/src/com/android/launcher3/LiveWallpaperListAdapter.java b/src/com/android/launcher3/LiveWallpaperListAdapter.java index a6facaa02..9d0f48bf0 100644 --- a/src/com/android/launcher3/LiveWallpaperListAdapter.java +++ b/src/com/android/launcher3/LiveWallpaperListAdapter.java @@ -17,12 +17,10 @@ package com.android.launcher3; import android.app.WallpaperInfo; +import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.AsyncTask; @@ -52,7 +50,7 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter private final LayoutInflater mInflater; private final PackageManager mPackageManager; - private List mWallpapers; + private List mWallpapers; @SuppressWarnings("unchecked") public LiveWallpaperListAdapter(Context context) { @@ -63,20 +61,11 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter new Intent(WallpaperService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); - mWallpapers = generatePlaceholderViews(list.size()); + mWallpapers = new ArrayList(); new LiveWallpaperEnumerator(context).execute(list); } - private List generatePlaceholderViews(int amount) { - ArrayList list = new ArrayList(amount); - for (int i = 0; i < amount; i++) { - LiveWallpaperInfo info = new LiveWallpaperInfo(); - list.add(info); - } - return list; - } - public int getCount() { if (mWallpapers == null) { return 0; @@ -84,7 +73,7 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter return mWallpapers.size(); } - public Object getItem(int position) { + public LiveWallpaperTile getItem(int position) { return mWallpapers.get(position); } @@ -103,31 +92,42 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter WallpaperPickerActivity.setWallpaperItemPaddingToZero((FrameLayout) view); - LiveWallpaperInfo wallpaperInfo = mWallpapers.get(position); + LiveWallpaperTile wallpaperInfo = mWallpapers.get(position); ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image); ImageView icon = (ImageView) view.findViewById(R.id.wallpaper_icon); - if (wallpaperInfo.thumbnail != null) { - image.setImageDrawable(wallpaperInfo.thumbnail); + if (wallpaperInfo.mThumbnail != null) { + image.setImageDrawable(wallpaperInfo.mThumbnail); icon.setVisibility(View.GONE); } else { - icon.setImageDrawable(wallpaperInfo.info.loadIcon(mPackageManager)); + icon.setImageDrawable(wallpaperInfo.mInfo.loadIcon(mPackageManager)); icon.setVisibility(View.VISIBLE); } TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label); - label.setText(wallpaperInfo.info.loadLabel(mPackageManager)); + label.setText(wallpaperInfo.mInfo.loadLabel(mPackageManager)); return view; } - public class LiveWallpaperInfo { - public Drawable thumbnail; - public WallpaperInfo info; - public Intent intent; + public static class LiveWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo { + private Drawable mThumbnail; + private WallpaperInfo mInfo; + public LiveWallpaperTile(Drawable thumbnail, WallpaperInfo info, Intent intent) { + mThumbnail = thumbnail; + mInfo = info; + } + public void onClick(WallpaperPickerActivity a) { + Intent preview = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); + preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, + mInfo.getComponent()); + a.onLiveWallpaperPickerLaunch(); + Utilities.startActivityForResultSafely( + a, preview, WallpaperPickerActivity.PICK_LIVE_WALLPAPER); + } } private class LiveWallpaperEnumerator extends - AsyncTask, LiveWallpaperInfo, Void> { + AsyncTask, LiveWallpaperTile, Void> { private Context mContext; private int mWallpaperPosition; @@ -168,12 +168,12 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter continue; } - LiveWallpaperInfo wallpaper = new LiveWallpaperInfo(); - wallpaper.intent = new Intent(WallpaperService.SERVICE_INTERFACE); - wallpaper.intent.setClassName(info.getPackageName(), info.getServiceName()); - wallpaper.info = info; Drawable thumb = info.loadThumbnail(packageManager); + Intent launchIntent = new Intent(WallpaperService.SERVICE_INTERFACE); + launchIntent.setClassName(info.getPackageName(), info.getServiceName()); + LiveWallpaperTile wallpaper = new LiveWallpaperTile(thumb, info, launchIntent); + // TODO: generate a default thumb /* final Resources res = mContext.getResources(); @@ -211,7 +211,6 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter thumb = new BitmapDrawable(res, thumbnail); }*/ - wallpaper.thumbnail = thumb; publishProgress(wallpaper); } @@ -219,9 +218,9 @@ public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter } @Override - protected void onProgressUpdate(LiveWallpaperInfo...infos) { - for (LiveWallpaperInfo info : infos) { - info.thumbnail.setDither(true); + protected void onProgressUpdate(LiveWallpaperTile...infos) { + for (LiveWallpaperTile info : infos) { + info.mThumbnail.setDither(true); if (mWallpaperPosition < mWallpapers.size()) { mWallpapers.set(mWallpaperPosition, info); } else { diff --git a/src/com/android/launcher3/SavedWallpaperImages.java b/src/com/android/launcher3/SavedWallpaperImages.java index 9766a8a23..f00f62ff6 100644 --- a/src/com/android/launcher3/SavedWallpaperImages.java +++ b/src/com/android/launcher3/SavedWallpaperImages.java @@ -16,6 +16,7 @@ package com.android.launcher3; +import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -27,6 +28,13 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.Log; import android.util.Pair; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListAdapter; + +import com.android.photos.BitmapRegionTileSource; import java.io.ByteArrayOutputStream; import java.io.File; @@ -35,21 +43,49 @@ import java.io.IOException; import java.util.ArrayList; -public class SavedWallpaperImages { +public class SavedWallpaperImages extends BaseAdapter implements ListAdapter { private static String TAG = "Launcher3.SavedWallpaperImages"; private ImageDb mDb; - ArrayList mIds; - ArrayList mThumbs; + ArrayList mImages; Context mContext; + LayoutInflater mLayoutInflater; + + public static class SavedWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo { + private int mDbId; + private Drawable mThumb; + public SavedWallpaperTile(int dbId, Drawable thumb) { + mDbId = dbId; + mThumb = thumb; + } + public void onClick(WallpaperPickerActivity a) { + String imageFilename = a.getSavedImages().getImageFilename(mDbId); + File file = new File(a.getFilesDir(), imageFilename); + CropView v = a.getCropView(); + v.setTileSource(new BitmapRegionTileSource(a, file.getAbsolutePath(), 1024, 0), null); + v.moveToLeft(); + v.setTouchEnabled(false); + } + public void onSave(WallpaperPickerActivity a) { + boolean finishActivityWhenDone = true; + String imageFilename = a.getSavedImages().getImageFilename(mDbId); + a.setWallpaper(imageFilename, finishActivityWhenDone); + } + public void onDelete(WallpaperPickerActivity a) { + a.getSavedImages().deleteImage(mDbId); + } + public boolean isSelectable() { + return true; + } + } - public SavedWallpaperImages(Context context) { + public SavedWallpaperImages(Activity context) { mDb = new ImageDb(context); mContext = context; + mLayoutInflater = context.getLayoutInflater(); } public void loadThumbnailsAndImageIdList() { - mIds = new ArrayList(); - mThumbs = new ArrayList(); + mImages = new ArrayList(); SQLiteDatabase db = mDb.getReadableDatabase(); Cursor result = db.query(ImageDb.TABLE_NAME, new String[] { ImageDb.COLUMN_ID, @@ -66,19 +102,31 @@ public class SavedWallpaperImages { File file = new File(mContext.getFilesDir(), filename); Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath()); if (thumb != null) { - mIds.add(result.getInt(0)); - mThumbs.add(new BitmapDrawable(thumb)); + mImages.add(new SavedWallpaperTile(result.getInt(0), new BitmapDrawable(thumb))); } } result.close(); } - public ArrayList getThumbnails() { - return mThumbs; + public int getCount() { + return mImages.size(); + } + + public SavedWallpaperTile getItem(int position) { + return mImages.get(position); } - public ArrayList getImageIds() { - return mIds; + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + Drawable thumbDrawable = mImages.get(position).mThumb; + if (thumbDrawable == null) { + Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position); + } + return WallpaperPickerActivity.createImageTileView( + mLayoutInflater, position, convertView, parent, thumbDrawable); } public String getImageFilename(int id) { diff --git a/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java b/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java index 9fd0d0a5f..ab2f5d72a 100644 --- a/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java +++ b/src/com/android/launcher3/ThirdPartyWallpaperPickerListAdapter.java @@ -41,6 +41,21 @@ public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements private List mThirdPartyWallpaperPickers = new ArrayList(); + public static class ThirdPartyWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo { + private ResolveInfo mResolveInfo; + public ThirdPartyWallpaperTile(ResolveInfo resolveInfo) { + mResolveInfo = resolveInfo; + } + public void onClick(WallpaperPickerActivity a) { + final ComponentName itemComponentName = new ComponentName( + mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name); + Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER); + launchIntent.setComponent(itemComponentName); + Utilities.startActivityForResultSafely( + a, launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY); + } + } + public ThirdPartyWallpaperPickerListAdapter(Context context) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mPackageManager = context.getPackageManager(); @@ -88,7 +103,7 @@ public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements return mThirdPartyWallpaperPickers.size(); } - public Object getItem(int position) { + public ResolveInfo getItem(int position) { return mThirdPartyWallpaperPickers.get(position); } diff --git a/src/com/android/launcher3/WallpaperCropActivity.java b/src/com/android/launcher3/WallpaperCropActivity.java index f35948058..bc8df6c1b 100644 --- a/src/com/android/launcher3/WallpaperCropActivity.java +++ b/src/com/android/launcher3/WallpaperCropActivity.java @@ -162,7 +162,6 @@ public class WallpaperCropActivity extends Activity { } protected void setWallpaper(String filePath, final boolean finishActivityWhenDone) { - BitmapCropTask cropTask = new BitmapCropTask(this, filePath, null, 0, 0, true, false, null); final Point bounds = cropTask.getImageBounds(); @@ -200,7 +199,7 @@ public class WallpaperCropActivity extends Activity { } } }; - BitmapCropTask cropTask = new BitmapCropTask(res, resId, + BitmapCropTask cropTask = new BitmapCropTask(this, res, resId, crop, outSize.x, outSize.y, true, false, onEndCrop); cropTask.execute(); @@ -287,7 +286,7 @@ public class WallpaperCropActivity extends Activity { } } }; - BitmapCropTask cropTask = new BitmapCropTask(uri, + BitmapCropTask cropTask = new BitmapCropTask(this, uri, cropRect, outWidth, outHeight, true, false, onEndCrop); if (onBitmapCroppedHandler != null) { cropTask.setOnBitmapCropped(onBitmapCroppedHandler); @@ -299,7 +298,7 @@ public class WallpaperCropActivity extends Activity { public void onBitmapCropped(byte[] imageBytes); } - protected class BitmapCropTask extends AsyncTask { + protected static class BitmapCropTask extends AsyncTask { Uri mInUri = null; Context mContext; String mInFilePath; @@ -309,7 +308,6 @@ public class WallpaperCropActivity extends Activity { RectF mCropBounds = null; int mOutWidth, mOutHeight; int mRotation = 0; // for now - protected final WallpaperManager mWPManager; String mOutputFormat = "jpg"; // for now boolean mSetWallpaper; boolean mSaveCroppedBitmap; @@ -324,7 +322,6 @@ public class WallpaperCropActivity extends Activity { boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) { mContext = c; mInFilePath = filePath; - mWPManager = WallpaperManager.getInstance(getApplicationContext()); init(cropBounds, outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable); } @@ -332,24 +329,23 @@ public class WallpaperCropActivity extends Activity { RectF cropBounds, int outWidth, int outHeight, boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) { mInImageBytes = imageBytes; - mWPManager = WallpaperManager.getInstance(getApplicationContext()); init(cropBounds, outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable); } - public BitmapCropTask(Uri inUri, + public BitmapCropTask(Context c, Uri inUri, RectF cropBounds, int outWidth, int outHeight, boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) { + mContext = c; mInUri = inUri; - mWPManager = WallpaperManager.getInstance(getApplicationContext()); init(cropBounds, outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable); } - public BitmapCropTask(Resources res, int inResId, + public BitmapCropTask(Context c, Resources res, int inResId, RectF cropBounds, int outWidth, int outHeight, boolean setWallpaper, boolean saveCroppedBitmap, Runnable onEndRunnable) { + mContext = c; mInResId = inResId; mResources = res; - mWPManager = WallpaperManager.getInstance(getApplicationContext()); init(cropBounds, outWidth, outHeight, setWallpaper, saveCroppedBitmap, onEndRunnable); } @@ -385,7 +381,7 @@ public class WallpaperCropActivity extends Activity { try { if (mInUri != null) { mInStream = new BufferedInputStream( - getContentResolver().openInputStream(mInUri)); + mContext.getContentResolver().openInputStream(mInUri)); } else if (mInFilePath != null) { mInStream = mContext.openFileInput(mInFilePath); } else if (mInImageBytes != null) { @@ -426,16 +422,17 @@ public class WallpaperCropActivity extends Activity { regenerateInputStream(); - if (mNoCrop && mInStream != null) { + WallpaperManager wallpaperManager = null; + if (mSetWallpaper) { + wallpaperManager = WallpaperManager.getInstance(mContext.getApplicationContext()); + } + if (mSetWallpaper && mNoCrop && mInStream != null) { try { - mWPManager.setStream(mInStream); + wallpaperManager.setStream(mInStream); } catch (IOException e) { Log.w(LOGTAG, "cannot write stream to wallpaper", e); failure = true; } - if (mOnEndRunnable != null) { - mOnEndRunnable.run(); - } return !failure; } if (mInStream != null) { @@ -534,26 +531,18 @@ public class WallpaperCropActivity extends Activity { ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(2048); if (crop.compress(cf, DEFAULT_COMPRESS_QUALITY, tmpOut)) { // If we need to set to the wallpaper, set it - if (mSetWallpaper && mWPManager != null) { - if (mWPManager == null) { - Log.w(LOGTAG, "no wallpaper manager"); - failure = true; - } else { - try { - byte[] outByteArray = tmpOut.toByteArray(); - mWPManager.setStream(new ByteArrayInputStream(outByteArray)); - if (mOnBitmapCroppedHandler != null) { - mOnBitmapCroppedHandler.onBitmapCropped(outByteArray); - } - } catch (IOException e) { - Log.w(LOGTAG, "cannot write stream to wallpaper", e); - failure = true; + if (mSetWallpaper && wallpaperManager != null) { + try { + byte[] outByteArray = tmpOut.toByteArray(); + wallpaperManager.setStream(new ByteArrayInputStream(outByteArray)); + if (mOnBitmapCroppedHandler != null) { + mOnBitmapCroppedHandler.onBitmapCropped(outByteArray); } + } catch (IOException e) { + Log.w(LOGTAG, "cannot write stream to wallpaper", e); + failure = true; } } - if (mOnEndRunnable != null) { - mOnEndRunnable.run(); - } } else { Log.w(LOGTAG, "cannot compress bitmap"); failure = true; @@ -569,8 +558,9 @@ public class WallpaperCropActivity extends Activity { @Override protected void onPostExecute(Boolean result) { - setResult(Activity.RESULT_OK); - finish(); + if (mOnEndRunnable != null) { + mOnEndRunnable.run(); + } } } diff --git a/src/com/android/launcher3/WallpaperPickerActivity.java b/src/com/android/launcher3/WallpaperPickerActivity.java index 868b1dfb8..5f35cde21 100644 --- a/src/com/android/launcher3/WallpaperPickerActivity.java +++ b/src/com/android/launcher3/WallpaperPickerActivity.java @@ -21,12 +21,10 @@ import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperInfo; import android.app.WallpaperManager; -import android.content.ComponentName; +import android.content.Context; import android.content.Intent; -import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.database.Cursor; import android.database.DataSetObserver; @@ -35,6 +33,7 @@ import android.graphics.Point; import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.RectF; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LevelListDrawable; import android.net.Uri; @@ -47,7 +46,6 @@ import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.SubMenu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; @@ -57,26 +55,20 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListAdapter; -import android.widget.SpinnerAdapter; import com.android.photos.BitmapRegionTileSource; -import java.io.File; +import java.io.IOException; import java.util.ArrayList; -import java.util.List; public class WallpaperPickerActivity extends WallpaperCropActivity { static final String TAG = "Launcher.WallpaperPickerActivity"; - private static final int IMAGE_PICK = 5; - private static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6; - private static final int PICK_LIVE_WALLPAPER = 7; + public static final int IMAGE_PICK = 5; + public static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6; + public static final int PICK_LIVE_WALLPAPER = 7; private static final String TEMP_WALLPAPER_TILES = "TEMP_WALLPAPER_TILES"; - private ArrayList mBundledWallpaperThumbs; - private ArrayList mBundledWallpaperResIds; - private Resources mWallpaperResources; - private View mSelectedThumb; private boolean mIgnoreNextTap; private OnClickListener mThumbnailOnClickListener; @@ -92,13 +84,80 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { private SavedWallpaperImages mSavedImages; private WallpaperInfo mLiveWallpaperInfoOnPickerLaunch; - private static class ThumbnailMetaData { - public TileType mTileType; - public Uri mWallpaperUri; - public int mSavedWallpaperDbId; - public int mWallpaperResId; - public LiveWallpaperListAdapter.LiveWallpaperInfo mLiveWallpaperInfo; - public ResolveInfo mThirdPartyWallpaperPickerInfo; + public static abstract class WallpaperTileInfo { + public void onClick(WallpaperPickerActivity a) {} + public void onSave(WallpaperPickerActivity a) {} + public void onDelete() {} + public boolean isSelectable() { return false; } + } + + public static class PickImageInfo extends WallpaperTileInfo { + public void onClick(WallpaperPickerActivity a) { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("image/*"); + Utilities.startActivityForResultSafely(a, intent, IMAGE_PICK); + } + } + + public static class UriWallpaperInfo extends WallpaperTileInfo { + private Uri mUri; + public UriWallpaperInfo(Uri uri) { + mUri = uri; + } + public void onClick(WallpaperPickerActivity a) { + CropView v = a.getCropView(); + v.setTileSource(new BitmapRegionTileSource( + a, mUri, 1024, 0), null); + v.setTouchEnabled(true); + } + + public void onSave(final WallpaperPickerActivity a) { + boolean finishActivityWhenDone = true; + OnBitmapCroppedHandler h = new OnBitmapCroppedHandler() { + public void onBitmapCropped(byte[] imageBytes) { + Point thumbSize = getDefaultThumbnailSize(a.getResources()); + Bitmap thumb = + createThumbnail(thumbSize, null, null, imageBytes, null, 0, true); + a.getSavedImages().writeImage(thumb, imageBytes); + } + }; + a.cropImageAndSetWallpaper(mUri, h, finishActivityWhenDone); + } + public boolean isSelectable() { + return true; + } + } + + public static class ResourceWallpaperInfo extends WallpaperTileInfo { + private Resources mResources; + private int mResId; + private Drawable mThumb; + + public ResourceWallpaperInfo(Resources res, int resId, Drawable thumb) { + mResources = res; + mResId = resId; + mThumb = thumb; + } + public void onClick(WallpaperPickerActivity a) { + BitmapRegionTileSource source = new BitmapRegionTileSource( + mResources, a, mResId, 1024, 0); + CropView v = a.getCropView(); + v.setTileSource(source, null); + Point wallpaperSize = WallpaperCropActivity.getDefaultWallpaperSize( + a.getResources(), a.getWindowManager()); + RectF crop = WallpaperCropActivity.getMaxCropRect( + source.getImageWidth(), source.getImageHeight(), + wallpaperSize.x, wallpaperSize.y, false); + v.setScale(wallpaperSize.x / crop.width()); + v.setTouchEnabled(false); + } + public void onSave(WallpaperPickerActivity a) { + boolean finishActivityWhenDone = true; + a.cropImageAndSetWallpaper(mResources, mResId, finishActivityWhenDone); + } + public boolean isSelectable() { + return true; + } } // called by onCreate; this is subclassed to overwrite WallpaperCropActivity @@ -151,63 +210,16 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } return; } + WallpaperTileInfo info = (WallpaperTileInfo) v.getTag(); if (mSelectedThumb != null) { mSelectedThumb.setSelected(false); + mSelectedThumb = null; } - - ThumbnailMetaData meta = (ThumbnailMetaData) v.getTag(); - if (meta.mTileType == TileType.WALLPAPER_RESOURCE || - meta.mTileType == TileType.SAVED_WALLPAPER || - meta.mTileType == TileType.WALLPAPER_URI) { + if (info.isSelectable()) { mSelectedThumb = v; v.setSelected(true); } - if (meta.mTileType == TileType.PICK_IMAGE) { - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("image/*"); - Utilities.startActivityForResultSafely( - WallpaperPickerActivity.this, intent, IMAGE_PICK); - } else if (meta.mTileType == TileType.WALLPAPER_URI) { - mCropView.setTileSource(new BitmapRegionTileSource(WallpaperPickerActivity.this, - meta.mWallpaperUri, 1024, 0), null); - mCropView.setTouchEnabled(true); - } else if (meta.mTileType == TileType.SAVED_WALLPAPER) { - String imageFilename = mSavedImages.getImageFilename(meta.mSavedWallpaperDbId); - File file = new File(getFilesDir(), imageFilename); - mCropView.setTileSource(new BitmapRegionTileSource(WallpaperPickerActivity.this, - file.getAbsolutePath(), 1024, 0), null); - mCropView.moveToLeft(); - mCropView.setTouchEnabled(false); - } else if (meta.mTileType == TileType.LIVE_WALLPAPER) { - Intent preview = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); - preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, - meta.mLiveWallpaperInfo.info.getComponent()); - WallpaperManager wm = - WallpaperManager.getInstance(WallpaperPickerActivity.this); - mLiveWallpaperInfoOnPickerLaunch = wm.getWallpaperInfo(); - Utilities.startActivityForResultSafely(WallpaperPickerActivity.this, - preview, PICK_LIVE_WALLPAPER); - } else if (meta.mTileType == TileType.WALLPAPER_RESOURCE) { - 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, false); - mCropView.setScale(wallpaperSize.x / crop.width()); - mCropView.setTouchEnabled(false); - } else if (meta.mTileType == TileType.THIRD_PARTY_WALLPAPER_PICKER) { - ResolveInfo info = meta.mThirdPartyWallpaperPickerInfo; - - final ComponentName itemComponentName = new ComponentName( - info.activityInfo.packageName, info.activityInfo.name); - Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER); - launchIntent.setComponent(itemComponentName); - Utilities.startActivityForResultSafely(WallpaperPickerActivity.this, - launchIntent, PICK_WALLPAPER_THIRD_PARTY_ACTIVITY); - } + info.onClick(WallpaperPickerActivity.this); } }; mLongClickListener = new View.OnLongClickListener() { @@ -231,29 +243,24 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { }; // Populate the built-in wallpapers - findBundledWallpapers(); + ArrayList wallpapers = findBundledWallpapers(); mWallpapersView = (LinearLayout) findViewById(R.id.wallpaper_list); - ImageAdapter ia = new ImageAdapter(this, mBundledWallpaperThumbs); - populateWallpapersFromAdapter( - mWallpapersView, ia, mBundledWallpaperResIds, TileType.WALLPAPER_RESOURCE, false, true); + BuiltInWallpapersAdapter ia = new BuiltInWallpapersAdapter(this, wallpapers); + populateWallpapersFromAdapter(mWallpapersView, ia, false, true); // Populate the saved wallpapers mSavedImages = new SavedWallpaperImages(this); mSavedImages.loadThumbnailsAndImageIdList(); - ArrayList savedWallpaperThumbs = mSavedImages.getThumbnails(); - ArrayList savedWallpaperIds = mSavedImages.getImageIds(); - ia = new ImageAdapter(this, savedWallpaperThumbs); - populateWallpapersFromAdapter( - mWallpapersView, ia, savedWallpaperIds, TileType.SAVED_WALLPAPER, true, true); + populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true, true); // Populate the live wallpapers - final LinearLayout liveWallpapersView = (LinearLayout) findViewById(R.id.live_wallpaper_list); + final LinearLayout liveWallpapersView = + (LinearLayout) findViewById(R.id.live_wallpaper_list); final LiveWallpaperListAdapter a = new LiveWallpaperListAdapter(this); a.registerDataSetObserver(new DataSetObserver() { public void onChanged() { liveWallpapersView.removeAllViews(); - populateWallpapersFromAdapter( - liveWallpapersView, a, null, TileType.LIVE_WALLPAPER, false, false); + populateWallpapersFromAdapter(liveWallpapersView, a, false, false); } }); @@ -262,8 +269,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { (LinearLayout) findViewById(R.id.third_party_wallpaper_list); final ThirdPartyWallpaperPickerListAdapter ta = new ThirdPartyWallpaperPickerListAdapter(this); - populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, null, - TileType.THIRD_PARTY_WALLPAPER_PICKER, false, false); + populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false, false); // Add a tile for the Gallery LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list); @@ -282,9 +288,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { galleryThumbnailBg.setColorFilter(colorOverlay, PorterDuff.Mode.SRC_ATOP); } - ThumbnailMetaData meta = new ThumbnailMetaData(); - meta.mTileType = TileType.PICK_IMAGE; - galleryThumbnail.setTag(meta); + PickImageInfo pickImageInfo = new PickImageInfo(); + galleryThumbnail.setTag(pickImageInfo); galleryThumbnail.setOnClickListener(mThumbnailOnClickListener); // Create smooth layout transitions for when items are deleted @@ -302,28 +307,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { new View.OnClickListener() { @Override public void onClick(View v) { - ThumbnailMetaData meta = (ThumbnailMetaData) mSelectedThumb.getTag(); - if (meta.mTileType == TileType.PICK_IMAGE) { - // shouldn't be selected, but do nothing - } else if (meta.mWallpaperUri != null) { - boolean finishActivityWhenDone = true; - OnBitmapCroppedHandler h = new OnBitmapCroppedHandler() { - public void onBitmapCropped(byte[] imageBytes) { - Bitmap thumb = createThumbnail(null, imageBytes, true); - mSavedImages.writeImage(thumb, imageBytes); - } - }; - cropImageAndSetWallpaper(meta.mWallpaperUri, h, finishActivityWhenDone); - } else if (meta.mSavedWallpaperDbId != 0) { - boolean finishActivityWhenDone = true; - String imageFilename = - mSavedImages.getImageFilename(meta.mSavedWallpaperDbId); - setWallpaper(imageFilename, finishActivityWhenDone); - } else if (meta.mWallpaperResId != 0) { - boolean finishActivityWhenDone = true; - cropImageAndSetWallpaper(mWallpaperResources, - meta.mWallpaperResId, finishActivityWhenDone); - } + WallpaperTileInfo info = (WallpaperTileInfo) mSelectedThumb.getTag(); + info.onSave(WallpaperPickerActivity.this); } }); @@ -376,8 +361,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { CheckableFrameLayout c = (CheckableFrameLayout) mWallpapersView.getChildAt(i); if (c.isChecked()) { - ThumbnailMetaData meta = (ThumbnailMetaData) c.getTag(); - mSavedImages.deleteImage(meta.mSavedWallpaperDbId); + ((WallpaperTileInfo) c.getTag()).onDelete(); viewsToRemove.add(c); } } @@ -441,34 +425,12 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - private enum TileType { - PICK_IMAGE, - WALLPAPER_RESOURCE, - WALLPAPER_URI, - SAVED_WALLPAPER, - LIVE_WALLPAPER, - THIRD_PARTY_WALLPAPER_PICKER - }; - private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter, - ArrayList imageIds, TileType tileType, boolean addLongPressHandler, boolean selectFirstTile) { + boolean addLongPressHandler, boolean selectFirstTile) { for (int i = 0; i < adapter.getCount(); i++) { FrameLayout thumbnail = (FrameLayout) adapter.getView(i, null, parent); parent.addView(thumbnail, i); - - ThumbnailMetaData meta = new ThumbnailMetaData(); - meta.mTileType = tileType; - if (tileType == TileType.WALLPAPER_RESOURCE) { - meta.mWallpaperResId = imageIds.get(i); - } else if (tileType == TileType.SAVED_WALLPAPER) { - meta.mSavedWallpaperDbId = imageIds.get(i); - } else if (tileType == TileType.LIVE_WALLPAPER) { - meta.mLiveWallpaperInfo = - (LiveWallpaperListAdapter.LiveWallpaperInfo) adapter.getItem(i); - } else if (tileType == TileType.THIRD_PARTY_WALLPAPER_PICKER) { - meta.mThirdPartyWallpaperPickerInfo = (ResolveInfo) adapter.getItem(i); - } - thumbnail.setTag(meta); + thumbnail.setTag(adapter.getItem(i)); if (addLongPressHandler) { addLongPressHandler(thumbnail); } @@ -479,16 +441,25 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - private Bitmap createThumbnail(Uri uri, byte[] imageBytes, boolean leftAligned) { - Resources res = getResources(); - int width = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth); - int height = res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight); + private static Point getDefaultThumbnailSize(Resources res) { + return new Point(res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth), + res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight)); + + } + + private static Bitmap createThumbnail(Point size, Context context, Uri uri, byte[] imageBytes, + Resources res, int resId, boolean leftAligned) { + int width = size.x; + int height = size.y; BitmapCropTask cropTask; if (uri != null) { - cropTask = new BitmapCropTask(uri, null, width, height, false, true, null); - } else { + cropTask = new BitmapCropTask(context, uri, null, width, height, false, true, null); + } else if (imageBytes != null) { cropTask = new BitmapCropTask(imageBytes, null, width, height, false, true, null); + } else { + cropTask = + new BitmapCropTask(context, res, resId, null, width, height, false, true, null); } Point bounds = cropTask.getImageBounds(); if (bounds == null) { @@ -515,7 +486,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { // Load the thumbnail ImageView image = (ImageView) pickedImageThumbnail.findViewById(R.id.wallpaper_image); - Bitmap thumb = createThumbnail(uri, null, false); + Point defaultSize = getDefaultThumbnailSize(this.getResources()); + Bitmap thumb = createThumbnail(defaultSize, this, uri, null, null, 0, false); if (thumb != null) { image.setImageBitmap(thumb); Drawable thumbDrawable = image.getDrawable(); @@ -525,10 +497,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } mWallpapersView.addView(pickedImageThumbnail, 0); - ThumbnailMetaData meta = new ThumbnailMetaData(); - meta.mTileType = TileType.WALLPAPER_URI; - meta.mWallpaperUri = uri; - pickedImageThumbnail.setTag(meta); + UriWallpaperInfo info = new UriWallpaperInfo(uri); + pickedImageThumbnail.setTag(info); pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener); mThumbnailOnClickListener.onClick(pickedImageThumbnail); } @@ -564,18 +534,24 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { v.setOnLongClickListener(mLongClickListener); } - private void findBundledWallpapers() { - mBundledWallpaperThumbs = new ArrayList(24); - mBundledWallpaperResIds = new ArrayList(24); + private ArrayList findBundledWallpapers() { + ArrayList bundledWallpapers = + new ArrayList(24); Pair r = getWallpaperArrayResourceId(); if (r != null) { try { - mWallpaperResources = getPackageManager().getResourcesForApplication(r.first); - addWallpapers(mWallpaperResources, r.first.packageName, r.second); + Resources wallpaperRes = getPackageManager().getResourcesForApplication(r.first); + bundledWallpapers = addWallpapers(wallpaperRes, r.first.packageName, r.second); } catch (PackageManager.NameNotFoundException e) { } } + //TODO: add default wallpaper + //Resources sysRes = Resources.getSystem(); + //int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android"); + //bundledWallpapers.add( + // new ResourceWallpaperInfo(sysRes, resId, new ColorDrawable(0xFFFF0000))); + return bundledWallpapers; } public Pair getWallpaperArrayResourceId() { @@ -592,23 +568,39 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - private void addWallpapers(Resources resources, String packageName, int listResId) { - final String[] extras = resources.getStringArray(listResId); + private ArrayList addWallpapers( + Resources res, String packageName, int listResId) { + ArrayList bundledWallpapers = + new ArrayList(24); + final String[] extras = res.getStringArray(listResId); for (String extra : extras) { - int res = resources.getIdentifier(extra, "drawable", packageName); - if (res != 0) { - final int thumbRes = resources.getIdentifier(extra + "_small", - "drawable", packageName); + int resId = res.getIdentifier(extra, "drawable", packageName); + if (resId != 0) { + final int thumbRes = res.getIdentifier(extra + "_small", "drawable", packageName); if (thumbRes != 0) { - mBundledWallpaperThumbs.add(resources.getDrawable(thumbRes)); - mBundledWallpaperResIds.add(res); + ResourceWallpaperInfo wallpaperInfo = + new ResourceWallpaperInfo(res, resId, res.getDrawable(thumbRes)); + bundledWallpapers.add(wallpaperInfo); // Log.d(TAG, "add: [" + packageName + "]: " + extra + " (" + res + ")"); } } else { Log.e(TAG, "Couldn't find wallpaper " + extra); } } + return bundledWallpapers; + } + + public CropView getCropView() { + return mCropView; + } + + public SavedWallpaperImages getSavedImages() { + return mSavedImages; + } + + public void onLiveWallpaperPickerLaunch() { + mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo(); } static class ZeroPaddingDrawable extends LevelListDrawable { @@ -625,21 +617,21 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } } - private static class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter { + private static class BuiltInWallpapersAdapter extends BaseAdapter implements ListAdapter { private LayoutInflater mLayoutInflater; - private ArrayList mThumbs; + private ArrayList mWallpapers; - ImageAdapter(Activity activity, ArrayList thumbs) { + BuiltInWallpapersAdapter(Activity activity, ArrayList wallpapers) { mLayoutInflater = activity.getLayoutInflater(); - mThumbs = thumbs; + mWallpapers = wallpapers; } public int getCount() { - return mThumbs.size(); + return mWallpapers.size(); } - public Object getItem(int position) { - return position; + public ResourceWallpaperInfo getItem(int position) { + return mWallpapers.get(position); } public long getItemId(int position) { @@ -647,27 +639,33 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } public View getView(int position, View convertView, ViewGroup parent) { - View view; - - if (convertView == null) { - view = mLayoutInflater.inflate(R.layout.wallpaper_picker_item, parent, false); - } else { - view = convertView; + Drawable thumb = mWallpapers.get(position).mThumb; + if (thumb == null) { + Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position); } + return createImageTileView(mLayoutInflater, position, convertView, parent, thumb); + } + } - setWallpaperItemPaddingToZero((FrameLayout) view); + public static View createImageTileView(LayoutInflater layoutInflater, int position, + View convertView, ViewGroup parent, Drawable thumb) { + View view; - ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image); + if (convertView == null) { + view = layoutInflater.inflate(R.layout.wallpaper_picker_item, parent, false); + } else { + view = convertView; + } - Drawable thumbDrawable = mThumbs.get(position); - if (thumbDrawable != null) { - image.setImageDrawable(thumbDrawable); - thumbDrawable.setDither(true); - } else { - Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position); - } + setWallpaperItemPaddingToZero((FrameLayout) view); - return view; + ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image); + + if (thumb != null) { + image.setImageDrawable(thumb); + thumb.setDither(true); } + + return view; } } -- cgit v1.2.3