blob: e302c986a388d0cb60db163b67baecbe2944915b [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;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040025import android.os.SystemProperties;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020026import android.util.Log;
27
28import java.util.HashMap;
29import java.util.Map;
30
31/**
32 * Application class for SystemUI.
33 */
34public class SystemUIApplication extends Application {
35
36 private static final String TAG = "SystemUIService";
37 private static final boolean DEBUG = false;
38
39 /**
40 * The classes of the stuff to start.
41 */
42 private final Class<?>[] SERVICES = new Class[] {
43 com.android.systemui.keyguard.KeyguardViewMediator.class,
Jorim Jaggid61f2272014-12-19 20:35:35 +010044 com.android.systemui.recents.Recents.class,
John Spurlock86005342014-05-23 11:58:00 -040045 com.android.systemui.volume.VolumeUI.class,
Jorim Jaggicff0acb2014-03-31 16:35:15 +020046 com.android.systemui.statusbar.SystemBars.class,
47 com.android.systemui.usb.StorageNotification.class,
48 com.android.systemui.power.PowerUI.class,
Alan Viverette5a399492014-07-14 16:19:38 -070049 com.android.systemui.media.RingtonePlayer.class
Jorim Jaggicff0acb2014-03-31 16:35:15 +020050 };
51
52 /**
53 * Hold a reference on the stuff we start.
54 */
55 private final SystemUI[] mServices = new SystemUI[SERVICES.length];
Jorim Jaggi3beffdf2014-04-03 17:37:37 +020056 private boolean mServicesStarted;
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040057 private boolean mBootCompleted;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020058 private final Map<Class<?>, Object> mComponents = new HashMap<Class<?>, Object>();
59
Adrian Roos070a0b62014-04-10 23:25:03 +020060 @Override
61 public void onCreate() {
62 super.onCreate();
63 // Set the application theme that is inherited by all services. Note that setting the
64 // application theme in the manifest does only work for activities. Keep this in sync with
65 // the theme set there.
66 setTheme(R.style.systemui_theme);
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040067
Dianne Hackbornd83a0962014-05-02 16:28:33 -070068 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
69 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040070 registerReceiver(new BroadcastReceiver() {
71 @Override
72 public void onReceive(Context context, Intent intent) {
73 if (mBootCompleted) return;
74
75 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");
76 unregisterReceiver(this);
77 mBootCompleted = true;
78 if (mServicesStarted) {
79 final int N = mServices.length;
80 for (int i = 0; i < N; i++) {
81 mServices[i].onBootCompleted();
82 }
83 }
84 }
Dianne Hackbornd83a0962014-05-02 16:28:33 -070085 }, filter);
Adrian Roos070a0b62014-04-10 23:25:03 +020086 }
87
Jorim Jaggi3beffdf2014-04-03 17:37:37 +020088 /**
89 * Makes sure that all the SystemUI services are running. If they are already running, this is a
90 * no-op. This is needed to conditinally start all the services, as we only need to have it in
91 * the main process.
92 *
93 * <p>This method must only be called from the main thread.</p>
94 */
95 public void startServicesIfNeeded() {
96 if (mServicesStarted) {
97 return;
98 }
Dan Sandlerdc5f16b2014-04-22 11:51:42 -040099
100 if (!mBootCompleted) {
101 // check to see if maybe it was already completed long before we began
102 // see ActivityManagerService.finishBooting()
103 if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
104 mBootCompleted = true;
105 if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
106 }
107 }
108
109 Log.v(TAG, "Starting SystemUI services.");
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200110 final int N = SERVICES.length;
111 for (int i=0; i<N; i++) {
112 Class<?> cl = SERVICES[i];
113 if (DEBUG) Log.d(TAG, "loading: " + cl);
114 try {
115 mServices[i] = (SystemUI)cl.newInstance();
116 } catch (IllegalAccessException ex) {
117 throw new RuntimeException(ex);
118 } catch (InstantiationException ex) {
119 throw new RuntimeException(ex);
120 }
121 mServices[i].mContext = this;
122 mServices[i].mComponents = mComponents;
123 if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
124 mServices[i].start();
Dan Sandlerdc5f16b2014-04-22 11:51:42 -0400125
126 if (mBootCompleted) {
127 mServices[i].onBootCompleted();
128 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200129 }
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200130 mServicesStarted = true;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200131 }
132
133 @Override
134 public void onConfigurationChanged(Configuration newConfig) {
Jorim Jaggi3beffdf2014-04-03 17:37:37 +0200135 if (mServicesStarted) {
136 int len = mServices.length;
137 for (int i = 0; i < len; i++) {
138 mServices[i].onConfigurationChanged(newConfig);
139 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200140 }
141 }
142
143 @SuppressWarnings("unchecked")
144 public <T> T getComponent(Class<T> interfaceType) {
145 return (T) mComponents.get(interfaceType);
146 }
147
148 public SystemUI[] getServices() {
149 return mServices;
150 }
151}