1 /* 2 * Copyright (C) 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 * 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.statusbar.layout.ui.viewmodel 18 19 import com.android.systemui.CoreStartable 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.dagger.qualifiers.Background 22 import com.android.systemui.display.data.repository.DisplayRepository 23 import com.android.systemui.display.data.repository.PerDisplayStore 24 import com.android.systemui.display.data.repository.SingleDisplayStore 25 import com.android.systemui.statusbar.core.StatusBarConnectedDisplays 26 import com.android.systemui.statusbar.data.repository.StatusBarContentInsetsProviderStore 27 import com.android.systemui.statusbar.data.repository.StatusBarPerDisplayStoreImpl 28 import dagger.Lazy 29 import dagger.Module 30 import dagger.Provides 31 import dagger.multibindings.ClassKey 32 import dagger.multibindings.IntoMap 33 import javax.inject.Inject 34 import kotlinx.coroutines.CoroutineScope 35 36 /** Provides per-display instances of [StatusBarContentInsetsViewModel]. */ 37 interface StatusBarContentInsetsViewModelStore : PerDisplayStore<StatusBarContentInsetsViewModel> 38 39 @SysUISingleton 40 class MultiDisplayStatusBarContentInsetsViewModelStore 41 @Inject 42 constructor( 43 @Background backgroundApplicationScope: CoroutineScope, 44 displayRepository: DisplayRepository, 45 private val statusBarContentInsetsProviderStore: StatusBarContentInsetsProviderStore, 46 ) : 47 StatusBarContentInsetsViewModelStore, 48 StatusBarPerDisplayStoreImpl<StatusBarContentInsetsViewModel>( 49 backgroundApplicationScope, 50 displayRepository, 51 ) { 52 createInstanceForDisplaynull53 override fun createInstanceForDisplay(displayId: Int): StatusBarContentInsetsViewModel? { 54 val insetsProvider = 55 statusBarContentInsetsProviderStore.forDisplay(displayId) ?: return null 56 return StatusBarContentInsetsViewModel(insetsProvider) 57 } 58 59 override val instanceClass = StatusBarContentInsetsViewModel::class.java 60 } 61 62 @SysUISingleton 63 class SingleDisplayStatusBarContentInsetsViewModelStore 64 @Inject 65 constructor(statusBarContentInsetsViewModel: StatusBarContentInsetsViewModel) : 66 StatusBarContentInsetsViewModelStore, 67 PerDisplayStore<StatusBarContentInsetsViewModel> by SingleDisplayStore( 68 defaultInstance = statusBarContentInsetsViewModel 69 ) 70 71 @Module 72 object StatusBarContentInsetsViewModelStoreModule { 73 @Provides 74 @SysUISingleton 75 @IntoMap 76 @ClassKey(StatusBarContentInsetsViewModelStore::class) storeAsCoreStartablenull77 fun storeAsCoreStartable( 78 multiDisplayLazy: Lazy<MultiDisplayStatusBarContentInsetsViewModelStore> 79 ): CoreStartable { 80 return if (StatusBarConnectedDisplays.isEnabled) { 81 return multiDisplayLazy.get() 82 } else { 83 CoreStartable.NOP 84 } 85 } 86 87 @Provides 88 @SysUISingleton storenull89 fun store( 90 singleDisplayLazy: Lazy<SingleDisplayStatusBarContentInsetsViewModelStore>, 91 multiDisplayLazy: Lazy<MultiDisplayStatusBarContentInsetsViewModelStore>, 92 ): StatusBarContentInsetsViewModelStore { 93 return if (StatusBarConnectedDisplays.isEnabled) { 94 multiDisplayLazy.get() 95 } else { 96 singleDisplayLazy.get() 97 } 98 } 99 } 100