FileDocCategorySizeDatePackage
MetaData.javaAPI DocAndroid 1.5 API10711Wed May 06 22:42:48 BST 2009com.android.providers.calendar

MetaData

public class MetaData extends Object
The global meta-data used for expanding the Instances table is stored in one row of the "CalendarMetaData" table. This class is used for caching those values to avoid repeatedly banging on the database. It is also used for writing the values back to the database, while maintaining the consistency of the cache.

Fields Summary
private Fields
mFields
The cached copy of the meta-data fields from the database.
private final android.database.sqlite.SQLiteOpenHelper
mOpenHelper
private boolean
mInitialized
private static final String[]
sCalendarMetaDataProjection
The column names in the CalendarMetaData table. This projection must contain all of the columns.
private static final int
METADATA_INDEX_LOCAL_TIMEZONE
private static final int
METADATA_INDEX_MIN_INSTANCE
private static final int
METADATA_INDEX_MAX_INSTANCE
private static final int
METADATA_INDEX_MIN_BUSYBIT
private static final int
METADATA_INDEX_MAX_BUSYBIT
Constructors Summary
public MetaData(android.database.sqlite.SQLiteOpenHelper openHelper)

    
       
        mOpenHelper = openHelper;
    
Methods Summary
public voidclearBusyBitRange()
Clears the time range for the BusyBits table.

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        db.beginTransaction();
        try {
            // If the fields have not been initialized from the database,
            // then read the database.
            if (!mInitialized) {
                readLocked(db);
            }
            writeLocked(mFields.timezone, mFields.minInstance, mFields.maxInstance,
                    0 /* startDay */, 0 /* endDay */);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    
public voidclearInstanceRange()
Clears the time range for the Instances table. The rows in the Instances table will be deleted (and regenerated) the next time that the Instances table is queried. Also clears the time range for the BusyBits table because that depends on the Instances table.

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        db.beginTransaction();
        try {
            // If the fields have not been initialized from the database,
            // then read the database.
            if (!mInitialized) {
                readLocked(db);
            }
            writeLocked(mFields.timezone, 0 /* begin */, 0 /* end */,
                    0 /* startDay */, 0 /* endDay */);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    
public com.android.providers.calendar.MetaData$FieldsgetFields()
Returns a copy of all the MetaData fields. This method grabs a database lock to read all the fields atomically.

return
a copy of all the MetaData fields.

        Fields fields = new Fields();
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        db.beginTransaction();
        try {
            // If the fields have not been initialized from the database,
            // then read the database.
            if (!mInitialized) {
                readLocked(db);
            }
            fields.timezone = mFields.timezone;
            fields.minInstance = mFields.minInstance;
            fields.maxInstance = mFields.maxInstance;
            fields.minBusyBit = mFields.minBusyBit;
            fields.maxBusyBit = mFields.maxBusyBit;
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        return fields;
    
public com.android.providers.calendar.MetaData$FieldsgetFieldsLocked()
This method must be called only while holding a database lock.

Returns a copy of all the MetaData fields. This method assumes the database lock has already been acquired.

return
a copy of all the MetaData fields.

        Fields fields = new Fields();

        // If the fields have not been initialized from the database,
        // then read the database.
        if (!mInitialized) {
            SQLiteDatabase db = mOpenHelper.getReadableDatabase();
            readLocked(db);
        }
        fields.timezone = mFields.timezone;
        fields.minInstance = mFields.minInstance;
        fields.maxInstance = mFields.maxInstance;
        fields.minBusyBit = mFields.minBusyBit;
        fields.maxBusyBit = mFields.maxBusyBit;
        return fields;
    
private voidreadLocked(android.database.sqlite.SQLiteDatabase db)
Reads the meta-data for the CalendarProvider from the database and updates the member variables. This method executes while the database lock is held. If there were no exceptions reading the database, mInitialized is set to true.

        String timezone = null;
        long minInstance = 0, maxInstance = 0;
        int minBusyBit = 0, maxBusyBit = 0;

        // Read the database directly.  We only do this once to initialize
        // the members of this class.
        Cursor cursor = db.query("CalendarMetaData", sCalendarMetaDataProjection,
                null, null, null, null, null);
        try {
            if (cursor.moveToNext()) {
                timezone = cursor.getString(METADATA_INDEX_LOCAL_TIMEZONE);
                minInstance = cursor.getLong(METADATA_INDEX_MIN_INSTANCE);
                maxInstance = cursor.getLong(METADATA_INDEX_MAX_INSTANCE);
                minBusyBit = cursor.getInt(METADATA_INDEX_MIN_BUSYBIT);
                maxBusyBit = cursor.getInt(METADATA_INDEX_MAX_BUSYBIT);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        // Cache the result of reading the database
        mFields.timezone = timezone;
        mFields.minInstance = minInstance;
        mFields.maxInstance = maxInstance;
        mFields.minBusyBit = minBusyBit;
        mFields.maxBusyBit = maxBusyBit;
        
        // Mark the fields as initialized
        mInitialized = true;
    
public voidwrite(java.lang.String timezone, long begin, long end, int startDay, int endDay)
Writes the meta-data for the CalendarProvider. The values to write are passed in as parameters. All of the values are updated atomically, including the cached copy of the meta-data.

param
timezone the local timezone used for Instance expansion
param
begin the start of the Instance expansion in UTC milliseconds
param
end the end of the Instance expansion in UTC milliseconds
param
startDay the start of the BusyBit expansion (the start Julian day)
param
endDay the end of the BusyBit expansion (the end Julian day)

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        db.beginTransaction();
        try {
            writeLocked(timezone, begin, end, startDay, endDay);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    
public voidwriteLocked(java.lang.String timezone, long begin, long end, int startDay, int endDay)
This method must be called only while holding a database lock.

Writes the meta-data for the CalendarProvider. The values to write are passed in as parameters. All of the values are updated atomically, including the cached copy of the meta-data.

param
timezone the local timezone used for Instance expansion
param
begin the start of the Instance expansion in UTC milliseconds
param
end the end of the Instance expansion in UTC milliseconds
param
startDay the start of the BusyBit expansion (the start Julian day)
param
endDay the end of the BusyBit expansion (the end Julian day)

        ContentValues values = new ContentValues();
        values.put("_id", 1);
        values.put(CalendarMetaData.LOCAL_TIMEZONE, timezone);
        values.put(CalendarMetaData.MIN_INSTANCE, begin);
        values.put(CalendarMetaData.MAX_INSTANCE, end);
        values.put(CalendarMetaData.MIN_BUSYBITS, startDay);
        values.put(CalendarMetaData.MAX_BUSYBITS, endDay);

        // Atomically update the database and the cached members.
        try {
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            db.replace("CalendarMetaData", null, values);
        } catch (RuntimeException e) {
            // Failed: zero the in-memory fields to force recomputation.
            mFields.timezone = null;
            mFields.minInstance = mFields.maxInstance = 0;
            mFields.minBusyBit = mFields.maxBusyBit = 0;
            throw e;
        }

        // Update the cached members last in case the database update fails
        mFields.timezone = timezone;
        mFields.minInstance = begin;
        mFields.maxInstance = end;
        mFields.minBusyBit = startDay;
        mFields.maxBusyBit = endDay;