John Spurlock | 5c45412 | 2013-06-17 07:35:46 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.provider.Settings; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import com.android.systemui.R; |
| 23 | import 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 | */ |
| 32 | public 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 Spurlock | d08de37 | 2013-06-24 13:06:08 -0400 | [diff] [blame] | 87 | mStatusBar.mComponents = mComponents; |
John Spurlock | 5c45412 | 2013-06-17 07:35:46 -0400 | [diff] [blame] | 88 | 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 | } |