diff --git a/build.gradle b/build.gradle index 88e39d0..f2c8ee5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { } dependencies { - classpath "com.android.tools.build:gradle:0.7.+" + classpath "com.android.tools.build:gradle:1.2.+" } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9b8ffdd..e7faee0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/library/build.gradle b/library/build.gradle index b8b9d9e..ee61c20 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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 diff --git a/library/src/main/java/com/readystatesoftware/sqliteasset/SQLiteAssetHelper.java b/library/src/main/java/com/readystatesoftware/sqliteasset/SQLiteAssetHelper.java index ebee1c9..6071f33 100755 --- a/library/src/main/java/com/readystatesoftware/sqliteasset/SQLiteAssetHelper.java +++ b/library/src/main/java/com/readystatesoftware/sqliteasset/SQLiteAssetHelper.java @@ -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 + *

+ * 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. - * + *

*

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. - * + *

*

For examples see * https://github.com/jgilfelt/android-sqlite-asset-helper - * + *

*

Note: this class assumes * monotonically increasing version numbers for upgrades. Also, there * is no concept of a database downgrade; installing a new version of @@ -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; @@ -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); @@ -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. - * + *

*

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.

- * + *

*

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() { @@ -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(); + } } } @@ -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. - * + *

*

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() { @@ -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); } @@ -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(); + } } } @@ -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(); @@ -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 paths = new ArrayList(); - 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()); @@ -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); } @@ -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 @@ -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(); } @@ -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) { @@ -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; } @@ -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) { @@ -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; } @@ -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); diff --git a/library/src/main/java/com/readystatesoftware/sqliteasset/Utils.java b/library/src/main/java/com/readystatesoftware/sqliteasset/Utils.java index 67cecaf..1811494 100644 --- a/library/src/main/java/com/readystatesoftware/sqliteasset/Utils.java +++ b/library/src/main/java/com/readystatesoftware/sqliteasset/Utils.java @@ -39,10 +39,11 @@ public static List 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(); diff --git a/library/src/main/java/com/readystatesoftware/sqliteasset/VersionComparator.java b/library/src/main/java/com/readystatesoftware/sqliteasset/VersionComparator.java index 60d4625..b6b11d4 100644 --- a/library/src/main/java/com/readystatesoftware/sqliteasset/VersionComparator.java +++ b/library/src/main/java/com/readystatesoftware/sqliteasset/VersionComparator.java @@ -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 @@ -33,18 +33,14 @@ class VersionComparator implements Comparator { * 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: - * databasename_fromVersionInteger_toVersionInteger + * 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: + * databasename_fromVersionInteger_toVersionInteger */ @Override public int compare(String file0, String file1) { diff --git a/samples/database-v1/build.gradle b/samples/database-v1/build.gradle index 708c923..3963842 100644 --- a/samples/database-v1/build.gradle +++ b/samples/database-v1/build.gradle @@ -1,19 +1,18 @@ -apply plugin: 'android' - -dependencies { - compile project(':library') -} +apply plugin: 'com.android.application' android { - buildToolsVersion '19.0.0' - compileSdkVersion 19 + compileSdkVersion 22 + buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 8 - packageName 'com.example.sqliteassethelper' - targetSdkVersion 19 + applicationId 'com.example.sqliteassethelper' + targetSdkVersion 22 versionCode 1 versionName '1.0' } + dependencies { + compile project(':library') + } } \ No newline at end of file diff --git a/samples/database-v13-upgrade/build.gradle b/samples/database-v13-upgrade/build.gradle index 131d755..918a704 100644 --- a/samples/database-v13-upgrade/build.gradle +++ b/samples/database-v13-upgrade/build.gradle @@ -2,34 +2,34 @@ buildscript { repositories { mavenCentral() } - dependencies { - classpath 'com.android.tools.build:gradle:0.7.+' - } } -apply plugin: 'android' + +apply plugin: 'com.android.application' repositories { mavenCentral() } android { - compileSdkVersion 19 - buildToolsVersion "19.0.0" + compileSdkVersion 22 + buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 8 - targetSdkVersion 19 + targetSdkVersion 22 versionCode 3 versionName "3.0" } + buildTypes { release { - runProguard false + minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } -} -dependencies { - compile project(':library') + dependencies { + compile project(':library') + } } + diff --git a/samples/database-v2-upgrade/build.gradle b/samples/database-v2-upgrade/build.gradle index 2be5d87..c5b4081 100644 --- a/samples/database-v2-upgrade/build.gradle +++ b/samples/database-v2-upgrade/build.gradle @@ -1,19 +1,18 @@ -apply plugin: 'android' - -dependencies { - compile project(':library') -} +apply plugin: 'com.android.application' android { - buildToolsVersion '19.0.0' - compileSdkVersion 19 + compileSdkVersion 22 + buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 8 - packageName 'com.example.sqliteassethelper' - targetSdkVersion 19 + applicationId 'com.example.sqliteassethelper' + targetSdkVersion 22 versionCode 2 versionName '2.0' } + dependencies { + compile project(':library') + } } \ No newline at end of file