blob: 52b5a54a7621b1df8f9617b420585b21c1eceef7 [file] [log] [blame]
Jorim Jaggicff0acb2014-03-31 16:35:15 +02001/*
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
17package com.android.systemui;
18
19import android.app.Application;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020024import android.content.res.Configuration;
Winsonfe67aba2015-09-22 14:04:46 -070025import android.os.Process;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040026import android.os.SystemProperties;
Winsonfe67aba2015-09-22 14:04:46 -070027import android.os.UserHandle;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020028import android.util.Log;
29
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010030import com.android.systemui.stackdivider.Divider;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010031
Jorim Jaggicff0acb2014-03-31 16:35:15 +020032import java.util.HashMap;
33import java.util.Map;
34
35/**
36 * Application class for SystemUI.
37 */
38public 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 Monk5e745172015-06-02 19:14:44 -040047 com.android.systemui.tuner.TunerService.class,
Jorim Jaggicff0acb2014-03-31 16:35:15 +020048 com.android.systemui.keyguard.KeyguardViewMediator.class,
Jorim Jaggid61f2272014-12-19 20:35:35 +010049 com.android.systemui.recents.Recents.class,
John Spurlock86005342014-05-23 11:58:00 -040050 com.android.systemui.volume.VolumeUI.class,
Jorim Jaggidd98d412015-11-18 15:57:38 -080051 Divider.class,
Jorim Jaggicff0acb2014-03-31 16:35:15 +020052 com.android.systemui.statusbar.SystemBars.class,
53 com.android.systemui.usb.StorageNotification.class,
54 com.android.systemui.power.PowerUI.class,
Jason Monk5e745172015-06-02 19:14:44 -040055 com.android.systemui.media.RingtonePlayer.class,
Michael Wright9209c9c2015-09-03 17:57:01 +010056 com.android.systemui.keyboard.KeyboardUI.class,
Muyuan Li94ce94e2016-02-24 16:20:54 -080057 com.android.systemui.tv.pip.PipUI.class,
Adrian Roos40ea0832016-07-14 14:19:55 -070058 com.android.systemui.shortcut.ShortcutKeyDispatcher.class,
59 com.android.systemui.VendorServices.class
Jorim Jaggicff0acb2014-03-31 16:35:15 +020060 };
61
62 /**
Winsonfe67aba2015-09-22 14:04:46 -070063 * The classes of the stuff to start for each user. This is a subset of the services listed
64 * above.
65 */
66 private final Class<?>[] SERVICES_PER_USER = new Class[] {
Jaewan Kim6a29c3e2016-04-12 18:07:07 +090067 com.android.systemui.recents.Recents.class,
68 com.android.systemui.tv.pip.PipUI.class
Winsonfe67aba2015-09-22 14:04:46 -070069 };
70
71 /**
Jorim Jaggicff0acb2014-03-31 16:35:15 +020072 * Hold a reference on the stuff we start.
73 */
74 private final SystemUI[] mServices = new SystemUI[SERVICES.length];
Jorim Jaggi3beffdf2014-04-03 17:37:37 +020075 private boolean mServicesStarted;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040076 private boolean mBootCompleted;
Winsonfe67aba2015-09-22 14:04:46 -070077 private final Map<Class<?>, Object> mComponents = new HashMap<>();
Jorim Jaggicff0acb2014-03-31 16:35:15 +020078
Adrian Roos070a0b62014-04-10 23:25:03 +020079 @Override
80 public void onCreate() {
81 super.onCreate();
82 // Set the application theme that is inherited by all services. Note that setting the
83 // application theme in the manifest does only work for activities. Keep this in sync with
84 // the theme set there.
85 setTheme(R.style.systemui_theme);
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040086
Xiyuan Xia1b30f792016-01-06 08:50:30 -080087 SystemUIFactory.createFromConfig(this);
88
Winsonfe67aba2015-09-22 14:04:46 -070089 if (Process.myUserHandle().equals(UserHandle.SYSTEM)) {
90 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
91 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
92 registerReceiver(new BroadcastReceiver() {
93 @Override
94 public void onReceive(Context context, Intent intent) {
95 if (mBootCompleted) return;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040096
Winsonfe67aba2015-09-22 14:04:46 -070097 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");
98 unregisterReceiver(this);
99 mBootCompleted = true;
100 if (mServicesStarted) {
101 final int N = mServices.length;
102 for (int i = 0; i < N; i++) {
103 mServices[i].onBootCompleted();
104 }
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400105 }
106 }
Winsonfe67aba2015-09-22 14:04:46 -0700107 }, filter);
108 } else {
109 // For a secondary user, boot-completed will never be called because it has already
110 // been broadcasted on startup for the primary SystemUI process. Instead, for
111 // components which require the SystemUI component to be initialized per-user, we
112 // start those components now for the current non-system user.
113 startServicesIfNeeded(SERVICES_PER_USER);
114 }
Adrian Roos070a0b62014-04-10 23:25:03 +0200115 }
116
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200117 /**
118 * Makes sure that all the SystemUI services are running. If they are already running, this is a
119 * no-op. This is needed to conditinally start all the services, as we only need to have it in
120 * the main process.
121 *
122 * <p>This method must only be called from the main thread.</p>
123 */
124 public void startServicesIfNeeded() {
Winsonfe67aba2015-09-22 14:04:46 -0700125 startServicesIfNeeded(SERVICES);
126 }
127
Winson3c2c34b2016-04-04 17:47:41 -0700128 /**
129 * Ensures that all the Secondary user SystemUI services are running. If they are already
130 * running, this is a no-op. This is needed to conditinally start all the services, as we only
131 * need to have it in the main process.
132 *
133 * <p>This method must only be called from the main thread.</p>
134 */
135 void startSecondaryUserServicesIfNeeded() {
136 startServicesIfNeeded(SERVICES_PER_USER);
137 }
138
Winsonfe67aba2015-09-22 14:04:46 -0700139 private void startServicesIfNeeded(Class<?>[] services) {
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200140 if (mServicesStarted) {
141 return;
142 }
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400143
144 if (!mBootCompleted) {
145 // check to see if maybe it was already completed long before we began
146 // see ActivityManagerService.finishBooting()
147 if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
148 mBootCompleted = true;
149 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
150 }
151 }
152
Winsonfe67aba2015-09-22 14:04:46 -0700153 Log.v(TAG, "Starting SystemUI services for user " +
154 Process.myUserHandle().getIdentifier() + ".");
155 final int N = services.length;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200156 for (int i=0; i<N; i++) {
Winsonfe67aba2015-09-22 14:04:46 -0700157 Class<?> cl = services[i];
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200158 if (DEBUG) Log.d(TAG, "loading: " + cl);
159 try {
Muyuan Li94ce94e2016-02-24 16:20:54 -0800160 Object newService = SystemUIFactory.getInstance().createInstance(cl);
161 mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService);
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200162 } catch (IllegalAccessException ex) {
163 throw new RuntimeException(ex);
164 } catch (InstantiationException ex) {
165 throw new RuntimeException(ex);
166 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800167
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200168 mServices[i].mContext = this;
169 mServices[i].mComponents = mComponents;
170 if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
171 mServices[i].start();
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400172
173 if (mBootCompleted) {
174 mServices[i].onBootCompleted();
175 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200176 }
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200177 mServicesStarted = true;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200178 }
179
180 @Override
181 public void onConfigurationChanged(Configuration newConfig) {
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200182 if (mServicesStarted) {
183 int len = mServices.length;
184 for (int i = 0; i < len; i++) {
Winson94a14852015-09-23 12:44:33 -0700185 if (mServices[i] != null) {
186 mServices[i].onConfigurationChanged(newConfig);
187 }
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200188 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200189 }
190 }
191
192 @SuppressWarnings("unchecked")
193 public <T> T getComponent(Class<T> interfaceType) {
194 return (T) mComponents.get(interfaceType);
195 }
196
197 public SystemUI[] getServices() {
198 return mServices;
199 }
200}