Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ lint/tmp/
*.hprof

publish.properties
.kotlin/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Android Dev Tools is a library that contains various QA/Debug tools to speed up
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/http-inspector?label=Http%20Inspector&color=%2373c248)
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/environment-manager?label=Environment%20Manager&color=%2373c248)
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/debug-menu?label=Debug%20Menu&color=%2373c248)
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/deeplink-launcher?label=Deeplink%20Launcher&color=%2373c248)
![Maven Central](https://img.shields.io/maven-central/v/com.trendyol.android.devtools/sharedpref-manager?label=SharedPref%20Manager&color=%2373c248)

## Tools ##
* [Autofill Service](#autofill-service)
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sqlite-ktx = "2.5.1"
startup = "1.1.1"
dialogs = "dialogs-1.4.0"
maven-publish = "0.33.0"
embeddedKoin = "3.5.6"

[libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity-compose" }
Expand Down Expand Up @@ -100,6 +101,8 @@ squareup-moshi-kotlin = { group = "com.squareup.moshi", name = "moshi-kotlin", v
squareup-okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
trendyol-uiComponents-dialogs = { group = "com.github.Trendyol.android-ui-components", name = "dialogs", version.ref = "dialogs" }
gradle-maven-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "maven-publish" }
embeddedKoinCore = { group = "io.insert-koin", name = "embedded-koin-core", version.ref = "embeddedKoin" }
embeddedKoinAndroid = { group = "io.insert-koin", name = "embedded-koin-android", version.ref = "embeddedKoin" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
4 changes: 3 additions & 1 deletion libraries/analytics-logger/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
}

group = "com.trendyol.android.devtools"
version = "0.7.1"
version = "0.8.0"

publishConfig {
defaultConfiguration(
Expand All @@ -57,4 +57,6 @@ dependencies {
implementation(libs.moshi)
implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
implementation(libs.embeddedKoinCore)
implementation(libs.embeddedKoinAndroid)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package com.trendyol.android.devtools.analyticslogger

import android.app.Application
import android.util.Log
import android.content.SharedPreferences
import androidx.core.content.edit
import com.trendyol.android.devtools.analyticslogger.internal.NotificationManager
import com.trendyol.android.devtools.analyticslogger.internal.di.ContextContainer
import com.trendyol.android.devtools.analyticslogger.internal.di.analyticsLoggerModule
import embedded.koin.android.ext.koin.androidContext
import embedded.koin.android.ext.koin.androidLogger
import embedded.koin.core.Koin
import embedded.koin.core.logger.Level
import embedded.koin.dsl.koinApplication
import kotlin.getValue

object AnalyticsLogger {

internal var instance: NotificationManager? = null
private const val TAG = "AnalyticsLogger"
private const val INIT_ERROR_MESSAGE = "Should call AnalyticsLogger.init(Application, Boolean) first."
internal lateinit var koin: Koin

private val instance: NotificationManager by lazy { koin.get() }
private val sharedPreferences: SharedPreferences by lazy { koin.get() }

fun init(application: Application, showNotification: Boolean = true) {
ContextContainer.initialize(application)
instance = NotificationManager(showNotification)
koin = koinApplication {
androidContext(application.applicationContext)
androidLogger(Level.DEBUG)

modules(
analyticsLoggerModule(
application = application,
showNotification = showNotification,
)
)
}.koin
}

/**
Expand All @@ -32,36 +48,32 @@ object AnalyticsLogger {
* ```
*/
fun setEventTransformFunction(jsFunction: String) {
ContextContainer
.analyticsContainer
.sharedPreferencesManager
sharedPreferences
.edit {
putString("jsTransformFunction", jsFunction)
}
}

fun getEventTransformFunction(): String {
return ContextContainer
.analyticsContainer
.sharedPreferencesManager
return sharedPreferences
.getString("jsTransformFunction", "")
.orEmpty()
}

fun show() {
instance?.show() ?: Log.w(TAG, INIT_ERROR_MESSAGE)
instance.show()
}

fun showNotification() {
instance?.showNotification() ?: Log.w(TAG, INIT_ERROR_MESSAGE)
instance.showNotification()
}

fun hideNotification() {
instance?.hideNotification() ?: Log.w(TAG, INIT_ERROR_MESSAGE)
instance.hideNotification()
}

fun report(key: String?, value: String?, platform: String?, source: String?) {
instance?.reportEvent(key, value, platform, source) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
instance.reportEvent(key, value, platform, source)
}

/**
Expand All @@ -80,6 +92,6 @@ object AnalyticsLogger {
source: String?,
isSuccess: Boolean? = null,
) {
instance?.reportEvent(key, value, platform, source, isSuccess) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
instance.reportEvent(key, value, platform, source, isSuccess)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.trendyol.android.devtools.analyticslogger.R
import com.trendyol.android.devtools.analyticslogger.internal.di.AnalyticsContainer
import com.trendyol.android.devtools.analyticslogger.internal.di.ContextContainer
import com.trendyol.android.devtools.analyticslogger.internal.di.AnalyticsLoggerKoinComponent
import com.trendyol.android.devtools.analyticslogger.internal.domain.manager.EventManager
import com.trendyol.android.devtools.analyticslogger.internal.ui.MainActivity
import embedded.koin.core.component.inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

internal class NotificationManager constructor(private var showNotification: Boolean) {
internal class NotificationManager(private var showNotification: Boolean) : AnalyticsLoggerKoinComponent {

private val superVisorJob = SupervisorJob()
private val scope = CoroutineScope(superVisorJob + Dispatchers.IO)
private val analyticsContainer: AnalyticsContainer by lazy { ContextContainer.analyticsContainer }
private val context: Context by inject()
private val eventManager: EventManager by inject()
private val intent by lazy {
Intent(ContextContainer.getContext(), MainActivity::class.java)
Intent(context, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
private var lastEvent: Pair<String?, String?>? = null
Expand All @@ -36,7 +38,7 @@ internal class NotificationManager constructor(private var showNotification: Boo
}

internal fun show() {
ContextContainer.getContext().startActivity(intent)
context.startActivity(intent)
}

internal fun showNotification() {
Expand All @@ -52,7 +54,7 @@ internal class NotificationManager constructor(private var showNotification: Boo
}

internal fun cancelNotification() {
NotificationManagerCompat.from(ContextContainer.getContext()).cancel(NOTIFICATION_ID)
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
}

internal fun reportEvent(
Expand All @@ -62,7 +64,7 @@ internal class NotificationManager constructor(private var showNotification: Boo
source: String?,
isSuccess: Boolean? = null,
) = scope.launch {
analyticsContainer.eventManager.insert(
eventManager.insert(
key = key,
value = value,
platform = platform,
Expand All @@ -79,43 +81,43 @@ internal class NotificationManager constructor(private var showNotification: Boo
private fun updateNotification(key: String?, platform: String?) {
if (showNotification.not()) return
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.getActivity(ContextContainer.getContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE)
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getActivity(ContextContainer.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
val content = ContextContainer.getContext().getString(
val content = context.getString(
R.string.analytics_logger_notification_content,
platform,
key,
)
val builder = NotificationCompat.Builder(
ContextContainer.getContext(),
ContextContainer.getContext().getString(R.string.analytics_logger_notification_channel_id),
context,
context.getString(R.string.analytics_logger_notification_channel_id),
)
.setSmallIcon(R.drawable.analytics_logger_insights)
.setContentTitle(ContextContainer.getContext().getString(R.string.analytics_logger_notification_title))
.setContentTitle(context.getString(R.string.analytics_logger_notification_title))
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setContentIntent(pendingIntent)

if (ActivityCompat.checkSelfPermission(
ContextContainer.getContext(),
context,
Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_GRANTED
) {
NotificationManagerCompat.from(ContextContainer.getContext()).notify(NOTIFICATION_ID, builder.build())
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
}
}

private fun initNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
ContextContainer.getContext().getString(R.string.analytics_logger_notification_channel_id),
ContextContainer.getContext().getString(R.string.analytics_logger_notification_channel_name),
context.getString(R.string.analytics_logger_notification_channel_id),
context.getString(R.string.analytics_logger_notification_channel_name),
NotificationManager.IMPORTANCE_LOW,
)
val notificationManager: NotificationManager =
ContextContainer.getContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.trendyol.android.devtools.analyticslogger.internal.di

import com.trendyol.android.devtools.analyticslogger.AnalyticsLogger
import embedded.koin.core.Koin
import embedded.koin.core.component.KoinComponent

internal interface AnalyticsLoggerKoinComponent : KoinComponent {

override fun getKoin(): Koin {
return AnalyticsLogger.koin
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.trendyol.android.devtools.analyticslogger.internal.di

import android.app.Application
import android.content.Context
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.trendyol.android.devtools.analyticslogger.internal.NotificationManager
import com.trendyol.android.devtools.analyticslogger.internal.data.database.EventDatabase
import com.trendyol.android.devtools.analyticslogger.internal.data.repository.EventRepository
import com.trendyol.android.devtools.analyticslogger.internal.data.repository.EventRepositoryImpl
import com.trendyol.android.devtools.analyticslogger.internal.data.repository.ExcludeKeysRepository
import com.trendyol.android.devtools.analyticslogger.internal.domain.manager.EventManager
import com.trendyol.android.devtools.analyticslogger.internal.domain.manager.EventManagerImpl
import com.trendyol.android.devtools.analyticslogger.internal.domain.usecase.ExcludeKeysUseCase
import com.trendyol.android.devtools.analyticslogger.internal.ui.MainViewModel
import embedded.koin.android.ext.koin.androidContext
import embedded.koin.androidx.viewmodel.dsl.viewModelOf
import embedded.koin.core.module.Module
import embedded.koin.dsl.bind
import embedded.koin.dsl.module

internal fun analyticsLoggerModule(
application: Application,
showNotification: Boolean,
): Module = module {
single<Context> { application.applicationContext }
single { NotificationManager(showNotification) }
single {
androidContext().getSharedPreferences("analytics_logger", Context.MODE_PRIVATE)
}
single { EventDatabase.create(get()) }
single { Moshi.Builder().add(KotlinJsonAdapterFactory()).build() }

single { EventRepositoryImpl(get()) } bind(EventRepository::class)
single { EventManagerImpl(get(), get()) } bind(EventManager::class)

single { ExcludeKeysRepository(get()) }
single { ExcludeKeysUseCase(get()) }

viewModelOf(::MainViewModel)
}

This file was deleted.

This file was deleted.

Loading
Loading