Skip to content

Commit

Permalink
3.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
DeKaN committed Oct 26, 2023
2 parents a0e23f3 + e0f3e16 commit 4e90cf7
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 239 deletions.
6 changes: 4 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ij_editorconfig_space_before_colon = false
ij_editorconfig_space_before_comma = false
ij_editorconfig_spaces_around_assignment_operators = true

[{*.gradle, *.groovy, *.gant, *.gdsl, *.gy}]
[{*.gradle,*.groovy,*.gant,*.gdsl,*.gy}]
ij_groovy_align_group_field_declarations = false
ij_groovy_align_multiline_array_initializer_expression = false
ij_groovy_align_multiline_assignment = false
Expand Down Expand Up @@ -398,14 +398,16 @@ ij_groovy_while_brace_force = never
ij_groovy_while_on_new_line = false
ij_groovy_wrap_long_lines = false

[*.{kt, kts}]
[*.{kt,kts}]
ktlint_code_style = android_studio
ktlint_standard_import-ordering = disabled
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = false
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Piano Analytics SDK for Android

## v3.3.4
* Added limit for event storage
* Updated dependencies:
- com.squareup.okhttp3:okhttp [4.11.0 -> 4.12.0]
https://square.github.io/okhttp/

## v3.3.3
* Added `track` function to `MediaHelper` for tracking custom media events.
* Changed behavior: `extraProps` in `MediaHelper` are added to all events, not only to "heartbeat" events.
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
android {
defaultConfig {
minSdk = 21
compileSdk = 33
compileSdk = 34
targetSdk = 34
applicationId = "com.example.piano_analytics_android"
versionCode = 1
Expand Down
14 changes: 6 additions & 8 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
[versions]
# Plugins
kotlin = "1.8.22"
android = "8.1.1"
versionUpdater = "0.47.0"
ktlint = "11.5.1"
android = "8.1.2"
versionUpdater = "0.49.0"
ktlint = "11.6.1"
dokka = "1.8.20"
mavenRelease = "0.25.3"
moshiIR = "0.22.1"

# AndroidX libraries
compatLibrary = "1.6.1"
annotationsLibrary = "1.6.0"
lifecycle = "2.6.2"
materialLibrary = "1.9.0"
materialLibrary = "1.10.0"

# Third party Libraries
googleAdsId = "18.0.1"
huaweiAdsId = "3.4.26.303"
retrofit = "2.6.4"
okhttp = "4.11.0"
okhttp = "4.12.0"
moshi = "1.15.0"
timber = "5.0.1"
viewBindingProperty = "1.5.9"
Expand All @@ -27,7 +26,7 @@ viewBindingProperty = "1.5.9"
junit = "4.13.2"
androidxTestCore = "1.5.0"
mockitoKotlin = "2.2.0"
mockitoCore = "5.5.0"
mockitoCore = "5.6.0"

[plugins]
android-library = { id = "com.android.library", version.ref = "android" }
Expand All @@ -40,7 +39,6 @@ moshiIR = { id = "dev.zacsweers.moshix", version.ref = "moshiIR"}
versionUpdater = { id = "com.github.ben-manes.versions", version.ref = "versionUpdater" }

[libraries]
annotations = { module = "androidx.annotation:annotation", version.ref = "annotationsLibrary" }
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "compatLibrary" }
lifecycleProcess = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle"}
material = { module = "com.google.android.material:material", version.ref = "materialLibrary" }
Expand Down
10 changes: 7 additions & 3 deletions piano-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ plugins {
alias(libs.plugins.mavenRelease)
}

@Suppress("PropertyName")
val GROUP: String by project

@Suppress("PropertyName")
val VERSION_NAME: String by project

group = GROUP
version = VERSION_NAME

android {
defaultConfig {
minSdk = 21
compileSdk = 33
compileSdk = 34
buildConfigField("String", "SDK_VERSION", """"${project.version}"""")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -43,8 +47,8 @@ kotlin {
}

ktlint {
version.set("0.50.0")
android.set(true)
android = true
version = "1.0.1"
}

dependencies {
Expand Down
2 changes: 2 additions & 0 deletions piano-analytics/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-dontwarn com.google.android.gms.**
-dontwarn com.huawei.hms.ads.**
2 changes: 1 addition & 1 deletion piano-analytics/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.3.3
VERSION_NAME=3.3.4
GROUP=io.piano.android
POM_NAME=Analytics
POM_ARTIFACT_ID=analytics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,35 @@ internal class DatabaseHelper(
}
}

internal fun EventRecord.toContentValues(): ContentValues =
ContentValues().apply {
put(EventRecord.DATA, dataEncoder.encode(data))
put(EventRecord.TIME, timestamp)
put(EventRecord.IS_SENT, isSent)
}
internal fun EventRecord.toContentValues(): ContentValues = ContentValues().apply {
put(EventRecord.DATA, dataEncoder.encode(data))
put(EventRecord.TIME, timestamp)
put(EventRecord.IS_SENT, isSent)
}

fun ContentValues.toEventRecord(): EventRecord =
EventRecord(
dataEncoder.decode(getAsString(EventRecord.DATA)),
getAsLong(EventRecord.TIME),
getAsLong(EventRecord.ID),
getAsBoolean(EventRecord.IS_SENT)
)
fun ContentValues.toEventRecord(): EventRecord = EventRecord(
dataEncoder.decode(getAsString(EventRecord.DATA)),
getAsLong(EventRecord.TIME),
getAsLong(EventRecord.ID),
getAsBoolean(EventRecord.IS_SENT)
)

fun save(eventRecord: EventRecord): Long =
eventRecord.id?.let { id ->
writableDatabase.update(
EventRecord.TABLE_NAME,
eventRecord.toContentValues(),
"${EventRecord.ID} = ?",
arrayOf(id.toString())
).toLong()
} ?: writableDatabase.insert(
fun save(eventRecord: EventRecord): Long = eventRecord.id?.let { id ->
writableDatabase.update(
EventRecord.TABLE_NAME,
null,
eventRecord.toContentValues()
)
eventRecord.toContentValues(),
"${EventRecord.ID} = ?",
arrayOf(id.toString())
).toLong()
} ?: writableDatabase.insert(
EventRecord.TABLE_NAME,
null,
eventRecord.toContentValues()
)

fun delete(eventRecord: EventRecord): Int =
eventRecord.id?.let { id ->
delete("${EventRecord.ID} = ?", id.toString())
} ?: -1
fun delete(eventRecord: EventRecord): Int = eventRecord.id?.let { id ->
delete("${EventRecord.ID} = ?", id.toString())
} ?: -1

fun delete(whereClause: String?, vararg whereArgs: String): Int =
writableDatabase.delete(EventRecord.TABLE_NAME, whereClause, whereArgs)
Expand All @@ -87,42 +83,40 @@ internal class DatabaseHelper(
having: String? = null,
orderBy: String? = "${EventRecord.TIME} ASC",
limit: String? = null,
): List<EventRecord> =
readableDatabase.query(
EventRecord.TABLE_NAME,
columns,
selection,
selectionArgs,
groupBy,
having,
orderBy,
limit
).use { c ->
generateSequence { if (c.moveToNext()) c else null }
.map(Companion::cursorRowToContentValues)
.map {
it.toEventRecord()
}.filter {
it.isValid
}.toList()
}
): List<EventRecord> = readableDatabase.query(
EventRecord.TABLE_NAME,
columns,
selection,
selectionArgs,
groupBy,
having,
orderBy,
limit
).use { c ->
generateSequence { if (c.moveToNext()) c else null }
.map(Companion::cursorRowToContentValues)
.map {
it.toEventRecord()
}.filter {
it.isValid
}.toList()
}

companion object {
const val DATABASE_VERSION = 1
private const val DATABASE_NAME = "events.db"

@JvmStatic
private fun cursorRowToContentValues(c: Cursor): ContentValues =
ContentValues().apply {
for (i in 0 until c.columnCount) {
val name = c.columnNames[i]
when (c.getType(i)) {
Cursor.FIELD_TYPE_BLOB -> put(name, c.getBlob(i))
Cursor.FIELD_TYPE_FLOAT -> put(name, c.getFloat(i))
Cursor.FIELD_TYPE_INTEGER -> put(name, c.getLong(i))
else -> put(name, c.getString(i))
}
private fun cursorRowToContentValues(c: Cursor): ContentValues = ContentValues().apply {
for (i in 0 until c.columnCount) {
val name = c.columnNames[i]
when (c.getType(i)) {
Cursor.FIELD_TYPE_BLOB -> put(name, c.getBlob(i))
Cursor.FIELD_TYPE_FLOAT -> put(name, c.getFloat(i))
Cursor.FIELD_TYPE_INTEGER -> put(name, c.getLong(i))
else -> put(name, c.getString(i))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ internal inline fun <T> delegatedPropertyWithDefaultValue(
}
}

internal fun <T> resettableProperty(
resetValue: T,
initializer: () -> T,
) = ResettableProperty(resetValue, initializer)
internal fun <T> resettableProperty(resetValue: T, initializer: () -> T) = ResettableProperty(resetValue, initializer)

internal class ResettableProperty<T>(
private val resetValue: T,
Expand All @@ -48,29 +45,25 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
default: Boolean = false,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Boolean> =
create(default, key, canBeSaved, prefs::getBoolean, prefs.edit()::putBoolean)
): ReadWriteProperty<Any, Boolean> = create(default, key, canBeSaved, prefs::getBoolean, prefs.edit()::putBoolean)

fun int(
default: Int = 0,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Int> =
create(default, key, canBeSaved, prefs::getInt, prefs.edit()::putInt)
): ReadWriteProperty<Any, Int> = create(default, key, canBeSaved, prefs::getInt, prefs.edit()::putInt)

fun float(
default: Float = 0f,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Float> =
create(default, key, canBeSaved, prefs::getFloat, prefs.edit()::putFloat)
): ReadWriteProperty<Any, Float> = create(default, key, canBeSaved, prefs::getFloat, prefs.edit()::putFloat)

fun long(
default: Long = 0L,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Long> =
create(default, key, canBeSaved, prefs::getLong, prefs.edit()::putLong)
): ReadWriteProperty<Any, Long> = create(default, key, canBeSaved, prefs::getLong, prefs.edit()::putLong)

fun string(
default: String = "",
Expand All @@ -86,19 +79,6 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
): ReadWriteProperty<Any, String?> =
create(default, key, canBeSaved, { k, d -> prefs.getString(k, d) }, prefs.edit()::putString)

fun stringSet(
default: Set<String> = emptySet(),
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Set<String>> =
create(
default,
key,
canBeSaved,
{ k, d -> prefs.getStringSet(k, d) as Set<String> },
prefs.edit()::putStringSet
)

private fun <T> create(
default: T,
key: String? = null,
Expand All @@ -108,8 +88,7 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
) = object : ReadWriteProperty<Any, T> {
private fun key(property: KProperty<*>) = key ?: property.name

override fun getValue(thisRef: Any, property: KProperty<*>): T =
getter(key(property), default)
override fun getValue(thisRef: Any, property: KProperty<*>): T = getter(key(property), default)

override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
val propertyKey = key(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ internal class EventPropertiesJsonAdapter(
is Double,
is Boolean,
is Array<*>,
-> Property(PropertyName(entry.key), entry.value)
-> {
val delimiterIndex = entry.key.lastIndexOf(DELIMITER)
val key = entry.key.substring(delimiterIndex + 1)
val type = if (delimiterIndex != -1) {
val prefix = entry.key.substring(0, delimiterIndex)
Property.Type.values().firstOrNull { it.prefix == prefix }
} else {
null
}
Property(PropertyName(key), entry.value, type)
}

else -> null
}
Expand All @@ -29,6 +39,15 @@ internal class EventPropertiesJsonAdapter(

override fun toJson(writer: JsonWriter, value: Set<Property>?) {
requireNotNull(value)
mapAdapter.toJson(writer, value.associate { it.name.key.lowercase() to it.value })
mapAdapter.toJson(
writer,
value.associate {
it.forceType?.prefix?.plus(DELIMITER).orEmpty() + it.name.key.lowercase() to it.value
}
)
}

companion object {
private const val DELIMITER = ":"
}
}
Loading

0 comments on commit 4e90cf7

Please sign in to comment.