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