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; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 25 | import android.os.SystemProperties; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | |
| 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
| 30 | |
| 31 | /** |
| 32 | * Application class for SystemUI. |
| 33 | */ |
| 34 | public 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 Jaggi | d61f227 | 2014-12-19 20:35:35 +0100 | [diff] [blame] | 44 | com.android.systemui.recents.Recents.class, |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 45 | com.android.systemui.volume.VolumeUI.class, |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 46 | com.android.systemui.statusbar.SystemBars.class, |
| 47 | com.android.systemui.usb.StorageNotification.class, |
| 48 | com.android.systemui.power.PowerUI.class, |
Alan Viverette | 5a39949 | 2014-07-14 16:19:38 -0700 | [diff] [blame] | 49 | com.android.systemui.media.RingtonePlayer.class |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Hold a reference on the stuff we start. |
| 54 | */ |
| 55 | private final SystemUI[] mServices = new SystemUI[SERVICES.length]; |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 56 | private boolean mServicesStarted; |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 57 | private boolean mBootCompleted; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 58 | private final Map<Class<?>, Object> mComponents = new HashMap<Class<?>, Object>(); |
| 59 | |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 60 | @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 Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 67 | |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 68 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 69 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Dan Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 70 | 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 Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 85 | }, filter); |
Adrian Roos | 070a0b6 | 2014-04-10 23:25:03 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 88 | /** |
| 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 Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 99 | |
| 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 Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 110 | 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 Sandler | dc5f16b | 2014-04-22 11:51:42 -0400 | [diff] [blame] | 125 | |
| 126 | if (mBootCompleted) { |
| 127 | mServices[i].onBootCompleted(); |
| 128 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 129 | } |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 130 | mServicesStarted = true; |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onConfigurationChanged(Configuration newConfig) { |
Jorim Jaggi | 3beffdf | 2014-04-03 17:37:37 +0200 | [diff] [blame] | 135 | if (mServicesStarted) { |
| 136 | int len = mServices.length; |
| 137 | for (int i = 0; i < len; i++) { |
| 138 | mServices[i].onConfigurationChanged(newConfig); |
| 139 | } |
Jorim Jaggi | cff0acb | 2014-03-31 16:35:15 +0200 | [diff] [blame] | 140 | } |
| 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 | } |