From 12ca6134bf818c7c28062aefa40281449042889a Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Tue, 15 Oct 2013 11:14:44 -0700 Subject: Use new default wallpaper API Change-Id: I77ce7f03f0d2c94d890d3eeee6750488b071b2ab --- res/layout/wallpaper_picker.xml | 5 + .../android/launcher3/SavedWallpaperImages.java | 1 + .../android/launcher3/WallpaperPickerActivity.java | 164 +++++++++++++++++---- 3 files changed, 141 insertions(+), 29 deletions(-) diff --git a/res/layout/wallpaper_picker.xml b/res/layout/wallpaper_picker.xml index c91cc7e6a..0492b7bd9 100644 --- a/res/layout/wallpaper_picker.xml +++ b/res/layout/wallpaper_picker.xml @@ -27,6 +27,11 @@ android:id="@+id/cropView" android:layout_width="match_parent" android:layout_height="match_parent" /> + wallpapers = findBundledWallpapers(); mWallpapersView = (LinearLayout) findViewById(R.id.wallpaper_list); BuiltInWallpapersAdapter ia = new BuiltInWallpapersAdapter(this, wallpapers); - populateWallpapersFromAdapter(mWallpapersView, ia, false, true); + populateWallpapersFromAdapter(mWallpapersView, ia, false); // Populate the saved wallpapers mSavedImages = new SavedWallpaperImages(this); mSavedImages.loadThumbnailsAndImageIdList(); - populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true, true); + populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true); // Populate the live wallpapers final LinearLayout liveWallpapersView = @@ -319,7 +360,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { a.registerDataSetObserver(new DataSetObserver() { public void onChanged() { liveWallpapersView.removeAllViews(); - populateWallpapersFromAdapter(liveWallpapersView, a, false, false); + populateWallpapersFromAdapter(liveWallpapersView, a, false); initializeScrollForRtl(); updateTileIndices(); } @@ -330,7 +371,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { (LinearLayout) findViewById(R.id.third_party_wallpaper_list); final ThirdPartyWallpaperPickerListAdapter ta = new ThirdPartyWallpaperPickerListAdapter(this); - populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false, false); + populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false); // Add a tile for the Gallery LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list); @@ -354,7 +395,33 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { pickImageTile.setTag(pickImageInfo); pickImageInfo.setView(pickImageTile); pickImageTile.setOnClickListener(mThumbnailOnClickListener); - pickImageInfo.setView(pickImageTile); + + // Add a tile for the default wallpaper + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + DefaultWallpaperInfo defaultWallpaperInfo = getDefaultWallpaper(); + FrameLayout defaultWallpaperTile = (FrameLayout) createImageTileView( + getLayoutInflater(), 0, null, mWallpapersView, defaultWallpaperInfo.mThumb); + setWallpaperItemPaddingToZero(defaultWallpaperTile); + defaultWallpaperTile.setTag(defaultWallpaperInfo); + mWallpapersView.addView(defaultWallpaperTile, 0); + defaultWallpaperTile.setOnClickListener(mThumbnailOnClickListener); + defaultWallpaperInfo.setView(defaultWallpaperTile); + } + + // Select the first item; wait for a layout pass so that we initialize the dimensions of + // cropView or the defaultWallpaperView first + mDefaultWallpaperView.addOnLayoutChangeListener(new OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, + int oldLeft, int oldTop, int oldRight, int oldBottom) { + if ((right - left) > 0 && (bottom - top) > 0) { + if (mWallpapersView.getChildCount() > 0) { + mThumbnailOnClickListener.onClick(mWallpapersView.getChildAt(0)); + } + v.removeOnLayoutChangeListener(this); + } + } + }); updateTileIndices(); @@ -520,7 +587,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter, - boolean addLongPressHandler, boolean selectFirstTile) { + boolean addLongPressHandler) { for (int i = 0; i < adapter.getCount(); i++) { FrameLayout thumbnail = (FrameLayout) adapter.getView(i, null, parent); parent.addView(thumbnail, i); @@ -531,9 +598,6 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { addLongPressHandler(thumbnail); } thumbnail.setOnClickListener(mThumbnailOnClickListener); - if (i == 0 && selectFirstTile) { - mThumbnailOnClickListener.onClick(thumbnail); - } } } @@ -700,18 +764,35 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { } // Add an entry for the default wallpaper (stored in system resources) - ResourceWallpaperInfo defaultWallpaperInfo = getDefaultWallpaperInfo(); - if (defaultWallpaperInfo != null) { - bundledWallpapers.add(0, defaultWallpaperInfo); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + ResourceWallpaperInfo defaultWallpaperInfo = getPreKKDefaultWallpaperInfo(); + if (defaultWallpaperInfo != null) { + bundledWallpapers.add(0, defaultWallpaperInfo); + } } return bundledWallpapers; } - private ResourceWallpaperInfo getDefaultWallpaperInfo() { + private boolean writeImageToFileAsJpeg(File f, Bitmap b) { + try { + f.createNewFile(); + FileOutputStream thumbFileStream = + openFileOutput(f.getName(), Context.MODE_PRIVATE); + b.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream); + thumbFileStream.close(); + return true; + } catch (IOException e) { + Log.e(TAG, "Error while writing bitmap to file " + e); + f.delete(); + } + return false; + } + + private ResourceWallpaperInfo getPreKKDefaultWallpaperInfo() { Resources sysRes = Resources.getSystem(); int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android"); - File defaultThumbFile = new File(getFilesDir(), "default_thumb.jpg"); + File defaultThumbFile = new File(getFilesDir(), DEFAULT_WALLPAPER_THUMBNAIL_FILENAME); Bitmap thumb = null; boolean defaultWallpaperExists = false; if (defaultThumbFile.exists()) { @@ -724,17 +805,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { thumb = createThumbnail( defaultThumbSize, this, null, null, sysRes, resId, rotation, false); if (thumb != null) { - try { - defaultThumbFile.createNewFile(); - FileOutputStream thumbFileStream = - openFileOutput(defaultThumbFile.getName(), Context.MODE_PRIVATE); - thumb.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream); - thumbFileStream.close(); - defaultWallpaperExists = true; - } catch (IOException e) { - Log.e(TAG, "Error while writing default wallpaper thumbnail to file " + e); - defaultThumbFile.delete(); - } + defaultWallpaperExists = writeImageToFileAsJpeg(defaultThumbFile, thumb); } } if (defaultWallpaperExists) { @@ -743,6 +814,37 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { return null; } + private DefaultWallpaperInfo getDefaultWallpaper() { + File defaultThumbFile = new File(getFilesDir(), DEFAULT_WALLPAPER_THUMBNAIL_FILENAME); + Bitmap thumb = null; + boolean defaultWallpaperExists = false; + if (defaultThumbFile.exists()) { + thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath()); + defaultWallpaperExists = true; + } else { + Resources res = getResources(); + Point defaultThumbSize = getDefaultThumbnailSize(res); + Paint p = new Paint(); + p.setFilterBitmap(true); + Drawable wallpaperDrawable = WallpaperManager.getInstance(this).getBuiltInDrawable( + defaultThumbSize.x, defaultThumbSize.y, true, 0.5f, 0.5f); + if (wallpaperDrawable != null) { + thumb = Bitmap.createBitmap( + defaultThumbSize.x, defaultThumbSize.y, Bitmap.Config.ARGB_8888); + Canvas c = new Canvas(thumb); + wallpaperDrawable.draw(c); + c.setBitmap(null); + } + if (thumb != null) { + defaultWallpaperExists = writeImageToFileAsJpeg(defaultThumbFile, thumb); + } + } + if (defaultWallpaperExists) { + return new DefaultWallpaperInfo(new BitmapDrawable(thumb)); + } + return null; + } + public Pair getWallpaperArrayResourceId() { // Context.getPackageName() may return the "original" package name, // com.android.launcher3; Resources needs the real package name, @@ -784,6 +886,10 @@ public class WallpaperPickerActivity extends WallpaperCropActivity { return mCropView; } + public ImageView getDefaultWallpaperView() { + return mDefaultWallpaperView; + } + public SavedWallpaperImages getSavedImages() { return mSavedImages; } -- cgit v1.2.3