Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2010 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.providers.calendar;
     18 
     19 import android.content.ContentValues;
     20 import android.database.Cursor;
     21 import android.database.sqlite.SQLiteDatabase;
     22 import android.database.sqlite.SQLiteOpenHelper;
     23 import android.util.Log;
     24 import com.google.common.annotations.VisibleForTesting;
     25 
     26 import java.util.TimeZone;
     27 
     28 /**
     29  * Class for managing a persistent Cache of (key, value) pairs. The persistent storage used is
     30  * a SQLite database.
     31  */
     32 public class CalendarCache {
     33     private static final String TAG = "CalendarCache";
     34 
     35     public static final String DATABASE_NAME = "CalendarCache";
     36 
     37     public static final String KEY_TIMEZONE_DATABASE_VERSION = "timezoneDatabaseVersion";
     38     public static final String DEFAULT_TIMEZONE_DATABASE_VERSION = "2009s";
     39 
     40     public static final String KEY_TIMEZONE_TYPE = "timezoneType";
     41     public static final String TIMEZONE_TYPE_AUTO = "auto";
     42     public static final String TIMEZONE_TYPE_HOME = "home";
     43 
     44     public static final String KEY_TIMEZONE_INSTANCES = "timezoneInstances";
     45     public static final String KEY_TIMEZONE_INSTANCES_PREVIOUS = "timezoneInstancesPrevious";
     46 
     47     public static final String COLUMN_NAME_ID = "_id";
     48     public static final String COLUMN_NAME_KEY = "key";
     49     public static final String COLUMN_NAME_VALUE = "value";
     50 
     51     private static final String[] sProjection = {
     52         COLUMN_NAME_KEY,
     53         COLUMN_NAME_VALUE
     54     };
     55 
     56     private static final int COLUMN_INDEX_KEY = 0;
     57     private static final int COLUMN_INDEX_VALUE = 1;
     58 
     59     private final SQLiteOpenHelper mOpenHelper;
     60 
     61     /**
     62      * This exception is thrown when the cache encounter a null key or a null database reference
     63      */
     64     public static class CacheException extends Exception {
     65         public CacheException() {
     66         }
     67 
     68         public CacheException(String detailMessage) {
     69             super(detailMessage);
     70         }
     71     }
     72 
     73     public CalendarCache(SQLiteOpenHelper openHelper) {
     74         mOpenHelper = openHelper;
     75     }
     76 
     77     public void writeTimezoneDatabaseVersion(String timezoneDatabaseVersion) throws CacheException {
     78         writeData(KEY_TIMEZONE_DATABASE_VERSION, timezoneDatabaseVersion);
     79     }
     80 
     81     public String readTimezoneDatabaseVersion() {
     82         try {
     83             return readData(KEY_TIMEZONE_DATABASE_VERSION);
     84         } catch (CacheException e) {
     85             Log.e(TAG, "Could not read timezone database version from CalendarCache");
     86         }
     87         return null;
     88     }
     89 
     90     @VisibleForTesting
     91     public void writeTimezoneType(String timezoneType) throws CacheException {
     92         writeData(KEY_TIMEZONE_TYPE, timezoneType);
     93     }
     94 
     95     public String readTimezoneType() {
     96         try {
     97             return readData(KEY_TIMEZONE_TYPE);
     98         } catch (CacheException e) {
     99             Log.e(TAG, "Cannot read timezone type from CalendarCache - using AUTO as default", e);
    100         }
    101         return TIMEZONE_TYPE_AUTO;
    102     }
    103 
    104     public void writeTimezoneInstances(String timezone) {
    105         try {
    106             writeData(KEY_TIMEZONE_INSTANCES, timezone);
    107         } catch (CacheException e) {
    108             Log.e(TAG, "Cannot write instances timezone to CalendarCache");
    109         }
    110     }
    111 
    112     public String readTimezoneInstances() {
    113         try {
    114             return readData(KEY_TIMEZONE_INSTANCES);
    115         } catch (CacheException e) {
    116             String localTimezone = TimeZone.getDefault().getID();
    117             Log.e(TAG, "Cannot read instances timezone from CalendarCache - using device one: " +
    118                     localTimezone, e);
    119             return localTimezone;
    120         }
    121     }
    122 
    123     public void writeTimezoneInstancesPrevious(String timezone) {
    124         try {
    125             writeData(KEY_TIMEZONE_INSTANCES_PREVIOUS, timezone);
    126         } catch (CacheException e) {
    127             Log.e(TAG, "Cannot write previous instance timezone to CalendarCache");
    128         }
    129     }
    130 
    131     public String readTimezoneInstancesPrevious() {
    132         try {
    133             return readData(KEY_TIMEZONE_INSTANCES_PREVIOUS);
    134         } catch (CacheException e) {
    135             Log.e(TAG, "Cannot read previous instances timezone from CalendarCache", e);
    136         }
    137         return null;
    138     }
    139 
    140     /**
    141      * Write a (key, value) pair in the Cache.
    142      *
    143      * @param key the key (must not be null)
    144      * @param value the value (can be null)
    145      * @throws CacheException when key is null
    146      */
    147     public void writeData(String key, String value) throws CacheException {
    148         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    149         db.beginTransaction();
    150         try {
    151             writeDataLocked(db, key, value);
    152             db.setTransactionSuccessful();
    153             if (Log.isLoggable(TAG, Log.VERBOSE)) {
    154                 Log.i(TAG, "Wrote (key, value) = [ " + key + ", " + value + "] ");
    155             }
    156         } finally {
    157             db.endTransaction();
    158         }
    159     }
    160 
    161     /**
    162      * Write a (key, value) pair in the database used by the cache. This call should be called into
    163      * a transaction.
    164      *
    165      * @param db the database (must not be null)
    166      * @param key the key (must not be null)
    167      * @param value the value
    168      * @throws CacheException when key or database are null
    169      */
    170     protected void writeDataLocked(SQLiteDatabase db, String key, String value)
    171             throws CacheException {
    172         if (null == db) {
    173             throw new CacheException("Database cannot be null");
    174         }
    175         if (null == key) {
    176             throw new CacheException("Cannot use null key for write");
    177         }
    178 
    179         ContentValues values = new ContentValues();
    180         values.put(COLUMN_NAME_ID, key.hashCode());
    181         values.put(COLUMN_NAME_KEY, key);
    182         values.put(COLUMN_NAME_VALUE, value);
    183 
    184         db.replace(DATABASE_NAME, null /* null column hack */, values);
    185     }
    186 
    187     /**
    188      * Read a value from the database used by the cache and depending on a key.
    189      *
    190      * @param key the key from which we want the value (must not be null)
    191      * @return the value that was found for the key. Can be null if no key has been found
    192      * @throws CacheException when key is null
    193      */
    194     public String readData(String key) throws CacheException {
    195         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    196         return readDataLocked(db, key);
    197     }
    198 
    199     /**
    200      * Read a value from the database used by the cache and depending on a key. The database should
    201      * be "readable" at minimum
    202      *
    203      * @param db the database (must not be null)
    204      * @param key the key from which we want the value (must not be null)
    205      * @return the value that was found for the key. Can be null if no value has been found for the
    206      * key.
    207      * @throws CacheException when key or database are null
    208      */
    209     protected String readDataLocked(SQLiteDatabase db, String key) throws CacheException {
    210         if (null == db) {
    211             throw new CacheException("Database cannot be null");
    212         }
    213         if (null == key) {
    214             throw new CacheException("Cannot use null key for read");
    215         }
    216 
    217         String rowValue = null;
    218 
    219         Cursor cursor = db.query(DATABASE_NAME, sProjection,
    220                 COLUMN_NAME_KEY + "=?", new String[] { key }, null, null, null);
    221         try {
    222             if (cursor.moveToNext()) {
    223                 rowValue = cursor.getString(COLUMN_INDEX_VALUE);
    224             }
    225             else {
    226                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
    227                     Log.i(TAG, "Could not find key = [ " + key + " ]");
    228                 }
    229             }
    230         } finally {
    231             cursor.close();
    232             cursor = null;
    233         }
    234         return rowValue;
    235     }
    236 }
    237