Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash caused by legacy app build #256

Merged
merged 4 commits into from
Feb 18, 2025
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
Expand All @@ -73,7 +73,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import com.segment.analytics.kotlin.android.utilities.AndroidKVS
import com.segment.analytics.kotlin.core.Analytics
import com.segment.analytics.kotlin.core.Storage
import com.segment.analytics.kotlin.core.StorageProvider
import com.segment.analytics.kotlin.core.utilities.EventStream
import com.segment.analytics.kotlin.core.utilities.FileEventStream
import com.segment.analytics.kotlin.core.utilities.KVS
import com.segment.analytics.kotlin.core.utilities.StorageImpl
import kotlinx.coroutines.CoroutineDispatcher
import sovran.kotlin.Store

@Deprecated("Use StorageProvider to create storage for Android instead")
class AndroidStorage(
context: Context,
private val store: Store,
store: Store,
writeKey: String,
private val ioDispatcher: CoroutineDispatcher,
ioDispatcher: CoroutineDispatcher,
directory: String? = null,
subject: String? = null
) : StorageImpl(
) : AndroidStorageImpl(
propertiesFile = AndroidKVS(context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE)),
eventStream = FileEventStream(context.getDir(directory ?: "segment-disk-queue", Context.MODE_PRIVATE)),
store = store,
Expand All @@ -28,6 +30,38 @@ class AndroidStorage(
ioDispatcher = ioDispatcher
)

open class AndroidStorageImpl(
propertiesFile: KVS,
eventStream: EventStream,
store: Store,
writeKey: String,
fileIndexKey: String,
ioDispatcher: CoroutineDispatcher
) : StorageImpl(
propertiesFile = propertiesFile,
eventStream = eventStream,
store = store,
writeKey = writeKey,
fileIndexKey = fileIndexKey,
ioDispatcher = ioDispatcher
) {
override fun read(key: Storage.Constants): String? {
return if (key == Storage.Constants.LegacyAppBuild) {
// The legacy app build number was stored as an integer so we have to get it
// as an integer and convert it to a String.
val noBuild = -1
val build = propertiesFile.get(key.rawVal, noBuild)
if (build != noBuild) {
build.toString()
} else {
null
}
} else {
super.read(key)
}
}
}

object AndroidStorageProvider : StorageProvider {
override fun createStorage(vararg params: Any): Storage {

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

val propertiesFile = AndroidKVS(sharedPreferences)
val eventStream = FileEventStream(eventDirectory)
return StorageImpl(propertiesFile, eventStream, analytics.store, config.writeKey, fileIndexKey, analytics.fileIODispatcher)
return AndroidStorageImpl(propertiesFile, eventStream, analytics.store, config.writeKey, fileIndexKey, analytics.fileIODispatcher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ class StorageTests {
androidStorage.remove(Storage.Constants.AppVersion)
assertEquals(null, map["segment.app.version"])
}

@Test
fun `test legacy app build`() = runTest {
map["build"] = 100
assertEquals("100", androidStorage.read(Storage.Constants.LegacyAppBuild))
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.InputStream
* for events storage
*/
open class StorageImpl(
internal val propertiesFile: KVS,
val propertiesFile: KVS,
internal val eventStream: EventStream,
private val store: Store,
private val writeKey: String,
Expand Down