Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
}

dependencies {
classpath "com.android.tools.build:gradle:0.7.+"
classpath "com.android.tools.build:gradle:1.2.+"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
13 changes: 9 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
apply plugin: 'android-library'
import com.android.builder.core.BuilderConstants

apply plugin: 'com.android.library'

android {
buildToolsVersion '19.0.0'
compileSdkVersion 19
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 7
}

defaultPublishConfig "release"
}


android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equals(com.android.builder.BuilderConstants.DEBUG)) {
if (name.equals(BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@
import java.util.zip.ZipInputStream;

/**
* A helper class to manage database creation and version management using
* A helper class to manage database creation and version management using
* an application's raw asset files.
*
* This class provides developers with a simple way to ship their Android app
* with an existing SQLite database (which may be pre-populated with data) and
* to manage its initial creation and any upgrades required with subsequent
* <p/>
* This class provides developers with a simple way to ship their Android app
* with an existing SQLite database (which may be pre-populated with data) and
* to manage its initial creation and any upgrades required with subsequent
* version releases.
*
* <p/>
* <p>This class makes it easy for {@link android.content.ContentProvider}
* implementations to defer opening and upgrading the database until first use,
* to avoid blocking application startup with long-running database upgrades.
*
* <p/>
* <p>For examples see <a href="https://github.com/jgilfelt/android-sqlite-asset-helper">
* https://github.com/jgilfelt/android-sqlite-asset-helper</a>
*
* <p/>
* <p class="note"><strong>Note:</strong> this class assumes
* monotonically increasing version numbers for upgrades. Also, there
* is no concept of a database downgrade; installing a new version of
Expand Down Expand Up @@ -76,26 +76,33 @@ public class SQLiteAssetHelper extends SQLiteOpenHelper {
private int mForcedUpgradeVersion = 0;

/**
* Create a helper object to create, open, and/or manage a database in
* Create a helper object to create, open, and/or manage a database in
* a specified location.
* This method always returns very quickly. The database is not actually
* created or opened until one of {@link #getWritableDatabase} or
* {@link #getReadableDatabase} is called.
*
* @param context to use to open or create the database
* @param name of the database file
* @param context to use to open or create the database
* @param name of the database file
* @param storageDirectory to store the database file upon creation; caller must
* ensure that the specified absolute path is available and can be written to
* @param factory to use for creating cursor objects, or null for the default
* @param version number of the database (starting at 1); if the database is older,
* SQL file(s) contained within the application assets folder will be used to
* upgrade the database
* ensure that the specified absolute path is available and can be
* written to
* @param factory to use for creating cursor objects, or null for the default
* @param version number of the database (starting at 1); if the database is older,
* SQL file(s) contained within the application assets folder will be
* used to
* upgrade the database
*/
public SQLiteAssetHelper(Context context, String name, String storageDirectory, CursorFactory factory, int version) {
public SQLiteAssetHelper(Context context, String name, String storageDirectory, CursorFactory
factory, int version) {
super(context, name, factory, version);

if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
if (name == null) throw new IllegalArgumentException("Database name cannot be null");
if (version < 1) {
throw new IllegalArgumentException("Version must be >= 1, was " + version);
}
if (name == null) {
throw new IllegalArgumentException("Database name cannot be null");
}

mContext = context;
mName = name;
Expand All @@ -112,18 +119,18 @@ public SQLiteAssetHelper(Context context, String name, String storageDirectory,
}

/**
* Create a helper object to create, open, and/or manage a database in
* Create a helper object to create, open, and/or manage a database in
* the application's default private data directory.
* This method always returns very quickly. The database is not actually
* created or opened until one of {@link #getWritableDatabase} or
* {@link #getReadableDatabase} is called.
*
* @param context to use to open or create the database
* @param name of the database file
* @param name of the database file
* @param factory to use for creating cursor objects, or null for the default
* @param version number of the database (starting at 1); if the database is older,
* SQL file(s) contained within the application assets folder will be used to
* upgrade the database
* SQL file(s) contained within the application assets folder will be used to
* upgrade the database
*/
public SQLiteAssetHelper(Context context, String name, CursorFactory factory, int version) {
this(context, name, null, factory, version);
Expand All @@ -133,19 +140,19 @@ public SQLiteAssetHelper(Context context, String name, CursorFactory factory, in
* Create and/or open a database that will be used for reading and writing.
* The first time this is called, the database will be extracted and copied
* from the application's assets folder.
*
* <p/>
* <p>Once opened successfully, the database is cached, so you can
* call this method every time you need to write to the database.
* (Make sure to call {@link #close} when you no longer need the database.)
* Errors such as bad permissions or a full disk may cause this method
* to fail, but future attempts may succeed if the problem is fixed.</p>
*
* <p/>
* <p class="caution">Database upgrade may take a long time, you
* should not call this method from the application main thread, including
* from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
*
* @throws SQLiteException if the database cannot be opened for writing
* @return a read/write database object valid until {@link #close} is called
* @throws SQLiteException if the database cannot be opened for writing
*/
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
Expand Down Expand Up @@ -210,13 +217,18 @@ public synchronized SQLiteDatabase getWritableDatabase() {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try { mDatabase.close(); } catch (Exception e) { }
try {
mDatabase.close();
} catch (Exception e) {
}
//mDatabase.unlock();
}
mDatabase = db;
} else {
//if (mDatabase != null) mDatabase.unlock();
if (db != null) db.close();
if (db != null) {
db.close();
}
}
}

Expand All @@ -230,15 +242,15 @@ public synchronized SQLiteDatabase getWritableDatabase() {
* to {@link #getWritableDatabase} may succeed, in which case the read-only
* database object will be closed and the read/write object will be returned
* in the future.
*
* <p/>
* <p class="caution">Like {@link #getWritableDatabase}, this method may
* take a long time to return, so you should not call it from the
* application main thread, including from
* {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
*
* @throws SQLiteException if the database cannot be opened
* @return a database object valid until {@link #getWritableDatabase}
* or {@link #close} is called.
* or {@link #close} is called.
* @throws SQLiteException if the database cannot be opened
*/
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
Expand All @@ -253,7 +265,9 @@ public synchronized SQLiteDatabase getReadableDatabase() {
try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (mName == null) throw e; // Can't open a temp database read-only!
if (mName == null) {
throw e; // Can't open a temp database read-only!
}
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
}

Expand All @@ -273,7 +287,9 @@ public synchronized SQLiteDatabase getReadableDatabase() {
return mDatabase;
} finally {
mIsInitializing = false;
if (db != null && db != mDatabase) db.close();
if (db != null && db != mDatabase) {
db.close();
}
}
}

Expand All @@ -282,7 +298,9 @@ public synchronized SQLiteDatabase getReadableDatabase() {
*/
@Override
public synchronized void close() {
if (mIsInitializing) throw new IllegalStateException("Closed during initialization");
if (mIsInitializing) {
throw new IllegalStateException("Closed during initialization");
}

if (mDatabase != null && mDatabase.isOpen()) {
mDatabase.close();
Expand All @@ -304,14 +322,16 @@ public final void onCreate(SQLiteDatabase db) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.w(TAG, "Upgrading database " + mName + " from version " + oldVersion + " to " + newVersion + "...");
Log.w(TAG, "Upgrading database " + mName + " from version " + oldVersion + " to " +
newVersion + "...");

ArrayList<String> paths = new ArrayList<String>();
getUpgradeFilePaths(oldVersion, newVersion-1, newVersion, paths);
getUpgradeFilePaths(oldVersion, newVersion - 1, newVersion, paths);

if (paths.isEmpty()) {
Log.e(TAG, "no upgrade script path from " + oldVersion + " to " + newVersion);
throw new SQLiteAssetException("no upgrade script path from " + oldVersion + " to " + newVersion);
throw new SQLiteAssetException("no upgrade script path from " + oldVersion + " to " +
newVersion);
}

Collections.sort(paths, new VersionComparator());
Expand All @@ -334,7 +354,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

Log.w(TAG, "Successfully upgraded database " + mName + " from version " + oldVersion + " to " + newVersion);
Log.w(TAG, "Successfully upgraded database " + mName + " from version " + oldVersion + " " +
"to " + newVersion);

}

Expand All @@ -349,7 +370,6 @@ public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
*
* @param version bypass upgrade up to this version number - should never be greater than the
* latest database version.
*
* @deprecated use {@link #setForcedUpgrade} instead.
*/
@Deprecated
Expand Down Expand Up @@ -381,7 +401,7 @@ private SQLiteDatabase createOrOpenDatabase(boolean force) throws SQLiteAssetExc
// test for the existence of the db file first and don't attempt open
// to prevent the error trace in log on API 14+
SQLiteDatabase db = null;
File file = new File (mDatabasePath + "/" + mName);
File file = new File(mDatabasePath + "/" + mName);
if (file.exists()) {
db = returnDatabase();
}
Expand All @@ -403,9 +423,10 @@ private SQLiteDatabase createOrOpenDatabase(boolean force) throws SQLiteAssetExc
}
}

private SQLiteDatabase returnDatabase(){
private SQLiteDatabase returnDatabase() {
try {
SQLiteDatabase db = SQLiteDatabase.openDatabase(mDatabasePath + "/" + mName, mFactory, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase db = SQLiteDatabase.openDatabase(mDatabasePath + "/" + mName,
mFactory, SQLiteDatabase.OPEN_READWRITE);
Log.i(TAG, "successfully opened database " + mName);
return db;
} catch (SQLiteException e) {
Expand Down Expand Up @@ -435,7 +456,9 @@ private void copyDatabaseFromAssets() throws SQLiteAssetException {
try {
is = mContext.getAssets().open(path + ".gz");
} catch (IOException e3) {
SQLiteAssetException se = new SQLiteAssetException("Missing " + mAssetPath + " file (or .zip, .gz archive) in assets, or target folder not writable");
SQLiteAssetException se = new SQLiteAssetException("Missing " + mAssetPath +
" file (or .zip, .gz archive) in assets, or target folder not " +
"writable");
se.setStackTrace(e3.getStackTrace());
throw se;
}
Expand All @@ -444,7 +467,9 @@ private void copyDatabaseFromAssets() throws SQLiteAssetException {

try {
File f = new File(mDatabasePath + "/");
if (!f.exists()) { f.mkdir(); }
if (!f.exists()) {
f.mkdir();
}
if (isZip) {
ZipInputStream zis = Utils.getFileFromZip(is);
if (zis == null) {
Expand All @@ -458,7 +483,8 @@ private void copyDatabaseFromAssets() throws SQLiteAssetException {
Log.w(TAG, "database copy complete");

} catch (IOException e) {
SQLiteAssetException se = new SQLiteAssetException("Unable to write " + dest + " to data directory");
SQLiteAssetException se = new SQLiteAssetException("Unable to write " + dest + " to " +
"data directory");
se.setStackTrace(e.getStackTrace());
throw se;
}
Expand Down Expand Up @@ -506,7 +532,8 @@ private void getUpgradeFilePaths(int baseVersion, int start, int end, ArrayList<
@SuppressWarnings("serial")
public static class SQLiteAssetException extends SQLiteException {

public SQLiteAssetException() {}
public SQLiteAssetException() {
}

public SQLiteAssetException(String error) {
super(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public static List<String> splitSqlScript(String script, char delim) {
return statements;
}

public static void writeExtractedFileToDisk(InputStream in, OutputStream outs) throws IOException {
public static void writeExtractedFileToDisk(InputStream in, OutputStream outs) throws
IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer))>0){
while ((length = in.read(buffer)) > 0) {
outs.write(buffer, 0, length);
}
outs.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import android.util.Log;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper.SQLiteAssetException;

import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper.SQLiteAssetException;

/**
* Compare paths by their upgrade version numbers, instead of using
* alphanumeric comparison on plain file names. This prevents the upgrade
Expand All @@ -33,18 +33,14 @@ class VersionComparator implements Comparator<String> {
* database names used are the same, as this function only compares the
* two version numbers.
*
* @param file0
* an upgrade script file name
* @param file1
* a second upgrade script file name to compare with file0
* @param file0 an upgrade script file name
* @param file1 a second upgrade script file name to compare with file0
* @return an integer < 0 if file0 should be applied before file1, 0 if
* they are equal (though that shouldn't happen), and > 0 if
* file0 should be applied after file1.
*
* @exception SQLiteAssetException
* thrown if the strings are not in the correct upgrade
* script format of:
* <code>databasename_fromVersionInteger_toVersionInteger</code>
* they are equal (though that shouldn't happen), and > 0 if
* file0 should be applied after file1.
* @throws SQLiteAssetException thrown if the strings are not in the correct upgrade
* script format of:
* <code>databasename_fromVersionInteger_toVersionInteger</code>
*/
@Override
public int compare(String file0, String file1) {
Expand Down
Loading