OSDN Git Service

Add default wallpaper to picker
authorMichael Jurka <mikejurka@google.com>
Mon, 23 Sep 2013 22:02:05 +0000 (23:02 +0100)
committerMichael Jurka <mikejurka@google.com>
Wed, 25 Sep 2013 13:50:28 +0000 (06:50 -0700)
Bug: 10852485

Change-Id: Idb70cd3bb0901f924da245016f5a8b96ec414470

src/com/android/launcher3/SavedWallpaperImages.java
src/com/android/launcher3/WallpaperPickerActivity.java

index f00f62f..cff2aee 100644 (file)
@@ -36,7 +36,6 @@ import android.widget.ListAdapter;
 
 import com.android.photos.BitmapRegionTileSource;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -100,6 +99,7 @@ public class SavedWallpaperImages extends BaseAdapter implements ListAdapter {
         while (result.moveToNext()) {
             String filename = result.getString(1);
             File file = new File(mContext.getFilesDir(), filename);
+
             Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath());
             if (thumb != null) {
                 mImages.add(new SavedWallpaperTile(result.getInt(0), new BitmapDrawable(thumb)));
@@ -181,12 +181,11 @@ public class SavedWallpaperImages extends BaseAdapter implements ListAdapter {
             imageFileStream.write(imageBytes);
             imageFileStream.close();
 
-            ByteArrayOutputStream stream = new ByteArrayOutputStream();
-            thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, stream);
             File thumbFile = File.createTempFile("wallpaperthumb", "", mContext.getFilesDir());
             FileOutputStream thumbFileStream =
                     mContext.openFileOutput(thumbFile.getName(), Context.MODE_PRIVATE);
-            thumbFileStream.write(stream.toByteArray());
+            thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream);
+            thumbFileStream.close();
 
             SQLiteDatabase db = mDb.getWritableDatabase();
             ContentValues values = new ContentValues();
index 5f35cde..d3c6c1a 100644 (file)
@@ -29,11 +29,12 @@ import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.DataSetObserver;
 import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 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.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LevelListDrawable;
 import android.net.Uri;
@@ -58,6 +59,8 @@ import android.widget.ListAdapter;
 
 import com.android.photos.BitmapRegionTileSource;
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 
@@ -462,7 +465,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
                     new BitmapCropTask(context, res, resId, null, width, height, false, true, null);
         }
         Point bounds = cropTask.getImageBounds();
-        if (bounds == null) {
+        if (bounds == null || bounds.x == 0 || bounds.y == 0) {
             return null;
         }
 
@@ -546,14 +549,48 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
             } 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)));
+
+        // Add an entry for the default wallpaper (stored in system resources)
+        ResourceWallpaperInfo defaultWallpaperInfo = getDefaultWallpaperInfo();
+        if (defaultWallpaperInfo != null) {
+            bundledWallpapers.add(0, defaultWallpaperInfo);
+        }
         return bundledWallpapers;
     }
 
+    private ResourceWallpaperInfo getDefaultWallpaperInfo() {
+        Resources sysRes = Resources.getSystem();
+        int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android");
+
+        File defaultThumbFile = new File(getFilesDir(), "default_thumb.jpg");
+        Bitmap thumb = null;
+        boolean defaultWallpaperExists = false;
+        if (defaultThumbFile.exists()) {
+            thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
+            defaultWallpaperExists = true;
+        } else {
+            Point defaultThumbSize = getDefaultThumbnailSize(getResources());
+            thumb = createThumbnail(defaultThumbSize, this, null, null, sysRes, resId, 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();
+                }
+            }
+        }
+        if (defaultWallpaperExists) {
+            return new ResourceWallpaperInfo(sysRes, resId, new BitmapDrawable(thumb));
+        }
+        return null;
+    }
+
     public Pair<ApplicationInfo, Integer> getWallpaperArrayResourceId() {
         // Context.getPackageName() may return the "original" package name,
         // com.android.launcher3; Resources needs the real package name,