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; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.content.res.Configuration; 25 import android.os.SystemProperties; 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, 44 com.android.systemui.recent.Recents.class, 45 com.android.systemui.volume.VolumeUI.class, 46 com.android.systemui.statusbar.SystemBars.class, 47 com.android.systemui.usb.StorageNotification.class, 48 com.android.systemui.power.PowerUI.class, 49 com.android.systemui.media.RingtonePlayer.class 50 }; 51 52 /** 53 * Hold a reference on the stuff we start. 54 */ 55 private final SystemUI[] mServices = new SystemUI[SERVICES.length]; 56 private boolean mServicesStarted; 57 private boolean mBootCompleted; 58 private final Map<Class<?>, Object> mComponents = new HashMap<Class<?>, Object>(); 59 60 @Override onCreate()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); 67 68 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); 69 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); 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 } 85 }, filter); 86 } 87 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 */ startServicesIfNeeded()95 public void startServicesIfNeeded() { 96 if (mServicesStarted) { 97 return; 98 } 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."); 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(); 125 126 if (mBootCompleted) { 127 mServices[i].onBootCompleted(); 128 } 129 } 130 mServicesStarted = true; 131 } 132 133 @Override onConfigurationChanged(Configuration newConfig)134 public void onConfigurationChanged(Configuration newConfig) { 135 if (mServicesStarted) { 136 int len = mServices.length; 137 for (int i = 0; i < len; i++) { 138 mServices[i].onConfigurationChanged(newConfig); 139 } 140 } 141 } 142 143 @SuppressWarnings("unchecked") getComponent(Class<T> interfaceType)144 public <T> T getComponent(Class<T> interfaceType) { 145 return (T) mComponents.get(interfaceType); 146 } 147 getServices()148 public SystemUI[] getServices() { 149 return mServices; 150 } 151 } 152