From 916f4c87fd299dddea054594abe8edb4daa9f774 Mon Sep 17 00:00:00 2001 From: jobykorahgeorge Date: Thu, 1 Aug 2024 23:34:44 +0530 Subject: [PATCH] App StartTime with Custom Event --- .../app/src/main/AndroidManifest.xml | 5 +++ .../java/com/ibm/instashop/MainActivity.kt | 5 +++ .../starttimehandler/LaunchTimeTracker.kt | 29 ++++++++++++++++ .../starttimehandler/StartupInitializer.kt | 33 +++++++++++++++++++ .../src/main/java/com/ibm/instashop/di/App.kt | 1 - 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/LaunchTimeTracker.kt create mode 100644 android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/StartupInitializer.kt diff --git a/android-compose-robotshop/app/src/main/AndroidManifest.xml b/android-compose-robotshop/app/src/main/AndroidManifest.xml index cc959a1..eb0a5ae 100644 --- a/android-compose-robotshop/app/src/main/AndroidManifest.xml +++ b/android-compose-robotshop/app/src/main/AndroidManifest.xml @@ -26,6 +26,11 @@ + \ No newline at end of file diff --git a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/MainActivity.kt b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/MainActivity.kt index 014523e..2b98378 100644 --- a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/MainActivity.kt +++ b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/MainActivity.kt @@ -8,9 +8,11 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.navigation.compose.rememberNavController +import com.ibm.instashop.common.starttimehandler.LaunchTimeTracker import com.ibm.instashop.graphs.base_grapg.RootNavigationGraph import com.ibm.instashop.ui.theme.RobotShopTheme import dagger.hilt.android.AndroidEntryPoint @@ -38,4 +40,7 @@ private fun ShowScreen(context: Context) { val navHostController = rememberNavController() RootNavigationGraph(navHostController = navHostController, context = context) + LaunchedEffect(Unit){ + LaunchTimeTracker.stopTimer() + } } \ No newline at end of file diff --git a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/LaunchTimeTracker.kt b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/LaunchTimeTracker.kt new file mode 100644 index 0000000..e23e56d --- /dev/null +++ b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/LaunchTimeTracker.kt @@ -0,0 +1,29 @@ +package com.ibm.instashop.common.starttimehandler + +import com.instana.android.CustomEvent +import com.instana.android.Instana + + +object LaunchTimeTracker{ + + private var initialTimeInNanos: Long = 0 + var launchTimeInNanos: Long = 0 + private var doneTracking = false + fun startTimer(){ + this.initialTimeInNanos = System.nanoTime() + } + + fun stopTimer():Boolean{ + if(doneTracking){ + return false + } + launchTimeInNanos = System.nanoTime() - this.initialTimeInNanos; + Instana.reportEvent(CustomEvent("APP_START_TIMINGS").apply { + meta = mapOf( + "APP_START_LOAD_TIME_DURATION" to (launchTimeInNanos/1_000_000).toString()+"ms", + ) + }) + doneTracking = true + return true + } +} \ No newline at end of file diff --git a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/StartupInitializer.kt b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/StartupInitializer.kt new file mode 100644 index 0000000..12ae8cf --- /dev/null +++ b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/common/starttimehandler/StartupInitializer.kt @@ -0,0 +1,33 @@ +package com.ibm.instashop.common.starttimehandler + +import android.content.ContentProvider +import android.content.ContentValues +import android.database.Cursor +import android.net.Uri + +class StartupInitializer : ContentProvider() { + override fun onCreate(): Boolean { + LaunchTimeTracker.startTimer() + return true + } + + override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? { + return null + } + + override fun getType(uri: Uri): String? { + return null + } + + override fun insert(uri: Uri, values: ContentValues?): Uri? { + return null + } + + override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int { + return 0 + } + + override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int { + return 0 + } +} \ No newline at end of file diff --git a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/di/App.kt b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/di/App.kt index 63c61d8..5698661 100644 --- a/android-compose-robotshop/app/src/main/java/com/ibm/instashop/di/App.kt +++ b/android-compose-robotshop/app/src/main/java/com/ibm/instashop/di/App.kt @@ -23,7 +23,6 @@ class App : Application(){ if(dataManager.getString(INSTANA_KEY).isNotEmpty()&&dataManager.getString(INSTANA_URL).isNotEmpty()){ setupInstana(dataManager.getString(INSTANA_KEY),dataManager.getString(INSTANA_URL)) } - setupInstana("dataManager.getString(INSTANA_KEY)","dataManager.getString(INSTANA_URL)") if(dataManager.getString(Constants.CURRENT_API_VERSION).isNotEmpty()){ CurrentVersionInUse = dataManager.getString(Constants.CURRENT_API_VERSION) }