Skip to content

Commit b83dbcd

Browse files
wenxi-zengWenxi Zeng
and
Wenxi Zeng
authored
Fix crash caused by legacy app build (#256)
* backwards compatible with legacy builds from analytics-android * add unit test * nit * fix ci issue --------- Co-authored-by: Wenxi Zeng <[email protected]>
1 parent d6ab914 commit b83dbcd

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

.github/workflows/build.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Grant execute permission for gradlew
2626
run: chmod +x gradlew
2727
- name: cache gradle dependencies
28-
uses: actions/cache@v2
28+
uses: actions/cache@v3
2929
with:
3030
path: |
3131
~/.gradle/caches
@@ -49,7 +49,7 @@ jobs:
4949
- name: Grant execute permission for gradlew
5050
run: chmod +x gradlew
5151
- name: cache gradle dependencies
52-
uses: actions/cache@v2
52+
uses: actions/cache@v3
5353
with:
5454
path: |
5555
~/.gradle/caches
@@ -73,7 +73,7 @@ jobs:
7373
- name: Grant execute permission for gradlew
7474
run: chmod +x gradlew
7575
- name: cache gradle dependencies
76-
uses: actions/cache@v2
76+
uses: actions/cache@v3
7777
with:
7878
path: |
7979
~/.gradle/caches

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Grant execute permission for gradlew
2626
run: chmod +x gradlew
2727
- name: cache gradle dependencies
28-
uses: actions/cache@v2
28+
uses: actions/cache@v3
2929
with:
3030
path: |
3131
~/.gradle/caches

.github/workflows/snapshot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Grant execute permission for gradlew
1414
run: chmod +x gradlew
1515
- name: cache gradle dependencies
16-
uses: actions/cache@v2
16+
uses: actions/cache@v3
1717
with:
1818
path: |
1919
~/.gradle/caches

android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt

+38-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ import com.segment.analytics.kotlin.android.utilities.AndroidKVS
66
import com.segment.analytics.kotlin.core.Analytics
77
import com.segment.analytics.kotlin.core.Storage
88
import com.segment.analytics.kotlin.core.StorageProvider
9+
import com.segment.analytics.kotlin.core.utilities.EventStream
910
import com.segment.analytics.kotlin.core.utilities.FileEventStream
11+
import com.segment.analytics.kotlin.core.utilities.KVS
1012
import com.segment.analytics.kotlin.core.utilities.StorageImpl
1113
import kotlinx.coroutines.CoroutineDispatcher
1214
import sovran.kotlin.Store
1315

1416
@Deprecated("Use StorageProvider to create storage for Android instead")
1517
class AndroidStorage(
1618
context: Context,
17-
private val store: Store,
19+
store: Store,
1820
writeKey: String,
19-
private val ioDispatcher: CoroutineDispatcher,
21+
ioDispatcher: CoroutineDispatcher,
2022
directory: String? = null,
2123
subject: String? = null
22-
) : StorageImpl(
24+
) : AndroidStorageImpl(
2325
propertiesFile = AndroidKVS(context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE)),
2426
eventStream = FileEventStream(context.getDir(directory ?: "segment-disk-queue", Context.MODE_PRIVATE)),
2527
store = store,
@@ -28,6 +30,38 @@ class AndroidStorage(
2830
ioDispatcher = ioDispatcher
2931
)
3032

33+
open class AndroidStorageImpl(
34+
propertiesFile: KVS,
35+
eventStream: EventStream,
36+
store: Store,
37+
writeKey: String,
38+
fileIndexKey: String,
39+
ioDispatcher: CoroutineDispatcher
40+
) : StorageImpl(
41+
propertiesFile = propertiesFile,
42+
eventStream = eventStream,
43+
store = store,
44+
writeKey = writeKey,
45+
fileIndexKey = fileIndexKey,
46+
ioDispatcher = ioDispatcher
47+
) {
48+
override fun read(key: Storage.Constants): String? {
49+
return if (key == Storage.Constants.LegacyAppBuild) {
50+
// The legacy app build number was stored as an integer so we have to get it
51+
// as an integer and convert it to a String.
52+
val noBuild = -1
53+
val build = propertiesFile.get(key.rawVal, noBuild)
54+
if (build != noBuild) {
55+
build.toString()
56+
} else {
57+
null
58+
}
59+
} else {
60+
super.read(key)
61+
}
62+
}
63+
}
64+
3165
object AndroidStorageProvider : StorageProvider {
3266
override fun createStorage(vararg params: Any): Storage {
3367

@@ -51,6 +85,6 @@ object AndroidStorageProvider : StorageProvider {
5185

5286
val propertiesFile = AndroidKVS(sharedPreferences)
5387
val eventStream = FileEventStream(eventDirectory)
54-
return StorageImpl(propertiesFile, eventStream, analytics.store, config.writeKey, fileIndexKey, analytics.fileIODispatcher)
88+
return AndroidStorageImpl(propertiesFile, eventStream, analytics.store, config.writeKey, fileIndexKey, analytics.fileIODispatcher)
5589
}
5690
}

android/src/test/java/com/segment/analytics/kotlin/android/StorageTests.kt

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ class StorageTests {
189189
androidStorage.remove(Storage.Constants.AppVersion)
190190
assertEquals(null, map["segment.app.version"])
191191
}
192+
193+
@Test
194+
fun `test legacy app build`() = runTest {
195+
map["build"] = 100
196+
assertEquals("100", androidStorage.read(Storage.Constants.LegacyAppBuild))
197+
}
192198
}
193199

194200
@Nested

core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import java.io.InputStream
2424
* for events storage
2525
*/
2626
open class StorageImpl(
27-
internal val propertiesFile: KVS,
27+
val propertiesFile: KVS,
2828
internal val eventStream: EventStream,
2929
private val store: Store,
3030
private val writeKey: String,

0 commit comments

Comments
 (0)