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; |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame^] | 32 | import com.android.systemui.fragments.FragmentService; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 33 | import com.android.systemui.keyboard.KeyboardUI; |
| 34 | import com.android.systemui.keyguard.KeyguardViewMediator; |
| 35 | import com.android.systemui.media.RingtonePlayer; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 36 | import com.android.systemui.plugins.OverlayPlugin; |
| 37 | import com.android.systemui.plugins.PluginListener; |
| 38 | import com.android.systemui.plugins.PluginManager; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 39 | import com.android.systemui.power.PowerUI; |
| 40 | import com.android.systemui.recents.Recents; |
| 41 | import com.android.systemui.shortcut.ShortcutKeyDispatcher; |
Jorim Jaggi | 1fcbab6 | 2015-11-04 16:39:50 +0100 | [diff] [blame] | 42 | import com.android.systemui.stackdivider.Divider; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 43 | import com.android.systemui.statusbar.SystemBars; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.PhoneStatusBar; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 45 | import com.android.systemui.tuner.TunerService; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 46 | import com.android.systemui.pip.PipUI; |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 47 | import com.android.systemui.usb.StorageNotification; |
| 48 | import com.android.systemui.volume.VolumeUI; |
Jorim Jaggi | 61f39a7 | 2015-10-29 16:54:18 +0100 | [diff] [blame] | 49 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 50 | import java.util.HashMap; |
| 51 | import java.util.Map; |
| 52 | |
| 53 | /** |
| 54 | * Application class for SystemUI. |
| 55 | */ |
| 56 | public class SystemUIApplication extends Application { |
| 57 | |
| 58 | private static final String TAG = "SystemUIService"; |
| 59 | private static final boolean DEBUG = false; |
| 60 | |
| 61 | /** |
| 62 | * The classes of the stuff to start. |
| 63 | */ |
| 64 | private final Class<?>[] SERVICES = new Class[] { |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame^] | 65 | FragmentService.class, |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 66 | TunerService.class, |
| 67 | KeyguardViewMediator.class, |
| 68 | Recents.class, |
| 69 | VolumeUI.class, |
Jorim Jaggi | dd98d41 | 2015-11-18 15:57:38 -0800 | [diff] [blame] | 70 | Divider.class, |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 71 | SystemBars.class, |
| 72 | StorageNotification.class, |
| 73 | PowerUI.class, |
| 74 | RingtonePlayer.class, |
| 75 | KeyboardUI.class, |
| 76 | PipUI.class, |
| 77 | ShortcutKeyDispatcher.class, |
| 78 | VendorServices.class, |
| 79 | LatencyTester.class |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 83 | * The classes of the stuff to start for each user. This is a subset of the services listed |
| 84 | * above. |
| 85 | */ |
| 86 | private final Class<?>[] SERVICES_PER_USER = new Class[] { |
Jorim Jaggi | c3fe204 | 2016-10-07 18:52:48 +0200 | [diff] [blame] | 87 | Recents.class, |
| 88 | PipUI.class |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 92 | * Hold a reference on the stuff we start. |
| 93 | */ |
| 94 | private final SystemUI[] mServices = new SystemUI[SERVICES.length]; |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 95 | private boolean mServicesStarted; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 96 | private boolean mBootCompleted; |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 97 | private final Map<Class<?>, Object> mComponents = new HashMap<>(); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 98 | |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 99 | @Override |
| 100 | public void onCreate() { |
| 101 | super.onCreate(); |
| 102 | // Set the application theme that is inherited by all services. Note that setting the |
| 103 | // application theme in the manifest does only work for activities. Keep this in sync with |
| 104 | // the theme set there. |
| 105 | setTheme(R.style.systemui_theme); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 106 | |
Xiyuan Xia | 1b30f79 | 2016-01-06 08:50:30 -0800 | [diff] [blame] | 107 | SystemUIFactory.createFromConfig(this); |
| 108 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 109 | if (Process.myUserHandle().equals(UserHandle.SYSTEM)) { |
| 110 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 111 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 112 | registerReceiver(new BroadcastReceiver() { |
| 113 | @Override |
| 114 | public void onReceive(Context context, Intent intent) { |
| 115 | if (mBootCompleted) return; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 116 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 117 | if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received"); |
| 118 | unregisterReceiver(this); |
| 119 | mBootCompleted = true; |
| 120 | if (mServicesStarted) { |
| 121 | final int N = mServices.length; |
| 122 | for (int i = 0; i < N; i++) { |
| 123 | mServices[i].onBootCompleted(); |
| 124 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 127 | }, filter); |
| 128 | } else { |
dooyoung.hwang | b647984 | 2016-08-31 14:15:22 +0900 | [diff] [blame] | 129 | // We don't need to startServices for sub-process that is doing some tasks. |
| 130 | // (screenshots, sweetsweetdesserts or tuner ..) |
| 131 | String processName = ActivityThread.currentProcessName(); |
| 132 | ApplicationInfo info = getApplicationInfo(); |
| 133 | if (processName != null && processName.startsWith(info.processName + ":")) { |
| 134 | return; |
| 135 | } |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 136 | // For a secondary user, boot-completed will never be called because it has already |
| 137 | // been broadcasted on startup for the primary SystemUI process. Instead, for |
| 138 | // components which require the SystemUI component to be initialized per-user, we |
| 139 | // start those components now for the current non-system user. |
| 140 | startServicesIfNeeded(SERVICES_PER_USER); |
| 141 | } |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 144 | /** |
| 145 | * Makes sure that all the SystemUI services are running. If they are already running, this is a |
| 146 | * no-op. This is needed to conditinally start all the services, as we only need to have it in |
| 147 | * the main process. |
| 148 | * |
| 149 | * <p>This method must only be called from the main thread.</p> |
| 150 | */ |
| 151 | public void startServicesIfNeeded() { |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 152 | startServicesIfNeeded(SERVICES); |
| 153 | } |
| 154 | |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Ensures that all the Secondary user SystemUI services are running. If they are already |
| 157 | * running, this is a no-op. This is needed to conditinally start all the services, as we only |
| 158 | * need to have it in the main process. |
| 159 | * |
| 160 | * <p>This method must only be called from the main thread.</p> |
| 161 | */ |
| 162 | void startSecondaryUserServicesIfNeeded() { |
| 163 | startServicesIfNeeded(SERVICES_PER_USER); |
| 164 | } |
| 165 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 166 | private void startServicesIfNeeded(Class<?>[] services) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 167 | if (mServicesStarted) { |
| 168 | return; |
| 169 | } |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 170 | |
| 171 | if (!mBootCompleted) { |
| 172 | // check to see if maybe it was already completed long before we began |
| 173 | // see ActivityManagerService.finishBooting() |
| 174 | if ("1".equals(SystemProperties.get("sys.boot_completed"))) { |
| 175 | mBootCompleted = true; |
| 176 | if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent"); |
| 177 | } |
| 178 | } |
| 179 | |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 180 | Log.v(TAG, "Starting SystemUI services for user " + |
| 181 | Process.myUserHandle().getIdentifier() + "."); |
| 182 | final int N = services.length; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 183 | for (int i=0; i<N; i++) { |
Winson | fe67aba | 2015-09-22 14:04:46 -0700 | [diff] [blame] | 184 | Class<?> cl = services[i]; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 185 | if (DEBUG) Log.d(TAG, "loading: " + cl); |
| 186 | try { |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 187 | Object newService = SystemUIFactory.getInstance().createInstance(cl); |
| 188 | mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService); |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 189 | } catch (IllegalAccessException ex) { |
| 190 | throw new RuntimeException(ex); |
| 191 | } catch (InstantiationException ex) { |
| 192 | throw new RuntimeException(ex); |
| 193 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 194 | |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 195 | mServices[i].mContext = this; |
| 196 | mServices[i].mComponents = mComponents; |
| 197 | if (DEBUG) Log.d(TAG, "running: " + mServices[i]); |
| 198 | mServices[i].start(); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 199 | |
| 200 | if (mBootCompleted) { |
| 201 | mServices[i].onBootCompleted(); |
| 202 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 203 | } |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 204 | PluginManager.getInstance(this).addPluginListener(OverlayPlugin.ACTION, |
| 205 | new PluginListener<OverlayPlugin>() { |
| 206 | @Override |
| 207 | public void onPluginConnected(OverlayPlugin plugin) { |
| 208 | PhoneStatusBar phoneStatusBar = getComponent(PhoneStatusBar.class); |
| 209 | if (phoneStatusBar != null) { |
| 210 | plugin.setup(phoneStatusBar.getStatusBarWindow(), |
| 211 | phoneStatusBar.getNavigationBarView()); |
| 212 | } |
| 213 | } |
| 214 | }, OverlayPlugin.VERSION, true /* Allow multiple plugins */); |
| 215 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 216 | mServicesStarted = true; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onConfigurationChanged(Configuration newConfig) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 221 | if (mServicesStarted) { |
| 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 | |
| 231 | @SuppressWarnings("unchecked") |
| 232 | public <T> T getComponent(Class<T> interfaceType) { |
| 233 | return (T) mComponents.get(interfaceType); |
| 234 | } |
| 235 | |
| 236 | public SystemUI[] getServices() { |
| 237 | return mServices; |
| 238 | } |
| 239 | } |