1 /* 2 * Copyright (C) 2023 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.android.systemui.people 18 19 import android.appwidget.AppWidgetManager 20 import android.os.Bundle 21 import android.util.Log 22 import androidx.activity.ComponentActivity 23 import androidx.activity.compose.setContent 24 import androidx.activity.enableEdgeToEdge 25 import androidx.lifecycle.Lifecycle 26 import androidx.lifecycle.ViewModelProvider 27 import androidx.lifecycle.lifecycleScope 28 import androidx.lifecycle.repeatOnLifecycle 29 import com.android.app.tracing.coroutines.launchTraced as launch 30 import com.android.compose.theme.PlatformTheme 31 import com.android.systemui.people.ui.compose.PeopleScreen 32 import com.android.systemui.people.ui.viewmodel.PeopleViewModel 33 import javax.inject.Inject 34 35 /** People Tile Widget configuration activity that shows the user their conversation tiles. */ 36 class PeopleSpaceActivity 37 @Inject 38 constructor(private val viewModelFactory: PeopleViewModel.Factory) : ComponentActivity() { onCreatenull39 override fun onCreate(savedInstanceState: Bundle?) { 40 super.onCreate(savedInstanceState) 41 enableEdgeToEdge() 42 43 setResult(RESULT_CANCELED) 44 45 // Update the widget ID coming from the intent. 46 val viewModel = ViewModelProvider(this, viewModelFactory)[PeopleViewModel::class.java] 47 val widgetId = 48 intent.getIntExtra( 49 AppWidgetManager.EXTRA_APPWIDGET_ID, 50 AppWidgetManager.INVALID_APPWIDGET_ID, 51 ) 52 viewModel.onWidgetIdChanged(widgetId) 53 54 // Make sure to refresh the tiles/conversations when the lifecycle is resumed, so that it 55 // updates them when going back to the Activity after leaving it. 56 // Note that we do this here instead of inside an effect in the PeopleScreen() composable 57 // because otherwise onTileRefreshRequested() will be called after the first composition, 58 // which will trigger a new recomposition and redraw, affecting the GPU memory (see 59 // b/276871425). 60 lifecycleScope.launch { 61 repeatOnLifecycle(Lifecycle.State.RESUMED) { viewModel.onTileRefreshRequested() } 62 } 63 64 // Set the content of the activity, using either the View or Compose implementation. 65 setContent { PlatformTheme { PeopleScreen(viewModel, onResult = { finishActivity(it) }) } } 66 } 67 finishActivitynull68 private fun finishActivity(result: PeopleViewModel.Result) { 69 if (result is PeopleViewModel.Result.Success) { 70 if (DEBUG) Log.d(TAG, "Widget added!") 71 setResult(RESULT_OK, result.data) 72 } else { 73 if (DEBUG) Log.d(TAG, "Activity dismissed with no widgets added!") 74 setResult(RESULT_CANCELED) 75 } 76 77 finish() 78 } 79 80 companion object { 81 private const val TAG = "PeopleSpaceActivity" 82 private const val DEBUG = PeopleSpaceUtils.DEBUG 83 } 84 } 85