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; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 31 | import android.util.Log; |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 32 | import android.util.TimingsTraceLog; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 33 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame] | 34 | import com.android.systemui.dagger.ContextComponentHelper; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 35 | import com.android.systemui.util.NotificationChannels; |
Jorim Jaggi | 61f39a7 | 2015-10-29 16:54:18 +0100 | [diff] [blame] | 36 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 37 | import java.lang.reflect.Constructor; |
| 38 | import java.lang.reflect.InvocationTargetException; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Application class for SystemUI. |
| 42 | */ |
Dave Mankoff | 95754e7 | 2019-11-14 11:39:18 -0500 | [diff] [blame] | 43 | public class SystemUIApplication extends Application implements |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 44 | SystemUIAppComponentFactory.ContextInitializer { |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 45 | |
Dan Sandler | dc2ddd7 | 2018-01-24 16:09:53 -0500 | [diff] [blame] | 46 | public static final String TAG = "SystemUIService"; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 47 | private static final boolean DEBUG = false; |
| 48 | |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 49 | private ContextComponentHelper mComponentHelper; |
| 50 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 51 | /** |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 52 | * Hold a reference on the stuff we start. |
| 53 | */ |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 54 | private SystemUI[] mServices; |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 55 | private boolean mServicesStarted; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 56 | private boolean mBootCompleted; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 57 | private SystemUIAppComponentFactory.ContextAvailableCallback mContextAvailableCallback; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 58 | |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 59 | public SystemUIApplication() { |
| 60 | super(); |
| 61 | Log.v(TAG, "SystemUIApplication constructed."); |
| 62 | } |
| 63 | |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 64 | @Override |
| 65 | public void onCreate() { |
| 66 | super.onCreate(); |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 67 | Log.v(TAG, "SystemUIApplication created."); |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 68 | // This line is used to setup Dagger's dependency injection and should be kept at the |
| 69 | // top of this method. |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 70 | TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming", |
| 71 | Trace.TRACE_TAG_APP); |
| 72 | log.traceBegin("DependencyInjection"); |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 73 | mContextAvailableCallback.onContextAvailable(this); |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 74 | mComponentHelper = SystemUIFactory |
| 75 | .getInstance().getRootComponent().getContextComponentHelper(); |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 76 | log.traceEnd(); |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 77 | |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 78 | // Set the application theme that is inherited by all services. Note that setting the |
| 79 | // application theme in the manifest does only work for activities. Keep this in sync with |
| 80 | // the theme set there. |
Lucas Dupin | e17ce52 | 2017-07-17 15:45:06 -0700 | [diff] [blame] | 81 | setTheme(R.style.Theme_SystemUI); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 82 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 83 | if (Process.myUserHandle().equals(UserHandle.SYSTEM)) { |
Beverly | 6ea063e | 2018-04-19 10:27:06 -0400 | [diff] [blame] | 84 | IntentFilter bootCompletedFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 85 | bootCompletedFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 86 | registerReceiver(new BroadcastReceiver() { |
| 87 | @Override |
| 88 | public void onReceive(Context context, Intent intent) { |
| 89 | if (mBootCompleted) return; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 90 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 91 | if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received"); |
| 92 | unregisterReceiver(this); |
| 93 | mBootCompleted = true; |
| 94 | if (mServicesStarted) { |
| 95 | final int N = mServices.length; |
| 96 | for (int i = 0; i < N; i++) { |
| 97 | mServices[i].onBootCompleted(); |
| 98 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 99 | } |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 100 | |
Beverly | 6ea063e | 2018-04-19 10:27:06 -0400 | [diff] [blame] | 101 | |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 102 | } |
Beverly | 6ea063e | 2018-04-19 10:27:06 -0400 | [diff] [blame] | 103 | }, bootCompletedFilter); |
| 104 | |
| 105 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 106 | registerReceiver(new BroadcastReceiver() { |
| 107 | @Override |
| 108 | public void onReceive(Context context, Intent intent) { |
| 109 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
| 110 | if (!mBootCompleted) return; |
| 111 | // Update names of SystemUi notification channels |
| 112 | NotificationChannels.createAll(context); |
| 113 | } |
| 114 | } |
| 115 | }, localeChangedFilter); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 116 | } else { |
dooyoung.hwang | b647984 | 2016-08-31 14:15:22 +0900 | [diff] [blame] | 117 | // We don't need to startServices for sub-process that is doing some tasks. |
| 118 | // (screenshots, sweetsweetdesserts or tuner ..) |
| 119 | String processName = ActivityThread.currentProcessName(); |
| 120 | ApplicationInfo info = getApplicationInfo(); |
| 121 | if (processName != null && processName.startsWith(info.processName + ":")) { |
| 122 | return; |
| 123 | } |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 124 | // For a secondary user, boot-completed will never be called because it has already |
| 125 | // been broadcasted on startup for the primary SystemUI process. Instead, for |
| 126 | // components which require the SystemUI component to be initialized per-user, we |
| 127 | // start those components now for the current non-system user. |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 128 | startSecondaryUserServicesIfNeeded(); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 129 | } |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 132 | /** |
| 133 | * Makes sure that all the SystemUI services are running. If they are already running, this is a |
| 134 | * no-op. This is needed to conditinally start all the services, as we only need to have it in |
| 135 | * the main process. |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 136 | * <p>This method must only be called from the main thread.</p> |
| 137 | */ |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 138 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 139 | public void startServicesIfNeeded() { |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 140 | String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents); |
Heemin Seog | 7d88476 | 2019-11-08 10:09:10 -0800 | [diff] [blame] | 141 | startServicesIfNeeded(/* metricsPrefix= */ "StartServices", names); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Ensures that all the Secondary user SystemUI services are running. If they are already |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 146 | * running, this is a no-op. This is needed to conditionally start all the services, as we only |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 147 | * need to have it in the main process. |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 148 | * <p>This method must only be called from the main thread.</p> |
| 149 | */ |
| 150 | void startSecondaryUserServicesIfNeeded() { |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 151 | String[] names = |
| 152 | getResources().getStringArray(R.array.config_systemUIServiceComponentsPerUser); |
Heemin Seog | 7d88476 | 2019-11-08 10:09:10 -0800 | [diff] [blame] | 153 | startServicesIfNeeded(/* metricsPrefix= */ "StartSecondaryServices", names); |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Heemin Seog | 7d88476 | 2019-11-08 10:09:10 -0800 | [diff] [blame] | 156 | private void startServicesIfNeeded(String metricsPrefix, String[] services) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 157 | if (mServicesStarted) { |
| 158 | return; |
| 159 | } |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 160 | mServices = new SystemUI[services.length]; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 161 | |
| 162 | if (!mBootCompleted) { |
| 163 | // check to see if maybe it was already completed long before we began |
| 164 | // see ActivityManagerService.finishBooting() |
| 165 | if ("1".equals(SystemProperties.get("sys.boot_completed"))) { |
| 166 | mBootCompleted = true; |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 167 | if (DEBUG) { |
| 168 | Log.v(TAG, "BOOT_COMPLETED was already sent"); |
| 169 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 173 | Log.v(TAG, "Starting SystemUI services for user " + |
| 174 | Process.myUserHandle().getIdentifier() + "."); |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 175 | TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming", |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 176 | Trace.TRACE_TAG_APP); |
Heemin Seog | 7d88476 | 2019-11-08 10:09:10 -0800 | [diff] [blame] | 177 | log.traceBegin(metricsPrefix); |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 178 | final int N = services.length; |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 179 | for (int i = 0; i < N; i++) { |
yoshiki iguchi | 61b3708 | 2017-11-29 16:46:32 +0900 | [diff] [blame] | 180 | String clsName = services[i]; |
| 181 | if (DEBUG) Log.d(TAG, "loading: " + clsName); |
Heemin Seog | 7d88476 | 2019-11-08 10:09:10 -0800 | [diff] [blame] | 182 | log.traceBegin(metricsPrefix + clsName); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 183 | long ti = System.currentTimeMillis(); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 184 | try { |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 185 | SystemUI obj = mComponentHelper.resolveSystemUI(clsName); |
| 186 | if (obj == null) { |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 187 | Constructor constructor = Class.forName(clsName).getConstructor(Context.class); |
| 188 | obj = (SystemUI) constructor.newInstance(this); |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 189 | } |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 190 | mServices[i] = obj; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 191 | } catch (ClassNotFoundException |
| 192 | | NoSuchMethodException |
| 193 | | IllegalAccessException |
| 194 | | InstantiationException |
| 195 | | InvocationTargetException ex) { |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 196 | throw new RuntimeException(ex); |
| 197 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 198 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 199 | if (DEBUG) Log.d(TAG, "running: " + mServices[i]); |
| 200 | mServices[i].start(); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 201 | log.traceEnd(); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 202 | |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 203 | // Warn if initialization of component takes too long |
| 204 | ti = System.currentTimeMillis() - ti; |
| 205 | if (ti > 1000) { |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 206 | Log.w(TAG, "Initialization of " + clsName + " took " + ti + " ms"); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 207 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 208 | if (mBootCompleted) { |
| 209 | mServices[i].onBootCompleted(); |
| 210 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 211 | } |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 212 | Dependency.get(InitController.class).executePostInitTasks(); |
Fyodor Kupolov | f359135 | 2017-06-23 18:20:40 -0700 | [diff] [blame] | 213 | log.traceEnd(); |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 214 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 215 | mServicesStarted = true; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onConfigurationChanged(Configuration newConfig) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 220 | if (mServicesStarted) { |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 221 | Dependency.staticOnConfigurationChanged(newConfig); |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 222 | int len = mServices.length; |
| 223 | for (int i = 0; i < len; i++) { |
Winson | 94a1485 | 2015-09-23 12:44:33 -0700 | [diff] [blame] | 224 | if (mServices[i] != null) { |
| 225 | mServices[i].onConfigurationChanged(newConfig); |
| 226 | } |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 227 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 231 | public SystemUI[] getServices() { |
| 232 | return mServices; |
| 233 | } |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 234 | |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 235 | @Override |
| 236 | public void setContextAvailableCallback( |
| 237 | SystemUIAppComponentFactory.ContextAvailableCallback callback) { |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 238 | mContextAvailableCallback = callback; |
| 239 | } |
| 240 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 241 | } |