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