Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Add home timezone support - DO NOT MERGE
Browse files Browse the repository at this point in the history
- add querying CalendarCache key/values thru CalendarProvider
- add unit tests

Change-Id: I87da6b5174c800dca76d3965a7d7cb8953003e1a
  • Loading branch information
Fabrice Di Meglio committed Sep 10, 2010
1 parent 6777058 commit 68040cf
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 85 deletions.
76 changes: 69 additions & 7 deletions src/com/android/providers/calendar/CalendarCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.TimeZone;

/**
* Class for managing a persistent Cache of (key, value) pairs. The persistent storage used is
* a SQLite database.
Expand All @@ -34,9 +36,16 @@ public class CalendarCache {
public static final String KEY_TIMEZONE_DATABASE_VERSION = "timezoneDatabaseVersion";
public static final String DEFAULT_TIMEZONE_DATABASE_VERSION = "2009s";

private static final String COLUMN_NAME_ID = "_id";
private static final String COLUMN_NAME_KEY = "key";
private static final String COLUMN_NAME_VALUE = "value";
public static final String KEY_TIMEZONE_TYPE = "timezoneType";
public static final String TIMEZONE_TYPE_AUTO = "auto";
public static final String TIMEZONE_TYPE_HOME = "home";

public static final String KEY_TIMEZONE_INSTANCES = "timezoneInstances";
public static final String KEY_TIMEZONE_INSTANCES_PREVIOUS = "timezoneInstancesPrevious";

public static final String COLUMN_NAME_ID = "_id";
public static final String COLUMN_NAME_KEY = "key";
public static final String COLUMN_NAME_VALUE = "value";

private static final String[] sProjection = {
COLUMN_NAME_KEY,
Expand Down Expand Up @@ -65,12 +74,65 @@ public CalendarCache(SQLiteOpenHelper openHelper) {
}

public void writeTimezoneDatabaseVersion(String timezoneDatabaseVersion) throws CacheException {
writeData(KEY_TIMEZONE_DATABASE_VERSION,
timezoneDatabaseVersion);
writeData(KEY_TIMEZONE_DATABASE_VERSION, timezoneDatabaseVersion);
}

public String readTimezoneDatabaseVersion() {
try {
return readData(KEY_TIMEZONE_DATABASE_VERSION);
} catch (CacheException e) {
Log.e(TAG, "Could not read timezone database version from CalendarCache");
}
return null;
}

public String readTimezoneDatabaseVersion() throws CacheException {
return readData(KEY_TIMEZONE_DATABASE_VERSION);
public void writeTimezoneType(String timezoneType) throws CacheException {
writeData(KEY_TIMEZONE_TYPE, timezoneType);
}

public String readTimezoneType() {
try {
return readData(KEY_TIMEZONE_TYPE);
} catch (CacheException e) {
Log.e(TAG, "Cannot read timezone type from CalendarCache - using AUTO as default", e);
}
return TIMEZONE_TYPE_AUTO;
}

public void writeTimezoneInstances(String timezone) {
try {
writeData(KEY_TIMEZONE_INSTANCES, timezone);
} catch (CacheException e) {
Log.e(TAG, "Cannot write instances timezone to CalendarCache");
}
}

public String readTimezoneInstances() {
try {
return readData(KEY_TIMEZONE_INSTANCES);
} catch (CacheException e) {
String localTimezone = TimeZone.getDefault().getID();
Log.e(TAG, "Cannot read instances timezone from CalendarCache - using device one: " +
localTimezone, e);
return localTimezone;
}
}

public void writeTimezoneInstancesPrevious(String timezone) {
try {
writeData(KEY_TIMEZONE_INSTANCES_PREVIOUS, timezone);
} catch (CacheException e) {
Log.e(TAG, "Cannot write previous instance timezone to CalendarCache");
}
}

public String readTimezoneInstancesPrevious() {
try {
return readData(KEY_TIMEZONE_INSTANCES_PREVIOUS);
} catch (CacheException e) {
Log.e(TAG, "Cannot read previous instances timezone from CalendarCache", e);
}
return null;
}

/**
Expand Down
77 changes: 68 additions & 9 deletions src/com/android/providers/calendar/CalendarDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.TimeZone;

/**
* Database helper for calendar. Designed as a singleton to make sure that all
Expand All @@ -58,7 +59,7 @@

// Note: if you update the version number, you must also update the code
// in upgradeDatabase() to modify the database (gracefully, if possible).
static final int DATABASE_VERSION = 100;
static final int DATABASE_VERSION = 101;

private static final int PRE_FROYO_SYNC_STATE_VERSION = 3;

Expand Down Expand Up @@ -332,7 +333,7 @@ private void bootstrapDB(SQLiteDatabase db) {

createCalendarMetaDataTable(db);

createCalendarCacheTable(db);
createCalendarCacheTable(db, null);

db.execSQL("CREATE TABLE Attendees (" +
"_id INTEGER PRIMARY KEY," +
Expand Down Expand Up @@ -416,7 +417,7 @@ private void createCalendarMetaDataTable(SQLiteDatabase db) {
");");
}

private void createCalendarCacheTable(SQLiteDatabase db) {
private void createCalendarCacheTable(SQLiteDatabase db, String oldTimezoneDbVersion) {
// This is a hack because versioning skipped version number 61 of schema
// TODO after version 70 this can be removed
db.execSQL("DROP TABLE IF EXISTS CalendarCache;");
Expand All @@ -428,14 +429,44 @@ private void createCalendarCacheTable(SQLiteDatabase db) {
"value TEXT" +
");");

initCalendarCacheTable(db);
initCalendarCacheTable(db, oldTimezoneDbVersion);
updateCalendarCacheTableTo101(db);
}

private void initCalendarCacheTable(SQLiteDatabase db) {
db.execSQL("INSERT INTO CalendarCache (_id, key, value) VALUES (" +
private void initCalendarCacheTable(SQLiteDatabase db, String oldTimezoneDbVersion) {
String timezoneDbVersion = (oldTimezoneDbVersion != null) ?
oldTimezoneDbVersion : CalendarCache.DEFAULT_TIMEZONE_DATABASE_VERSION;

// Set the default timezone database version
db.execSQL("INSERT OR REPLACE INTO CalendarCache (_id, key, value) VALUES (" +
CalendarCache.KEY_TIMEZONE_DATABASE_VERSION.hashCode() + "," +
"'" + CalendarCache.KEY_TIMEZONE_DATABASE_VERSION + "'," +
"'" + CalendarCache.DEFAULT_TIMEZONE_DATABASE_VERSION + "'" +
"'" + timezoneDbVersion + "'" +
");");
}

private void updateCalendarCacheTableTo101(SQLiteDatabase db) {
// Define the default timezone type for Instances timezone management
db.execSQL("INSERT INTO CalendarCache (_id, key, value) VALUES (" +
CalendarCache.KEY_TIMEZONE_TYPE.hashCode() + "," +
"'" + CalendarCache.KEY_TIMEZONE_TYPE + "'," +
"'" + CalendarCache.TIMEZONE_TYPE_AUTO + "'" +
");");

String defaultTimezone = TimeZone.getDefault().getID();

// Define the default timezone for Instances
db.execSQL("INSERT INTO CalendarCache (_id, key, value) VALUES (" +
CalendarCache.KEY_TIMEZONE_INSTANCES.hashCode() + "," +
"'" + CalendarCache.KEY_TIMEZONE_INSTANCES + "'," +
"'" + defaultTimezone + "'" +
");");

// Define the default previous timezone for Instances
db.execSQL("INSERT INTO CalendarCache (_id, key, value) VALUES (" +
CalendarCache.KEY_TIMEZONE_INSTANCES_PREVIOUS.hashCode() + "," +
"'" + CalendarCache.KEY_TIMEZONE_INSTANCES_PREVIOUS + "'," +
"'" + defaultTimezone + "'" +
");");
}

Expand Down Expand Up @@ -545,6 +576,10 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Froyo version "70" already has the CalendarCache fix
oldVersion = 100;
}
if(oldVersion == 100) {
upgradeToVersion101(db);
oldVersion = 101;
}
} catch (SQLiteException e) {
Log.e(TAG, "onUpgrade: SQLiteException, recreating db. " + e);
dropTables(db);
Expand Down Expand Up @@ -577,9 +612,21 @@ private static boolean fixAllDayTime(Time time, String timezone, Long timeInMill
return false;
}

@VisibleForTesting
void upgradeToVersion101(SQLiteDatabase db) {
updateCalendarCacheTableTo101(db);
}

@VisibleForTesting
void upgradeToVersion100(SQLiteDatabase db) {
createCalendarCacheTable(db);
Cursor cursor = db.rawQuery("SELECT value from CalendarCache WHERE key=?",
new String[] {"timezoneDatabaseVersion"});

String oldTimezoneDbVersion = null;
if (cursor != null && cursor.moveToNext()) {
oldTimezoneDbVersion = cursor.getString(0);
}
initCalendarCacheTable(db, oldTimezoneDbVersion);
}

@VisibleForTesting
Expand Down Expand Up @@ -814,7 +861,19 @@ private void upgradeToVersion62(SQLiteDatabase db) {
}

private void upgradeToVersion61(SQLiteDatabase db) {
createCalendarCacheTable(db);
db.execSQL("DROP TABLE IF EXISTS CalendarCache;");

// IF NOT EXISTS should be normal pattern for table creation
db.execSQL("CREATE TABLE IF NOT EXISTS CalendarCache (" +
"_id INTEGER PRIMARY KEY," +
"key TEXT NOT NULL," +
"value TEXT" +
");");

db.execSQL("INSERT INTO CalendarCache (key, value) VALUES (" +
"'" + CalendarCache.KEY_TIMEZONE_DATABASE_VERSION + "'," +
"'" + CalendarCache.DEFAULT_TIMEZONE_DATABASE_VERSION + "'" +
");");
}

private void upgradeToVersion60(SQLiteDatabase db) {
Expand Down

0 comments on commit 68040cf

Please sign in to comment.