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