1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.example.tracing.demo.ui.theme
18
19 import android.app.Activity
20 import android.os.Build
21 import androidx.compose.foundation.isSystemInDarkTheme
22 import androidx.compose.material3.MaterialTheme
23 import androidx.compose.material3.darkColorScheme
24 import androidx.compose.material3.dynamicDarkColorScheme
25 import androidx.compose.material3.dynamicLightColorScheme
26 import androidx.compose.material3.lightColorScheme
27 import androidx.compose.runtime.Composable
28 import androidx.compose.runtime.SideEffect
29 import androidx.compose.ui.graphics.Color
30 import androidx.compose.ui.graphics.toArgb
31 import androidx.compose.ui.platform.LocalContext
32 import androidx.compose.ui.platform.LocalView
33 import androidx.core.view.ViewCompat
34
35 private val DarkColorScheme =
36 darkColorScheme(surface = Blue, onSurface = Navy, primary = Navy, onPrimary = Chartreuse)
37
38 private val LightColorScheme =
39 lightColorScheme(surface = Blue, onSurface = Color.White, primary = LightBlue, onPrimary = Navy)
40
41 @Suppress("DEPRECATION")
42 @Composable
BasicsCodelabThemenull43 fun BasicsCodelabTheme(
44 darkTheme: Boolean = isSystemInDarkTheme(),
45 // Dynamic color is available on Android 12+
46 dynamicColor: Boolean = true,
47 content: @Composable () -> Unit,
48 ) {
49 val colorScheme =
50 when {
51 dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
52 val context = LocalContext.current
53 if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
54 }
55 darkTheme -> DarkColorScheme
56 else -> LightColorScheme
57 }
58 val view = LocalView.current
59 if (!view.isInEditMode) {
60 SideEffect {
61 (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
62 ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
63 }
64 }
65
66 MaterialTheme(colorScheme = colorScheme, typography = Typography, content = content)
67 }
68