Skip to content

Commit

Permalink
feat: add event bridge module (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Mar 19, 2022
1 parent 8b7bada commit 49229ac
Showing 41 changed files with 332 additions and 87 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/pull-request-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Test and Lint

on: [pull_request]

@@ -13,4 +13,6 @@ jobs:
distribution: 'zulu'
- uses: actions/checkout@v2
- name: Unit Test
run: ./gradlew testDebugUnitTest
run: ./gradlew testDebugUnitTest
- name: Lint
run: ./gradlew ktlintCheck
2 changes: 1 addition & 1 deletion android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import com.amplitude.id.IdentityContainer

open class Amplitude(
configuration: Configuration
): Amplitude(configuration) {
) : Amplitude(configuration) {

override fun build() {
idContainer = IdentityContainer.getInstance(IdentityConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = FileIdentityStorageProvider()))
3 changes: 1 addition & 2 deletions android/src/main/java/com/amplitude/android/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.amplitude.android

import android.content.Context
import com.amplitude.android.utilities.AndroidLoggerProvider
import com.amplitude.core.Configuration
import com.amplitude.core.LoggerProvider
import com.amplitude.core.StorageProvider
import com.amplitude.android.utilities.AndroidLoggerProvider
import com.amplitude.android.utilities.AndroidStorageProvider
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.utilities.FileStorageProvider

Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import com.amplitude.core.Amplitude
import com.amplitude.core.Storage
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.utilities.FileStorageProvider

class AndroidStorage(
val amplitude: Amplitude
@@ -30,7 +29,7 @@ class AndroidStorage(
}
}

class AndroidStorageProvider: StorageProvider {
class AndroidStorageProvider : StorageProvider {
override fun getStorage(amplitude: Amplitude): Storage {
return AndroidStorage(amplitude)
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
buildscript {
ext.kotlin_version = "1.5.10"
repositories {
maven { url "https://plugins.gradle.org/m2/" }
mavenCentral()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.2.1"
}
}

@@ -25,4 +27,6 @@ allprojects{
jvmTarget = "1.8"
}
}

apply plugin: "org.jlleitschuh.gradle.ktlint"
}
Original file line number Diff line number Diff line change
@@ -9,14 +9,16 @@ import android.location.LocationManager
import android.os.Build
import android.provider.Settings.Secure
import android.telephony.TelephonyManager
import com.amplitude.common.ContextProvider
import java.io.IOException
import java.lang.Exception
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
import java.lang.NullPointerException
import java.lang.reflect.InvocationTargetException
import java.util.*
import com.amplitude.common.ContextProvider
import java.util.Locale
import java.util.UUID
import kotlin.collections.ArrayList

class AndroidContextProvider(private val context: Context, locationListening: Boolean) :
ContextProvider {
@@ -48,18 +50,18 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
var appSetId: String

init {
advertisingId = fetchAdvertisingId();
versionName = fetchVersionName();
advertisingId = fetchAdvertisingId()
versionName = fetchVersionName()
osName = OS_NAME
osVersion = fetchOsVersion();
brand = fetchBrand();
manufacturer = fetchManufacturer();
model = fetchModel();
carrier = fetchCarrier();
country = fetchCountry();
language = fetchLanguage();
gpsEnabled = checkGPSEnabled();
appSetId = fetchAppSetId();
osVersion = fetchOsVersion()
brand = fetchBrand()
manufacturer = fetchManufacturer()
model = fetchModel()
carrier = fetchCarrier()
country = fetchCountry()
language = fetchLanguage()
gpsEnabled = checkGPSEnabled()
appSetId = fetchAppSetId()
}

/**
@@ -116,7 +118,7 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
return if (!country.isNullOrEmpty()) {
country
} else countryFromLocale
}// Customized Android System without Google Play Service Installed// sometimes the location manager is unavailable// Bad lat / lon values can cause Geocoder to throw IllegalArgumentExceptions// failed to fetch geocoder// Failed to reverse geocode location
} // Customized Android System without Google Play Service Installed// sometimes the location manager is unavailable// Bad lat / lon values can cause Geocoder to throw IllegalArgumentExceptions// failed to fetch geocoder// Failed to reverse geocode location

// Failed to reverse geocode location
private val countryFromLocation: String?
@@ -323,7 +325,7 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
val advertisingId: String
get() = cachedInfo!!.advertisingId
val appSetId: String
get() = cachedInfo!!.appSetId// other causes// failed to get providers list
get() = cachedInfo!!.appSetId // other causes// failed to get providers list
// Don't crash if the device does not have location services.

// It's possible that the location service is running out of process
Original file line number Diff line number Diff line change
@@ -3,15 +3,14 @@ package com.amplitude.common.android
import android.util.Log
import com.amplitude.common.Logger

class AndroidLogger(): Logger
{
class AndroidLogger() : Logger {
override var logMode: Logger.LogMode = Logger.LogMode.INFO
private val tag = "Amplitude"

override fun debug(message: String) {
if (logMode <= Logger.LogMode.DEBUG) {
Log.d(tag, message)
}
if (logMode <= Logger.LogMode.DEBUG) {
Log.d(tag, message)
}
}

override fun error(message: String) {
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.amplitude.common.android

import android.Manifest
import android.content.pm.PackageManager

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import java.lang.Exception

object Utils {

fun checkLocationPermissionAllowed(context: Context?): Boolean {
return checkPermissionAllowed(context, Manifest.permission.ACCESS_COARSE_LOCATION) ||
checkPermissionAllowed(context, Manifest.permission.ACCESS_FINE_LOCATION)
checkPermissionAllowed(context, Manifest.permission.ACCESS_FINE_LOCATION)
}

fun checkPermissionAllowed(context: Context?, permission: String?): Boolean {
3 changes: 1 addition & 2 deletions common/src/main/java/com/amplitude/common/ContextProvider.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package com.amplitude.common

interface ContextProvider {
}
interface ContextProvider
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ dependencies {
// MAIN DEPS
compileOnly 'org.json:json:20211205'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation project(":event-bridge")

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'

23 changes: 13 additions & 10 deletions core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
@@ -10,11 +10,18 @@ import com.amplitude.core.platform.Plugin
import com.amplitude.core.platform.Timeline
import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.ContextPlugin
import com.amplitude.core.utilities.AnalyticsEventReceiver
import com.amplitude.core.utilities.AnalyticsIdentityListener
import com.amplitude.eventbridge.EventBridgeContainer
import com.amplitude.eventbridge.EventChannel
import com.amplitude.id.IMIdentityStorageProvider
import com.amplitude.id.IdentityConfiguration
import com.amplitude.id.IdentityContainer
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import java.util.concurrent.Executors

open class Amplitude internal constructor(
@@ -24,7 +31,7 @@ open class Amplitude internal constructor(
val amplitudeDispatcher: CoroutineDispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher(),
val networkIODispatcher: CoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher(),
val storageIODispatcher: CoroutineDispatcher = Executors.newFixedThreadPool(2).asCoroutineDispatcher()
){
) {

internal val timeline: Timeline
val storage: Storage
@@ -47,11 +54,11 @@ open class Amplitude internal constructor(
open fun build() {
idContainer = IdentityContainer.getInstance(IdentityConfiguration(instanceName = configuration.instanceName, apiKey = configuration.apiKey, identityStorageProvider = IMIdentityStorageProvider()))
idContainer.identityManager.addIdentityListener(AnalyticsIdentityListener(store))
EventBridgeContainer.getInstance(configuration.instanceName).eventBridge.setEventReceiver(EventChannel.EVENT, AnalyticsEventReceiver(this))
add(ContextPlugin())
add(AmplitudeDestination())

amplitudeScope.launch (amplitudeDispatcher) {

amplitudeScope.launch(amplitudeDispatcher) {
}
}

@@ -65,7 +72,6 @@ open class Amplitude internal constructor(
}

fun identify(identify: Identify) {

}

fun identify(userId: String) {
@@ -77,11 +83,9 @@ open class Amplitude internal constructor(
}

fun groupIdentify(identify: Identify) {

}

fun setGroup(groupType: String, groupName: Array<String>) {

}

@Deprecated("Please use 'revenue' instead.", ReplaceWith("revenue"))
@@ -113,7 +117,7 @@ open class Amplitude internal constructor(
}
}

fun add(plugin: Plugin) : Amplitude {
fun add(plugin: Plugin): Amplitude {
when (plugin) {
is ObservePlugin -> {
this.store.add(plugin)
@@ -138,6 +142,5 @@ open class Amplitude internal constructor(
}

fun flush() {

}
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/Constants.kt
Original file line number Diff line number Diff line change
@@ -10,4 +10,4 @@ object Constants {
const val AMP_REVENUE_EVENT = "revenue_amount"
const val MAX_PROPERTY_KEYS = 1024
const val MAX_STRING_LENGTH = 1024
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/LoggerProvider.kt
Original file line number Diff line number Diff line change
@@ -4,4 +4,4 @@ import com.amplitude.common.Logger

interface LoggerProvider {
fun getLogger(amplitude: Amplitude): Logger
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/events/BaseEvent.kt
Original file line number Diff line number Diff line change
@@ -6,6 +6,6 @@ open class BaseEvent : EventOptions() {
open lateinit var eventType: String
var eventProperties: JSONObject? = null
var userProperties: JSONObject? = null
var groups:JSONObject? = null
var groups: JSONObject? = null
var groupProperties: JSONObject? = null
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package com.amplitude.core.events

class GroupIdentifyEvent : BaseEvent() {
}
class GroupIdentifyEvent : BaseEvent()
4 changes: 1 addition & 3 deletions core/src/main/java/com/amplitude/core/events/Identify.kt
Original file line number Diff line number Diff line change
@@ -19,10 +19,8 @@ class Identify() {
internal val properties: MutableMap<String, Any> = mutableMapOf()

init {

}

internal fun setUserProperty(operation: IdentifyOperation, property: String, value: Any) {

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package com.amplitude.core.events

class IdentifyEvent : BaseEvent() {
}
class IdentifyEvent : BaseEvent()
2 changes: 1 addition & 1 deletion core/src/main/java/com/amplitude/core/events/Plan.kt
Original file line number Diff line number Diff line change
@@ -4,4 +4,4 @@ open class Plan(
var branch: String?,
var source: String?,
var version: String?
)
)
Original file line number Diff line number Diff line change
@@ -4,4 +4,4 @@ import com.amplitude.core.Constants

class RevenueEvent : BaseEvent() {
override var eventType = Constants.AMP_REVENUE_EVENT
}
}
Original file line number Diff line number Diff line change
@@ -3,10 +3,13 @@ package com.amplitude.core.platform
import com.amplitude.core.Amplitude
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.utilities.HttpClient
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.Exception
import java.util.concurrent.atomic.AtomicInteger

@@ -106,7 +109,6 @@ internal class EventPipeline(
}
} catch (e: Exception) {
e.printStackTrace()

}
// @TODO: handle failures and retry
}
4 changes: 2 additions & 2 deletions core/src/main/java/com/amplitude/core/platform/Plugin.kt
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ interface EventPlugin : Plugin {
open fun flush() {}
}

abstract class DestinationPlugin: EventPlugin {
abstract class DestinationPlugin : EventPlugin {
override val type: Plugin.Type = Plugin.Type.Destination
private val timeline: Timeline = Timeline()
override lateinit var amplitude: Amplitude
@@ -100,7 +100,7 @@ abstract class DestinationPlugin: EventPlugin {
}
}

abstract class ObservePlugin: Plugin {
abstract class ObservePlugin : Plugin {
override val type: Plugin.Type = Plugin.Type.Observe

abstract fun onUserIdChanged(userId: String?)
Original file line number Diff line number Diff line change
@@ -50,5 +50,6 @@ class AmplitudeDestination : DestinationPlugin() {
)
pipeline.start()
}
add(IdentityEventSender())
}
}
}
Loading

0 comments on commit 49229ac

Please sign in to comment.