Skip to content

Commit

Permalink
Merge pull request #93 from rees46/feat/add-gaid-support
Browse files Browse the repository at this point in the history
feat: add GAID fetching and saving to google_ad_id
  • Loading branch information
TorinAsakura authored Jan 10, 2025
2 parents 5d68371 + 7e4c9ba commit d3cd1c3
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 12 deletions.
2 changes: 2 additions & 0 deletions personalization-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ dependencies {
implementation("androidx.test:rules:1.6.1")
implementation("androidx.test.ext:junit:1.2.1")

implementation("com.google.android.gms:play-services-ads-identifier:17.0.0")

kapt 'com.google.dagger:dagger-compiler:2.48'
}

Expand Down
1 change: 1 addition & 0 deletions personalization-sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="com.google.android.gms.permission.AD_ID" />
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
17 changes: 16 additions & 1 deletion personalization-sdk/src/main/kotlin/com/personalization/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCas
import com.personalization.sdk.domain.usecases.recommendation.SetRecommendedByUseCase
import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase
import com.personalization.sdk.domain.usecases.userSettings.InitUserSettingsUseCase
import com.personalization.sdk.domain.usecases.userSettings.InitializeAdvertisingIdUseCase
import com.personalization.stories.StoriesManager
import com.personalization.stories.views.StoriesView
import com.personalization.utils.DomainFormattingUtils.formatApiDomain
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Locale
import javax.inject.Inject
import org.json.JSONException
Expand All @@ -46,6 +50,9 @@ open class SDK {
private var onMessageListener: OnMessageListener? = null
private var search: Search = Search(JSONObject())

@Inject
internal lateinit var initializeAdvertisingIdUseCase: InitializeAdvertisingIdUseCase

@Inject
lateinit var notificationHandler: NotificationHandler

Expand Down Expand Up @@ -103,7 +110,6 @@ open class SDK {
@Inject
lateinit var notificationHelper: NotificationHelper


/**
* @param shopId Shop key
*/
Expand Down Expand Up @@ -155,6 +161,10 @@ open class SDK {
autoSendPushToken = autoSendPushToken,
needReInitialization = needReInitialization
)

CoroutineScope(Dispatchers.IO).launch {
initializeAdvertisingIdUseCase.invoke()
}
}

private fun initNetworkUseCase(url: String?) {
Expand Down Expand Up @@ -222,6 +232,11 @@ open class SDK {
*/
fun getSid(): String = getUserSettingsValueUseCase.getSid()

/**
* Return the Advertising ID
*/
fun getAdvertisingId(): String = getUserSettingsValueUseCase.getAdvertisingId()

/**
* Returns the session ID
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.personalization.sdk.data.di

import com.personalization.sdk.data.repositories.advertising.AdvertisingRepositoryImpl
import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl
import com.personalization.sdk.data.repositories.notification.NotificationRepositoryImpl
import com.personalization.sdk.data.repositories.preferences.PreferencesRepositoryImpl
import com.personalization.sdk.data.repositories.recommendation.RecommendationRepositoryImpl
import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl
import com.personalization.sdk.domain.repositories.AdvertisingRepository
import com.personalization.sdk.domain.repositories.NetworkRepository
import com.personalization.sdk.domain.repositories.NotificationRepository
import com.personalization.sdk.domain.repositories.PreferencesRepository
Expand Down Expand Up @@ -34,4 +36,7 @@ abstract class RepositoriesModule {
@Binds
abstract fun bindNotificationRepository(impl: NotificationRepositoryImpl): NotificationRepository

@Binds
abstract fun bindAdvertisingRepository(impl: AdvertisingRepositoryImpl): AdvertisingRepository

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.personalization.sdk.data.repositories.advertising

import android.content.Context
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.personalization.sdk.domain.repositories.AdvertisingRepository
import java.util.UUID
import javax.inject.Inject

class AdvertisingRepositoryImpl @Inject constructor(
private val context: Context
) : AdvertisingRepository {
override suspend fun fetchAdvertisingId(): String =
try {
AdvertisingIdClient.getAdvertisingIdInfo(context).id
} catch (e: Exception) {
generateDefaultAdvertisingId()
}

private fun generateDefaultAdvertisingId(): String {
return UUID(0L, 0L).toString()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import org.json.JSONObject

private const val DEFAULT_DID = ""
private const val DEFAULT_SID = ""
private const val DEFAULT_ADVERTISING_ID = "00000000-0000-0000-0000-000000000000"
private const val DEFAULT_SID_LAST_ACT_TIME = 0L

private const val DID_KEY = "did"
private const val SID_KEY = "sid"
private const val ADVERTISING_ID_KEY = "google_ad_id"
private const val SID_LAST_ACT_KEY = "sid_last_act"

class UserSettingsDataSource @AssistedInject constructor(
private val preferencesDataSource: PreferencesDataSource,
@Assisted("shopId") private val shopId: String,
Expand Down Expand Up @@ -52,19 +62,15 @@ class UserSettingsDataSource @AssistedInject constructor(
internal fun saveDid(value: String) = preferencesDataSource.saveValue(DID_KEY, value)
internal fun removeDid() = preferencesDataSource.removeValue(DID_KEY)

internal fun saveAdvertisingId(value: String) =
preferencesDataSource.saveValue(ADVERTISING_ID_KEY, value)

internal fun getAdvertisingId() =
preferencesDataSource.getValue(ADVERTISING_ID_KEY, DEFAULT_ADVERTISING_ID)

internal fun getIsInitialized(): Boolean = isInitialized
internal fun setIsInitialized(value: Boolean) {
isInitialized = value
}

companion object {

private const val DEFAULT_DID = ""
private const val DEFAULT_SID = ""
private const val DEFAULT_SID_LAST_ACT_TIME = 0L

private const val DID_KEY = "did"
private const val SID_KEY = "sid"
private const val SID_LAST_ACT_KEY = "sid_last_act"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.personalization.sdk.data.repositories.userSettings
import com.personalization.sdk.data.di.DataSourcesModule
import com.personalization.sdk.domain.models.NotificationSource
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import javax.inject.Inject
import org.json.JSONObject
import javax.inject.Inject

class UserSettingsRepositoryImpl @Inject constructor(
private val userSettingsDataSourceFactory: DataSourcesModule.UserSettingsDataSourceFactory,
Expand Down Expand Up @@ -59,6 +59,13 @@ class UserSettingsRepositoryImpl @Inject constructor(
userSettingsDataSource.setIsInitialized(value)
}

override fun saveAdvertisingId(value: String) {
userSettingsDataSource.saveAdvertisingId(value)
}

override fun getAdvertisingId(): String =
userSettingsDataSource.getAdvertisingId()

override fun addParams(
params: JSONObject,
notificationSource: NotificationSource?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.personalization.sdk.domain.repositories

interface AdvertisingRepository {
suspend fun fetchAdvertisingId(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ interface UserSettingsRepository {
fun getIsInitialized(): Boolean
fun updateIsInitialized(value: Boolean)

fun saveAdvertisingId(value: String)
fun getAdvertisingId(): String

fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class GetUserSettingsValueUseCase @Inject constructor(
fun getSidLastActTime(): Long = userSettingsRepository.getSidLastActTime()

fun getIsInitialized(): Boolean = userSettingsRepository.getIsInitialized()

fun getAdvertisingId(): String = userSettingsRepository.getAdvertisingId()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.personalization.sdk.domain.usecases.userSettings

import com.personalization.sdk.domain.repositories.AdvertisingRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import javax.inject.Inject

internal class InitializeAdvertisingIdUseCase @Inject constructor(
private val advertisingRepository: AdvertisingRepository,
private val userSettingsRepository: UserSettingsRepository
) {
suspend fun invoke() {
val advertisingId = advertisingRepository.fetchAdvertisingId()
userSettingsRepository.saveAdvertisingId(advertisingId)
}
}

0 comments on commit d3cd1c3

Please sign in to comment.