1 /*
2  * Copyright (C) 2019 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.dagger;
18 
19 import static com.android.systemui.Dependency.TIME_TICK_HANDLER_NAME;
20 
21 import android.app.INotificationManager;
22 import android.content.Context;
23 import android.content.SharedPreferences;
24 import android.hardware.display.AmbientDisplayConfiguration;
25 import android.hardware.display.NightDisplayListener;
26 import android.os.Handler;
27 import android.os.HandlerThread;
28 import android.os.Looper;
29 import android.os.ServiceManager;
30 import android.util.DisplayMetrics;
31 import android.view.Choreographer;
32 import android.view.IWindowManager;
33 import android.view.LayoutInflater;
34 import android.view.WindowManager;
35 
36 import com.android.internal.logging.MetricsLogger;
37 import com.android.internal.logging.UiEventLogger;
38 import com.android.internal.logging.UiEventLoggerImpl;
39 import com.android.internal.util.NotificationMessagingUtil;
40 import com.android.internal.widget.LockPatternUtils;
41 import com.android.keyguard.ViewMediatorCallback;
42 import com.android.systemui.Prefs;
43 import com.android.systemui.broadcast.BroadcastDispatcher;
44 import com.android.systemui.broadcast.logging.BroadcastDispatcherLogger;
45 import com.android.systemui.dagger.qualifiers.Background;
46 import com.android.systemui.dagger.qualifiers.Main;
47 import com.android.systemui.doze.AlwaysOnDisplayPolicy;
48 import com.android.systemui.dump.DumpManager;
49 import com.android.systemui.keyguard.KeyguardViewMediator;
50 import com.android.systemui.plugins.PluginInitializerImpl;
51 import com.android.systemui.shared.plugins.PluginManager;
52 import com.android.systemui.shared.plugins.PluginManagerImpl;
53 import com.android.systemui.shared.system.ActivityManagerWrapper;
54 import com.android.systemui.shared.system.DevicePolicyManagerWrapper;
55 import com.android.systemui.statusbar.CommandQueue;
56 import com.android.systemui.statusbar.NavigationBarController;
57 import com.android.systemui.statusbar.phone.AutoHideController;
58 import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
59 import com.android.systemui.statusbar.policy.ConfigurationController;
60 import com.android.systemui.statusbar.policy.DataSaverController;
61 import com.android.systemui.statusbar.policy.NetworkController;
62 import com.android.systemui.util.leak.LeakDetector;
63 
64 import java.util.concurrent.Executor;
65 
66 import javax.inject.Named;
67 import javax.inject.Singleton;
68 
69 import dagger.Module;
70 import dagger.Provides;
71 
72 /**
73  * Provides dependencies for the root component of sysui injection.
74  *
75  * Only SystemUI owned classes and instances should go in here. Other, framework-owned classes
76  * should go in {@link SystemServicesModule}.
77  *
78  * See SystemUI/docs/dagger.md
79  */
80 @Module
81 public class DependencyProvider {
82 
83     @Singleton
84     @Provides
85     @Named(TIME_TICK_HANDLER_NAME)
provideTimeTickHandler()86     public Handler provideTimeTickHandler() {
87         HandlerThread thread = new HandlerThread("TimeTick");
88         thread.start();
89         return new Handler(thread.getLooper());
90     }
91 
92     /** */
93     @Provides
94     @Main
provideSharePreferences(Context context)95     public SharedPreferences provideSharePreferences(Context context) {
96         return Prefs.get(context);
97     }
98 
99     /** */
100     @Provides
provideAmbientDisplayConfiguration(Context context)101     public AmbientDisplayConfiguration provideAmbientDisplayConfiguration(Context context) {
102         return new AmbientDisplayConfiguration(context);
103     }
104 
105     @Singleton
106     @Provides
provideDataSaverController(NetworkController networkController)107     public DataSaverController provideDataSaverController(NetworkController networkController) {
108         return networkController.getDataSaverController();
109     }
110 
111     @Singleton
112     @Provides
provideDisplayMetrics(Context context, WindowManager windowManager)113     public DisplayMetrics provideDisplayMetrics(Context context, WindowManager windowManager) {
114         DisplayMetrics displayMetrics = new DisplayMetrics();
115         context.getDisplay().getMetrics(displayMetrics);
116         return displayMetrics;
117     }
118 
119     /** */
120     @Singleton
121     @Provides
provideINotificationManager()122     public INotificationManager provideINotificationManager() {
123         return INotificationManager.Stub.asInterface(
124                 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
125     }
126 
127     /** */
128     @Singleton
129     @Provides
providerLayoutInflater(Context context)130     public LayoutInflater providerLayoutInflater(Context context) {
131         return LayoutInflater.from(context);
132     }
133 
134     @Singleton
135     @Provides
provideLeakDetector()136     public LeakDetector provideLeakDetector() {
137         return LeakDetector.create();
138 
139     }
140 
141     @Singleton
142     @Provides
provideMetricsLogger()143     public MetricsLogger provideMetricsLogger() {
144         return new MetricsLogger();
145     }
146 
147     @Singleton
148     @Provides
provideNightDisplayListener(Context context, @Background Handler bgHandler)149     public NightDisplayListener provideNightDisplayListener(Context context,
150             @Background Handler bgHandler) {
151         return new NightDisplayListener(context, bgHandler);
152     }
153 
154     @Singleton
155     @Provides
providePluginManager(Context context)156     public PluginManager providePluginManager(Context context) {
157         return new PluginManagerImpl(context, new PluginInitializerImpl());
158     }
159 
160     @Singleton
161     @Provides
provideNavigationBarController(Context context, @Main Handler mainHandler, CommandQueue commandQueue)162     public NavigationBarController provideNavigationBarController(Context context,
163             @Main Handler mainHandler, CommandQueue commandQueue) {
164         return new NavigationBarController(context, mainHandler, commandQueue);
165     }
166 
167     @Singleton
168     @Provides
provideConfigurationController(Context context)169     public ConfigurationController provideConfigurationController(Context context) {
170         return new ConfigurationControllerImpl(context);
171     }
172 
173     /** */
174     @Singleton
175     @Provides
provideAutoHideController(Context context, @Main Handler mainHandler, IWindowManager iWindowManager)176     public AutoHideController provideAutoHideController(Context context,
177             @Main Handler mainHandler, IWindowManager iWindowManager) {
178         return new AutoHideController(context, mainHandler, iWindowManager);
179     }
180 
181     @Singleton
182     @Provides
provideActivityManagerWrapper()183     public ActivityManagerWrapper provideActivityManagerWrapper() {
184         return ActivityManagerWrapper.getInstance();
185     }
186 
187     /** Provides and initializes the {#link BroadcastDispatcher} for SystemUI */
188     @Singleton
189     @Provides
providesBroadcastDispatcher( Context context, @Background Looper backgroundLooper, @Background Executor backgroundExecutor, DumpManager dumpManager, BroadcastDispatcherLogger logger )190     public BroadcastDispatcher providesBroadcastDispatcher(
191             Context context,
192             @Background Looper backgroundLooper,
193             @Background Executor backgroundExecutor,
194             DumpManager dumpManager,
195             BroadcastDispatcherLogger logger
196     ) {
197         BroadcastDispatcher bD = new BroadcastDispatcher(context, backgroundLooper,
198                 backgroundExecutor, dumpManager, logger);
199         bD.initialize();
200         return bD;
201     }
202 
203     @Singleton
204     @Provides
provideDevicePolicyManagerWrapper()205     public DevicePolicyManagerWrapper provideDevicePolicyManagerWrapper() {
206         return DevicePolicyManagerWrapper.getInstance();
207     }
208 
209     /** */
210     @Provides
provideLockPatternUtils(Context context)211     public LockPatternUtils provideLockPatternUtils(Context context) {
212         return new LockPatternUtils(context);
213     }
214 
215     /** */
216     @Provides
provideAlwaysOnDisplayPolicy(Context context)217     public AlwaysOnDisplayPolicy provideAlwaysOnDisplayPolicy(Context context) {
218         return new AlwaysOnDisplayPolicy(context);
219     }
220 
221     /***/
222     @Provides
provideNotificationMessagingUtil(Context context)223     public NotificationMessagingUtil provideNotificationMessagingUtil(Context context) {
224         return new NotificationMessagingUtil(context);
225     }
226 
227     /** */
228     @Provides
providesViewMediatorCallback(KeyguardViewMediator viewMediator)229     public ViewMediatorCallback providesViewMediatorCallback(KeyguardViewMediator viewMediator) {
230         return viewMediator.getViewMediatorCallback();
231     }
232 
233     /** */
234     @Singleton
235     @Provides
providesChoreographer()236     public Choreographer providesChoreographer() {
237         return Choreographer.getInstance();
238     }
239 
240     /** Provides an instance of {@link com.android.internal.logging.UiEventLogger} */
241     @Singleton
242     @Provides
provideUiEventLogger()243     static UiEventLogger provideUiEventLogger() {
244         return new UiEventLoggerImpl();
245     }
246 }
247