From 2e004d073483d1c11e459d21571bf9594d18b194 Mon Sep 17 00:00:00 2001 From: d34d Date: Fri, 17 Jul 2015 15:56:41 -0700 Subject: Avoid OOME when generating many wallpaper previews A pack containing a large collection of wallpapers could cause the preview generation to fail with an OOME since we were storing all the previews in memory before writing them to disk and adding their entries into the database. Wallpaper previews are now stored on disk within the wallpaper preview generator and the path is returned in the WallpaperItem object instead of the bitmaps. Change-Id: I1dca215bc2fab9266c0baa4bce2a39e2ff1238a6 --- .../themes/provider/PreviewGenerationService.java | 147 +++++---------------- .../themes/provider/ThemesProvider.java | 3 +- .../themes/provider/util/BitmapUtils.java | 17 ++- .../themes/provider/util/PreviewUtils.java | 89 +++++++++++++ .../provider/util/WallpaperPreviewGenerator.java | 52 ++++++-- 5 files changed, 178 insertions(+), 130 deletions(-) create mode 100644 src/org/cyanogenmod/themes/provider/util/PreviewUtils.java diff --git a/src/org/cyanogenmod/themes/provider/PreviewGenerationService.java b/src/org/cyanogenmod/themes/provider/PreviewGenerationService.java index bf838d9..3414144 100644 --- a/src/org/cyanogenmod/themes/provider/PreviewGenerationService.java +++ b/src/org/cyanogenmod/themes/provider/PreviewGenerationService.java @@ -25,16 +25,15 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.ThemeConfig; import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.Bitmap.CompressFormat; import android.os.Bundle; import android.os.FileUtils; import android.provider.ThemesContract.ThemesColumns; import android.provider.ThemesContract.PreviewColumns; -import android.text.TextUtils; import android.util.Log; import org.cyanogenmod.themes.provider.util.BootAnimationPreviewGenerator; import org.cyanogenmod.themes.provider.util.IconPreviewGenerator; import org.cyanogenmod.themes.provider.util.IconPreviewGenerator.IconItems; +import org.cyanogenmod.themes.provider.util.PreviewUtils; import org.cyanogenmod.themes.provider.util.StylePreviewGenerator; import org.cyanogenmod.themes.provider.util.StylePreviewGenerator.StyleItems; import org.cyanogenmod.themes.provider.util.SystemUiPreviewGenerator; @@ -43,9 +42,7 @@ import org.cyanogenmod.themes.provider.util.WallpaperPreviewGenerator; import org.cyanogenmod.themes.provider.util.WallpaperPreviewGenerator.WallpaperItem; import org.cyanogenmod.themes.provider.util.WallpaperPreviewGenerator.WallpaperItems; -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; @@ -57,8 +54,6 @@ public class PreviewGenerationService extends IntentService { public static final String ACTION_UPDATE = "org.cyanogenmod.themes.provider.action.update"; public static final String EXTRA_PKG_NAME = "extra_pkg_name"; - public static final String PREVIEWS_DIR = "previews"; - private static final String TAG = PreviewGenerationService.class.getName(); public PreviewGenerationService() { @@ -107,6 +102,11 @@ public class PreviewGenerationService extends IntentService { Log.e(TAG, "Unable to get package info for " + pkgName, e); } if (isSystemTheme || info != null) { + String filesDir = this.getFilesDir().getAbsolutePath(); + String themePreviewsDir = + PreviewUtils.getPreviewsDir(filesDir) + File.separator + pkgName; + clearThemePreviewsDir(themePreviewsDir); + SystemUiItems items = null; try { items = !hasSystemUi ? null : @@ -176,49 +176,46 @@ public class PreviewGenerationService extends IntentService { List themeValues = new ArrayList(); ContentValues values = null; String filesDir = this.getFilesDir().getAbsolutePath(); - String themePreviewsDir = - filesDir + File.separator + PREVIEWS_DIR + File.separator + pkgName; String path = null; - clearThemePreviewsDir(themePreviewsDir); clearThemeFromPreviewDB(resolver, pkgName); if (items != null) { - path = compressAndSavePng(items.statusbarBackground, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.statusbarBackground, filesDir, pkgName, PreviewColumns.STATUSBAR_BACKGROUND); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BACKGROUND, path); themeValues.add(values); - path = compressAndSavePng(items.bluetoothIcon, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.bluetoothIcon, filesDir, pkgName, PreviewColumns.STATUSBAR_BLUETOOTH_ICON); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BLUETOOTH_ICON, path); themeValues.add(values); - path = compressAndSavePng(items.wifiIcon, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.wifiIcon, filesDir, pkgName, PreviewColumns.STATUSBAR_WIFI_ICON); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_WIFI_ICON, path); themeValues.add(values); - path = compressAndSavePng(items.signalIcon, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.signalIcon, filesDir, pkgName, PreviewColumns.STATUSBAR_SIGNAL_ICON); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_SIGNAL_ICON, path); themeValues.add(values); - path = compressAndSavePng(items.batteryPortrait, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.batteryPortrait, filesDir, pkgName, PreviewColumns.STATUSBAR_BATTERY_PORTRAIT); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BATTERY_PORTRAIT, path); themeValues.add(values); - path = compressAndSavePng(items.batteryLandscape, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.batteryLandscape, filesDir, pkgName, PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE, path); themeValues.add(values); - path = compressAndSavePng(items.batteryCircle, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.batteryCircle, filesDir, pkgName, PreviewColumns.STATUSBAR_BATTERY_CIRCLE); values = createPreviewEntryString(id, PreviewColumns.STATUSBAR_BATTERY_CIRCLE, path); @@ -232,46 +229,45 @@ public class PreviewGenerationService extends IntentService { PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END, items.wifiMarginEnd); themeValues.add(values); - path = compressAndSavePng(items.navbarBackground, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.navbarBackground, filesDir, pkgName, PreviewColumns.NAVBAR_BACKGROUND); values = createPreviewEntryString(id, PreviewColumns.NAVBAR_BACKGROUND, path); themeValues.add(values); - path = compressAndSavePng(items.navbarBack, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.navbarBack, filesDir, pkgName, PreviewColumns.NAVBAR_BACK_BUTTON); values = createPreviewEntryString(id, PreviewColumns.NAVBAR_BACK_BUTTON, path); themeValues.add(values); - path = compressAndSavePng(items.navbarHome, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.navbarHome, filesDir, pkgName, PreviewColumns.NAVBAR_HOME_BUTTON); values = createPreviewEntryString(id, PreviewColumns.NAVBAR_HOME_BUTTON, path); themeValues.add(values); - path = compressAndSavePng(items.navbarRecent, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(items.navbarRecent, filesDir, pkgName, PreviewColumns.NAVBAR_RECENT_BUTTON); values = createPreviewEntryString(id, PreviewColumns.NAVBAR_RECENT_BUTTON, path); themeValues.add(values); } if (icons != null) { - path = compressAndSavePng(icons.icon1, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(icons.icon1, filesDir, pkgName, PreviewColumns.ICON_PREVIEW_1); values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_1, path); themeValues.add(values); - path = compressAndSavePng(icons.icon2, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(icons.icon2, filesDir, pkgName, PreviewColumns.ICON_PREVIEW_2); values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_2, path); themeValues.add(values); - path = compressAndSavePng(icons.icon3, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(icons.icon3, filesDir, pkgName, PreviewColumns.ICON_PREVIEW_3); values = createPreviewEntryString(id, PreviewColumns.ICON_PREVIEW_3, path); themeValues.add(values); } if (wallpaperItems != null) { for (int i = 0; i < wallpaperItems.wallpapers.size(); i++) { - String componentID = String.format("%03d", i); WallpaperItem wallpaperItem = wallpaperItems.wallpapers.get(i); if (wallpaperItem == null) continue; @@ -282,54 +278,48 @@ public class PreviewGenerationService extends IntentService { themeValues.add(values); } - if (wallpaperItem.preview != null) { - path = compressAndSaveJpg(wallpaperItem.preview, filesDir, pkgName, - PreviewColumns.WALLPAPER_PREVIEW + componentID); + if (wallpaperItem.previewPath != null) { values = createPreviewEntryString(id, i, - PreviewColumns.WALLPAPER_PREVIEW, path); + PreviewColumns.WALLPAPER_PREVIEW, wallpaperItem.previewPath); themeValues.add(values); } - if (wallpaperItem.thumbnail != null) { - path = compressAndSavePng(wallpaperItem.thumbnail, filesDir, pkgName, - PreviewColumns.WALLPAPER_THUMBNAIL + componentID); + if (wallpaperItem.thumbnailPath != null) { values = createPreviewEntryString(id, i, - PreviewColumns.WALLPAPER_THUMBNAIL, path); + PreviewColumns.WALLPAPER_THUMBNAIL, wallpaperItem.thumbnailPath); themeValues.add(values); } } if (wallpaperItems.lockscreen != null) { - if (wallpaperItems.lockscreen.preview != null) { - path = compressAndSaveJpg(wallpaperItems.lockscreen.preview, filesDir, - pkgName, PreviewColumns.LOCK_WALLPAPER_PREVIEW); + if (wallpaperItems.lockscreen.previewPath != null) { values = createPreviewEntryString(id, - PreviewColumns.LOCK_WALLPAPER_PREVIEW, path); + PreviewColumns.LOCK_WALLPAPER_PREVIEW, + wallpaperItems.lockscreen.previewPath); themeValues.add(values); } - if (wallpaperItems.lockscreen.thumbnail != null) { - path = compressAndSavePng(wallpaperItems.lockscreen.thumbnail, filesDir, - pkgName, PreviewColumns.LOCK_WALLPAPER_THUMBNAIL); + if (wallpaperItems.lockscreen.thumbnailPath != null) { values = createPreviewEntryString(id, - PreviewColumns.LOCK_WALLPAPER_THUMBNAIL, path); + PreviewColumns.LOCK_WALLPAPER_THUMBNAIL, + wallpaperItems.lockscreen.thumbnailPath); themeValues.add(values); } } } if (styleItems != null) { - path = compressAndSavePng(styleItems.thumbnail, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(styleItems.thumbnail, filesDir, pkgName, PreviewColumns.STYLE_THUMBNAIL); values = createPreviewEntryString(id, PreviewColumns.STYLE_THUMBNAIL, path); themeValues.add(values); - path = compressAndSavePng(styleItems.preview, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(styleItems.preview, filesDir, pkgName, PreviewColumns.STYLE_PREVIEW); values = createPreviewEntryString(id, PreviewColumns.STYLE_PREVIEW, path); themeValues.add(values); } if (bootAnim != null) { - path = compressAndSavePng(bootAnim, filesDir, pkgName, + path = PreviewUtils.compressAndSavePng(bootAnim, filesDir, pkgName, PreviewColumns.BOOTANIMATION_THUMBNAIL); values = createPreviewEntryString(id, PreviewColumns.BOOTANIMATION_THUMBNAIL, path); @@ -383,62 +373,6 @@ public class PreviewGenerationService extends IntentService { return values; } - private static String compressAndSavePng(Bitmap bmp, String baseDir, String pkgName, - String fileName) { - byte[] image = getBitmapBlobPng(bmp); - return saveCompressedImage(image, baseDir, pkgName, fileName); - } - - private static String compressAndSaveJpg(Bitmap bmp, String baseDir, String pkgName, - String fileName) { - byte[] image = getBitmapBlobJpg(bmp); - return saveCompressedImage(image, baseDir, pkgName, fileName); - } - - private static byte[] getBitmapBlobPng(Bitmap bmp) { - return getBitmapBlob(bmp, CompressFormat.PNG, 100); - } - - private static byte[] getBitmapBlobJpg(Bitmap bmp) { - return getBitmapBlob(bmp, CompressFormat.JPEG, 80); - } - - private static byte[] getBitmapBlob(Bitmap bmp, CompressFormat format, int quality) { - if (bmp == null) return null; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - bmp.compress(format, quality, out); - return out.toByteArray(); - } - - private static String saveCompressedImage(byte[] image, String baseDir, String pkgName, - String fileName) { - if (image == null) return null; - // Create relevant directories - String previewsDir = baseDir + File.separator + PREVIEWS_DIR; - String pkgDir = previewsDir + File.separator + pkgName; - String filePath = pkgDir + File.separator + fileName; - createDirIfNotExists(previewsDir); - createDirIfNotExists(pkgDir); - - // Save blob - FileOutputStream outputStream; - final File pkgPreviewDir = new File(pkgDir); - try { - File outFile = new File(pkgPreviewDir, fileName); - outputStream = new FileOutputStream(outFile); - outputStream.write(image); - outputStream.close(); - FileUtils.setPermissions(outFile, - FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH, - -1, -1); - } catch (Exception e) { - Log.w(TAG, "Unable to save preview " + pkgName + File.separator + fileName, e); - filePath = null; - } - - return filePath; - } - public static void clearThemePreviewsDir(String path) { File directory = new File(path); FileUtils.deleteContents(directory); @@ -457,19 +391,4 @@ public class PreviewGenerationService extends IntentService { return context.getContentResolver().query(ThemesColumns.CONTENT_URI, null, selection, selectionArgs, null); } - - private static boolean dirExists(String dirPath) { - final File dir = new File(dirPath); - return dir.exists() && dir.isDirectory(); - } - - private static void createDirIfNotExists(String dirPath) { - if (!dirExists(dirPath)) { - File dir = new File(dirPath); - if (dir.mkdir()) { - FileUtils.setPermissions(dir, FileUtils.S_IRWXU | - FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1); - } - } - } } diff --git a/src/org/cyanogenmod/themes/provider/ThemesProvider.java b/src/org/cyanogenmod/themes/provider/ThemesProvider.java index 418faf0..edeb802 100644 --- a/src/org/cyanogenmod/themes/provider/ThemesProvider.java +++ b/src/org/cyanogenmod/themes/provider/ThemesProvider.java @@ -45,6 +45,7 @@ import android.util.Log; import org.cyanogenmod.themes.provider.ThemesOpenHelper.MixnMatchTable; import org.cyanogenmod.themes.provider.ThemesOpenHelper.PreviewsTable; import org.cyanogenmod.themes.provider.ThemesOpenHelper.ThemesTable; +import org.cyanogenmod.themes.provider.util.PreviewUtils; import org.cyanogenmod.themes.provider.util.ProviderUtils; import java.io.File; @@ -128,7 +129,7 @@ public class ThemesProvider extends ContentProvider { String pkgName = c.getString(idx); String filesDir = getContext().getFilesDir().getAbsolutePath(); String themePreviewsDir = filesDir + File.separator + - PreviewGenerationService.PREVIEWS_DIR + File.separator + pkgName; + PreviewUtils.PREVIEWS_DIR + File.separator + pkgName; PreviewGenerationService.clearThemePreviewsDir(themePreviewsDir); } c.close(); diff --git a/src/org/cyanogenmod/themes/provider/util/BitmapUtils.java b/src/org/cyanogenmod/themes/provider/util/BitmapUtils.java index 002970b..de1bf98 100644 --- a/src/org/cyanogenmod/themes/provider/util/BitmapUtils.java +++ b/src/org/cyanogenmod/themes/provider/util/BitmapUtils.java @@ -22,11 +22,12 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; import android.graphics.Rect; -import android.net.Uri; import android.util.Log; import android.util.TypedValue; +import java.io.ByteArrayOutputStream; import java.io.Closeable; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -158,6 +159,20 @@ public class BitmapUtils { return bitmap; } + public static byte[] getBitmapBlobPng(Bitmap bmp) { + return getBitmapBlob(bmp, Bitmap.CompressFormat.PNG, 100); + } + + public static byte[] getBitmapBlobJpg(Bitmap bmp) { + return getBitmapBlob(bmp, Bitmap.CompressFormat.JPEG, 80); + } + + public static byte[] getBitmapBlob(Bitmap bmp, Bitmap.CompressFormat format, int quality) { + if (bmp == null) return null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + bmp.compress(format, quality, out); + return out.toByteArray(); + } /** * For excessively large images with an awkward ratio we diff --git a/src/org/cyanogenmod/themes/provider/util/PreviewUtils.java b/src/org/cyanogenmod/themes/provider/util/PreviewUtils.java new file mode 100644 index 0000000..de86092 --- /dev/null +++ b/src/org/cyanogenmod/themes/provider/util/PreviewUtils.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.cyanogenmod.themes.provider.util; + +import android.graphics.Bitmap; +import android.os.FileUtils; +import android.util.Log; + +import java.io.File; +import java.io.FileOutputStream; + +public class PreviewUtils { + private static final String TAG = PreviewUtils.class.getSimpleName(); + + public static final String PREVIEWS_DIR = "previews"; + + public static boolean dirExists(String dirPath) { + final File dir = new File(dirPath); + return dir.exists() && dir.isDirectory(); + } + + public static void createDirIfNotExists(String dirPath) { + if (!dirExists(dirPath)) { + File dir = new File(dirPath); + if (dir.mkdir()) { + FileUtils.setPermissions(dir, FileUtils.S_IRWXU | + FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, -1, -1); + } + } + } + + public static String getPreviewsDir(String baseDir) { + return baseDir + File.separator + PREVIEWS_DIR; + } + + private static String saveCompressedImage(byte[] image, String baseDir, String pkgName, + String fileName) { + if (image == null) return null; + // Create relevant directories + String previewsDir = PreviewUtils.getPreviewsDir(baseDir); + String pkgDir = previewsDir + File.separator + pkgName; + String filePath = pkgDir + File.separator + fileName; + PreviewUtils.createDirIfNotExists(previewsDir); + PreviewUtils.createDirIfNotExists(pkgDir); + + // Save blob + FileOutputStream outputStream; + final File pkgPreviewDir = new File(pkgDir); + try { + File outFile = new File(pkgPreviewDir, fileName); + outputStream = new FileOutputStream(outFile); + outputStream.write(image); + outputStream.close(); + FileUtils.setPermissions(outFile, + FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH, + -1, -1); + } catch (Exception e) { + Log.w(TAG, "Unable to save preview " + pkgName + File.separator + fileName, e); + filePath = null; + } + + return filePath; + } + + public static String compressAndSavePng(Bitmap bmp, String baseDir, String pkgName, + String fileName) { + byte[] image = BitmapUtils.getBitmapBlobPng(bmp); + return saveCompressedImage(image, baseDir, pkgName, fileName); + } + + public static String compressAndSaveJpg(Bitmap bmp, String baseDir, String pkgName, + String fileName) { + byte[] image = BitmapUtils.getBitmapBlobJpg(bmp); + return saveCompressedImage(image, baseDir, pkgName, fileName); + } +} diff --git a/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java b/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java index f10a2f7..894d710 100644 --- a/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java +++ b/src/org/cyanogenmod/themes/provider/util/WallpaperPreviewGenerator.java @@ -24,6 +24,7 @@ import android.content.res.Resources; import android.content.res.ThemeConfig; import android.graphics.Bitmap; +import android.provider.ThemesContract; import android.text.TextUtils; import org.cyanogenmod.themes.provider.R; @@ -51,14 +52,18 @@ public class WallpaperPreviewGenerator { WallpaperItems items = new WallpaperItems(); WallpaperItem item = null; Bitmap preview = null; + String baseDir = mContext.getFilesDir().getAbsolutePath(); + String pkgName; if (themeInfo == null) { + pkgName = ThemeConfig.SYSTEM_DEFAULT; Resources res = mContext.getPackageManager().getThemedResourcesForApplication("android", ThemeConfig.SYSTEM_DEFAULT); - item = new WallpaperItem(); - item.preview = BitmapUtils.decodeResource(res, + if (preview != null) { + preview.recycle(); + } + preview = BitmapUtils.decodeResource(res, com.android.internal.R.drawable.default_wallpaper, mPreviewSize, mPreviewSize); - item.thumbnail = Bitmap.createScaledBitmap(item.preview, mThumbnailSize, mThumbnailSize, - true); + item = createWallpaperItems(0, baseDir, null, pkgName, preview); if (item != null) { items.wallpapers.add(item); items.lockscreen = item; @@ -66,45 +71,64 @@ public class WallpaperPreviewGenerator { } else { final Context themeContext = mContext.createPackageContext(themeInfo.packageName, 0); final AssetManager assets = themeContext.getAssets(); + pkgName = themeInfo.packageName; // Get all wallpapers List paths = ThemeUtils.getWallpaperPathList(assets); + int id = 0; for (String path : paths) { if (!TextUtils.isEmpty(path)) { + if (preview != null) { + preview.recycle(); + } preview = BitmapUtils.getBitmapFromAsset(themeContext, path, mPreviewSize, mPreviewSize); - item = createWallpaperItems(path, preview); + item = createWallpaperItems(id, baseDir, path, pkgName, preview); if (item != null) { items.wallpapers.add(item); + id++; } } } // Get the lockscreen String path = ThemeUtils.getLockscreenWallpaperPath(assets); if (!TextUtils.isEmpty(path)) { + if (preview != null) { + preview.recycle(); + } preview = BitmapUtils.getBitmapFromAsset(themeContext, path, mPreviewSize, mPreviewSize); - items.lockscreen = createWallpaperItems(path, preview); + items.lockscreen = createWallpaperItems(0, baseDir, path, pkgName, preview); } } return items; } - private WallpaperItem createWallpaperItems(String path, Bitmap preview) { - if (TextUtils.isEmpty(path) || preview == null) { + private WallpaperItem createWallpaperItems(int id, String baseDir, String assetPath, + String pkgName, Bitmap preview) { + if (TextUtils.isEmpty(assetPath) && preview == null) { return null; } + + String componentID = String.format("%03d", id); WallpaperItem item = new WallpaperItem(); - item.assetPath = path; - item.preview = preview; - item.thumbnail = Bitmap.createScaledBitmap(item.preview, mThumbnailSize, mThumbnailSize, - true); + + item.assetPath = assetPath; + if (preview != null) { + String fileName = ThemesContract.PreviewColumns.WALLPAPER_PREVIEW + componentID; + item.previewPath = PreviewUtils.compressAndSaveJpg(preview, baseDir, pkgName, fileName); + Bitmap thumbnail = Bitmap.createScaledBitmap(preview, mThumbnailSize, mThumbnailSize, + true); + fileName = ThemesContract.PreviewColumns.WALLPAPER_THUMBNAIL + componentID; + item.thumbnailPath = PreviewUtils.compressAndSavePng(thumbnail, baseDir, pkgName, + fileName); + } return item; } public class WallpaperItem { public String assetPath; - public Bitmap preview; - public Bitmap thumbnail; + public String previewPath; + public String thumbnailPath; } public class WallpaperItems { -- cgit v1.2.3