diff --git a/wrench-app/build.gradle b/wrench-app/build.gradle index 423aa9c..9676dd8 100644 --- a/wrench-app/build.gradle +++ b/wrench-app/build.gradle @@ -62,8 +62,8 @@ android { dependencies { testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2-alpha1' - androidTestImplementation 'com.android.support.test:runner:1.0.2-alpha1' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2-beta1' + androidTestImplementation 'com.android.support.test:runner:1.0.2-beta1' androidTestImplementation 'android.arch.persistence.room:testing:1.1.0-beta2' annotationProcessor "android.arch.lifecycle:compiler:1.1.1" diff --git a/wrench-app/src/androidTest/java/com/izettle/wrench/provider/WrenchProviderTest.java b/wrench-app/src/androidTest/java/com/izettle/wrench/provider/WrenchProviderTest.java index dbd2f4e..fa19a59 100644 --- a/wrench-app/src/androidTest/java/com/izettle/wrench/provider/WrenchProviderTest.java +++ b/wrench-app/src/androidTest/java/com/izettle/wrench/provider/WrenchProviderTest.java @@ -171,6 +171,6 @@ private Nut getNut(String value) { } private Bolt getBolt(String key) { - return new Bolt(0, "bolttype", key, "boltvalue"); + return new Bolt(0L, "bolttype", key, "boltvalue"); } } \ No newline at end of file diff --git a/wrench-core/build.gradle b/wrench-core/build.gradle index a6f4929..a4d5d83 100644 --- a/wrench-core/build.gradle +++ b/wrench-core/build.gradle @@ -1,14 +1,11 @@ apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' android { compileSdkVersion 27 defaultConfig { - minSdkVersion 15 + minSdkVersion 1 targetSdkVersion 27 - versionCode 1 - versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -43,7 +40,6 @@ android { dependencies { testImplementation 'junit:junit:4.12' implementation 'com.android.support:support-annotations:27.1.1' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } apply from: rootProject.file('gradle/gradle-mvn-push.gradle') diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.java b/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.java new file mode 100644 index 0000000..5702eff --- /dev/null +++ b/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.java @@ -0,0 +1,123 @@ +package com.izettle.wrench.core; + +import android.content.ContentValues; +import android.database.Cursor; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.StringDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +public class Bolt { + @NonNull + @BoltType + private final String type; + @NonNull + private final String key; + @Nullable + private final String value; + private long id; + + public Bolt(Long id, @NonNull String type, @NonNull String key, @Nullable String value) { + this.id = id; + this.type = type; + this.key = key; + this.value = value; + } + + public Bolt() { + type = ""; + key = ""; + value = null; + } + + public static Bolt fromContentValues(ContentValues values) { + Long id = values.getAsLong(ColumnNames.Bolt.COL_ID); + @BoltType + String type = values.getAsString(ColumnNames.Bolt.COL_TYPE); + String key = values.getAsString(ColumnNames.Bolt.COL_KEY); + String value = values.getAsString(ColumnNames.Bolt.COL_VALUE); + + return new Bolt(id, type, key, value); + } + + public static Bolt fromCursor(Cursor cursor) { + long id = getNullsafeLong(cursor, ColumnNames.Bolt.COL_ID); + @BoltType + String type = getNullsafeString(cursor, ColumnNames.Bolt.COL_TYPE); + String key = getNullsafeString(cursor, ColumnNames.Bolt.COL_KEY); + String value = cursor.getString(cursor.getColumnIndexOrThrow(ColumnNames.Bolt.COL_VALUE)); + + return new Bolt(id, type, key, value); + } + + @NonNull + private static Long getNullsafeLong(Cursor cursor, String columnName) { + int columnIndex = cursor.getColumnIndexOrThrow(columnName); + if (cursor.isNull(columnIndex)) { + throw new IllegalStateException(columnName + " was null"); + } + return cursor.getLong(columnIndex); + } + + @NonNull + private static String getNullsafeString(Cursor cursor, String columnName) { + int columnIndex = cursor.getColumnIndexOrThrow(columnName); + if (cursor.isNull(columnIndex)) { + throw new IllegalStateException(columnName + " was null"); + } + return cursor.getString(columnIndex); + } + + @NonNull + public String getKey() { + return key; + } + + @NonNull + public String getType() { + return type; + } + + @Nullable + public String getValue() { + return value; + } + + public ContentValues toContentValues() { + ContentValues contentValues = new ContentValues(); + + contentValues.put(ColumnNames.Bolt.COL_ID, id); + contentValues.put(ColumnNames.Bolt.COL_KEY, key); + contentValues.put(ColumnNames.Bolt.COL_VALUE, value); + contentValues.put(ColumnNames.Bolt.COL_TYPE, type); + + return contentValues; + } + + public Long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @NonNull + public Bolt copy(Long id, String type, String key, String value) { + return new Bolt(id, type, key, value); + } + + @Retention(RetentionPolicy.SOURCE) + @StringDef({TYPE.BOOLEAN, TYPE.STRING, TYPE.INTEGER, TYPE.ENUM}) + public @interface BoltType { + } + + public interface TYPE { + String BOOLEAN = "boolean"; + String STRING = "string"; + String INTEGER = "integer"; + String ENUM = "enum"; + } +} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.kt b/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.kt deleted file mode 100644 index d6d6579..0000000 --- a/wrench-core/src/main/java/com/izettle/wrench/core/Bolt.kt +++ /dev/null @@ -1,70 +0,0 @@ -package com.izettle.wrench.core - -import android.content.ContentValues -import android.database.Cursor -import android.support.annotation.StringDef - -data class Bolt(var id: Long = 0, - @BoltType val type: String, - val key: String = "", - val value: String? = null) { - - fun toContentValues(): ContentValues { - val contentValues = ContentValues() - - contentValues.put(ColumnNames.Bolt.COL_ID, id) - contentValues.put(ColumnNames.Bolt.COL_KEY, key) - contentValues.put(ColumnNames.Bolt.COL_VALUE, value) - contentValues.put(ColumnNames.Bolt.COL_TYPE, type) - - return contentValues - } - - @StringDef(TYPE.BOOLEAN, TYPE.STRING, TYPE.INTEGER, TYPE.ENUM) - @Retention(AnnotationRetention.SOURCE) - annotation class BoltType - - object TYPE { - const val BOOLEAN = "boolean" - const val STRING = "string" - const val INTEGER = "integer" - const val ENUM = "enum" - } - - companion object { - - @JvmStatic - fun fromContentValues(values: ContentValues): Bolt { - - return Bolt(id = values.getAsLong(ColumnNames.Bolt.COL_ID) ?: 0, - type = values.getAsString(ColumnNames.Bolt.COL_TYPE), - key = values.getAsString(ColumnNames.Bolt.COL_KEY), - value = values.getAsString(ColumnNames.Bolt.COL_VALUE)) - } - - @JvmStatic - fun fromCursor(cursor: Cursor): Bolt { - return Bolt(id = cursor.getLongOrThrow(ColumnNames.Bolt.COL_ID), - type = cursor.getStringOrThrow(ColumnNames.Bolt.COL_TYPE), - key = cursor.getStringOrThrow(ColumnNames.Bolt.COL_KEY), - value = cursor.getStringOrNull(ColumnNames.Bolt.COL_VALUE) - ) - } - } -} - - -private fun Cursor.getStringOrThrow(columnName: String): String = getStringOrNull(columnName)!! - -private fun Cursor.getStringOrNull(columnName: String): String? { - val index = getColumnIndexOrThrow(columnName) - return if (isNull(index)) null else getString(index) -} - -private fun Cursor.getLongOrThrow(columnName: String): Long = getLongOrNull(columnName)!! - -private fun Cursor.getLongOrNull(columnName: String): Long? { - val index = getColumnIndexOrThrow(columnName) - return if (isNull(index)) null else getLong(index) -} - diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.java b/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.java new file mode 100644 index 0000000..5e4fe72 --- /dev/null +++ b/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.java @@ -0,0 +1,16 @@ +package com.izettle.wrench.core; + +public final class ColumnNames { + public interface Bolt { + String COL_KEY = "configurationKey"; + String COL_ID = "id"; + String COL_VALUE = "value"; + String COL_TYPE = "configurationType"; + } + + public interface Nut { + String COL_ID = "id"; + String COL_VALUE = "value"; + String COL_CONFIG_ID = "configurationId"; + } +} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.kt b/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.kt deleted file mode 100644 index 85af12e..0000000 --- a/wrench-core/src/main/java/com/izettle/wrench/core/ColumnNames.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.izettle.wrench.core - -class ColumnNames { - object Bolt { - const val COL_KEY = "configurationKey" - const val COL_ID = "id" - const val COL_VALUE = "value" - const val COL_TYPE = "configurationType" - } - - object Nut { - const val COL_ID = "id" - const val COL_VALUE = "value" - const val COL_CONFIG_ID = "configurationId" - } -} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/Nut.java b/wrench-core/src/main/java/com/izettle/wrench/core/Nut.java new file mode 100644 index 0000000..e1bde4b --- /dev/null +++ b/wrench-core/src/main/java/com/izettle/wrench/core/Nut.java @@ -0,0 +1,33 @@ +package com.izettle.wrench.core; + +import android.content.ContentValues; + +public class Nut { + private final long id; + private final long configurationId; + private final String value; + + public Nut(long configurationId, String value) { + this.id = 0; + this.configurationId = configurationId; + this.value = value; + } + + public Nut(long id, long configurationId, String value) { + this.id = id; + this.configurationId = configurationId; + this.value = value; + } + + public ContentValues toContentValues() { + ContentValues contentValues = new ContentValues(); + if (id > 0) { + contentValues.put(ColumnNames.Nut.COL_ID, id); + } + + contentValues.put(ColumnNames.Nut.COL_CONFIG_ID, configurationId); + contentValues.put(ColumnNames.Nut.COL_VALUE, value); + + return contentValues; + } +} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/Nut.kt b/wrench-core/src/main/java/com/izettle/wrench/core/Nut.kt deleted file mode 100644 index 34e107a..0000000 --- a/wrench-core/src/main/java/com/izettle/wrench/core/Nut.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.izettle.wrench.core - -import android.content.ContentValues - -data class Nut(val id: Long = 0, - val configurationId: Long = 0, - val value: String? = null) { - - constructor(configurationId: Long, value: String?) : this(0, configurationId, value) - - fun toContentValues(): ContentValues { - val contentValues = ContentValues() - if (id > 0) { - contentValues.put(ColumnNames.Nut.COL_ID, id) - } - contentValues.put(ColumnNames.Nut.COL_CONFIG_ID, configurationId) - contentValues.put(ColumnNames.Nut.COL_VALUE, value) - - return contentValues - } -} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.java b/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.java new file mode 100644 index 0000000..4bec9b4 --- /dev/null +++ b/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.java @@ -0,0 +1,42 @@ +package com.izettle.wrench.core; + +import android.net.Uri; + +public final class WrenchProviderContract { + public static final String WRENCH_AUTHORITY = BuildConfig.WRENCH_AUTHORITY; + + public static final String WRENCH_API_VERSION = "API_VERSION"; + + private static Uri boltUri = Uri.parse("content://" + WRENCH_AUTHORITY + "/currentConfiguration"); + private static Uri nutUri = Uri.parse("content://" + WRENCH_AUTHORITY + "/predefinedConfigurationValue"); + + public static Uri boltUri(long id) { + return boltUri + .buildUpon() + .appendPath(String.valueOf(id)) + .appendQueryParameter(WRENCH_API_VERSION, String.valueOf(BuildConfig.WRENCH_API_VERSION)) + .build(); + } + + public static Uri boltUri(String key) { + return boltUri + .buildUpon() + .appendPath(key) + .appendQueryParameter(WRENCH_API_VERSION, String.valueOf(BuildConfig.WRENCH_API_VERSION)) + .build(); + } + + public static Uri boltUri() { + return boltUri + .buildUpon() + .appendQueryParameter(WRENCH_API_VERSION, String.valueOf(BuildConfig.WRENCH_API_VERSION)) + .build(); + } + + public static Uri nutUri() { + return nutUri + .buildUpon() + .appendQueryParameter(WRENCH_API_VERSION, String.valueOf(BuildConfig.WRENCH_API_VERSION)) + .build(); + } +} diff --git a/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.kt b/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.kt deleted file mode 100644 index b54c702..0000000 --- a/wrench-core/src/main/java/com/izettle/wrench/core/WrenchProviderContract.kt +++ /dev/null @@ -1,47 +0,0 @@ -package com.izettle.wrench.core - -import android.net.Uri - -object WrenchProviderContract { - const val WRENCH_AUTHORITY = BuildConfig.WRENCH_AUTHORITY - - const val WRENCH_API_VERSION = "API_VERSION" - - private val boltUri = Uri.parse("content://$WRENCH_AUTHORITY/currentConfiguration") - private val nutUri = Uri.parse("content://$WRENCH_AUTHORITY/predefinedConfigurationValue") - - @JvmStatic - fun boltUri(id: Long): Uri { - return boltUri - .buildUpon() - .appendPath(id.toString()) - .appendQueryParameter(WRENCH_API_VERSION, BuildConfig.WRENCH_API_VERSION.toString()) - .build() - - } - - @JvmStatic - fun boltUri(key: String): Uri { - return boltUri - .buildUpon() - .appendPath(key) - .appendQueryParameter(WRENCH_API_VERSION, BuildConfig.WRENCH_API_VERSION.toString()) - .build() - } - - @JvmStatic - fun boltUri(): Uri { - return boltUri - .buildUpon() - .appendQueryParameter(WRENCH_API_VERSION, BuildConfig.WRENCH_API_VERSION.toString()) - .build() - } - - @JvmStatic - fun nutUri(): Uri { - return nutUri - .buildUpon() - .appendQueryParameter(WRENCH_API_VERSION, BuildConfig.WRENCH_API_VERSION.toString()) - .build() - } -} diff --git a/wrench-prefs/src/main/java/com/izettle/wrench/preferences/WrenchPreferences.kt b/wrench-prefs/src/main/java/com/izettle/wrench/preferences/WrenchPreferences.kt index afbe519..3d10ed5 100644 --- a/wrench-prefs/src/main/java/com/izettle/wrench/preferences/WrenchPreferences.kt +++ b/wrench-prefs/src/main/java/com/izettle/wrench/preferences/WrenchPreferences.kt @@ -26,7 +26,7 @@ open class WrenchPreferences(context: Context) { } } - return Bolt(type = boltType, key = key) + return Bolt(0, boltType, key, null) } private fun insertBolt(contentResolver: ContentResolver, bolt: Bolt): Uri? { @@ -37,7 +37,7 @@ open class WrenchPreferences(context: Context) { var bolt = getBolt(contentResolver, Bolt.TYPE.ENUM, key) ?: return defValue if (bolt.id == 0L) { - bolt = bolt.copy(key = key, type = Bolt.TYPE.ENUM, value = defValue.toString()) + bolt = bolt.copy(bolt.id, key, Bolt.TYPE.ENUM, defValue.toString()) val uri = insertBolt(contentResolver, bolt) bolt.id = uri!!.lastPathSegment.toLong() @@ -54,7 +54,7 @@ open class WrenchPreferences(context: Context) { var bolt = getBolt(contentResolver, Bolt.TYPE.STRING, key) ?: return defValue if (bolt.id == 0L) { - bolt = bolt.copy(key = key, type = Bolt.TYPE.STRING, value = defValue) + bolt = bolt.copy(bolt.id, key, Bolt.TYPE.STRING, defValue) insertBolt(contentResolver, bolt) } @@ -65,7 +65,7 @@ open class WrenchPreferences(context: Context) { var bolt = getBolt(contentResolver, Bolt.TYPE.BOOLEAN, key) ?: return defValue if (bolt.id == 0L) { - bolt = bolt.copy(key = key, type = Bolt.TYPE.BOOLEAN, value = defValue.toString()) + bolt = bolt.copy(bolt.id, key, Bolt.TYPE.BOOLEAN, defValue.toString()) insertBolt(contentResolver, bolt) } @@ -76,7 +76,7 @@ open class WrenchPreferences(context: Context) { var bolt = getBolt(contentResolver, Bolt.TYPE.INTEGER, key) ?: return defValue if (bolt.id == 0L) { - bolt = bolt.copy(key = key, type = Bolt.TYPE.INTEGER, value = defValue.toString()) + bolt = bolt.copy(bolt.id, key, Bolt.TYPE.INTEGER, defValue.toString()) insertBolt(contentResolver, bolt) } diff --git a/wrench-sample/build.gradle b/wrench-sample/build.gradle index e5e39be..36fb4d3 100644 --- a/wrench-sample/build.gradle +++ b/wrench-sample/build.gradle @@ -50,8 +50,9 @@ dependencies { testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.18.0' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2-alpha1' - androidTestImplementation 'com.android.support.test:runner:1.0.2-alpha1' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2-beta1' + androidTestImplementation 'com.android.support.test:runner:1.0.2-beta1' + androidTestImplementation 'com.android.support.test:rules:1.0.2-beta1' androidTestImplementation project(":wrench-core") androidTestImplementation project(":wrench-prefs") diff --git a/wrench-sample/src/androidTest/java/com/example/wrench/LaunchTest.java b/wrench-sample/src/androidTest/java/com/example/wrench/LaunchTest.java index cbcb93e..746896e 100644 --- a/wrench-sample/src/androidTest/java/com/example/wrench/LaunchTest.java +++ b/wrench-sample/src/androidTest/java/com/example/wrench/LaunchTest.java @@ -108,7 +108,7 @@ private static Bolt getBolt(ContentResolver contentResolver, @Bolt.BoltType Stri cursor.close(); } } - return new Bolt(0, type, key, ""); + return new Bolt(0L, type, key, ""); } private static Uri insertBolt(ContentResolver contentResolver, Bolt bolt) { diff --git a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/WrenchLiveData.java b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/WrenchLiveData.java index f7fddf5..dfedfd2 100644 --- a/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/WrenchLiveData.java +++ b/wrench-sample/src/main/java/com/example/wrench/BoltLiveData/WrenchLiveData.java @@ -65,7 +65,7 @@ private static Bolt getBolt(ContentResolver contentResolver, @Bolt.BoltType Stri cursor.close(); } } - return new Bolt(0, type, key, ""); + return new Bolt(0L, type, key, ""); } String getType() { diff --git a/wrench-service/src/main/java/com/izettle/wrench/service/WrenchService.kt b/wrench-service/src/main/java/com/izettle/wrench/service/WrenchService.kt index 0c855d2..19f5a3e 100644 --- a/wrench-service/src/main/java/com/izettle/wrench/service/WrenchService.kt +++ b/wrench-service/src/main/java/com/izettle/wrench/service/WrenchService.kt @@ -82,6 +82,6 @@ class WrenchService : IntentService("WrenchService") { } } - return Bolt(type = boltType, key = key) + return Bolt(0, boltType, key, null) } }