diff --git a/performance/build.gradle.kts b/performance/build.gradle.kts new file mode 100644 index 000000000..7c779ce36 --- /dev/null +++ b/performance/build.gradle.kts @@ -0,0 +1,38 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.compose.compiler) +} + +kotlin { + jvmToolchain(17) +} + +android { + namespace = "com.example.android.performance" + compileSdk = libs.versions.compileSdk.get().toInt() + + defaultConfig { + applicationId = "com.example.android.performance" + minSdk = libs.versions.minSdk.get().toInt() + targetSdk = libs.versions.targetSdk.get().toInt() + versionCode = 1 + versionName = "1.0" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + + buildFeatures { + compose = true + } +} + +dependencies { + implementation(libs.androidx.activity.compose) + implementation(libs.androidx.compose.runtime) + implementation(libs.androidx.lifecycle.runtime.compose) + implementation(libs.kotlinx.coroutines.android) +} \ No newline at end of file diff --git a/performance/src/main/AndroidManifest.xml b/performance/src/main/AndroidManifest.xml new file mode 100644 index 000000000..c9a15e479 --- /dev/null +++ b/performance/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/performance/src/main/java/com/example/performance/vitals/LaunchTime.kt b/performance/src/main/java/com/example/performance/vitals/LaunchTime.kt new file mode 100644 index 000000000..174cc91fc --- /dev/null +++ b/performance/src/main/java/com/example/performance/vitals/LaunchTime.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.performance.vitals + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.runtime.Composable +import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + + // [START android_performance_launch_time_fully_drawn_reporter] + class MainActivity : ComponentActivity() { + + sealed interface ActivityState { + data object LOADING : ActivityState + data object LOADED : ActivityState + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setContent { + var activityState by remember { + mutableStateOf(ActivityState.LOADING as ActivityState) + } + fullyDrawnReporter.addOnReportDrawnListener { + activityState = ActivityState.LOADED + } + ReportFullyDrawnTheme { + when(activityState) { + is ActivityState.LOADING -> { + // Display the loading UI. + } + is ActivityState.LOADED -> { + // Display the full UI. + } + } + } + SideEffect { + fullyDrawnReporter.addReporter() + lifecycleScope.launch(Dispatchers.IO) { + // Perform the background operation. + fullyDrawnReporter.removeReporter() + } + fullyDrawnReporter.addReporter() + lifecycleScope.launch(Dispatchers.IO) { + // Perform the background operation. + fullyDrawnReporter.removeReporter() + } + } + } + } + } + // [END android_performance_launch_time_fully_drawn_reporter] + +@Composable +private fun ReportFullyDrawnTheme(content: @Composable () -> Unit) { + content() +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 31638d155..9c3d0ef53 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -60,5 +60,6 @@ include( ":cars", ":installprompt", ":telecom", - ":room" + ":room", + ":performance" ) \ No newline at end of file