Home | History | Annotate | Download | only in launcher3
      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 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.BaseAdapter;
     34 import android.widget.ListAdapter;
     35 
     36 import java.io.File;
     37 import java.io.FileOutputStream;
     38 import java.io.IOException;
     39 import java.util.ArrayList;
     40 
     41 
     42 public class SavedWallpaperImages extends BaseAdapter implements ListAdapter {
     43     private static String TAG = "Launcher3.SavedWallpaperImages";
     44     private ImageDb mDb;
     45     ArrayList<SavedWallpaperTile> mImages;
     46     Context mContext;
     47     LayoutInflater mLayoutInflater;
     48 
     49     public static class SavedWallpaperTile extends WallpaperPickerActivity.FileWallpaperInfo {
     50         private int mDbId;
     51         public SavedWallpaperTile(int dbId, File target, Drawable thumb) {
     52             super(target, thumb);
     53             mDbId = dbId;
     54         }
     55 
     56         @Override
     57         public void onDelete(WallpaperPickerActivity a) {
     58             a.getSavedImages().deleteImage(mDbId);
     59         }
     60     }
     61 
     62     public SavedWallpaperImages(Context context) {
     63         // We used to store the saved images in the cache directory, but that meant they'd get
     64         // deleted sometimes-- move them to the data directory
     65         ImageDb.moveFromCacheDirectoryIfNecessary(context);
     66         mDb = new ImageDb(context);
     67         mContext = context;
     68         mLayoutInflater = LayoutInflater.from(context);
     69     }
     70 
     71     public void loadThumbnailsAndImageIdList() {
     72         mImages = new ArrayList<SavedWallpaperTile>();
     73         SQLiteDatabase db = mDb.getReadableDatabase();
     74         Cursor result = db.query(ImageDb.TABLE_NAME,
     75                 new String[] { ImageDb.COLUMN_ID,
     76                     ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME,
     77                     ImageDb.COLUMN_IMAGE_FILENAME}, // cols to return
     78                 null, // select query
     79                 null, // args to select query
     80                 null,
     81                 null,
     82                 ImageDb.COLUMN_ID + " DESC",
     83                 null);
     84 
     85         while (result.moveToNext()) {
     86             String filename = result.getString(1);
     87             File file = new File(mContext.getFilesDir(), filename);
     88 
     89             Bitmap thumb = BitmapFactory.decodeFile(file.getAbsolutePath());
     90             if (thumb != null) {
     91                 mImages.add(new SavedWallpaperTile(result.getInt(0),
     92                         new File(mContext.getFilesDir(), result.getString(2)),
     93                         new BitmapDrawable(thumb)));
     94             }
     95         }
     96         result.close();
     97     }
     98 
     99     public int getCount() {
    100         return mImages.size();
    101     }
    102 
    103     public SavedWallpaperTile getItem(int position) {
    104         return mImages.get(position);
    105     }
    106 
    107     public long getItemId(int position) {
    108         return position;
    109     }
    110 
    111     public View getView(int position, View convertView, ViewGroup parent) {
    112         Drawable thumbDrawable = mImages.get(position).mThumb;
    113         if (thumbDrawable == null) {
    114             Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position);
    115         }
    116         return WallpaperPickerActivity.createImageTileView(
    117                 mLayoutInflater, convertView, parent, thumbDrawable);
    118     }
    119 
    120     private Pair<String, String> getImageFilenames(int id) {
    121         SQLiteDatabase db = mDb.getReadableDatabase();
    122         Cursor result = db.query(ImageDb.TABLE_NAME,
    123                 new String[] { ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME,
    124                     ImageDb.COLUMN_IMAGE_FILENAME }, // cols to return
    125                 ImageDb.COLUMN_ID + " = ?", // select query
    126                 new String[] { Integer.toString(id) }, // args to select query
    127                 null,
    128                 null,
    129                 null,
    130                 null);
    131         if (result.getCount() > 0) {
    132             result.moveToFirst();
    133             String thumbFilename = result.getString(0);
    134             String imageFilename = result.getString(1);
    135             result.close();
    136             return new Pair<String, String>(thumbFilename, imageFilename);
    137         } else {
    138             return null;
    139         }
    140     }
    141 
    142     public void deleteImage(int id) {
    143         Pair<String, String> filenames = getImageFilenames(id);
    144         File imageFile = new File(mContext.getFilesDir(), filenames.first);
    145         imageFile.delete();
    146         File thumbFile = new File(mContext.getFilesDir(), filenames.second);
    147         thumbFile.delete();
    148         SQLiteDatabase db = mDb.getWritableDatabase();
    149         db.delete(ImageDb.TABLE_NAME,
    150                 ImageDb.COLUMN_ID + " = ?", // SELECT query
    151                 new String[] {
    152                     Integer.toString(id) // args to SELECT query
    153                 });
    154     }
    155 
    156     public void writeImage(Bitmap thumbnail, byte[] imageBytes) {
    157         try {
    158             File imageFile = File.createTempFile("wallpaper", "", mContext.getFilesDir());
    159             FileOutputStream imageFileStream =
    160                     mContext.openFileOutput(imageFile.getName(), Context.MODE_PRIVATE);
    161             imageFileStream.write(imageBytes);
    162             imageFileStream.close();
    163 
    164             File thumbFile = File.createTempFile("wallpaperthumb", "", mContext.getFilesDir());
    165             FileOutputStream thumbFileStream =
    166                     mContext.openFileOutput(thumbFile.getName(), Context.MODE_PRIVATE);
    167             thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream);
    168             thumbFileStream.close();
    169 
    170             SQLiteDatabase db = mDb.getWritableDatabase();
    171             ContentValues values = new ContentValues();
    172             values.put(ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, thumbFile.getName());
    173             values.put(ImageDb.COLUMN_IMAGE_FILENAME, imageFile.getName());
    174             db.insert(ImageDb.TABLE_NAME, null, values);
    175         } catch (IOException e) {
    176             Log.e(TAG, "Failed writing images to storage " + e);
    177         }
    178     }
    179 
    180     static class ImageDb extends SQLiteOpenHelper {
    181         final static int DB_VERSION = 1;
    182         final static String TABLE_NAME = "saved_wallpaper_images";
    183         final static String COLUMN_ID = "id";
    184         final static String COLUMN_IMAGE_THUMBNAIL_FILENAME = "image_thumbnail";
    185         final static String COLUMN_IMAGE_FILENAME = "image";
    186 
    187         Context mContext;
    188 
    189         public ImageDb(Context context) {
    190             super(context, context.getDatabasePath(LauncherFiles.WALLPAPER_IMAGES_DB).getPath(),
    191                     null, DB_VERSION);
    192             // Store the context for later use
    193             mContext = context;
    194         }
    195 
    196         public static void moveFromCacheDirectoryIfNecessary(Context context) {
    197             // We used to store the saved images in the cache directory, but that meant they'd get
    198             // deleted sometimes-- move them to the data directory
    199             File oldSavedImagesFile = new File(context.getCacheDir(),
    200                     LauncherFiles.WALLPAPER_IMAGES_DB);
    201             File savedImagesFile = context.getDatabasePath(LauncherFiles.WALLPAPER_IMAGES_DB);
    202             if (oldSavedImagesFile.exists()) {
    203                 oldSavedImagesFile.renameTo(savedImagesFile);
    204             }
    205         }
    206         @Override
    207         public void onCreate(SQLiteDatabase database) {
    208             database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
    209                     COLUMN_ID + " INTEGER NOT NULL, " +
    210                     COLUMN_IMAGE_THUMBNAIL_FILENAME + " TEXT NOT NULL, " +
    211                     COLUMN_IMAGE_FILENAME + " TEXT NOT NULL, " +
    212                     "PRIMARY KEY (" + COLUMN_ID + " ASC) " +
    213                     ");");
    214         }
    215 
    216         @Override
    217         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    218             if (oldVersion != newVersion) {
    219                 // Delete all the records; they'll be repopulated as this is a cache
    220                 db.execSQL("DELETE FROM " + TABLE_NAME);
    221             }
    222         }
    223     }
    224 }
    225