Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
dooyoung.hwang | b647984 | 2016-08-31 14:15:22 +0900 | [diff] [blame] | 19 | import android.app.ActivityThread; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 20 | import android.app.Application; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
dooyoung.hwang | b647984 | 2016-08-31 14:15:22 +0900 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 27 | import android.os.Process; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 28 | import android.os.SystemProperties; |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 29 | import android.os.Trace; |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 31 | import android.util.ArraySet; |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 32 | import android.util.TimingsTraceLog; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 35 | import com.android.systemui.globalactions.GlobalActionsComponent; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 36 | import com.android.systemui.keyboard.KeyboardUI; |
| 37 | import com.android.systemui.keyguard.KeyguardViewMediator; |
| 38 | import com.android.systemui.media.RingtonePlayer; |
Jason Monk | 97a06a1 | 2016-11-09 15:53:30 -0500 | [diff] [blame] | 39 | import com.android.systemui.pip.PipUI; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 40 | import com.android.systemui.plugins.GlobalActions; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 41 | import com.android.systemui.plugins.OverlayPlugin; |
Jason Monk | 5bec68f | 2017-02-08 20:45:10 -0800 | [diff] [blame] | 42 | import com.android.systemui.plugins.Plugin; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 43 | import com.android.systemui.plugins.PluginListener; |
| 44 | import com.android.systemui.plugins.PluginManager; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 45 | import com.android.systemui.power.PowerUI; |
| 46 | import com.android.systemui.recents.Recents; |
| 47 | import com.android.systemui.shortcut.ShortcutKeyDispatcher; |
Jorim Jaggi | 1fcbab6 | 2015-11-04 16:39:50 +0100 | [diff] [blame] | 48 | import com.android.systemui.stackdivider.Divider; |
Jason Monk | b5b09201 | 2017-01-05 11:35:34 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 50 | import com.android.systemui.statusbar.phone.StatusBar; |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 51 | import com.android.systemui.statusbar.phone.StatusBarWindowManager; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 52 | import com.android.systemui.usb.StorageNotification; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 53 | import com.android.systemui.util.NotificationChannels; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 54 | import com.android.systemui.util.leak.GarbageMonitor; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 55 | import com.android.systemui.volume.VolumeUI; |
Jorim Jaggi | 61f39a7 | 2015-10-29 16:54:18 +0100 | [diff] [blame] | 56 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 57 | import java.util.HashMap; |
| 58 | import java.util.Map; |
| 59 | |
| 60 | /** |
| 61 | * Application class for SystemUI. |
| 62 | */ |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 63 | public class SystemUIApplication extends Application implements SysUiServiceProvider { |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 64 | |
| 65 | private static final String TAG = "SystemUIService"; |
| 66 | private static final boolean DEBUG = false; |
| 67 | |
| 68 | /** |
| 69 | * The classes of the stuff to start. |
| 70 | */ |
| 71 | private final Class<?>[] SERVICES = new Class[] { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 72 | Dependency.class, |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 73 | NotificationChannels.class, |
Jason Monk | b5b09201 | 2017-01-05 11:35:34 -0500 | [diff] [blame] | 74 | CommandQueue.CommandQueueStart.class, |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 75 | KeyguardViewMediator.class, |
| 76 | Recents.class, |
| 77 | VolumeUI.class, |
Jorim Jaggi | dd98d41 | 2015-11-18 15:57:38 -0800 | [diff] [blame] | 78 | Divider.class, |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 79 | SystemBars.class, |
| 80 | StorageNotification.class, |
| 81 | PowerUI.class, |
| 82 | RingtonePlayer.class, |
| 83 | KeyboardUI.class, |
| 84 | PipUI.class, |
| 85 | ShortcutKeyDispatcher.class, |
| 86 | VendorServices.class, |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 87 | GarbageMonitor.Service.class, |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 88 | LatencyTester.class, |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 89 | GlobalActionsComponent.class, |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 90 | RoundedCorners.class, |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 94 | * The classes of the stuff to start for each user. This is a subset of the services listed |
| 95 | * above. |
| 96 | */ |
| 97 | private final Class<?>[] SERVICES_PER_USER = new Class[] { |
Winson Chung | 0c7592f | 2017-02-06 09:04:07 -0800 | [diff] [blame] | 98 | Dependency.class, |
Geoffrey Pitsch | 790e091 | 2017-03-24 13:37:13 -0400 | [diff] [blame] | 99 | NotificationChannels.class, |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 100 | Recents.class |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /** |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 104 | * Hold a reference on the stuff we start. |
| 105 | */ |
| 106 | private final SystemUI[] mServices = new SystemUI[SERVICES.length]; |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 107 | private boolean mServicesStarted; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 108 | private boolean mBootCompleted; |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 109 | private final Map<Class<?>, Object> mComponents = new HashMap<>(); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 110 | |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 111 | @Override |
| 112 | public void onCreate() { |
| 113 | super.onCreate(); |
| 114 | // Set the application theme that is inherited by all services. Note that setting the |
| 115 | // application theme in the manifest does only work for activities. Keep this in sync with |
| 116 | // the theme set there. |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 117 | setTheme(R.style.Theme_SystemUI); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 118 | |
Xiyuan Xia | 1b30f79 | 2016-01-06 08:50:30 -0800 | [diff] [blame] | 119 | SystemUIFactory.createFromConfig(this); |
| 120 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 121 | if (Process.myUserHandle().equals(UserHandle.SYSTEM)) { |
| 122 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 123 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 124 | registerReceiver(new BroadcastReceiver() { |
| 125 | @Override |
| 126 | public void onReceive(Context context, Intent intent) { |
| 127 | if (mBootCompleted) return; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 128 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 129 | if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received"); |
| 130 | unregisterReceiver(this); |
| 131 | mBootCompleted = true; |
| 132 | if (mServicesStarted) { |
| 133 | final int N = mServices.length; |
| 134 | for (int i = 0; i < N; i++) { |
| 135 | mServices[i].onBootCompleted(); |
| 136 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 139 | }, filter); |
| 140 | } else { |
dooyoung.hwang | b647984 | 2016-08-31 14:15:22 +0900 | [diff] [blame] | 141 | // We don't need to startServices for sub-process that is doing some tasks. |
| 142 | // (screenshots, sweetsweetdesserts or tuner ..) |
| 143 | String processName = ActivityThread.currentProcessName(); |
| 144 | ApplicationInfo info = getApplicationInfo(); |
| 145 | if (processName != null && processName.startsWith(info.processName + ":")) { |
| 146 | return; |
| 147 | } |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 148 | // For a secondary user, boot-completed will never be called because it has already |
| 149 | // been broadcasted on startup for the primary SystemUI process. Instead, for |
| 150 | // components which require the SystemUI component to be initialized per-user, we |
| 151 | // start those components now for the current non-system user. |
| 152 | startServicesIfNeeded(SERVICES_PER_USER); |
| 153 | } |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 156 | /** |
| 157 | * Makes sure that all the SystemUI services are running. If they are already running, this is a |
| 158 | * no-op. This is needed to conditinally start all the services, as we only need to have it in |
| 159 | * the main process. |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 160 | * <p>This method must only be called from the main thread.</p> |
| 161 | */ |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 162 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 163 | public void startServicesIfNeeded() { |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 164 | startServicesIfNeeded(SERVICES); |
| 165 | } |
| 166 | |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Ensures that all the Secondary user SystemUI services are running. If they are already |
| 169 | * running, this is a no-op. This is needed to conditinally start all the services, as we only |
| 170 | * need to have it in the main process. |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 171 | * <p>This method must only be called from the main thread.</p> |
| 172 | */ |
| 173 | void startSecondaryUserServicesIfNeeded() { |
| 174 | startServicesIfNeeded(SERVICES_PER_USER); |
| 175 | } |
| 176 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 177 | private void startServicesIfNeeded(Class<?>[] services) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 178 | if (mServicesStarted) { |
| 179 | return; |
| 180 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 181 | |
| 182 | if (!mBootCompleted) { |
| 183 | // check to see if maybe it was already completed long before we began |
| 184 | // see ActivityManagerService.finishBooting() |
| 185 | if ("1".equals(SystemProperties.get("sys.boot_completed"))) { |
| 186 | mBootCompleted = true; |
| 187 | if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent"); |
| 188 | } |
| 189 | } |
| 190 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 191 | Log.v(TAG, "Starting SystemUI services for user " + |
| 192 | Process.myUserHandle().getIdentifier() + "."); |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 193 | TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming", |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 194 | Trace.TRACE_TAG_APP); |
| 195 | log.traceBegin("StartServices"); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 196 | final int N = services.length; |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 197 | for (int i = 0; i < N; i++) { |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 198 | Class<?> cl = services[i]; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 199 | if (DEBUG) Log.d(TAG, "loading: " + cl); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 200 | log.traceBegin("StartServices" + cl.getSimpleName()); |
| 201 | long ti = System.currentTimeMillis(); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 202 | try { |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 203 | |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 204 | Object newService = SystemUIFactory.getInstance().createInstance(cl); |
| 205 | mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 206 | } catch (IllegalAccessException ex) { |
| 207 | throw new RuntimeException(ex); |
| 208 | } catch (InstantiationException ex) { |
| 209 | throw new RuntimeException(ex); |
| 210 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 211 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 212 | mServices[i].mContext = this; |
| 213 | mServices[i].mComponents = mComponents; |
| 214 | if (DEBUG) Log.d(TAG, "running: " + mServices[i]); |
| 215 | mServices[i].start(); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 216 | log.traceEnd(); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 217 | |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 218 | // Warn if initialization of component takes too long |
| 219 | ti = System.currentTimeMillis() - ti; |
| 220 | if (ti > 1000) { |
| 221 | Log.w(TAG, "Initialization of " + cl.getName() + " took " + ti + " ms"); |
| 222 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 223 | if (mBootCompleted) { |
| 224 | mServices[i].onBootCompleted(); |
| 225 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 226 | } |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 227 | log.traceEnd(); |
Jason Monk | 5bec68f | 2017-02-08 20:45:10 -0800 | [diff] [blame] | 228 | Dependency.get(PluginManager.class).addPluginListener( |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 229 | new PluginListener<OverlayPlugin>() { |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 230 | private ArraySet<OverlayPlugin> mOverlays; |
| 231 | |
| 232 | @Override |
| 233 | public void onPluginConnected(OverlayPlugin plugin, Context pluginContext) { |
| 234 | StatusBar statusBar = getComponent(StatusBar.class); |
| 235 | if (statusBar != null) { |
| 236 | plugin.setup(statusBar.getStatusBarWindow(), |
| 237 | statusBar.getNavigationBarView()); |
| 238 | } |
| 239 | // Lazy init. |
| 240 | if (mOverlays == null) mOverlays = new ArraySet<>(); |
| 241 | if (plugin.holdStatusBarOpen()) { |
| 242 | mOverlays.add(plugin); |
| 243 | Dependency.get(StatusBarWindowManager.class).setStateListener(b -> |
| 244 | mOverlays.forEach(o -> o.setCollapseDesired(b))); |
| 245 | Dependency.get(StatusBarWindowManager.class).setForcePluginOpen( |
| 246 | mOverlays.size() != 0); |
| 247 | |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onPluginDisconnected(OverlayPlugin plugin) { |
| 253 | mOverlays.remove(plugin); |
| 254 | Dependency.get(StatusBarWindowManager.class).setForcePluginOpen( |
| 255 | mOverlays.size() != 0); |
| 256 | } |
Jason Monk | 5bec68f | 2017-02-08 20:45:10 -0800 | [diff] [blame] | 257 | }, OverlayPlugin.class, true /* Allow multiple plugins */); |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 258 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 259 | mServicesStarted = true; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void onConfigurationChanged(Configuration newConfig) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 264 | if (mServicesStarted) { |
| 265 | int len = mServices.length; |
| 266 | for (int i = 0; i < len; i++) { |
Winson | 94a1485 | 2015-09-23 12:44:33 -0700 | [diff] [blame] | 267 | if (mServices[i] != null) { |
| 268 | mServices[i].onConfigurationChanged(newConfig); |
| 269 | } |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 270 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | @SuppressWarnings("unchecked") |
| 275 | public <T> T getComponent(Class<T> interfaceType) { |
| 276 | return (T) mComponents.get(interfaceType); |
| 277 | } |
| 278 | |
| 279 | public SystemUI[] getServices() { |
| 280 | return mServices; |
| 281 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 282 | } |