Skip to content

Commit

Permalink
Release 1.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cristidregan committed Feb 22, 2024
1 parent b24d754 commit 3ada72c
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 16 deletions.
2 changes: 1 addition & 1 deletion OmetriaSDK/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val versionName = "1.6.1"
val versionName = "1.6.2"

plugins {
id("com.android.library")
Expand Down
12 changes: 12 additions & 0 deletions OmetriaSDK/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@
android:usesCleartextTraffic="true"
tools:targetApi="m">
<activity android:name=".notification.NotificationInteractionActivity" />

<receiver
android:name=".notification.OmetriaMessagingReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
<meta-data
android:name="com.google.android.gms.cloudmessaging.FINISHED_AFTER_HANDLED"
android:value="true" />
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private const val ARE_NOTIFICATIONS_ENABLED_KEY = "ARE_NOTIFICATIONS_ENABLED_KEY
private const val IS_FIRST_PERMISSION_UPDATE_EVENT_KEY = "IS_FIRST_PERMISSION_UPDATE_EVENT_KEY"
private const val JSON_ARRAY = "[]"
private const val SDK_VERSION_RN_KEY = "SDK_VERSION_RN_KEY"
private const val API_TOKEN_KEY = "API_TOKEN_KEY"

internal class LocalCache(private val context: Context) {

Expand Down Expand Up @@ -165,4 +166,12 @@ internal class LocalCache(private val context: Context) {
fun getSdkVersionRN(): String? {
return localCacheEncryptedPreferences.getString(SDK_VERSION_RN_KEY, null)
}

fun saveApiToken(apiToken: String) {
localCacheEncryptedPreferences.edit().putString(API_TOKEN_KEY, apiToken).apply()
}

fun getApiToken(): String? {
return localCacheEncryptedPreferences.getString(API_TOKEN_KEY, null)
}
}
67 changes: 62 additions & 5 deletions OmetriaSDK/src/main/java/com/android/ometriasdk/core/Ometria.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.android.ometriasdk.core

import android.app.Application
import android.app.Notification.COLOR_DEFAULT
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.webkit.URLUtil
Expand Down Expand Up @@ -39,8 +40,10 @@ import com.android.ometriasdk.notification.NotificationHandler
import com.android.ometriasdk.notification.OMETRIA_CHANNEL_NAME
import com.android.ometriasdk.notification.OmetriaNotification
import com.android.ometriasdk.notification.OmetriaNotificationInteractionHandler
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.RemoteMessage
import java.util.*
import java.util.UUID

/**
* The primary class that allows instantiating and integrating Ometria in your application
Expand Down Expand Up @@ -126,15 +129,49 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
}
application.registerActivityLifecycleCallbacks(it.activityLifecycleHelper)
}

if (it.localCache.getPushToken().isNullOrEmpty()) {
it.retrieveFirebaseToken()
}

it.localCache.saveApiToken(apiToken)
}

/**
* A lightweight initialization of Ometria, only for internal usage.
* Note: Not all SDK functions will be available after this initialization.
* @return An initialized Ometria instance object.
*/
internal fun initializeForInternalUsage(context: Context) =
instance.also {
it.executor = OmetriaThreadPoolExecutor()
it.localCache = LocalCache(context)

val apiToken = it.localCache.getApiToken()
apiToken ?: return@also

it.ometriaConfig = OmetriaConfig(apiToken, context)
it.repository = Repository(
Client(ConnectionFactory(it.ometriaConfig)),
it.localCache,
it.executor
)
it.eventHandler = EventHandler(context, it.repository)
it.isInitialized = true
}

private fun clearOldInstanceIfNeeded() {
if (instance.isInitialized) {
instance.flush()
instance.clear()
clearOldInstance()
}
}

internal fun clearOldInstance() {
instance.flush()
instance.clear()
instance.isInitialized = false
}

/**
* Instances are safe to store, since they're immutable and always the same.
* @return An existing Ometria instance
Expand All @@ -149,6 +186,24 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
}
}

internal fun isReactNativeUsage(): Boolean = repository.getSdkVersionRN() != null

private fun retrieveFirebaseToken() {
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Logger.w(
Constants.Logger.EVENTS,
"Fetching FCM registration token failed."
)
return@OnCompleteListener
}

val token = task.result
Logger.d(Constants.Logger.PUSH_NOTIFICATIONS, "Token - $token")
instance.onNewToken(token)
})
}

private fun shouldGenerateInstallationId(): Boolean = repository.getInstallationId() == null

internal fun generateInstallationId() {
Expand Down Expand Up @@ -195,7 +250,9 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {
}

fun onNewToken(token: String) {
trackPushTokenRefreshedEvent(token)
if (localCache.getPushToken() != token) {
trackPushTokenRefreshedEvent(token)
}
}

private fun trackEvent(type: OmetriaEventType, data: Map<String, Any>? = null) {
Expand Down Expand Up @@ -272,7 +329,7 @@ class Ometria private constructor() : OmetriaNotificationInteractionHandler {

/**
* Track the current app user being deidentified.
* An app user has deidentified themselves. This basically means: a user has logged out.*
* An app user has deidentified themselves. This basically means: a user has logged out.
*/
fun trackProfileDeidentifiedEvent() {
trackEvent(OmetriaEventType.PROFILE_DEIDENTIFIED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.android.ometriasdk.core

import android.app.Application
import android.content.Context

internal class OmetriaConfig(
var apiToken: String,
var application: Application
var application: Context
) {
var enableDebugging: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.android.ometriasdk.core.Constants
import com.android.ometriasdk.core.network.model.OmetriaApiRequest
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar
import java.util.Locale

/**
* @return A hashcode used to compare and group cached events in batches when performing flush.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import com.android.ometriasdk.core.Ometria
import com.android.ometriasdk.core.Repository
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar
import java.util.Locale
import java.util.UUID
import java.util.concurrent.TimeUnit

private const val FLUSH_LIMIT = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.android.ometriasdk.core.event
import android.os.Build
import com.android.ometriasdk.BuildConfig
import com.android.ometriasdk.core.Constants.Common.PLATFORM
import java.util.*
import java.util.Date

internal data class OmetriaEvent(
val eventId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import com.android.ometriasdk.core.Logger
import com.android.ometriasdk.core.network.model.OmetriaApiError
import com.android.ometriasdk.core.network.model.OmetriaApiRequest
import org.json.JSONException
import java.io.*
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.nio.charset.Charset

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.android.ometriasdk.notification

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.android.ometriasdk.core.Ometria
import com.android.ometriasdk.core.Ometria.Companion.clearOldInstance
import com.android.ometriasdk.core.event.OmetriaEventType
import com.android.ometriasdk.core.network.toOmetriaNotificationBody
import com.google.firebase.messaging.RemoteMessage

/**
* Used as Push notification interceptor.
* This will not replace Firebase's own BroadcastReceiver.
* The purpose of using this BroadcastReceive is to log [OmetriaEventType.NOTIFICATION_RECEIVED] event
* when the app is killed for React Native use-case.
*/
class OmetriaMessagingReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
val remoteMessage = RemoteMessage(intent?.extras)

try {
Ometria.instance()
} catch (e: IllegalStateException) {
context?.let {
Ometria.initializeForInternalUsage(context)

if (Ometria.instance().isReactNativeUsage()) {
val ometriaNotificationBody = remoteMessage.toOmetriaNotificationBody()
val ometriaNotificationContext = ometriaNotificationBody?.context

ometriaNotificationContext?.let {
Ometria.instance().trackNotificationReceivedEvent(it)
clearOldInstance()
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To install the library inside **Android Studio**, declare it as dependency in yo

```gradle
dependencies {
implementation 'com.ometria:android-sdk:1.6.1'
implementation 'com.ometria:android-sdk:1.6.2'
}
```

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "com.android.sample"
minSdk = 23
targetSdk = 33
versionCode = 9
versionName = "1.0.9"
versionCode = 11
versionName = "1.2.0"
}
signingConfigs {
getByName("debug") {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/android/sample/SampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.android.sample.presentation.MainActivity
import com.android.sample.presentation.OMETRIA_NOTIFICATION_STRING_EXTRA_KEY

class SampleApp : Application(), OmetriaNotificationInteractionHandler {

companion object {
lateinit var instance: SampleApp
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:layout_margin="16dp"
android:drawableEnd="@drawable/ic_arrow_right_alt_24px"
android:drawablePadding="5dp"
android:text="Update API Toekn"
android:text="@string/update_api_token"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/detailsBTN" />

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<string name="hint_email">Email</string>
<string name="enter_your_api_token">Please enter your API token</string>
<string name="do_it_later">Do it later</string>
<string name="update_api_token">Update API Token</string>
</resources>

0 comments on commit 3ada72c

Please sign in to comment.