-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6402b2a
commit 56c1d6a
Showing
10 changed files
with
133 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
android/src/main/java/com/amplitude/android/utilities/AndroidKVS.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.amplitude.android.utilities | ||
|
||
import android.content.SharedPreferences | ||
import com.amplitude.id.utilities.KeyValueStore | ||
|
||
class AndroidKVS(private val sharedPreferences: SharedPreferences) : KeyValueStore { | ||
override fun getLong(key: String, defaultVal: Long): Long { | ||
return sharedPreferences.getLong(key, defaultVal) | ||
} | ||
|
||
override fun putLong(key: String, value: Long): Boolean { | ||
return sharedPreferences.edit().putLong(key, value).commit() | ||
} | ||
} |
77 changes: 66 additions & 11 deletions
77
android/src/main/java/com/amplitude/android/utilities/AndroidStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,112 @@ | ||
package com.amplitude.android.utilities | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.amplitude.core.Amplitude | ||
import com.amplitude.core.Configuration | ||
import com.amplitude.core.EventCallBack | ||
import com.amplitude.core.Storage | ||
import com.amplitude.core.StorageProvider | ||
import com.amplitude.core.events.BaseEvent | ||
import com.amplitude.core.platform.EventPipeline | ||
import com.amplitude.core.utilities.EventsFileManager | ||
import com.amplitude.core.utilities.EventsFileStorage | ||
import com.amplitude.core.utilities.FileResponseHandler | ||
import com.amplitude.core.utilities.JSONUtil | ||
import com.amplitude.core.utilities.ResponseHandler | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import org.json.JSONArray | ||
import java.io.BufferedReader | ||
import java.io.File | ||
|
||
class AndroidStorage( | ||
val amplitude: Amplitude | ||
) : Storage { | ||
context: Context, | ||
apiKey: String | ||
) : Storage, EventsFileStorage { | ||
|
||
companion object { | ||
const val STORAGE_PREFIX = "amplitude-android" | ||
} | ||
|
||
private val sharedPreferences: SharedPreferences = | ||
context.getSharedPreferences("$STORAGE_PREFIX-$apiKey", Context.MODE_PRIVATE) | ||
private val storageDirectory: File = context.getDir("amplitude-disk-queue", Context.MODE_PRIVATE) | ||
private val eventsFile = | ||
EventsFileManager(storageDirectory, apiKey, AndroidKVS(sharedPreferences)) | ||
private val eventCallbacksMap = mutableMapOf<String, EventCallBack>() | ||
|
||
override suspend fun writeEvent(event: BaseEvent) { | ||
TODO("Not yet implemented") | ||
eventsFile.storeEvent(JSONUtil.eventToString(event)) | ||
event.callback?.let { callback -> | ||
event.insertId?. let { | ||
eventCallbacksMap.put(it, callback) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun write(key: Storage.Constants, value: String) { | ||
TODO("Not yet implemented") | ||
sharedPreferences.edit().putString(key.rawVal, value).apply() | ||
} | ||
|
||
override suspend fun rollover() { | ||
TODO("Not yet implemented") | ||
eventsFile.rollover() | ||
} | ||
|
||
override fun read(key: Storage.Constants): String? { | ||
TODO("Not yet implemented") | ||
return sharedPreferences.getString(key.rawVal, null) | ||
} | ||
|
||
override fun readEventsContent(): List<Any> { | ||
TODO("Not yet implemented") | ||
return eventsFile.read() | ||
} | ||
|
||
override fun getEventsString(content: Any): String { | ||
TODO("Not yet implemented") | ||
val bufferedReader: BufferedReader = File(content as String).bufferedReader() | ||
bufferedReader.use { | ||
return it.readText() | ||
} | ||
} | ||
|
||
override fun getResponseHandler( | ||
storage: Storage, | ||
eventPipeline: EventPipeline, | ||
configuration: Configuration, | ||
scope: CoroutineScope, | ||
dispatcher: CoroutineDispatcher, | ||
events: Any, | ||
eventsString: String | ||
): ResponseHandler { | ||
TODO("Not yet implemented") | ||
return FileResponseHandler( | ||
this, | ||
eventPipeline, | ||
configuration, | ||
scope, | ||
dispatcher, | ||
events as String, | ||
eventsString | ||
) | ||
} | ||
|
||
override fun removeFile(filePath: String): Boolean { | ||
return eventsFile.remove(filePath) | ||
} | ||
|
||
override fun getEventCallback(insertId: String): EventCallBack? { | ||
return eventCallbacksMap.getOrDefault(insertId, null) | ||
} | ||
|
||
override fun removeEventCallback(insertId: String) { | ||
eventCallbacksMap.remove(insertId) | ||
} | ||
|
||
override fun splitEventFile(filePath: String, events: JSONArray) { | ||
eventsFile.splitFile(filePath, events) | ||
} | ||
} | ||
|
||
class AndroidStorageProvider : StorageProvider { | ||
override fun getStorage(amplitude: Amplitude): Storage { | ||
return AndroidStorage(amplitude) | ||
val configuration = amplitude.configuration as com.amplitude.android.Configuration | ||
return AndroidStorage(configuration.context, configuration.apiKey) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters