SystemUIApplication.java revision 3beffdf4a0081a97356f46d50e10541b1a213153
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
17package com.android.systemui;
18
19import android.app.Application;
20import android.content.res.Configuration;
21import android.util.Log;
22
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * Application class for SystemUI.
28 */
29public class SystemUIApplication extends Application {
30
31    private static final String TAG = "SystemUIService";
32    private static final boolean DEBUG = false;
33
34    /**
35     * The classes of the stuff to start.
36     */
37    private final Class<?>[] SERVICES = new Class[] {
38            com.android.systemui.keyguard.KeyguardViewMediator.class,
39            com.android.systemui.recent.Recents.class,
40            com.android.systemui.statusbar.SystemBars.class,
41            com.android.systemui.usb.StorageNotification.class,
42            com.android.systemui.power.PowerUI.class,
43            com.android.systemui.media.RingtonePlayer.class,
44            com.android.systemui.settings.SettingsUI.class,
45    };
46
47    /**
48     * Hold a reference on the stuff we start.
49     */
50    private final SystemUI[] mServices = new SystemUI[SERVICES.length];
51    private boolean mServicesStarted;
52    private final Map<Class<?>, Object> mComponents = new HashMap<Class<?>, Object>();
53
54    /**
55     * Makes sure that all the SystemUI services are running. If they are already running, this is a
56     * no-op. This is needed to conditinally start all the services, as we only need to have it in
57     * the main process.
58     *
59     * <p>This method must only be called from the main thread.</p>
60     */
61    public void startServicesIfNeeded() {
62        if (mServicesStarted) {
63            return;
64        }
65        final int N = SERVICES.length;
66        for (int i=0; i<N; i++) {
67            Class<?> cl = SERVICES[i];
68            if (DEBUG) Log.d(TAG, "loading: " + cl);
69            try {
70                mServices[i] = (SystemUI)cl.newInstance();
71            } catch (IllegalAccessException ex) {
72                throw new RuntimeException(ex);
73            } catch (InstantiationException ex) {
74                throw new RuntimeException(ex);
75            }
76            mServices[i].mContext = this;
77            mServices[i].mComponents = mComponents;
78            if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
79            mServices[i].start();
80        }
81        mServicesStarted = true;
82    }
83
84    @Override
85    public void onConfigurationChanged(Configuration newConfig) {
86        if (mServicesStarted) {
87            int len = mServices.length;
88            for (int i = 0; i < len; i++) {
89                mServices[i].onConfigurationChanged(newConfig);
90            }
91        }
92    }
93
94    @SuppressWarnings("unchecked")
95    public <T> T getComponent(Class<T> interfaceType) {
96        return (T) mComponents.get(interfaceType);
97    }
98
99    public SystemUI[] getServices() {
100        return mServices;
101    }
102}
103