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