1 /*
2  * Copyright (C) 2024 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 
18 package com.android.systemui.communal.widgets
19 
20 import android.appwidget.AppWidgetManager
21 import android.content.Context
22 import android.content.res.Resources
23 import android.os.Looper
24 import com.android.systemui.dagger.SysUISingleton
25 import com.android.systemui.dagger.qualifiers.Application
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.dagger.qualifiers.Main
28 import com.android.systemui.log.LogBuffer
29 import com.android.systemui.log.dagger.CommunalLog
30 import com.android.systemui.res.R
31 import com.android.systemui.user.domain.interactor.SelectedUserInteractor
32 import dagger.Module
33 import dagger.Provides
34 import java.util.Optional
35 import javax.inject.Named
36 import kotlinx.coroutines.CoroutineScope
37 
38 @Module
39 interface CommunalWidgetModule {
40     companion object {
41         const val APP_WIDGET_HOST_ID = 116
42         const val DEFAULT_WIDGETS = "default_widgets"
43 
44         @SysUISingleton
45         @Provides
provideAppWidgetManagernull46         fun provideAppWidgetManager(@Application context: Context): Optional<AppWidgetManager> {
47             return Optional.ofNullable(AppWidgetManager.getInstance(context))
48         }
49 
50         @SysUISingleton
51         @Provides
provideCommunalAppWidgetHostnull52         fun provideCommunalAppWidgetHost(
53             @Application context: Context,
54             @Background backgroundScope: CoroutineScope,
55             interactionHandler: WidgetInteractionHandler,
56             @Main looper: Looper,
57             @CommunalLog logBuffer: LogBuffer,
58         ): CommunalAppWidgetHost {
59             return CommunalAppWidgetHost(
60                 context,
61                 backgroundScope,
62                 APP_WIDGET_HOST_ID,
63                 interactionHandler,
64                 looper,
65                 logBuffer,
66             )
67         }
68 
69         @SysUISingleton
70         @Provides
provideCommunalWidgetHostnull71         fun provideCommunalWidgetHost(
72             @Application applicationScope: CoroutineScope,
73             appWidgetManager: Optional<AppWidgetManager>,
74             appWidgetHost: CommunalAppWidgetHost,
75             selectedUserInteractor: SelectedUserInteractor,
76             @CommunalLog logBuffer: LogBuffer,
77         ): CommunalWidgetHost {
78             return CommunalWidgetHost(
79                 applicationScope,
80                 appWidgetManager,
81                 appWidgetHost,
82                 selectedUserInteractor,
83                 logBuffer,
84             )
85         }
86 
87         @Provides
88         @Named(DEFAULT_WIDGETS)
provideDefaultWidgetsnull89         fun provideDefaultWidgets(@Main resources: Resources): Array<String> {
90             return resources.getStringArray(R.array.config_communalWidgetAllowlist)
91         }
92     }
93 }
94