Back to project page androidata.
The source code is released under:
Apache License
If you think the Android project androidata listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.stanidesis.androidata; //from w w w . j a va 2 s. c o m import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.stanidesis.androidata.table.ITable; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by Stanley Idesis on 8/23/14. */ public class AndroiData implements AndroiDataCenter.DatabaseProvider { /** * Log tag */ public static final String TAG = AndroiData.class.getSimpleName(); /** * Static instance */ private static AndroiData sInstance; /** * Access point for retrieving an instance of AndroiData. * @return */ public static AndroiData get() { if (sInstance == null) { sInstance = new AndroiData(); } return sInstance; } private boolean mInitialized; private boolean mOpened; private boolean mOpening; private Options mOptions; private Context mContext; private SQLiteDatabase mReadOnlyDatabase; private SQLiteDatabase mWritableDatabase; private Map<ITable, AndroiDataCenter> mDataCenters; private AndroiData() { mInitialized = false; mOpened = false; mDataCenters = new HashMap<ITable, AndroiDataCenter>(); } /** * Invoke this method before attempting to perform any * other task. * * @param context * @param options * @return */ public AndroiData initialize(Context context, Options options) { if (mInitialized) { throw new IllegalStateException(getClass().getSimpleName() + " already initialized."); } else if (options == null) { throw new IllegalArgumentException("options may not be null"); } else if (options.dbVersion < 0) { throw new IllegalArgumentException("Database version must be greater than or equal to 0"); } else if (options.dbName == null) { throw new IllegalArgumentException("Database name may not be null"); } else if (options.dataCenters == null || options.dataCenters.size() == 0) { throw new IllegalArgumentException("At least one AndroiDataCenter must be present"); } mContext = context; mOptions = options.copy(); for (AndroiDataCenter dataCenter : mOptions.dataCenters) { mDataCenters.put(dataCenter.getTable(), dataCenter); dataCenter.setDatabaseProvider(this); } mInitialized = true; return this; } /** * Opens the databases underneath. */ public synchronized void open() { if (!mInitialized) { throw new IllegalStateException("Must initialize before calling #open()"); } open(false); } /** * Opens the database, optionally synchronous. * It is recommended to open the database on a background * thread. AndroiData assures this if you pass false. * * @param synchronous */ public synchronized void open(boolean synchronous) { if (!mInitialized) { throw new IllegalStateException("Must initialize before calling #open()"); } if (mOpened) { throw new IllegalStateException("Already open, do not call #open() twice"); } if (mOpening) { if (mOptions.debug) { Log.w(TAG, "Called open twice."); } return; } mOpening = true; ITable[] iTables = new ITable[mOptions.dataCenters.size()]; int i = 0; for (AndroiDataCenter center : mOptions.dataCenters) { iTables[i++] = center.getTable(); } final AndroiDatabaseOpenHelper androiDatabaseOpenHelper = new AndroiDatabaseOpenHelper(mContext, mOptions.dbName, mOptions.dbVersion, iTables); if (synchronous) { mReadOnlyDatabase = androiDatabaseOpenHelper.getReadableDatabase(); mWritableDatabase = androiDatabaseOpenHelper.getWritableDatabase(); mOpened = mReadOnlyDatabase != null && mWritableDatabase != null; mOpening = false; if (mOptions.debug) { if (mOpened) { Log.v(TAG, "Database opened synchronously"); } else { Log.e(TAG, "Failed to open database synchronously"); } } } else { new Thread(new Runnable() { @Override public void run() { mReadOnlyDatabase = androiDatabaseOpenHelper.getReadableDatabase(); mWritableDatabase = androiDatabaseOpenHelper.getWritableDatabase(); mOpened = mReadOnlyDatabase != null && mWritableDatabase != null; mOpening = false; if (mOptions.debug) { if (mOpened) { Log.v(TAG, "Database opened asynchronously"); } else { Log.e(TAG, "Failed to open database asynchronously"); } } } }).start(); } } public synchronized void close() { if (!mOpened) { throw new IllegalStateException("AndroiData was never opened, call #open() before closing"); } mReadOnlyDatabase.close(); mReadOnlyDatabase = null; mWritableDatabase.close(); mWritableDatabase = null; mOpened = false; if (mOptions.debug) { Log.v(TAG, "Database closed"); } } /** * Deletes the database completely */ public synchronized void delete() { if (mOpened) { close(); } mContext.deleteDatabase(mOptions.dbName); if (mOptions.debug) { Log.v(TAG, "Database \'" + mOptions.dbName + "\' deleted"); } } public boolean isDebug() { return mOptions.debug; } public String getDBName() { return mOptions.dbName; } public int getDBVersion() { return mOptions.dbVersion; } public synchronized boolean isOpening() { return mOpening; } public synchronized boolean isOpen() { return mOpened; } @Override public SQLiteDatabase getReadOnlyDatabase() { if (!mOpened) { throw new IllegalStateException("AndroiData was never opened, call #open() before recovering a database"); } return mReadOnlyDatabase; } @Override public SQLiteDatabase getWritableDatabase() { if (!mOpened) { throw new IllegalStateException("AndroiData was never opened, call #open() before recovering a database"); } return mWritableDatabase; } public <Table extends ITable> AndroiDataCenter<?, Table> getDataCenter(Table table) { return mDataCenters.get(table); } /** * Recover an AndroiDataCenter class by its class type. * * @since v1.0.5 * @param dataCenterClass * @param <Center> * @return */ public <Center extends AndroiDataCenter> Center getDataCenter(Class<? extends Center> dataCenterClass) { for (AndroiDataCenter center : mDataCenters.values()) { if (center.getClass() == dataCenterClass) { return (Center) center; } } if (isDebug()) { Log.e(TAG, "Failed to find AndroiDataCenter for class: " + dataCenterClass); } return null; } /** * Provide options for AndroiData using this * nested class. */ public static class Options { Options copy() { Options options = new Options(); options.dbName = dbName; options.dbVersion = dbVersion; options.dataCenters = dataCenters; options.debug = debug; return options; } String dbName = null; int dbVersion = -1; List<AndroiDataCenter> dataCenters = null; boolean debug; public void setDatabaseName(String databaseName) { dbName = databaseName; } public void setDatabaseVersion(int databaseVersion) { dbVersion = databaseVersion; } public void setDataCenters(List<AndroiDataCenter> dataCenters) { this.dataCenters = dataCenters; } public void setDebug(boolean debug) { this.debug = debug; } } }