Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.content.ContentValues; |
| 20 | import android.content.Context; |
| 21 | import android.database.Cursor; |
| 22 | import android.database.sqlite.SQLiteDatabase; |
| 23 | import android.database.sqlite.SQLiteOpenHelper; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.util.Log; |
| 29 | import android.util.Pair; |
| 30 | |
| 31 | import java.io.ByteArrayOutputStream; |
| 32 | import java.io.File; |
| 33 | import java.io.FileOutputStream; |
| 34 | import java.io.IOException; |
| 35 | import java.util.ArrayList; |
| 36 | |
| 37 | |
| 38 | public class SavedWallpaperImages { |
| 39 | private static String TAG = "Launcher3.SavedWallpaperImages"; |
| 40 | private ImageDb mDb; |
| 41 | ArrayList<Integer> mIds; |
| 42 | ArrayList<Drawable> mThumbs; |
| 43 | Context mContext; |
| 44 | |
| 45 | public SavedWallpaperImages(Context context) { |
| 46 | mDb = new ImageDb(context); |
| 47 | mContext = context; |
| 48 | } |
| 49 | |
| 50 | public void loadThumbnailsAndImageIdList() { |
| 51 | mIds = new ArrayList<Integer>(); |
| 52 | mThumbs = new ArrayList<Drawable>(); |
| 53 | SQLiteDatabase db = mDb.getReadableDatabase(); |
| 54 | Cursor result = db.query(ImageDb.TABLE_NAME, |
| 55 | new String[] { ImageDb.COLUMN_ID, |
| 56 | ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME }, // cols to return |
| 57 | null, // select query |
| 58 | null, // args to select query |
| 59 | null, |
| 60 | null, |
| 61 | null, |
| 62 | null); |
| 63 | |
| 64 | while (result.moveToNext()) { |
| 65 | String filename = result.getString(1); |
| 66 | File file = new File(mContext.getFilesDir(), filename); |
| 67 | Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath()); |
| 68 | if (thumb != null) { |
| 69 | mIds.add(result.getInt(0)); |
| 70 | mThumbs.add(new BitmapDrawable(thumb)); |
| 71 | } |
| 72 | } |
| 73 | result.close(); |
| 74 | } |
| 75 | |
| 76 | public ArrayList<Drawable> getThumbnails() { |
| 77 | return mThumbs; |
| 78 | } |
| 79 | |
| 80 | public ArrayList<Integer> getImageIds() { |
| 81 | return mIds; |
| 82 | } |
| 83 | |
| 84 | public String getImageFilename(int id) { |
| 85 | Pair<String, String> filenames = getImageFilenames(id); |
| 86 | if (filenames != null) { |
| 87 | return filenames.second; |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | private Pair<String, String> getImageFilenames(int id) { |
| 93 | SQLiteDatabase db = mDb.getReadableDatabase(); |
| 94 | Cursor result = db.query(ImageDb.TABLE_NAME, |
| 95 | new String[] { ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, |
| 96 | ImageDb.COLUMN_IMAGE_FILENAME }, // cols to return |
| 97 | ImageDb.COLUMN_ID + " = ?", // select query |
| 98 | new String[] { Integer.toString(id) }, // args to select query |
| 99 | null, |
| 100 | null, |
| 101 | null, |
| 102 | null); |
| 103 | if (result.getCount() > 0) { |
| 104 | result.moveToFirst(); |
| 105 | String thumbFilename = result.getString(0); |
| 106 | String imageFilename = result.getString(1); |
| 107 | result.close(); |
| 108 | return new Pair<String, String>(thumbFilename, imageFilename); |
| 109 | } else { |
| 110 | return null; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public void deleteImage(int id) { |
| 115 | Pair<String, String> filenames = getImageFilenames(id); |
| 116 | File imageFile = new File(mContext.getFilesDir(), filenames.first); |
| 117 | imageFile.delete(); |
| 118 | File thumbFile = new File(mContext.getFilesDir(), filenames.second); |
| 119 | thumbFile.delete(); |
| 120 | SQLiteDatabase db = mDb.getWritableDatabase(); |
| 121 | db.delete(ImageDb.TABLE_NAME, |
| 122 | ImageDb.COLUMN_ID + " = ?", // SELECT query |
| 123 | new String[] { |
| 124 | Integer.toString(id) // args to SELECT query |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | public void writeImage(Bitmap thumbnail, byte[] imageBytes) { |
| 129 | try { |
| 130 | File imageFile = File.createTempFile("wallpaper", "", mContext.getFilesDir()); |
| 131 | FileOutputStream imageFileStream = |
| 132 | mContext.openFileOutput(imageFile.getName(), Context.MODE_PRIVATE); |
| 133 | imageFileStream.write(imageBytes); |
| 134 | imageFileStream.close(); |
| 135 | |
| 136 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 137 | thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, stream); |
| 138 | File thumbFile = File.createTempFile("wallpaperthumb", "", mContext.getFilesDir()); |
| 139 | FileOutputStream thumbFileStream = |
| 140 | mContext.openFileOutput(thumbFile.getName(), Context.MODE_PRIVATE); |
| 141 | thumbFileStream.write(stream.toByteArray()); |
| 142 | |
| 143 | SQLiteDatabase db = mDb.getWritableDatabase(); |
| 144 | ContentValues values = new ContentValues(); |
| 145 | values.put(ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, thumbFile.getName()); |
| 146 | values.put(ImageDb.COLUMN_IMAGE_FILENAME, imageFile.getName()); |
| 147 | db.insert(ImageDb.TABLE_NAME, null, values); |
| 148 | } catch (IOException e) { |
| 149 | Log.e(TAG, "Failed writing images to storage " + e); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static class ImageDb extends SQLiteOpenHelper { |
| 154 | final static int DB_VERSION = 1; |
| 155 | final static String DB_NAME = "saved_wallpaper_images.db"; |
| 156 | final static String TABLE_NAME = "saved_wallpaper_images"; |
| 157 | final static String COLUMN_ID = "id"; |
| 158 | final static String COLUMN_IMAGE_THUMBNAIL_FILENAME = "image_thumbnail"; |
| 159 | final static String COLUMN_IMAGE_FILENAME = "image"; |
| 160 | |
| 161 | Context mContext; |
| 162 | |
| 163 | public ImageDb(Context context) { |
| 164 | super(context, new File(context.getCacheDir(), DB_NAME).getPath(), null, DB_VERSION); |
| 165 | // Store the context for later use |
| 166 | mContext = context; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void onCreate(SQLiteDatabase database) { |
| 171 | database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + |
| 172 | COLUMN_ID + " INTEGER NOT NULL, " + |
| 173 | COLUMN_IMAGE_THUMBNAIL_FILENAME + " TEXT NOT NULL, " + |
| 174 | COLUMN_IMAGE_FILENAME + " TEXT NOT NULL, " + |
| 175 | "PRIMARY KEY (" + COLUMN_ID + " ASC) " + |
| 176 | ");"); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 181 | if (oldVersion != newVersion) { |
| 182 | // Delete all the records; they'll be repopulated as this is a cache |
| 183 | db.execSQL("DELETE FROM " + TABLE_NAME); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |