1 /*
2  * Copyright (C) 2021 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.dreams.dagger;
18 
19 import android.annotation.Nullable;
20 import android.app.Service;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.PackageManager;
24 import android.content.res.Resources;
25 
26 import com.android.dream.lowlight.dagger.LowLightDreamModule;
27 import com.android.settingslib.dream.DreamBackend;
28 import com.android.systemui.ambient.touch.scrim.dagger.ScrimModule;
29 import com.android.systemui.complication.dagger.RegisteredComplicationsModule;
30 import com.android.systemui.dagger.SysUISingleton;
31 import com.android.systemui.dagger.qualifiers.Main;
32 import com.android.systemui.dreams.DreamOverlayNotificationCountProvider;
33 import com.android.systemui.dreams.DreamOverlayService;
34 import com.android.systemui.dreams.SystemDialogsCloser;
35 import com.android.systemui.dreams.complication.dagger.ComplicationComponent;
36 import com.android.systemui.dreams.homecontrols.DreamServiceDelegate;
37 import com.android.systemui.dreams.homecontrols.DreamServiceDelegateImpl;
38 import com.android.systemui.dreams.homecontrols.HomeControlsDreamService;
39 import com.android.systemui.qs.QsEventLogger;
40 import com.android.systemui.qs.pipeline.shared.TileSpec;
41 import com.android.systemui.qs.tiles.viewmodel.QSTileConfig;
42 import com.android.systemui.qs.tiles.viewmodel.QSTilePolicy;
43 import com.android.systemui.qs.tiles.viewmodel.QSTileUIConfig;
44 import com.android.systemui.res.R;
45 import com.android.systemui.touch.TouchInsetManager;
46 
47 import dagger.Binds;
48 import dagger.Module;
49 import dagger.Provides;
50 import dagger.multibindings.ClassKey;
51 import dagger.multibindings.IntoMap;
52 import dagger.multibindings.StringKey;
53 
54 import java.util.Optional;
55 import java.util.concurrent.Executor;
56 
57 import javax.inject.Named;
58 
59 /**
60  * Dagger Module providing Dream-related functionality.
61  */
62 @Module(includes = {
63             RegisteredComplicationsModule.class,
64             LowLightDreamModule.class,
65             ScrimModule.class
66         },
67         subcomponents = {
68             ComplicationComponent.class,
69             DreamOverlayComponent.class,
70         })
71 public interface DreamModule {
72     String DREAM_ONLY_ENABLED_FOR_DOCK_USER = "dream_only_enabled_for_dock_user";
73     String DREAM_OVERLAY_SERVICE_COMPONENT = "dream_overlay_service_component";
74     String DREAM_OVERLAY_ENABLED = "dream_overlay_enabled";
75     String DREAM_TOUCH_INSET_MANAGER = "dream_touch_inset_manager";
76     String DREAM_SUPPORTED = "dream_supported";
77     String DREAM_OVERLAY_WINDOW_TITLE = "dream_overlay_window_title";
78     String HOME_CONTROL_PANEL_DREAM_COMPONENT = "home_control_panel_dream_component";
79     String DREAM_TILE_SPEC = "dream";
80 
81     /**
82      * Provides the dream component
83      */
84     @Provides
85     @Named(DREAM_OVERLAY_SERVICE_COMPONENT)
providesDreamOverlayService(Context context)86     static ComponentName providesDreamOverlayService(Context context) {
87         return new ComponentName(context, DreamOverlayService.class);
88     }
89 
90     /**
91      * Provides the home control panel component
92      */
93     @Provides
94     @Nullable
95     @Named(HOME_CONTROL_PANEL_DREAM_COMPONENT)
providesHomeControlPanelComponent(Context context)96     static ComponentName providesHomeControlPanelComponent(Context context) {
97         final String homeControlPanelComponent = context.getResources()
98                 .getString(R.string.config_homePanelDreamComponent);
99         if (homeControlPanelComponent.isEmpty()) {
100             return null;
101         }
102         return ComponentName.unflattenFromString(homeControlPanelComponent);
103     }
104 
105     /**
106      * Provides Home Controls Dream Service
107      */
108     @Binds
109     @IntoMap
110     @ClassKey(HomeControlsDreamService.class)
bindHomeControlsDreamService( HomeControlsDreamService service)111     Service bindHomeControlsDreamService(
112             HomeControlsDreamService service);
113 
114     /**
115      * Provides a touch inset manager for dreams.
116      */
117     @Provides
118     @Named(DREAM_TOUCH_INSET_MANAGER)
providesTouchInsetManager(@ain Executor executor)119     static TouchInsetManager providesTouchInsetManager(@Main Executor executor) {
120         return new TouchInsetManager(executor);
121     }
122 
123     /**
124      * Provides whether dream overlay is enabled.
125      */
126     @Provides
127     @Named(DREAM_OVERLAY_ENABLED)
providesDreamOverlayEnabled(PackageManager packageManager, @Named(DREAM_OVERLAY_SERVICE_COMPONENT) ComponentName component)128     static Boolean providesDreamOverlayEnabled(PackageManager packageManager,
129             @Named(DREAM_OVERLAY_SERVICE_COMPONENT) ComponentName component) {
130         try {
131             return packageManager.getServiceInfo(component, PackageManager.GET_META_DATA).enabled;
132         } catch (PackageManager.NameNotFoundException e) {
133             return false;
134         }
135     }
136 
137     /**
138      * Provides an instance of the dream backend.
139      */
140     @Provides
providesDreamBackend(Context context)141     static DreamBackend providesDreamBackend(Context context) {
142         return DreamBackend.getInstance(context);
143     }
144 
145     /**
146      * Provides an instance of a {@link DreamOverlayNotificationCountProvider}.
147      */
148     @SysUISingleton
149     @Provides
150     static Optional<DreamOverlayNotificationCountProvider>
providesDreamOverlayNotificationCountProvider()151             providesDreamOverlayNotificationCountProvider() {
152         // If we decide to bring this back, we should gate it on a config that can be changed in
153         // an overlay.
154         return Optional.empty();
155     }
156 
157     /**
158      * Provides an implementation for {@link SystemDialogsCloser} that calls
159      * {@link Context.closeSystemDialogs}.
160      */
161     @Provides
providesSystemDialogsCloser(Context context)162     static SystemDialogsCloser providesSystemDialogsCloser(Context context) {
163         return () -> context.closeSystemDialogs();
164     }
165 
166     /** */
167     @Provides
168     @Named(DREAM_ONLY_ENABLED_FOR_DOCK_USER)
providesDreamOnlyEnabledForDockUser(@ain Resources resources)169     static boolean providesDreamOnlyEnabledForDockUser(@Main Resources resources) {
170         return resources.getBoolean(
171                 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser);
172     }
173 
174     /** */
175     @Provides
176     @Named(DREAM_SUPPORTED)
providesDreamSupported(@ain Resources resources)177     static boolean providesDreamSupported(@Main Resources resources) {
178         return resources.getBoolean(com.android.internal.R.bool.config_dreamsSupported);
179     }
180 
181     /** */
182     @Provides
183     @Named(DREAM_OVERLAY_WINDOW_TITLE)
providesDreamOverlayWindowTitle(@ain Resources resources)184     static String providesDreamOverlayWindowTitle(@Main Resources resources) {
185         return resources.getString(R.string.app_label);
186     }
187 
188     /** Provides config for the dream tile */
189     @Provides
190     @IntoMap
191     @StringKey(DREAM_TILE_SPEC)
provideDreamTileConfig(QsEventLogger uiEventLogger)192     static QSTileConfig provideDreamTileConfig(QsEventLogger uiEventLogger) {
193         TileSpec tileSpec = TileSpec.create(DREAM_TILE_SPEC);
194         return new QSTileConfig(tileSpec,
195                 new QSTileUIConfig.Resource(
196                         R.drawable.ic_qs_screen_saver,
197                         R.string.quick_settings_screensaver_label),
198                 uiEventLogger.getNewInstanceId(),
199                 tileSpec.getSpec(),
200                 QSTilePolicy.NoRestrictions.INSTANCE
201                 );
202     }
203 
204 
205     /** Provides delegate to allow for testing of dream service */
206     @Binds
bindDreamDelegate(DreamServiceDelegateImpl impl)207     DreamServiceDelegate bindDreamDelegate(DreamServiceDelegateImpl impl);
208 
209 }
210