From 8dc1eff79ce4f6033d826736985e8e3ce410246e Mon Sep 17 00:00:00 2001 From: Nan Date: Tue, 19 May 2026 14:49:04 -0700 Subject: [PATCH] chore: demo app log listener cleanup * Logs were being duplicated in logcat * remove extraneous logs --- .../notification/HmsMessageServiceAppLevel.kt | 29 ++++---- .../sdktest/application/MainApplication.kt | 21 +++--- .../sdktest/data/network/OneSignalService.kt | 47 ++++++------ .../data/repository/OneSignalRepository.kt | 73 +++++++++---------- .../sdktest/ui/main/MainViewModel.kt | 15 ++-- .../com/onesignal/sdktest/util/LogManager.kt | 56 +++++++------- .../onesignal/sdktest/util/TooltipHelper.kt | 2 +- 7 files changed, 113 insertions(+), 130 deletions(-) diff --git a/examples/demo/app/src/huawei/java/com/onesignal/sdktest/notification/HmsMessageServiceAppLevel.kt b/examples/demo/app/src/huawei/java/com/onesignal/sdktest/notification/HmsMessageServiceAppLevel.kt index eed818c15..f142f7d8f 100644 --- a/examples/demo/app/src/huawei/java/com/onesignal/sdktest/notification/HmsMessageServiceAppLevel.kt +++ b/examples/demo/app/src/huawei/java/com/onesignal/sdktest/notification/HmsMessageServiceAppLevel.kt @@ -5,6 +5,7 @@ import android.util.Log import com.huawei.hms.push.HmsMessageService import com.huawei.hms.push.RemoteMessage import com.onesignal.notifications.bridges.OneSignalHmsEventBridge +import com.onesignal.sdktest.util.LogManager /** * HMS Message Service for handling Huawei Push notifications. @@ -14,10 +15,6 @@ import com.onesignal.notifications.bridges.OneSignalHmsEventBridge */ class HmsMessageServiceAppLevel : HmsMessageService() { - companion object { - private const val TAG = "OneSignalHMS" - } - /** * When an app calls the getToken method to apply for a token from the server, * if the server does not return the token during current method calling, @@ -26,7 +23,7 @@ class HmsMessageServiceAppLevel : HmsMessageService() { * Otherwise, you need to start a new Job for callback processing. */ override fun onNewToken(token: String, bundle: Bundle) { - Log.d(TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token bundle: $bundle") + Log.d(LogManager.TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token bundle: $bundle") // Forward event on to OneSignal SDK OneSignalHmsEventBridge.onNewToken(this, token, bundle) @@ -34,7 +31,7 @@ class HmsMessageServiceAppLevel : HmsMessageService() { @Deprecated("Deprecated in Java") override fun onNewToken(token: String) { - Log.d(TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token") + Log.d(LogManager.TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token") // Forward event on to OneSignal SDK OneSignalHmsEventBridge.onNewToken(this, token) @@ -48,18 +45,18 @@ class HmsMessageServiceAppLevel : HmsMessageService() { * Start a new Job if more time is needed. */ override fun onMessageReceived(message: RemoteMessage) { - Log.d(TAG, "HMS onMessageReceived: $message") - Log.d(TAG, "HMS onMessageReceived.ttl: ${message.ttl}") - Log.d(TAG, "HMS onMessageReceived.data: ${message.data}") + Log.d(LogManager.TAG, "HMS onMessageReceived: $message") + Log.d(LogManager.TAG, "HMS onMessageReceived.ttl: ${message.ttl}") + Log.d(LogManager.TAG, "HMS onMessageReceived.data: ${message.data}") message.notification?.let { notification -> - Log.d(TAG, "HMS onMessageReceived.title: ${notification.title}") - Log.d(TAG, "HMS onMessageReceived.body: ${notification.body}") - Log.d(TAG, "HMS onMessageReceived.icon: ${notification.icon}") - Log.d(TAG, "HMS onMessageReceived.color: ${notification.color}") - Log.d(TAG, "HMS onMessageReceived.channelId: ${notification.channelId}") - Log.d(TAG, "HMS onMessageReceived.imageURL: ${notification.imageUrl}") - Log.d(TAG, "HMS onMessageReceived.tag: ${notification.tag}") + Log.d(LogManager.TAG, "HMS onMessageReceived.title: ${notification.title}") + Log.d(LogManager.TAG, "HMS onMessageReceived.body: ${notification.body}") + Log.d(LogManager.TAG, "HMS onMessageReceived.icon: ${notification.icon}") + Log.d(LogManager.TAG, "HMS onMessageReceived.color: ${notification.color}") + Log.d(LogManager.TAG, "HMS onMessageReceived.channelId: ${notification.channelId}") + Log.d(LogManager.TAG, "HMS onMessageReceived.imageURL: ${notification.imageUrl}") + Log.d(LogManager.TAG, "HMS onMessageReceived.tag: ${notification.tag}") } // Forward event on to OneSignal SDK diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.kt index 7df8fec38..2919085db 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.kt @@ -28,7 +28,6 @@ import com.onesignal.user.state.UserChangedState class MainApplication : MultiDexApplication() { companion object { - private const val TAG = "OneSignalExample" private const val SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION = 2000L } @@ -51,7 +50,7 @@ class MainApplication : MultiDexApplication() { LogLevel.ERROR, LogLevel.FATAL -> AppLogLevel.ERROR LogLevel.NONE -> return@addLogListener } - LogManager.log("SDK", event.entry, level) + LogManager.appendToPanel(event.entry, level) } // Get or set the OneSignal App ID @@ -74,7 +73,7 @@ class MainApplication : MultiDexApplication() { // Initialize OneSignal on main thread (required) // Crash handler + ANR detector are initialized early inside initWithContext OneSignal.initWithContext(this, appId) - LogManager.i(TAG, "OneSignal init completed (crash handler, ANR detector, and logging active)") + LogManager.i("OneSignal init completed (crash handler, ANR detector, and logging active)") // Set up all OneSignal listeners setupOneSignalListeners() @@ -86,37 +85,37 @@ class MainApplication : MultiDexApplication() { private fun setupOneSignalListeners() { OneSignal.InAppMessages.addLifecycleListener(object : IInAppMessageLifecycleListener { override fun onWillDisplay(event: IInAppMessageWillDisplayEvent) { - LogManager.d(TAG, "onWillDisplayInAppMessage") + LogManager.d("onWillDisplayInAppMessage") } override fun onDidDisplay(event: IInAppMessageDidDisplayEvent) { - LogManager.d(TAG, "onDidDisplayInAppMessage") + LogManager.d("onDidDisplayInAppMessage") } override fun onWillDismiss(event: IInAppMessageWillDismissEvent) { - LogManager.d(TAG, "onWillDismissInAppMessage") + LogManager.d("onWillDismissInAppMessage") } override fun onDidDismiss(event: IInAppMessageDidDismissEvent) { - LogManager.d(TAG, "onDidDismissInAppMessage") + LogManager.d("onDidDismissInAppMessage") } }) OneSignal.InAppMessages.addClickListener(object : IInAppMessageClickListener { override fun onClick(event: IInAppMessageClickEvent) { - LogManager.d(TAG, "IInAppMessageClickListener.onClick") + LogManager.d("IInAppMessageClickListener.onClick") } }) OneSignal.Notifications.addClickListener(object : INotificationClickListener { override fun onClick(event: INotificationClickEvent) { - LogManager.d(TAG, "INotificationClickListener.onClick fired with event: $event") + LogManager.d("INotificationClickListener.onClick fired with event: $event") } }) OneSignal.Notifications.addForegroundLifecycleListener(object : INotificationLifecycleListener { override fun onWillDisplay(event: INotificationWillDisplayEvent) { - LogManager.d(TAG, "INotificationLifecycleListener.onWillDisplay fired with event: $event") + LogManager.d("INotificationLifecycleListener.onWillDisplay fired with event: $event") val notification: IDisplayableNotification = event.notification @@ -135,7 +134,7 @@ class MainApplication : MultiDexApplication() { OneSignal.User.addObserver(object : IUserStateObserver { override fun onUserStateChange(state: UserChangedState) { - LogManager.i(TAG, "User state changed: onesignalId=${state.current.onesignalId}, externalId=${state.current.externalId}") + LogManager.i("User state changed: onesignalId=${state.current.onesignalId}, externalId=${state.current.externalId}") } }) diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/data/network/OneSignalService.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/data/network/OneSignalService.kt index 09a65333d..d34254d5f 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/data/network/OneSignalService.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/data/network/OneSignalService.kt @@ -18,7 +18,6 @@ import java.net.URL */ object OneSignalService { - private const val TAG = "OneSignalService" private const val ONESIGNAL_API_URL = "https://onesignal.com/api/v1/notifications" private const val ONESIGNAL_API_BASE_URL = "https://api.onesignal.com" @@ -37,13 +36,13 @@ object OneSignalService { val subscription = OneSignal.User.pushSubscription if (!subscription.optedIn) { - LogManager.w(TAG, "Cannot send notification - user not opted in") + LogManager.w("Cannot send notification - user not opted in") return@withContext false } val subscriptionId = subscription.id if (subscriptionId.isNullOrEmpty()) { - LogManager.w(TAG, "Cannot send notification - no subscription ID") + LogManager.w("Cannot send notification - no subscription ID") return@withContext false } @@ -59,17 +58,17 @@ object OneSignalService { // Add large icon if available type.largeIcon?.let { put("large_icon", it) - LogManager.d(TAG, "Adding large_icon: $it") + LogManager.d("Adding large_icon: $it") } // Add big picture if available type.bigPicture?.let { put("big_picture", it) - LogManager.d(TAG, "Adding big_picture: $it") + LogManager.d("Adding big_picture: $it") } } - LogManager.d(TAG, "Sending notification: ${notificationJson.toString(2)}") - LogManager.d(TAG, "Request URL: $ONESIGNAL_API_URL") + LogManager.d("Sending notification: ${notificationJson.toString(2)}") + LogManager.d("Request URL: $ONESIGNAL_API_URL") val connection = (URL(ONESIGNAL_API_URL).openConnection() as HttpURLConnection).apply { useCaches = false @@ -90,16 +89,16 @@ object OneSignalService { if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_CREATED) { val response = connection.inputStream.bufferedReader().use { it.readText() } - LogManager.d(TAG, "Notification sent successfully: $response") + LogManager.d("Notification sent successfully: $response") return@withContext true } else { val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error" - LogManager.e(TAG, "Failed to send notification (HTTP $responseCode): $errorResponse") - LogManager.e(TAG, "Request body was: ${notificationJson.toString()}") + LogManager.e("Failed to send notification (HTTP $responseCode): $errorResponse") + LogManager.e("Request body was: ${notificationJson.toString()}") return@withContext false } } catch (e: Exception) { - LogManager.e(TAG, "Error sending notification", e) + LogManager.e("Error sending notification", e) return@withContext false } } @@ -111,13 +110,13 @@ object OneSignalService { val subscription = OneSignal.User.pushSubscription if (!subscription.optedIn) { - LogManager.w(TAG, "Cannot send notification - user not opted in") + LogManager.w("Cannot send notification - user not opted in") return@withContext false } val subscriptionId = subscription.id if (subscriptionId.isNullOrEmpty()) { - LogManager.w(TAG, "Cannot send notification - no subscription ID") + LogManager.w("Cannot send notification - no subscription ID") return@withContext false } @@ -150,15 +149,15 @@ object OneSignalService { if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_CREATED) { val response = connection.inputStream.bufferedReader().use { it.readText() } - LogManager.d(TAG, "Custom notification sent successfully: $response") + LogManager.d("Custom notification sent successfully: $response") return@withContext true } else { val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error" - LogManager.e(TAG, "Failed to send custom notification (HTTP $responseCode): $errorResponse") + LogManager.e("Failed to send custom notification (HTTP $responseCode): $errorResponse") return@withContext false } } catch (e: Exception) { - LogManager.e(TAG, "Error sending custom notification", e) + LogManager.e("Error sending custom notification", e) return@withContext false } } @@ -173,18 +172,18 @@ object OneSignalService { */ suspend fun fetchUser(aliasLabel: String, aliasValue: String, jwt: String? = null): UserData? = withContext(Dispatchers.IO) { if (aliasValue.isEmpty()) { - LogManager.w(TAG, "Cannot fetch user - aliasValue is empty") + LogManager.w("Cannot fetch user - aliasValue is empty") return@withContext null } if (appId.isEmpty()) { - LogManager.w(TAG, "Cannot fetch user - appId not set") + LogManager.w("Cannot fetch user - appId not set") return@withContext null } try { val url = "$ONESIGNAL_API_BASE_URL/apps/$appId/users/by/$aliasLabel/$aliasValue" - LogManager.d(TAG, "Fetching user data from: $url") + LogManager.d("Fetching user data from: $url") val connection = (URL(url).openConnection() as HttpURLConnection).apply { useCaches = false @@ -201,22 +200,22 @@ object OneSignalService { if (responseCode == HttpURLConnection.HTTP_OK) { val response = connection.inputStream.bufferedReader().use { it.readText() } - LogManager.d(TAG, "User data fetched successfully, parsing response...") + LogManager.d("User data fetched successfully, parsing response...") try { val userData = parseUserResponse(response) - LogManager.d(TAG, "Parsed user data: aliases=${userData.aliases.size}, tags=${userData.tags.size}, emails=${userData.emails.size}, sms=${userData.smsNumbers.size}") + LogManager.d("Parsed user data: aliases=${userData.aliases.size}, tags=${userData.tags.size}, emails=${userData.emails.size}, sms=${userData.smsNumbers.size}") return@withContext userData } catch (e: Exception) { - LogManager.e(TAG, "Error parsing user response", e) + LogManager.e("Error parsing user response", e) return@withContext null } } else { val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error" - LogManager.e(TAG, "Failed to fetch user (HTTP $responseCode): $errorResponse") + LogManager.e("Failed to fetch user (HTTP $responseCode): $errorResponse") return@withContext null } } catch (e: Exception) { - LogManager.e(TAG, "Error fetching user", e) + LogManager.e("Error fetching user", e) return@withContext null } } diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/data/repository/OneSignalRepository.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/data/repository/OneSignalRepository.kt index 774b03fd9..c448365a0 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/data/repository/OneSignalRepository.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/data/repository/OneSignalRepository.kt @@ -5,6 +5,7 @@ import com.onesignal.OneSignal import com.onesignal.sdktest.data.model.NotificationType import com.onesignal.sdktest.data.network.OneSignalService import com.onesignal.sdktest.data.network.UserData +import com.onesignal.sdktest.util.LogManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -14,45 +15,41 @@ import kotlinx.coroutines.withContext */ class OneSignalRepository { - companion object { - private const val TAG = "OneSignalRepository" - } - // User operations suspend fun loginUser(externalUserId: String, jwtToken: String? = null) = withContext(Dispatchers.IO) { - Log.d(TAG, "Logging in user with externalUserId: $externalUserId, jwt: ${if (jwtToken != null) "provided" else "none"}") + Log.d(LogManager.TAG, "Logging in user with externalUserId: $externalUserId, jwt: ${if (jwtToken != null) "provided" else "none"}") OneSignal.login(externalUserId, jwtToken) - Log.d(TAG, "Logged in user with onesignalId: ${OneSignal.User.onesignalId}") + Log.d(LogManager.TAG, "Logged in user with onesignalId: ${OneSignal.User.onesignalId}") } suspend fun updateUserJwt(externalUserId: String, jwtToken: String) = withContext(Dispatchers.IO) { - Log.d(TAG, "Updating JWT for externalUserId: $externalUserId") + Log.d(LogManager.TAG, "Updating JWT for externalUserId: $externalUserId") OneSignal.updateUserJwt(externalUserId, jwtToken) } suspend fun logoutUser() = withContext(Dispatchers.IO) { - Log.d(TAG, "Logging out user") + Log.d(LogManager.TAG, "Logging out user") OneSignal.logout() } // Alias operations fun addAlias(label: String, id: String) { - Log.d(TAG, "Adding alias: $label -> $id") + Log.d(LogManager.TAG, "Adding alias: $label -> $id") OneSignal.User.addAlias(label, id) } fun addAliases(aliases: Map) { - Log.d(TAG, "Adding aliases: $aliases") + Log.d(LogManager.TAG, "Adding aliases: $aliases") OneSignal.User.addAliases(aliases) } fun removeAlias(label: String) { - Log.d(TAG, "Removing alias: $label") + Log.d(LogManager.TAG, "Removing alias: $label") OneSignal.User.removeAlias(label) } fun removeAliases(labels: Collection) { - Log.d(TAG, "Removing aliases: $labels") + Log.d(LogManager.TAG, "Removing aliases: $labels") if (labels.isNotEmpty()) { OneSignal.User.removeAliases(labels) } @@ -60,44 +57,44 @@ class OneSignalRepository { // Email operations fun addEmail(email: String) { - Log.d(TAG, "Adding email: $email") + Log.d(LogManager.TAG, "Adding email: $email") OneSignal.User.addEmail(email) } fun removeEmail(email: String) { - Log.d(TAG, "Removing email: $email") + Log.d(LogManager.TAG, "Removing email: $email") OneSignal.User.removeEmail(email) } // SMS operations fun addSms(smsNumber: String) { - Log.d(TAG, "Adding SMS: $smsNumber") + Log.d(LogManager.TAG, "Adding SMS: $smsNumber") OneSignal.User.addSms(smsNumber) } fun removeSms(smsNumber: String) { - Log.d(TAG, "Removing SMS: $smsNumber") + Log.d(LogManager.TAG, "Removing SMS: $smsNumber") OneSignal.User.removeSms(smsNumber) } // Tag operations fun addTag(key: String, value: String) { - Log.d(TAG, "Adding tag: $key -> $value") + Log.d(LogManager.TAG, "Adding tag: $key -> $value") OneSignal.User.addTag(key, value) } fun addTags(tags: Map) { - Log.d(TAG, "Adding tags: $tags") + Log.d(LogManager.TAG, "Adding tags: $tags") OneSignal.User.addTags(tags) } fun removeTag(key: String) { - Log.d(TAG, "Removing tag: $key") + Log.d(LogManager.TAG, "Removing tag: $key") OneSignal.User.removeTag(key) } fun removeTags(keys: Collection) { - Log.d(TAG, "Removing tags: $keys") + Log.d(LogManager.TAG, "Removing tags: $keys") if (keys.isNotEmpty()) { OneSignal.User.removeTags(keys) } @@ -109,22 +106,22 @@ class OneSignalRepository { // Trigger operations fun addTrigger(key: String, value: String) { - Log.d(TAG, "Adding trigger: $key -> $value") + Log.d(LogManager.TAG, "Adding trigger: $key -> $value") OneSignal.InAppMessages.addTrigger(key, value) } fun addTriggers(triggers: Map) { - Log.d(TAG, "Adding triggers: $triggers") + Log.d(LogManager.TAG, "Adding triggers: $triggers") OneSignal.InAppMessages.addTriggers(triggers) } fun removeTrigger(key: String) { - Log.d(TAG, "Removing trigger: $key") + Log.d(LogManager.TAG, "Removing trigger: $key") OneSignal.InAppMessages.removeTrigger(key) } fun clearTriggers(keys: Collection) { - Log.d(TAG, "Clearing triggers: $keys") + Log.d(LogManager.TAG, "Clearing triggers: $keys") if (keys.isNotEmpty()) { OneSignal.InAppMessages.removeTriggers(keys) } @@ -132,23 +129,23 @@ class OneSignalRepository { // Outcome operations fun sendOutcome(name: String) { - Log.d(TAG, "Sending outcome: $name") + Log.d(LogManager.TAG, "Sending outcome: $name") OneSignal.Session.addOutcome(name) } fun sendUniqueOutcome(name: String) { - Log.d(TAG, "Sending unique outcome: $name") + Log.d(LogManager.TAG, "Sending unique outcome: $name") OneSignal.Session.addUniqueOutcome(name) } fun sendOutcomeWithValue(name: String, value: Float) { - Log.d(TAG, "Sending outcome with value: $name -> $value") + Log.d(LogManager.TAG, "Sending outcome with value: $name -> $value") OneSignal.Session.addOutcomeWithValue(name, value) } // Track Event fun trackEvent(name: String, properties: Map?) { - Log.d(TAG, "Tracking event: $name with properties: $properties") + Log.d(LogManager.TAG, "Tracking event: $name with properties: $properties") OneSignal.User.trackEvent(name, properties) } @@ -162,7 +159,7 @@ class OneSignalRepository { } fun setPushEnabled(enabled: Boolean) { - Log.d(TAG, "Setting push enabled: $enabled") + Log.d(LogManager.TAG, "Setting push enabled: $enabled") if (enabled) { OneSignal.User.pushSubscription.optIn() } else { @@ -176,7 +173,7 @@ class OneSignalRepository { } fun setInAppMessagesPaused(paused: Boolean) { - Log.d(TAG, "Setting in-app messages paused: $paused") + Log.d(LogManager.TAG, "Setting in-app messages paused: $paused") OneSignal.InAppMessages.paused = paused } @@ -186,18 +183,18 @@ class OneSignalRepository { } fun setLocationShared(shared: Boolean) { - Log.d(TAG, "Setting location shared: $shared") + Log.d(LogManager.TAG, "Setting location shared: $shared") OneSignal.Location.isShared = shared } suspend fun promptLocation() = withContext(Dispatchers.IO) { - Log.d(TAG, "Prompting for location permission") + Log.d(LogManager.TAG, "Prompting for location permission") OneSignal.Location.requestPermission() } // Notifications suspend fun promptPushPermission() = withContext(Dispatchers.IO) { - Log.d(TAG, "Prompting for push permission") + Log.d(LogManager.TAG, "Prompting for push permission") OneSignal.Notifications.requestPermission(true) } @@ -207,18 +204,18 @@ class OneSignalRepository { // Send notifications suspend fun sendNotification(type: NotificationType): Boolean { - Log.d(TAG, "Sending notification: ${type.title}") + Log.d(LogManager.TAG, "Sending notification: ${type.title}") return OneSignalService.sendNotification(type) } suspend fun sendCustomNotification(title: String, body: String): Boolean { - Log.d(TAG, "Sending custom notification: $title") + Log.d(LogManager.TAG, "Sending custom notification: $title") return OneSignalService.sendCustomNotification(title, body) } // Privacy consent fun setConsentRequired(required: Boolean) { - Log.d(TAG, "Setting consent required: $required") + Log.d(LogManager.TAG, "Setting consent required: $required") OneSignal.consentRequired = required } @@ -227,7 +224,7 @@ class OneSignalRepository { } fun setPrivacyConsent(granted: Boolean) { - Log.d(TAG, "Setting privacy consent: $granted") + Log.d(LogManager.TAG, "Setting privacy consent: $granted") OneSignal.consentGiven = granted } @@ -242,7 +239,7 @@ class OneSignalRepository { // Fetch user data from API suspend fun fetchUser(aliasLabel: String, aliasValue: String, jwt: String? = null): UserData? = withContext(Dispatchers.IO) { - Log.d(TAG, "Fetching user data by $aliasLabel: $aliasValue") + Log.d(LogManager.TAG, "Fetching user data by $aliasLabel: $aliasValue") OneSignalService.fetchUser(aliasLabel, aliasValue, jwt) } } diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/ui/main/MainViewModel.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/ui/main/MainViewModel.kt index 46a7d4f50..a08561984 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/ui/main/MainViewModel.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/ui/main/MainViewModel.kt @@ -100,14 +100,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I private val triggersList = mutableListOf>() init { - LogManager.info("App initialized") + LogManager.i("App initialized") loadInitialState() OneSignal.User.pushSubscription.addObserver(this) OneSignal.Notifications.addPermissionObserver(this) OneSignal.User.addObserver(this) OneSignal.addUserJwtInvalidatedListener(this) - android.util.Log.d("MainViewModel", "init: observers registered, current onesignalId=${OneSignal.User.onesignalId}") - LogManager.debug("OneSignal ID: ${OneSignal.User.onesignalId ?: "not set"}") + LogManager.d("OneSignal ID: ${OneSignal.User.onesignalId ?: "not set"}") } // IPermissionObserver @@ -117,7 +116,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I // IUserStateObserver - called when user changes (login/logout) override fun onUserStateChange(state: UserChangedState) { - android.util.Log.d("MainViewModel", "onUserStateChange fired: ${state.current.onesignalId}") viewModelScope.launch(Dispatchers.Main) { loadExistingAliases() loadExistingTags() @@ -209,7 +207,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I _isLoading.value = false } } catch (e: Exception) { - android.util.Log.e("MainViewModel", "Error fetching user data", e) withContext(Dispatchers.Main) { logError("Failed to fetch user data: ${e.message}") _isLoading.value = false @@ -648,14 +645,14 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I private fun showToast(message: String) { _toastMessage.value = message - LogManager.info(message) + LogManager.i(message) } fun clearToast() { _toastMessage.value = null } // Logging utilities - private fun logError(message: String) = LogManager.error(message) - private fun logDebug(message: String) = LogManager.debug(message) + private fun logError(message: String) = LogManager.e(message) + private fun logDebug(message: String) = LogManager.d(message) override fun onPushSubscriptionChange(state: PushSubscriptionChangedState) { _pushSubscriptionId.postValue(state.current.id) @@ -663,7 +660,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I } override fun onUserJwtInvalidated(event: UserJwtInvalidatedEvent) { - LogManager.warn("JWT invalidated for externalId: ${event.externalId}") + LogManager.w("JWT invalidated for externalId: ${event.externalId}") showToast("JWT invalidated for: ${event.externalId}") } diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/util/LogManager.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/util/LogManager.kt index ce6a6f9d6..8b7bf349e 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/util/LogManager.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/util/LogManager.kt @@ -14,7 +14,7 @@ import java.util.Locale */ object LogManager { - private const val TAG = "OneSignalDemo" + const val TAG = "OneSignalDemo" private const val MAX_LOGS = 100 private val _logs = mutableStateListOf() @@ -26,16 +26,22 @@ object LogManager { /** * Log with custom tag (used by SDK log listener) */ - fun log(tag: String, message: String, level: LogLevel) { + fun log(message: String, level: LogLevel) { // Forward to Android logcat (can happen on any thread) when (level) { - LogLevel.DEBUG -> Log.d(tag, message) - LogLevel.INFO -> Log.i(tag, message) - LogLevel.WARN -> Log.w(tag, message) - LogLevel.ERROR -> Log.e(tag, message) + LogLevel.DEBUG -> Log.d(TAG, message) + LogLevel.INFO -> Log.i(TAG, message) + LogLevel.WARN -> Log.w(TAG, message) + LogLevel.ERROR -> Log.e(TAG, message) } - - // Create entry with current timestamp + appendToPanel(message, level) + } + + /** + * The SDK already writes to logcat with its own "OneSignal" tag, + * so the in-app log listener forwards UI-only to avoid duplicate logcat lines. + */ + fun appendToPanel(message: String, level: LogLevel) { val entry = LogEntry( timestamp = timeFormat.format(Date()), message = message, @@ -60,34 +66,22 @@ object LogManager { } // Convenience methods with default tag - fun d(message: String) = log(TAG, message, LogLevel.DEBUG) - fun i(message: String) = log(TAG, message, LogLevel.INFO) - fun w(message: String) = log(TAG, message, LogLevel.WARN) - fun e(message: String) = log(TAG, message, LogLevel.ERROR) - - // Methods with custom tag (mimics android.util.Log API) - fun d(tag: String, message: String) = log(tag, message, LogLevel.DEBUG) - fun i(tag: String, message: String) = log(tag, message, LogLevel.INFO) - fun w(tag: String, message: String) = log(tag, message, LogLevel.WARN) - fun e(tag: String, message: String) = log(tag, message, LogLevel.ERROR) + fun d(message: String) = log(message, LogLevel.DEBUG) + fun i(message: String) = log(message, LogLevel.INFO) + fun w(message: String) = log(message, LogLevel.WARN) + fun e(message: String) = log(message, LogLevel.ERROR) // Methods with throwable (mimics android.util.Log API) - fun e(tag: String, message: String, throwable: Throwable) { - Log.e(tag, message, throwable) - log(tag, "$message: ${throwable.message}", LogLevel.ERROR) + fun e(message: String, throwable: Throwable) { + Log.e(TAG, message, throwable) + log("$message: ${throwable.message}", LogLevel.ERROR) } - fun w(tag: String, message: String, throwable: Throwable) { - Log.w(tag, message, throwable) - log(tag, "$message: ${throwable.message}", LogLevel.WARN) + fun w(message: String, throwable: Throwable) { + Log.w(TAG, message, throwable) + log("$message: ${throwable.message}", LogLevel.WARN) } - - // Legacy methods for compatibility - fun info(message: String) = i(message) - fun debug(message: String) = d(message) - fun warn(message: String) = w(message) - fun error(message: String) = e(message) - + fun clear() { if (Looper.myLooper() == Looper.getMainLooper()) { _logs.clear() diff --git a/examples/demo/app/src/main/java/com/onesignal/sdktest/util/TooltipHelper.kt b/examples/demo/app/src/main/java/com/onesignal/sdktest/util/TooltipHelper.kt index 0a8c65a3a..e59fec8ec 100644 --- a/examples/demo/app/src/main/java/com/onesignal/sdktest/util/TooltipHelper.kt +++ b/examples/demo/app/src/main/java/com/onesignal/sdktest/util/TooltipHelper.kt @@ -72,7 +72,7 @@ object TooltipHelper { } } catch (e: Exception) { // Tooltips are non-critical; log and continue - android.util.Log.w("TooltipHelper", "Failed to fetch tooltip content: ${e.message}") + android.util.Log.w(LogManager.TAG, "Failed to fetch tooltip content: ${e.message}") } } }