TP-12345 | Refactor | Publishing | Spotless (#152)
This commit is contained in:
177
app/src/main/java/com/navi/alfred/demo/AlfredDemoApplication.kt
Normal file
177
app/src/main/java/com/navi/alfred/demo/AlfredDemoApplication.kt
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright © 2023-2024 by Navi Technologies Limited
|
||||
* * All rights reserved. Strictly confidential
|
||||
*
|
||||
*/
|
||||
|
||||
package com.navi.alfred.demo
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.IntentFilter
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import com.github.anrwatchdog.ANRWatchDog
|
||||
import com.navi.alfred.AlfredConfig
|
||||
import com.navi.alfred.AlfredManager
|
||||
import com.navi.alfred.AlfredManager.isAlfredRecordingEnabled
|
||||
import com.navi.alfred.demo.activity.MainActivity
|
||||
import com.navi.alfred.demo.activity.SWWActivity
|
||||
import com.navi.alfred.utils.AlfredConstants
|
||||
import com.navi.alfred.utils.AlfredConstants.BROADCAST_ACTION_TYPE
|
||||
import com.navi.alfred.utils.log
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class AlfredDemoApplication : Application(), Application.ActivityLifecycleCallbacks {
|
||||
private var appForegroundCounter: Int = 0
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
registerActivityLifecycleCallbacks(this)
|
||||
|
||||
val alfredConfig =
|
||||
AlfredConfig(
|
||||
"Alfred Demo App",
|
||||
"1020",
|
||||
"4.9.9",
|
||||
"qa",
|
||||
"oMv77fgpBg9NFGom0Psizbf7lbrdBVJz"
|
||||
)
|
||||
AlfredManager.init(alfredConfig, this)
|
||||
getAlfredCruiseInfo()
|
||||
|
||||
// Dumping anr data to click stream
|
||||
ANRWatchDog()
|
||||
.setIgnoreDebugger(true)
|
||||
.setReportMainThreadOnly()
|
||||
.setANRListener {
|
||||
if (it.cause?.stackTrace.isNullOrEmpty()) {
|
||||
return@setANRListener
|
||||
}
|
||||
val className = it.cause?.stackTrace?.get(0)?.className.orEmpty()
|
||||
val anrEventProperties =
|
||||
mutableMapOf(
|
||||
"SCREEN_NAME" to ("Alfred Demo App"),
|
||||
"METHOD_NAME" to it.cause?.stackTrace?.get(0)?.methodName.orEmpty(),
|
||||
"LINE_NUMBER" to it.cause?.stackTrace?.get(0)?.lineNumber.toString(),
|
||||
"APP_IN_FOREGROUND" to isAppInForeground().toString()
|
||||
)
|
||||
anrEventProperties["STACK_TRACE"] = it.cause?.stackTrace?.get(0).toString()
|
||||
AlfredManager.handleAnrEvent(anrEventProperties)
|
||||
}
|
||||
.start()
|
||||
|
||||
// Crash Reporting to backend
|
||||
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
Thread.setDefaultUncaughtExceptionHandler { thread, exception ->
|
||||
if (exception.stackTrace.isNullOrEmpty()) {
|
||||
defaultHandler?.uncaughtException(thread, exception)
|
||||
return@setDefaultUncaughtExceptionHandler
|
||||
}
|
||||
try {
|
||||
val crashEventProperties =
|
||||
mutableMapOf(
|
||||
"SCREEN_NAME" to ("Alfred Demo App"),
|
||||
"METHOD_NAME" to exception.stackTrace[0]?.methodName.orEmpty(),
|
||||
"LINE_NUMBER" to exception.stackTrace[0]?.lineNumber.toString(),
|
||||
"APP_IN_FOREGROUND" to isAppInForeground().toString()
|
||||
)
|
||||
exception.stackTrace[0]?.let { stackTraceElement ->
|
||||
crashEventProperties["STACK_TRACE"] = stackTraceElement.toString()
|
||||
}
|
||||
AlfredManager.handleCrashEvent(crashEventProperties)
|
||||
} finally {
|
||||
defaultHandler?.uncaughtException(thread, exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
|
||||
|
||||
override fun onActivityStarted(activity: Activity) {
|
||||
appForegroundCounter++
|
||||
}
|
||||
|
||||
override fun onActivityResumed(activity: Activity) {
|
||||
if (appForegroundCounter > 0) {
|
||||
startAlfredRecording(activity)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityPaused(activity: Activity) {}
|
||||
|
||||
override fun onActivityStopped(activity: Activity) {
|
||||
appForegroundCounter--
|
||||
if (appForegroundCounter == 0) {
|
||||
AlfredManager.stopRecording()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
|
||||
|
||||
override fun onActivityDestroyed(activity: Activity) {}
|
||||
|
||||
private fun startAlfredRecording(activity: Activity) {
|
||||
if (isAlfredRecordingEnabled()) {
|
||||
val screenName: String?
|
||||
val moduleName: String?
|
||||
when (activity) {
|
||||
is MainActivity -> {
|
||||
screenName = "MainActivity"
|
||||
moduleName = "Demo App"
|
||||
}
|
||||
is SWWActivity -> {
|
||||
screenName = "SWWActivity"
|
||||
moduleName = "Demo App"
|
||||
}
|
||||
else -> {
|
||||
screenName = AlfredConstants.THIRD_PARTY_SCREEN
|
||||
moduleName = AlfredConstants.THIRD_PARTY_MODULE
|
||||
}
|
||||
}
|
||||
try {
|
||||
AlfredManager.startRecording(
|
||||
activity.applicationContext,
|
||||
activity.window.decorView.rootView,
|
||||
screenName,
|
||||
moduleName
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
e.log()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isAppInForeground(): Boolean {
|
||||
return appForegroundCounter >= 1
|
||||
}
|
||||
|
||||
private fun getAlfredCruiseInfo() {
|
||||
Log.d("Alfred", "Alfred - getAlfredCruiseInfo started")
|
||||
val appNetworkReceiver = AlfredNetworkFailureReceiver()
|
||||
val intentFilter = IntentFilter(BROADCAST_ACTION_TYPE)
|
||||
registerReceiver(appNetworkReceiver, intentFilter)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
Log.d(
|
||||
"Alfred",
|
||||
"Alfred - getAlfredCruiseInfo started inside try thread = ${Thread.currentThread().name}"
|
||||
)
|
||||
AlfredManager.getAlfredCruiseInfo(
|
||||
cruiseApiSuccessful = { response ->
|
||||
Log.d(
|
||||
"Alfred",
|
||||
"Alfred - getAlfredCruiseInfo cruiseApiSuccessful h response = $response"
|
||||
)
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.d("Alfred", "Alfred - getAlfredCruiseInfo catch e = $e")
|
||||
e.log()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user