|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * https://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.wear.snippets.alwayson |
| 18 | + |
| 19 | +import android.Manifest |
| 20 | +import android.content.pm.PackageManager |
| 21 | +import android.os.Build |
| 22 | +import android.os.Bundle |
| 23 | +import android.os.SystemClock |
| 24 | +import android.util.Log |
| 25 | +import androidx.activity.ComponentActivity |
| 26 | +import androidx.activity.compose.setContent |
| 27 | +import androidx.activity.result.contract.ActivityResultContracts |
| 28 | +import androidx.compose.foundation.layout.Box |
| 29 | +import androidx.compose.foundation.layout.Column |
| 30 | +import androidx.compose.foundation.layout.PaddingValues |
| 31 | +import androidx.compose.foundation.layout.Spacer |
| 32 | +import androidx.compose.foundation.layout.fillMaxSize |
| 33 | +import androidx.compose.foundation.layout.height |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.runtime.getValue |
| 36 | +import androidx.compose.runtime.mutableStateOf |
| 37 | +import androidx.compose.runtime.produceState |
| 38 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 39 | +import androidx.compose.runtime.setValue |
| 40 | +import androidx.compose.ui.Alignment |
| 41 | +import androidx.compose.ui.Modifier |
| 42 | +import androidx.compose.ui.platform.LocalContext |
| 43 | +import androidx.compose.ui.tooling.preview.Preview |
| 44 | +import androidx.compose.ui.unit.dp |
| 45 | +import androidx.core.content.ContextCompat |
| 46 | +import androidx.wear.compose.material3.MaterialTheme |
| 47 | +import androidx.wear.compose.material3.SwitchButton |
| 48 | +import androidx.wear.compose.material3.Text |
| 49 | +import androidx.wear.compose.material3.dynamicColorScheme |
| 50 | +import androidx.wear.tooling.preview.devices.WearDevices |
| 51 | +import com.google.android.horologist.compose.ambient.AmbientAware |
| 52 | +import com.google.android.horologist.compose.ambient.AmbientState |
| 53 | +import kotlinx.coroutines.delay |
| 54 | + |
| 55 | +private const val TAG = "AlwaysOnActivity" |
| 56 | + |
| 57 | +class AlwaysOnActivity : ComponentActivity() { |
| 58 | + private val requestPermissionLauncher = |
| 59 | + registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> |
| 60 | + if (isGranted) { |
| 61 | + Log.d(TAG, "POST_NOTIFICATIONS permission granted") |
| 62 | + } else { |
| 63 | + Log.w(TAG, "POST_NOTIFICATIONS permission denied") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 68 | + super.onCreate(savedInstanceState) |
| 69 | + Log.d(TAG, "onCreate: Activity created") |
| 70 | + |
| 71 | + setTheme(android.R.style.Theme_DeviceDefault) |
| 72 | + |
| 73 | + // Check and request notification permission |
| 74 | + checkAndRequestNotificationPermission() |
| 75 | + |
| 76 | + setContent { WearApp() } |
| 77 | + } |
| 78 | + |
| 79 | + private fun checkAndRequestNotificationPermission() { |
| 80 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 81 | + when { |
| 82 | + ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == |
| 83 | + PackageManager.PERMISSION_GRANTED -> { |
| 84 | + Log.d(TAG, "POST_NOTIFICATIONS permission already granted") |
| 85 | + } |
| 86 | + shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> { |
| 87 | + Log.d(TAG, "Should show permission rationale") |
| 88 | + // You could show a dialog here explaining why the permission is needed |
| 89 | + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) |
| 90 | + } |
| 91 | + else -> { |
| 92 | + Log.d(TAG, "Requesting POST_NOTIFICATIONS permission") |
| 93 | + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@Composable |
| 101 | +// [START android_wear_ongoing_activity_elapsedtime] |
| 102 | +fun ElapsedTime(ambientState: AmbientState) { |
| 103 | + // [START_EXCLUDE] |
| 104 | + val startTimeMs = rememberSaveable { SystemClock.elapsedRealtime() } |
| 105 | + |
| 106 | + val elapsedMs by |
| 107 | + produceState(initialValue = 0L, key1 = startTimeMs) { |
| 108 | + while (true) { // time doesn't stop! |
| 109 | + value = SystemClock.elapsedRealtime() - startTimeMs |
| 110 | + // In ambient mode, update every minute instead of every second |
| 111 | + val updateInterval = if (ambientState.isAmbient) 60_000L else 1_000L |
| 112 | + delay(updateInterval - (value % updateInterval)) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + val totalSeconds = elapsedMs / 1_000L |
| 117 | + val minutes = totalSeconds / 60 |
| 118 | + val seconds = totalSeconds % 60 |
| 119 | + |
| 120 | + // [END_EXCLUDE] |
| 121 | + val timeText = |
| 122 | + if (ambientState.isAmbient) { |
| 123 | + // Show "mm:--" format in ambient mode |
| 124 | + "%02d:--".format(minutes) |
| 125 | + } else { |
| 126 | + // Show full "mm:ss" format in interactive mode |
| 127 | + "%02d:%02d".format(minutes, seconds) |
| 128 | + } |
| 129 | + |
| 130 | + Text(text = timeText, style = MaterialTheme.typography.numeralMedium) |
| 131 | +} |
| 132 | +// [END android_wear_ongoing_activity_elapsedtime] |
| 133 | + |
| 134 | +@Preview( |
| 135 | + device = WearDevices.LARGE_ROUND, |
| 136 | + backgroundColor = 0xff000000, |
| 137 | + showBackground = true, |
| 138 | + group = "Devices - Large Round", |
| 139 | + showSystemUi = true, |
| 140 | +) |
| 141 | +@Composable |
| 142 | +fun WearApp() { |
| 143 | + val context = LocalContext.current |
| 144 | + var isOngoingActivity by rememberSaveable { mutableStateOf(AlwaysOnService.isRunning) } |
| 145 | + MaterialTheme( |
| 146 | + colorScheme = dynamicColorScheme(LocalContext.current) ?: MaterialTheme.colorScheme |
| 147 | + ) { |
| 148 | + // [START android_wear_ongoing_activity_ambientaware] |
| 149 | + AmbientAware { ambientState -> |
| 150 | + // [START_EXCLUDE] |
| 151 | + Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { |
| 152 | + Column(horizontalAlignment = Alignment.CenterHorizontally) { |
| 153 | + Text(text = "Elapsed Time", style = MaterialTheme.typography.titleLarge) |
| 154 | + Spacer(modifier = Modifier.height(8.dp)) |
| 155 | + // [END_EXCLUDE] |
| 156 | + ElapsedTime(ambientState = ambientState) |
| 157 | + // [START_EXCLUDE] |
| 158 | + Spacer(modifier = Modifier.height(8.dp)) |
| 159 | + SwitchButton( |
| 160 | + checked = isOngoingActivity, |
| 161 | + onCheckedChange = { newState -> |
| 162 | + Log.d(TAG, "Switch button changed: $newState") |
| 163 | + isOngoingActivity = newState |
| 164 | + |
| 165 | + if (newState) { |
| 166 | + Log.d(TAG, "Starting AlwaysOnService") |
| 167 | + AlwaysOnService.startService(context) |
| 168 | + } else { |
| 169 | + Log.d(TAG, "Stopping AlwaysOnService") |
| 170 | + AlwaysOnService.stopService(context) |
| 171 | + } |
| 172 | + }, |
| 173 | + contentPadding = PaddingValues(horizontal = 8.dp, vertical = 4.dp), |
| 174 | + ) { |
| 175 | + Text( |
| 176 | + text = "Ongoing Activity", |
| 177 | + style = MaterialTheme.typography.bodyExtraSmall, |
| 178 | + ) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + // [END_EXCLUDE] |
| 183 | + } |
| 184 | + // [END android_wear_ongoing_activity_ambientaware] |
| 185 | + } |
| 186 | +} |
0 commit comments