1 /*
<lambda>null2  * 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.smartspace.data.repository
18 
19 import android.app.smartspace.SmartspaceTarget
20 import android.os.Parcelable
21 import android.widget.RemoteViews
22 import com.android.systemui.communal.smartspace.CommunalSmartspaceController
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.dagger.qualifiers.Main
25 import com.android.systemui.plugins.BcSmartspaceDataPlugin
26 import java.util.concurrent.Executor
27 import javax.inject.Inject
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.MutableStateFlow
30 import kotlinx.coroutines.flow.onCompletion
31 import kotlinx.coroutines.flow.onStart
32 
33 interface SmartspaceRepository {
34     /** Whether [RemoteViews] are passed through smartspace targets. */
35     val isSmartspaceRemoteViewsEnabled: Boolean
36 
37     /** Smartspace targets for the communal surface. */
38     val communalSmartspaceTargets: Flow<List<SmartspaceTarget>>
39 }
40 
41 @SysUISingleton
42 class SmartspaceRepositoryImpl
43 @Inject
44 constructor(
45     private val communalSmartspaceController: CommunalSmartspaceController,
46     @Main private val uiExecutor: Executor,
47 ) : SmartspaceRepository, BcSmartspaceDataPlugin.SmartspaceTargetListener {
48 
49     override val isSmartspaceRemoteViewsEnabled: Boolean
50         get() = android.app.smartspace.flags.Flags.remoteViews()
51 
52     private val _communalSmartspaceTargets: MutableStateFlow<List<SmartspaceTarget>> =
53         MutableStateFlow(emptyList())
54     override val communalSmartspaceTargets: Flow<List<SmartspaceTarget>> =
55         _communalSmartspaceTargets
<lambda>null56             .onStart {
57                 uiExecutor.execute {
58                     communalSmartspaceController.addListener(
59                         listener = this@SmartspaceRepositoryImpl
60                     )
61                 }
62             }
<lambda>null63             .onCompletion {
64                 uiExecutor.execute {
65                     communalSmartspaceController.removeListener(
66                         listener = this@SmartspaceRepositoryImpl
67                     )
68                 }
69             }
70 
onSmartspaceTargetsUpdatednull71     override fun onSmartspaceTargetsUpdated(targetsNullable: MutableList<out Parcelable>?) {
72         targetsNullable?.let { targets ->
73             _communalSmartspaceTargets.value = targets.filterIsInstance<SmartspaceTarget>()
74         }
75             ?: run { _communalSmartspaceTargets.value = emptyList() }
76     }
77 }
78