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