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