blob: 05282fee87c01013c5ef5e408e99c2c819c54422 [file] [log] [blame]
John Spurlock5c454122013-06-17 07:35:46 -04001/*
2 * Copyright (C) 2013 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.statusbar;
18
19import android.provider.Settings;
20import android.util.Log;
21
22import com.android.systemui.R;
23import com.android.systemui.SystemUI;
24
25/**
26 * Ensure a single status bar service implementation is running at all times.
27 *
28 * <p>The implementation either comes from a service component running in a remote process (defined
29 * using a secure setting), else falls back to using the in-process implementation according
30 * to the product config.
31 */
32public class SystemBars extends SystemUI implements ServiceMonitor.Callbacks {
33 private static final String TAG = "SystemBars";
34 private static final boolean DEBUG = true;
35 private static final int WAIT_FOR_BARS_TO_DIE = 500;
36
37 // manages the implementation coming from the remote process
38 private ServiceMonitor mServiceMonitor;
39
40 // in-process fallback implementation, per the product config
41 private BaseStatusBar mStatusBar;
42
43 @Override
44 public void start() {
45 if (DEBUG) Log.d(TAG, "start");
46 mServiceMonitor = new ServiceMonitor(TAG, DEBUG,
47 mContext, Settings.Secure.BAR_SERVICE_COMPONENT, this);
48 mServiceMonitor.start(); // will call onNoService if no remote service is found
49 }
50
51 @Override
52 public void onNoService() {
53 if (DEBUG) Log.d(TAG, "onNoService");
54 createStatusBarFromConfig(); // fallback to using an in-process implementation
55 }
56
57 @Override
58 public long onServiceStartAttempt() {
59 if (DEBUG) Log.d(TAG, "onServiceStartAttempt mStatusBar="+mStatusBar);
60 if (mStatusBar != null) {
61 // tear down the in-process version, we'll recreate it again if needed
62 mStatusBar.destroy();
63 mStatusBar = null;
64 return WAIT_FOR_BARS_TO_DIE;
65 }
66 return 0;
67 }
68
69 private void createStatusBarFromConfig() {
70 if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
71 final String clsName = mContext.getString(R.string.config_statusBarComponent);
72 if (clsName == null || clsName.length() == 0) {
73 throw andLog("No status bar component configured", null);
74 }
75 Class<?> cls = null;
76 try {
77 cls = mContext.getClassLoader().loadClass(clsName);
78 } catch (Throwable t) {
79 throw andLog("Error loading status bar component: " + clsName, t);
80 }
81 try {
82 mStatusBar = (BaseStatusBar) cls.newInstance();
83 } catch (Throwable t) {
84 throw andLog("Error creating status bar component: " + clsName, t);
85 }
86 mStatusBar.mContext = mContext;
John Spurlockd08de372013-06-24 13:06:08 -040087 mStatusBar.mComponents = mComponents;
John Spurlock5c454122013-06-17 07:35:46 -040088 mStatusBar.start();
89 if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
90 }
91
92 private RuntimeException andLog(String msg, Throwable t) {
93 Log.w(TAG, msg, t);
94 throw new RuntimeException(msg, t);
95 }
96}