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