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