1f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka/* 2f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Copyright (C) 2013 The Android Open Source Project 3f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * 4f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Licensed under the Apache License, Version 2.0 (the "License"); 5f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * you may not use this file except in compliance with the License. 6f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * You may obtain a copy of the License at 7f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * 8f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * http://www.apache.org/licenses/LICENSE-2.0 9f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * 10f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Unless required by applicable law or agreed to in writing, software 11f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * distributed under the License is distributed on an "AS IS" BASIS, 12f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * See the License for the specific language governing permissions and 14f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * limitations under the License. 15f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka */ 16f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 17748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyalpackage com.android.wallpaperpicker; 18f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 19f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.content.ContentValues; 20f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.content.Context; 21f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.database.Cursor; 22f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.database.sqlite.SQLiteDatabase; 23f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.database.sqlite.SQLiteOpenHelper; 24f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.Bitmap; 25f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.BitmapFactory; 26f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.drawable.BitmapDrawable; 27f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.drawable.Drawable; 28f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.util.Log; 294428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal 30748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyalimport com.android.wallpaperpicker.tileinfo.FileWallpaperInfo; 31f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 32f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport java.io.File; 33f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport java.io.FileOutputStream; 34f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport java.io.IOException; 35f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport java.util.ArrayList; 364428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyalimport java.util.List; 37f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 384428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyalpublic class SavedWallpaperImages { 39f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 40748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyal private static String TAG = "SavedWallpaperImages"; 41f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 424428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal public static class SavedWallpaperInfo extends FileWallpaperInfo { 434428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal 44f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka private int mDbId; 454428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal 464428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal public SavedWallpaperInfo(int dbId, File target, Drawable thumb) { 47723f3b60d150ebda26849bc57493bfc7afc43cc7Sunny Goyal super(target, thumb); 48f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka mDbId = dbId; 49f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 50723f3b60d150ebda26849bc57493bfc7afc43cc7Sunny Goyal 51f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka @Override 52f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka public void onDelete(WallpaperPickerActivity a) { 53f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka a.getSavedImages().deleteImage(mDbId); 54f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 55f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 56f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 574428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal private final ImageDb mDb; 584428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal private final Context mContext; 594428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal 607c34dd41f64c30be2348979b2d6847257498d541Sunny Goyal public SavedWallpaperImages(Context context) { 61c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka // We used to store the saved images in the cache directory, but that meant they'd get 62c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka // deleted sometimes-- move them to the data directory 63c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka ImageDb.moveFromCacheDirectoryIfNecessary(context); 64f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka mDb = new ImageDb(context); 65f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka mContext = context; 66f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 67f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 684428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal public List<SavedWallpaperInfo> loadThumbnailsAndImageIdList() { 694428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal List<SavedWallpaperInfo> result = new ArrayList<SavedWallpaperInfo>(); 704428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal 71f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka SQLiteDatabase db = mDb.getReadableDatabase(); 724428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal Cursor c = db.query(ImageDb.TABLE_NAME, 73f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka new String[] { ImageDb.COLUMN_ID, 74723f3b60d150ebda26849bc57493bfc7afc43cc7Sunny Goyal ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, 75723f3b60d150ebda26849bc57493bfc7afc43cc7Sunny Goyal ImageDb.COLUMN_IMAGE_FILENAME}, // cols to return 76f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, // select query 77f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, // args to select query 78f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, 79f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, 80f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ImageDb.COLUMN_ID + " DESC", 81f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null); 82f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 834428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal while (c.moveToNext()) { 844428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal String filename = c.getString(1); 85f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka File file = new File(mContext.getFilesDir(), filename); 86f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 87f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath()); 88f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka if (thumb != null) { 894428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal result.add(new SavedWallpaperInfo(c.getInt(0), 904428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal new File(mContext.getFilesDir(), c.getString(2)), 914428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal new BitmapDrawable(mContext.getResources(), thumb))); 92f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 93f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 944428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal c.close(); 954428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal return result; 96f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 97f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 984428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal public void deleteImage(int id) { 994428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal SQLiteDatabase db = mDb.getWritableDatabase(); 100f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 101f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka Cursor result = db.query(ImageDb.TABLE_NAME, 102f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka new String[] { ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, 103f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ImageDb.COLUMN_IMAGE_FILENAME }, // cols to return 104f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ImageDb.COLUMN_ID + " = ?", // select query 105f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka new String[] { Integer.toString(id) }, // args to select query 106f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, 107f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, 108f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null, 109f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka null); 1104428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal if (result.moveToFirst()) { 1114428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal new File(mContext.getFilesDir(), result.getString(0)).delete(); 1124428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal new File(mContext.getFilesDir(), result.getString(1)).delete(); 113f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 1144428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal result.close(); 115f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 116f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka db.delete(ImageDb.TABLE_NAME, 117f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ImageDb.COLUMN_ID + " = ?", // SELECT query 118f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka new String[] { 119f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka Integer.toString(id) // args to SELECT query 120f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka }); 121f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 122f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 123f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka public void writeImage(Bitmap thumbnail, byte[] imageBytes) { 124f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka try { 125f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka File imageFile = File.createTempFile("wallpaper", "", mContext.getFilesDir()); 126f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka FileOutputStream imageFileStream = 127f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka mContext.openFileOutput(imageFile.getName(), Context.MODE_PRIVATE); 128f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka imageFileStream.write(imageBytes); 129f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka imageFileStream.close(); 130f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 131f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka File thumbFile = File.createTempFile("wallpaperthumb", "", mContext.getFilesDir()); 132f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka FileOutputStream thumbFileStream = 133f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka mContext.openFileOutput(thumbFile.getName(), Context.MODE_PRIVATE); 134f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream); 135f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka thumbFileStream.close(); 136f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 137f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka SQLiteDatabase db = mDb.getWritableDatabase(); 138f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ContentValues values = new ContentValues(); 139f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka values.put(ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, thumbFile.getName()); 140f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka values.put(ImageDb.COLUMN_IMAGE_FILENAME, imageFile.getName()); 141f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka db.insert(ImageDb.TABLE_NAME, null, values); 142f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } catch (IOException e) { 143f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka Log.e(TAG, "Failed writing images to storage " + e); 144f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 145f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 146f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 1474428db78eed5d453c3bb376e6e28977c21b1c9ddSunny Goyal private static class ImageDb extends SQLiteOpenHelper { 148f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka final static int DB_VERSION = 1; 149f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka final static String TABLE_NAME = "saved_wallpaper_images"; 150f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka final static String COLUMN_ID = "id"; 151f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka final static String COLUMN_IMAGE_THUMBNAIL_FILENAME = "image_thumbnail"; 152f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka final static String COLUMN_IMAGE_FILENAME = "image"; 153f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 154f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka public ImageDb(Context context) { 155748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyal super(context, context.getDatabasePath(WallpaperFiles.WALLPAPER_IMAGES_DB).getPath(), 1564014015c33c07d1b15fae8e0a4485761e6401ac0Helena Josol null, DB_VERSION); 157f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 158f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 159c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka public static void moveFromCacheDirectoryIfNecessary(Context context) { 160c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka // We used to store the saved images in the cache directory, but that meant they'd get 161c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka // deleted sometimes-- move them to the data directory 1624014015c33c07d1b15fae8e0a4485761e6401ac0Helena Josol File oldSavedImagesFile = new File(context.getCacheDir(), 163748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyal WallpaperFiles.WALLPAPER_IMAGES_DB); 164748dfe025b128ea88ba626c8bfb7da315d5af29cSunny Goyal File savedImagesFile = context.getDatabasePath(WallpaperFiles.WALLPAPER_IMAGES_DB); 165c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka if (oldSavedImagesFile.exists()) { 166c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka oldSavedImagesFile.renameTo(savedImagesFile); 167c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka } 168c88493d52ee3033b20676f9648323c8d212268a5Michael Jurka } 169f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka @Override 170f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka public void onCreate(SQLiteDatabase database) { 171f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + 172f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka COLUMN_ID + " INTEGER NOT NULL, " + 173f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka COLUMN_IMAGE_THUMBNAIL_FILENAME + " TEXT NOT NULL, " + 174f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka COLUMN_IMAGE_FILENAME + " TEXT NOT NULL, " + 175f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka "PRIMARY KEY (" + COLUMN_ID + " ASC) " + 176f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka ");"); 177f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 178f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka 179f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka @Override 180f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 181f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka if (oldVersion != newVersion) { 182f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka // Delete all the records; they'll be repopulated as this is a cache 183f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka db.execSQL("DELETE FROM " + TABLE_NAME); 184f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 185f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 186f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka } 187f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka} 188