/* * Copyright (C) 2025 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 * * http://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.android.eyedroppertester import android.content.Intent import android.graphics.Color import android.os.Bundle import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Surface import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.android.eyedroppertester.ui.LaunchEyeDropperScreen import com.android.eyedroppertester.ui.MainViewModel private const val TAG = "TestMainActivity" /** * The entry point activity that starts EyeDropper and observes the ColorState * to trigger recomposition when a new color is received as a result. */ class MainActivity : ComponentActivity() { private val mainViewModel: MainViewModel by viewModels() private val activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == RESULT_OK) { val colorInt = result.data?.getIntExtra(Intent.EXTRA_COLOR, Color.BLACK) mainViewModel.updateColor(colorInt ?: Color.BLACK) Log.d(TAG, "resultCode == OK, set color to $colorInt") } else { mainViewModel.resetColor() Log.d(TAG, "resultCode != OK, resetting color.") } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { // Collect Flow into Compose State, only when Lifecycle is minActiveState=STARTED val uiColorState by mainViewModel.uiColorState.collectAsStateWithLifecycle() Surface( modifier = Modifier.fillMaxSize(), ) { LaunchEyeDropperScreen( uiColorState, onClick = { launchEyeDropper(activityLauncher) } ) } } } private fun launchEyeDropper(launcher: ActivityResultLauncher) { // Explicit intent to launch EyeDropper // TODO: b/399172476 - Add `OPEN_EYE_DROPPER` to Intent class. val eyeDropperIntent = Intent(Intent.ACTION_OPEN_EYE_DROPPER) Log.d(TAG, "EyeDropper launched") launcher.launch(eyeDropperIntent) } }