1 /*
2  * Copyright (C) 2016 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;
18 
19 import android.app.AlarmManager;
20 import android.content.Context;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.util.Log;
24 import android.view.ViewGroup;
25 
26 import com.android.internal.colorextraction.ColorExtractor.GradientColors;
27 import com.android.internal.util.function.TriConsumer;
28 import com.android.internal.widget.LockPatternUtils;
29 import com.android.keyguard.KeyguardUpdateMonitor;
30 import com.android.keyguard.ViewMediatorCallback;
31 import com.android.systemui.keyguard.DismissCallbackRegistry;
32 import com.android.systemui.plugins.FalsingManager;
33 import com.android.systemui.plugins.statusbar.StatusBarStateController;
34 import com.android.systemui.screenshot.ScreenshotNotificationSmartActionsProvider;
35 import com.android.systemui.statusbar.KeyguardIndicationController;
36 import com.android.systemui.statusbar.NotificationMediaManager;
37 import com.android.systemui.statusbar.ScrimView;
38 import com.android.systemui.statusbar.notification.NotificationWakeUpCoordinator;
39 import com.android.systemui.statusbar.phone.DozeParameters;
40 import com.android.systemui.statusbar.phone.KeyguardBouncer;
41 import com.android.systemui.statusbar.phone.KeyguardBypassController;
42 import com.android.systemui.statusbar.phone.LockIcon;
43 import com.android.systemui.statusbar.phone.LockscreenWallpaper;
44 import com.android.systemui.statusbar.phone.NotificationIconAreaController;
45 import com.android.systemui.statusbar.phone.ScrimController;
46 import com.android.systemui.statusbar.phone.ScrimState;
47 import com.android.systemui.statusbar.phone.StatusBar;
48 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
49 import com.android.systemui.statusbar.phone.UnlockMethodCache;
50 import com.android.systemui.statusbar.policy.KeyguardMonitor;
51 import com.android.systemui.volume.VolumeDialogComponent;
52 
53 import java.util.concurrent.Executor;
54 import java.util.function.Consumer;
55 
56 import dagger.Module;
57 import dagger.Provides;
58 
59 /**
60  * Class factory to provide customizable SystemUI components.
61  */
62 public class SystemUIFactory {
63     private static final String TAG = "SystemUIFactory";
64 
65     static SystemUIFactory mFactory;
66     protected SystemUIRootComponent mRootComponent;
67 
getInstance()68     public static <T extends SystemUIFactory> T getInstance() {
69         return (T) mFactory;
70     }
71 
createFromConfig(Context context)72     public static void createFromConfig(Context context) {
73         final String clsName = context.getString(R.string.config_systemUIFactoryComponent);
74         if (clsName == null || clsName.length() == 0) {
75             throw new RuntimeException("No SystemUIFactory component configured");
76         }
77 
78         try {
79             Class<?> cls = null;
80             cls = context.getClassLoader().loadClass(clsName);
81             mFactory = (SystemUIFactory) cls.newInstance();
82             mFactory.init(context);
83         } catch (Throwable t) {
84             Log.w(TAG, "Error creating SystemUIFactory component: " + clsName, t);
85             throw new RuntimeException(t);
86         }
87     }
88 
SystemUIFactory()89     public SystemUIFactory() {}
90 
init(Context context)91     private void init(Context context) {
92         mRootComponent = buildSystemUIRootComponent(context);
93 
94         // Every other part of our codebase currently relies on Dependency, so we
95         // really need to ensure the Dependency gets initialized early on.
96         Dependency.initDependencies(mRootComponent);
97     }
98 
buildSystemUIRootComponent(Context context)99     protected SystemUIRootComponent buildSystemUIRootComponent(Context context) {
100         return DaggerSystemUIRootComponent.builder()
101                 .dependencyProvider(new com.android.systemui.DependencyProvider())
102                 .contextHolder(new ContextHolder(context))
103                 .build();
104     }
105 
getRootComponent()106     public SystemUIRootComponent getRootComponent() {
107         return mRootComponent;
108     }
109 
createStatusBarKeyguardViewManager(Context context, ViewMediatorCallback viewMediatorCallback, LockPatternUtils lockPatternUtils)110     public StatusBarKeyguardViewManager createStatusBarKeyguardViewManager(Context context,
111             ViewMediatorCallback viewMediatorCallback, LockPatternUtils lockPatternUtils) {
112         return new StatusBarKeyguardViewManager(context, viewMediatorCallback, lockPatternUtils);
113     }
114 
115     /**
116      * Creates an instance of ScreenshotNotificationSmartActionsProvider.
117      * This method is overridden in vendor specific implementation of Sys UI.
118      */
119     public ScreenshotNotificationSmartActionsProvider
createScreenshotNotificationSmartActionsProvider(Context context, Executor executor, Handler uiHandler)120             createScreenshotNotificationSmartActionsProvider(Context context,
121             Executor executor,
122             Handler uiHandler) {
123         return new ScreenshotNotificationSmartActionsProvider();
124     }
125 
createKeyguardBouncer(Context context, ViewMediatorCallback callback, LockPatternUtils lockPatternUtils, ViewGroup container, DismissCallbackRegistry dismissCallbackRegistry, KeyguardBouncer.BouncerExpansionCallback expansionCallback, FalsingManager falsingManager, KeyguardBypassController bypassController)126     public KeyguardBouncer createKeyguardBouncer(Context context, ViewMediatorCallback callback,
127             LockPatternUtils lockPatternUtils, ViewGroup container,
128             DismissCallbackRegistry dismissCallbackRegistry,
129             KeyguardBouncer.BouncerExpansionCallback expansionCallback,
130             FalsingManager falsingManager, KeyguardBypassController bypassController) {
131         return new KeyguardBouncer(context, callback, lockPatternUtils, container,
132                 dismissCallbackRegistry, falsingManager,
133                 expansionCallback, UnlockMethodCache.getInstance(context),
134                 KeyguardUpdateMonitor.getInstance(context), bypassController,
135                 new Handler(Looper.getMainLooper()));
136     }
137 
createScrimController(ScrimView scrimBehind, ScrimView scrimInFront, LockscreenWallpaper lockscreenWallpaper, TriConsumer<ScrimState, Float, GradientColors> scrimStateListener, Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters, AlarmManager alarmManager, KeyguardMonitor keyguardMonitor)138     public ScrimController createScrimController(ScrimView scrimBehind, ScrimView scrimInFront,
139             LockscreenWallpaper lockscreenWallpaper,
140             TriConsumer<ScrimState, Float, GradientColors> scrimStateListener,
141             Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters,
142             AlarmManager alarmManager, KeyguardMonitor keyguardMonitor) {
143         return new ScrimController(scrimBehind, scrimInFront, scrimStateListener,
144                 scrimVisibleListener, dozeParameters, alarmManager, keyguardMonitor);
145     }
146 
createNotificationIconAreaController(Context context, StatusBar statusBar, NotificationWakeUpCoordinator wakeUpCoordinator, KeyguardBypassController keyguardBypassController, StatusBarStateController statusBarStateController)147     public NotificationIconAreaController createNotificationIconAreaController(Context context,
148             StatusBar statusBar,
149             NotificationWakeUpCoordinator wakeUpCoordinator,
150             KeyguardBypassController keyguardBypassController,
151             StatusBarStateController statusBarStateController) {
152         return new NotificationIconAreaController(context, statusBar, statusBarStateController,
153                 wakeUpCoordinator, keyguardBypassController,
154                 Dependency.get(NotificationMediaManager.class));
155     }
156 
createKeyguardIndicationController(Context context, ViewGroup indicationArea, LockIcon lockIcon)157     public KeyguardIndicationController createKeyguardIndicationController(Context context,
158             ViewGroup indicationArea, LockIcon lockIcon) {
159         return new KeyguardIndicationController(context, indicationArea, lockIcon);
160     }
161 
createVolumeDialogComponent(SystemUI systemUi, Context context)162     public VolumeDialogComponent createVolumeDialogComponent(SystemUI systemUi, Context context) {
163         return new VolumeDialogComponent(systemUi, context);
164     }
165 
166     @Module
167     public static class ContextHolder {
168         private Context mContext;
169 
ContextHolder(Context context)170         public ContextHolder(Context context) {
171             mContext = context;
172         }
173 
174         @Provides
provideContext()175         public Context provideContext() {
176             return mContext;
177         }
178     }
179 }
180