1 /* 2 * Copyright (C) 2010 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; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.Build; 22 import android.os.Handler; 23 import android.os.IBinder; 24 import android.os.Process; 25 import android.os.SystemProperties; 26 import android.os.UserHandle; 27 import android.util.Slog; 28 29 import com.android.internal.os.BinderInternal; 30 import com.android.systemui.broadcast.BroadcastDispatcher; 31 import com.android.systemui.dagger.qualifiers.Main; 32 import com.android.systemui.dump.DumpHandler; 33 import com.android.systemui.dump.LogBufferEulogizer; 34 import com.android.systemui.dump.LogBufferFreezer; 35 import com.android.systemui.dump.SystemUIAuxiliaryDumpService; 36 import com.android.systemui.res.R; 37 import com.android.systemui.shared.system.UncaughtExceptionPreHandlerManager; 38 import com.android.systemui.statusbar.policy.BatteryStateNotifier; 39 40 import java.io.FileDescriptor; 41 import java.io.PrintWriter; 42 43 import javax.inject.Inject; 44 45 public class SystemUIService extends Service { 46 47 private final Handler mMainHandler; 48 private final DumpHandler mDumpHandler; 49 private final BroadcastDispatcher mBroadcastDispatcher; 50 private final LogBufferEulogizer mLogBufferEulogizer; 51 private final LogBufferFreezer mLogBufferFreezer; 52 private final BatteryStateNotifier mBatteryStateNotifier; 53 54 private final UncaughtExceptionPreHandlerManager mUncaughtExceptionPreHandlerManager; 55 56 @Inject SystemUIService( @ain Handler mainHandler, DumpHandler dumpHandler, BroadcastDispatcher broadcastDispatcher, LogBufferEulogizer logBufferEulogizer, LogBufferFreezer logBufferFreezer, BatteryStateNotifier batteryStateNotifier, UncaughtExceptionPreHandlerManager uncaughtExceptionPreHandlerManager)57 public SystemUIService( 58 @Main Handler mainHandler, 59 DumpHandler dumpHandler, 60 BroadcastDispatcher broadcastDispatcher, 61 LogBufferEulogizer logBufferEulogizer, 62 LogBufferFreezer logBufferFreezer, 63 BatteryStateNotifier batteryStateNotifier, 64 UncaughtExceptionPreHandlerManager uncaughtExceptionPreHandlerManager) { 65 super(); 66 mMainHandler = mainHandler; 67 mDumpHandler = dumpHandler; 68 mBroadcastDispatcher = broadcastDispatcher; 69 mLogBufferEulogizer = logBufferEulogizer; 70 mLogBufferFreezer = logBufferFreezer; 71 mBatteryStateNotifier = batteryStateNotifier; 72 mUncaughtExceptionPreHandlerManager = uncaughtExceptionPreHandlerManager; 73 } 74 75 @Override onCreate()76 public void onCreate() { 77 super.onCreate(); 78 79 // Start all of SystemUI 80 ((SystemUIApplication) getApplication()).startSystemUserServicesIfNeeded(); 81 82 // Finish initializing dump logic 83 mLogBufferFreezer.attach(mBroadcastDispatcher); 84 85 // Attempt to dump all LogBuffers for any uncaught exception 86 mUncaughtExceptionPreHandlerManager.registerHandler( 87 (thread, throwable) -> mLogBufferEulogizer.record(throwable)); 88 89 // If configured, set up a battery notification 90 if (getResources().getBoolean(R.bool.config_showNotificationForUnknownBatteryState)) { 91 mBatteryStateNotifier.startListening(); 92 } 93 94 // For debugging RescueParty 95 if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("debug.crash_sysui", false)) { 96 throw new RuntimeException(); 97 } 98 99 if (Build.IS_DEBUGGABLE) { 100 // b/71353150 - looking for leaked binder proxies 101 BinderInternal.nSetBinderProxyCountEnabled(true); 102 BinderInternal.nSetBinderProxyCountWatermarks( 103 /* high= */ 1000, /* low= */ 900, /* warning= */ 950); 104 BinderInternal.setBinderProxyCountCallback( 105 new BinderInternal.BinderProxyCountEventListener() { 106 @Override 107 public void onLimitReached(int uid) { 108 Slog.w(SystemUIApplication.TAG, 109 "uid " + uid + " sent too many Binder proxies to uid " 110 + Process.myUid()); 111 } 112 }, mMainHandler); 113 } 114 115 // Bind the dump service so we can dump extra info during a bug report 116 startServiceAsUser( 117 new Intent(getApplicationContext(), SystemUIAuxiliaryDumpService.class), 118 UserHandle.SYSTEM); 119 } 120 121 @Override onBind(Intent intent)122 public IBinder onBind(Intent intent) { 123 return null; 124 } 125 126 @Override dump(FileDescriptor fd, PrintWriter pw, String[] args)127 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 128 // If no args are passed, assume we're being dumped as part of a bug report (sadly, we have 129 // no better way to guess whether this is taking place). Set the appropriate dump priority 130 // (CRITICAL) to reflect that this is taking place. 131 String[] massagedArgs = args; 132 if (args.length == 0) { 133 massagedArgs = new String[] { 134 DumpHandler.PRIORITY_ARG, 135 DumpHandler.PRIORITY_ARG_CRITICAL}; 136 } 137 138 mDumpHandler.dump(fd, pw, massagedArgs); 139 } 140 } 141