15c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock/*
22a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * Copyright (C) 2017 The Android Open Source Project
35c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock *
42a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
52a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * except in compliance with the License. You may obtain a copy of the License at
65c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock *
75c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
85c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock *
92a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
102a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
112a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * KIND, either express or implied. See the License for the specific language governing
122a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk * permissions and limitations under the License.
135c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock */
145c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
152a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monkpackage com.android.systemui;
165c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
17777dcdeeb67c570168e04d5096d2a7a9eb408ceaDaniel Sandlerimport android.content.res.Configuration;
185c4541246c6a70f53552423dc35940386788bd5fJohn Spurlockimport android.provider.Settings;
195c4541246c6a70f53552423dc35940386788bd5fJohn Spurlockimport android.util.Log;
205c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
215c4541246c6a70f53552423dc35940386788bd5fJohn Spurlockimport com.android.systemui.R;
225c4541246c6a70f53552423dc35940386788bd5fJohn Spurlockimport com.android.systemui.SystemUI;
235c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
240ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlockimport java.io.FileDescriptor;
250ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlockimport java.io.PrintWriter;
260ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock
275c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock/**
28bb983d20d2cacebdf81efe88f8ff4aa7304fbff7Julia Reynolds * Ensure a single status bar service implementation is running at all times, using the in-process
29bb983d20d2cacebdf81efe88f8ff4aa7304fbff7Julia Reynolds * implementation according to the product config.
305c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock */
31bb983d20d2cacebdf81efe88f8ff4aa7304fbff7Julia Reynoldspublic class SystemBars extends SystemUI {
325c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    private static final String TAG = "SystemBars";
33342cad772be1973875dea6a31ceb04bc72e7a6b5John Spurlock    private static final boolean DEBUG = false;
345c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    private static final int WAIT_FOR_BARS_TO_DIE = 500;
355c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
365c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    // in-process fallback implementation, per the product config
372a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk    private SystemUI mStatusBar;
385c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
395c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    @Override
405c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    public void start() {
415c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        if (DEBUG) Log.d(TAG, "start");
42bb983d20d2cacebdf81efe88f8ff4aa7304fbff7Julia Reynolds        createStatusBarFromConfig();
435c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    }
445c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
450ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock    @Override
460ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
470ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock        if (mStatusBar != null) {
480ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock            mStatusBar.dump(fd, pw, args);
490ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock        }
500ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock    }
510ec64c65fb7fbfd89556bc33f5caab4ef0937fd4John Spurlock
525c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    private void createStatusBarFromConfig() {
535c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
545c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        final String clsName = mContext.getString(R.string.config_statusBarComponent);
555c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        if (clsName == null || clsName.length() == 0) {
565c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock            throw andLog("No status bar component configured", null);
575c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        }
585c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        Class<?> cls = null;
595c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        try {
605c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock            cls = mContext.getClassLoader().loadClass(clsName);
615c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        } catch (Throwable t) {
625c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock            throw andLog("Error loading status bar component: " + clsName, t);
635c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        }
645c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        try {
652a6ea9c2a1b52b0386270ec73e1e6d6a9b614a34Jason Monk            mStatusBar = (SystemUI) cls.newInstance();
665c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        } catch (Throwable t) {
675c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock            throw andLog("Error creating status bar component: " + clsName, t);
685c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        }
695c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        mStatusBar.mContext = mContext;
70d08de37a2223d5183620098737eb93907a4ba92cJohn Spurlock        mStatusBar.mComponents = mComponents;
715c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        mStatusBar.start();
725c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
735c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    }
745c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock
755c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    private RuntimeException andLog(String msg, Throwable t) {
765c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        Log.w(TAG, msg, t);
775c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock        throw new RuntimeException(msg, t);
785c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock    }
795c4541246c6a70f53552423dc35940386788bd5fJohn Spurlock}
80