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