blob: b2b612787fd2fe7cf3ed0d6e7ce840b664e0e620 [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,
58 com.android.systemui.shortcut.ShortcutKeyDispatcher.class
Jorim Jaggicff0acb2014-03-31 16:35:15 +020059 };
60
61 /**
Winsonfe67aba2015-09-22 14:04:46 -070062 * The classes of the stuff to start for each user. This is a subset of the services listed
63 * above.
64 */
65 private final Class<?>[] SERVICES_PER_USER = new Class[] {
66 com.android.systemui.recents.Recents.class
67 };
68
69 /**
Jorim Jaggicff0acb2014-03-31 16:35:15 +020070 * Hold a reference on the stuff we start.
71 */
72 private final SystemUI[] mServices = new SystemUI[SERVICES.length];
Jorim Jaggi3beffdf2014-04-03 17:37:37 +020073 private boolean mServicesStarted;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040074 private boolean mBootCompleted;
Winsonfe67aba2015-09-22 14:04:46 -070075 private final Map<Class<?>, Object> mComponents = new HashMap<>();
Jorim Jaggicff0acb2014-03-31 16:35:15 +020076
Adrian Roos070a0b62014-04-10 23:25:03 +020077 @Override
78 public void onCreate() {
79 super.onCreate();
80 // Set the application theme that is inherited by all services. Note that setting the
81 // application theme in the manifest does only work for activities. Keep this in sync with
82 // the theme set there.
83 setTheme(R.style.systemui_theme);
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040084
Xiyuan Xia1b30f792016-01-06 08:50:30 -080085 SystemUIFactory.createFromConfig(this);
86
Winsonfe67aba2015-09-22 14:04:46 -070087 if (Process.myUserHandle().equals(UserHandle.SYSTEM)) {
88 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
89 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
90 registerReceiver(new BroadcastReceiver() {
91 @Override
92 public void onReceive(Context context, Intent intent) {
93 if (mBootCompleted) return;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040094
Winsonfe67aba2015-09-22 14:04:46 -070095 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");
96 unregisterReceiver(this);
97 mBootCompleted = true;
98 if (mServicesStarted) {
99 final int N = mServices.length;
100 for (int i = 0; i < N; i++) {
101 mServices[i].onBootCompleted();
102 }
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400103 }
104 }
Winsonfe67aba2015-09-22 14:04:46 -0700105 }, filter);
106 } else {
107 // For a secondary user, boot-completed will never be called because it has already
108 // been broadcasted on startup for the primary SystemUI process. Instead, for
109 // components which require the SystemUI component to be initialized per-user, we
110 // start those components now for the current non-system user.
111 startServicesIfNeeded(SERVICES_PER_USER);
112 }
Adrian Roos070a0b62014-04-10 23:25:03 +0200113 }
114
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200115 /**
116 * Makes sure that all the SystemUI services are running. If they are already running, this is a
117 * no-op. This is needed to conditinally start all the services, as we only need to have it in
118 * the main process.
119 *
120 * <p>This method must only be called from the main thread.</p>
121 */
122 public void startServicesIfNeeded() {
Winsonfe67aba2015-09-22 14:04:46 -0700123 startServicesIfNeeded(SERVICES);
124 }
125
126 private void startServicesIfNeeded(Class<?>[] services) {
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200127 if (mServicesStarted) {
128 return;
129 }
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400130
131 if (!mBootCompleted) {
132 // check to see if maybe it was already completed long before we began
133 // see ActivityManagerService.finishBooting()
134 if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
135 mBootCompleted = true;
136 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
137 }
138 }
139
Winsonfe67aba2015-09-22 14:04:46 -0700140 Log.v(TAG, "Starting SystemUI services for user " +
141 Process.myUserHandle().getIdentifier() + ".");
142 final int N = services.length;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200143 for (int i=0; i<N; i++) {
Winsonfe67aba2015-09-22 14:04:46 -0700144 Class<?> cl = services[i];
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200145 if (DEBUG) Log.d(TAG, "loading: " + cl);
146 try {
Muyuan Li94ce94e2016-02-24 16:20:54 -0800147 Object newService = SystemUIFactory.getInstance().createInstance(cl);
148 mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService);
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200149 } catch (IllegalAccessException ex) {
150 throw new RuntimeException(ex);
151 } catch (InstantiationException ex) {
152 throw new RuntimeException(ex);
153 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800154
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200155 mServices[i].mContext = this;
156 mServices[i].mComponents = mComponents;
157 if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
158 mServices[i].start();
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400159
160 if (mBootCompleted) {
161 mServices[i].onBootCompleted();
162 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200163 }
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200164 mServicesStarted = true;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200165 }
166
167 @Override
168 public void onConfigurationChanged(Configuration newConfig) {
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200169 if (mServicesStarted) {
170 int len = mServices.length;
171 for (int i = 0; i < len; i++) {
Winson94a14852015-09-23 12:44:33 -0700172 if (mServices[i] != null) {
173 mServices[i].onConfigurationChanged(newConfig);
174 }
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200175 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200176 }
177 }
178
179 @SuppressWarnings("unchecked")
180 public <T> T getComponent(Class<T> interfaceType) {
181 return (T) mComponents.get(interfaceType);
182 }
183
184 public SystemUI[] getServices() {
185 return mServices;
186 }
187}