• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.NonNull;
20 import android.content.Context;
21 import android.content.res.AssetManager;
22 import android.content.res.Resources;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.util.Log;
26 import android.view.ViewGroup;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.internal.widget.LockPatternUtils;
30 import com.android.keyguard.KeyguardUpdateMonitor;
31 import com.android.keyguard.ViewMediatorCallback;
32 import com.android.systemui.bubbles.BubbleController;
33 import com.android.systemui.dagger.DaggerSystemUIRootComponent;
34 import com.android.systemui.dagger.DependencyProvider;
35 import com.android.systemui.dagger.SystemUIRootComponent;
36 import com.android.systemui.keyguard.DismissCallbackRegistry;
37 import com.android.systemui.plugins.FalsingManager;
38 import com.android.systemui.plugins.statusbar.StatusBarStateController;
39 import com.android.systemui.screenshot.ScreenshotNotificationSmartActionsProvider;
40 import com.android.systemui.statusbar.NotificationListener;
41 import com.android.systemui.statusbar.NotificationMediaManager;
42 import com.android.systemui.statusbar.notification.NotificationWakeUpCoordinator;
43 import com.android.systemui.statusbar.phone.BackGestureTfClassifierProvider;
44 import com.android.systemui.statusbar.phone.DozeParameters;
45 import com.android.systemui.statusbar.phone.KeyguardBouncer;
46 import com.android.systemui.statusbar.phone.KeyguardBypassController;
47 import com.android.systemui.statusbar.phone.NotificationIconAreaController;
48 import com.android.systemui.statusbar.phone.StatusBar;
49 import com.android.systemui.statusbar.policy.KeyguardStateController;
50 
51 import java.util.concurrent.Executor;
52 
53 import dagger.Module;
54 import dagger.Provides;
55 
56 /**
57  * Class factory to provide customizable SystemUI components.
58  */
59 public class SystemUIFactory {
60     private static final String TAG = "SystemUIFactory";
61 
62     static SystemUIFactory mFactory;
63     private SystemUIRootComponent mRootComponent;
64 
getInstance()65     public static <T extends SystemUIFactory> T getInstance() {
66         return (T) mFactory;
67     }
68 
createFromConfig(Context context)69     public static void createFromConfig(Context context) {
70         if (mFactory != null) {
71             return;
72         }
73 
74         final String clsName = context.getString(R.string.config_systemUIFactoryComponent);
75         if (clsName == null || clsName.length() == 0) {
76             throw new RuntimeException("No SystemUIFactory component configured");
77         }
78 
79         try {
80             Class<?> cls = null;
81             cls = context.getClassLoader().loadClass(clsName);
82             mFactory = (SystemUIFactory) cls.newInstance();
83             mFactory.init(context);
84         } catch (Throwable t) {
85             Log.w(TAG, "Error creating SystemUIFactory component: " + clsName, t);
86             throw new RuntimeException(t);
87         }
88     }
89 
90     @VisibleForTesting
cleanup()91     static void cleanup() {
92         mFactory = null;
93     }
94 
SystemUIFactory()95     public SystemUIFactory() {}
96 
init(Context context)97     private void init(Context context) {
98         mRootComponent = buildSystemUIRootComponent(context);
99 
100         // Every other part of our codebase currently relies on Dependency, so we
101         // really need to ensure the Dependency gets initialized early on.
102 
103         Dependency dependency = new Dependency();
104         mRootComponent.createDependency().createSystemUI(dependency);
105         dependency.start();
106     }
107 
initWithRootComponent(@onNull SystemUIRootComponent rootComponent)108     protected void initWithRootComponent(@NonNull SystemUIRootComponent rootComponent) {
109         if (mRootComponent != null) {
110             throw new RuntimeException("Root component can be set only once.");
111         }
112 
113         mRootComponent = rootComponent;
114     }
115 
buildSystemUIRootComponent(Context context)116     protected SystemUIRootComponent buildSystemUIRootComponent(Context context) {
117         return DaggerSystemUIRootComponent.builder()
118                 .dependencyProvider(new DependencyProvider())
119                 .contextHolder(new ContextHolder(context))
120                 .build();
121     }
122 
getRootComponent()123     public SystemUIRootComponent getRootComponent() {
124         return mRootComponent;
125     }
126 
127     /** Returns the list of system UI components that should be started. */
getSystemUIServiceComponents(Resources resources)128     public String[] getSystemUIServiceComponents(Resources resources) {
129         return resources.getStringArray(R.array.config_systemUIServiceComponents);
130     }
131 
132     /** Returns the list of system UI components that should be started per user. */
getSystemUIServiceComponentsPerUser(Resources resources)133     public String[] getSystemUIServiceComponentsPerUser(Resources resources) {
134         return resources.getStringArray(R.array.config_systemUIServiceComponentsPerUser);
135     }
136 
137     /**
138      * Creates an instance of ScreenshotNotificationSmartActionsProvider.
139      * This method is overridden in vendor specific implementation of Sys UI.
140      */
141     public ScreenshotNotificationSmartActionsProvider
createScreenshotNotificationSmartActionsProvider(Context context, Executor executor, Handler uiHandler)142             createScreenshotNotificationSmartActionsProvider(Context context,
143             Executor executor,
144             Handler uiHandler) {
145         return new ScreenshotNotificationSmartActionsProvider();
146     }
147 
createKeyguardBouncer(Context context, ViewMediatorCallback callback, LockPatternUtils lockPatternUtils, ViewGroup container, DismissCallbackRegistry dismissCallbackRegistry, KeyguardBouncer.BouncerExpansionCallback expansionCallback, KeyguardStateController keyguardStateController, FalsingManager falsingManager, KeyguardBypassController bypassController)148     public KeyguardBouncer createKeyguardBouncer(Context context, ViewMediatorCallback callback,
149             LockPatternUtils lockPatternUtils, ViewGroup container,
150             DismissCallbackRegistry dismissCallbackRegistry,
151             KeyguardBouncer.BouncerExpansionCallback expansionCallback,
152             KeyguardStateController keyguardStateController, FalsingManager falsingManager,
153             KeyguardBypassController bypassController) {
154         return new KeyguardBouncer(context, callback, lockPatternUtils, container,
155                 dismissCallbackRegistry, falsingManager,
156                 expansionCallback, keyguardStateController,
157                 Dependency.get(KeyguardUpdateMonitor.class), bypassController,
158                 new Handler(Looper.getMainLooper()));
159     }
160 
createNotificationIconAreaController(Context context, StatusBar statusBar, NotificationWakeUpCoordinator wakeUpCoordinator, KeyguardBypassController keyguardBypassController, StatusBarStateController statusBarStateController)161     public NotificationIconAreaController createNotificationIconAreaController(Context context,
162             StatusBar statusBar,
163             NotificationWakeUpCoordinator wakeUpCoordinator,
164             KeyguardBypassController keyguardBypassController,
165             StatusBarStateController statusBarStateController) {
166         return new NotificationIconAreaController(context, statusBar, statusBarStateController,
167                 wakeUpCoordinator, keyguardBypassController,
168                 Dependency.get(NotificationMediaManager.class),
169                 Dependency.get(NotificationListener.class),
170                 Dependency.get(DozeParameters.class),
171                 Dependency.get(BubbleController.class));
172     }
173 
174     @Module
175     public static class ContextHolder {
176         private Context mContext;
177 
ContextHolder(Context context)178         public ContextHolder(Context context) {
179             mContext = context;
180         }
181 
182         @Provides
provideContext()183         public Context provideContext() {
184             return mContext;
185         }
186     }
187 
188     /**
189      * Creates an instance of BackGestureTfClassifierProvider.
190      * This method is overridden in vendor specific implementation of Sys UI.
191      */
createBackGestureTfClassifierProvider( AssetManager am, String modelName)192     public BackGestureTfClassifierProvider createBackGestureTfClassifierProvider(
193             AssetManager am, String modelName) {
194         return new BackGestureTfClassifierProvider();
195     }
196 }
197