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.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.content.res.Resources;
23 
24 import com.android.dream.lowlight.dagger.LowLightDreamModule;
25 import com.android.settingslib.dream.DreamBackend;
26 import com.android.systemui.R;
27 import com.android.systemui.complication.dagger.RegisteredComplicationsModule;
28 import com.android.systemui.dagger.SysUISingleton;
29 import com.android.systemui.dagger.qualifiers.Main;
30 import com.android.systemui.dreams.DreamOverlayNotificationCountProvider;
31 import com.android.systemui.dreams.DreamOverlayService;
32 import com.android.systemui.dreams.complication.dagger.ComplicationComponent;
33 import com.android.systemui.dreams.touch.scrim.dagger.ScrimModule;
34 import com.android.systemui.touch.TouchInsetManager;
35 
36 import dagger.Module;
37 import dagger.Provides;
38 
39 import java.util.Optional;
40 import java.util.concurrent.Executor;
41 
42 import javax.inject.Named;
43 
44 /**
45  * Dagger Module providing Dream-related functionality.
46  */
47 @Module(includes = {
48             RegisteredComplicationsModule.class,
49             LowLightDreamModule.class,
50             ScrimModule.class
51         },
52         subcomponents = {
53             ComplicationComponent.class,
54             DreamOverlayComponent.class,
55         })
56 public interface DreamModule {
57     String DREAM_ONLY_ENABLED_FOR_DOCK_USER = "dream_only_enabled_for_dock_user";
58     String DREAM_OVERLAY_SERVICE_COMPONENT = "dream_overlay_service_component";
59     String DREAM_OVERLAY_ENABLED = "dream_overlay_enabled";
60     String DREAM_TOUCH_INSET_MANAGER = "dream_touch_inset_manager";
61     String DREAM_SUPPORTED = "dream_supported";
62     String DREAM_OVERLAY_WINDOW_TITLE = "dream_overlay_window_title";
63 
64     /**
65      * Provides the dream component
66      */
67     @Provides
68     @Named(DREAM_OVERLAY_SERVICE_COMPONENT)
providesDreamOverlayService(Context context)69     static ComponentName providesDreamOverlayService(Context context) {
70         return new ComponentName(context, DreamOverlayService.class);
71     }
72 
73     /**
74      * Provides a touch inset manager for dreams.
75      */
76     @Provides
77     @Named(DREAM_TOUCH_INSET_MANAGER)
providesTouchInsetManager(@ain Executor executor)78     static TouchInsetManager providesTouchInsetManager(@Main Executor executor) {
79         return new TouchInsetManager(executor);
80     }
81 
82     /**
83      * Provides whether dream overlay is enabled.
84      */
85     @Provides
86     @Named(DREAM_OVERLAY_ENABLED)
providesDreamOverlayEnabled(PackageManager packageManager, @Named(DREAM_OVERLAY_SERVICE_COMPONENT) ComponentName component)87     static Boolean providesDreamOverlayEnabled(PackageManager packageManager,
88             @Named(DREAM_OVERLAY_SERVICE_COMPONENT) ComponentName component) {
89         try {
90             return packageManager.getServiceInfo(component, PackageManager.GET_META_DATA).enabled;
91         } catch (PackageManager.NameNotFoundException e) {
92             return false;
93         }
94     }
95 
96     /**
97      * Provides an instance of the dream backend.
98      */
99     @Provides
providesDreamBackend(Context context)100     static DreamBackend providesDreamBackend(Context context) {
101         return DreamBackend.getInstance(context);
102     }
103 
104     /**
105      * Provides an instance of a {@link DreamOverlayNotificationCountProvider}.
106      */
107     @SysUISingleton
108     @Provides
109     static Optional<DreamOverlayNotificationCountProvider>
providesDreamOverlayNotificationCountProvider()110             providesDreamOverlayNotificationCountProvider() {
111         // If we decide to bring this back, we should gate it on a config that can be changed in
112         // an overlay.
113         return Optional.empty();
114     }
115 
116     /** */
117     @Provides
118     @Named(DREAM_ONLY_ENABLED_FOR_DOCK_USER)
providesDreamOnlyEnabledForDockUser(@ain Resources resources)119     static boolean providesDreamOnlyEnabledForDockUser(@Main Resources resources) {
120         return resources.getBoolean(
121                 com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser);
122     }
123 
124     /** */
125     @Provides
126     @Named(DREAM_SUPPORTED)
providesDreamSupported(@ain Resources resources)127     static boolean providesDreamSupported(@Main Resources resources) {
128         return resources.getBoolean(com.android.internal.R.bool.config_dreamsSupported);
129     }
130 
131     /** */
132     @Provides
133     @Named(DREAM_OVERLAY_WINDOW_TITLE)
providesDreamOverlayWindowTitle(@ain Resources resources)134     static String providesDreamOverlayWindowTitle(@Main Resources resources) {
135         return resources.getString(R.string.app_label);
136     }
137 }
138