1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui;
16
17import android.content.res.Configuration;
18import android.provider.Settings;
19import android.util.Log;
20
21import com.android.systemui.R;
22import com.android.systemui.SystemUI;
23
24import java.io.FileDescriptor;
25import java.io.PrintWriter;
26
27/**
28 * Ensure a single status bar service implementation is running at all times, using the in-process
29 * implementation according to the product config.
30 */
31public class SystemBars extends SystemUI {
32    private static final String TAG = "SystemBars";
33    private static final boolean DEBUG = false;
34    private static final int WAIT_FOR_BARS_TO_DIE = 500;
35
36    // in-process fallback implementation, per the product config
37    private SystemUI mStatusBar;
38
39    @Override
40    public void start() {
41        if (DEBUG) Log.d(TAG, "start");
42        createStatusBarFromConfig();
43    }
44
45    @Override
46    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
47        if (mStatusBar != null) {
48            mStatusBar.dump(fd, pw, args);
49        }
50    }
51
52    private void createStatusBarFromConfig() {
53        if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
54        final String clsName = mContext.getString(R.string.config_statusBarComponent);
55        if (clsName == null || clsName.length() == 0) {
56            throw andLog("No status bar component configured", null);
57        }
58        Class<?> cls = null;
59        try {
60            cls = mContext.getClassLoader().loadClass(clsName);
61        } catch (Throwable t) {
62            throw andLog("Error loading status bar component: " + clsName, t);
63        }
64        try {
65            mStatusBar = (SystemUI) cls.newInstance();
66        } catch (Throwable t) {
67            throw andLog("Error creating status bar component: " + clsName, t);
68        }
69        mStatusBar.mContext = mContext;
70        mStatusBar.mComponents = mComponents;
71        mStatusBar.start();
72        if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
73    }
74
75    private RuntimeException andLog(String msg, Throwable t) {
76        Log.w(TAG, msg, t);
77        throw new RuntimeException(msg, t);
78    }
79}
80