1 /*
2  * Copyright (C) 2014 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.keyguard;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.os.Binder;
22 import android.os.Bundle;
23 import android.os.Debug;
24 import android.os.IBinder;
25 import android.os.Process;
26 import android.os.Trace;
27 import android.util.Log;
28 
29 import com.android.internal.policy.IKeyguardDismissCallback;
30 import com.android.internal.policy.IKeyguardDrawnCallback;
31 import com.android.internal.policy.IKeyguardExitCallback;
32 import com.android.internal.policy.IKeyguardService;
33 import com.android.internal.policy.IKeyguardStateCallback;
34 import com.android.systemui.SystemUIApplication;
35 
36 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
37 
38 public class KeyguardService extends Service {
39     static final String TAG = "KeyguardService";
40     static final String PERMISSION = android.Manifest.permission.CONTROL_KEYGUARD;
41 
42     private KeyguardViewMediator mKeyguardViewMediator;
43 
44     @Override
onCreate()45     public void onCreate() {
46         ((SystemUIApplication) getApplication()).startServicesIfNeeded();
47         mKeyguardViewMediator =
48                 ((SystemUIApplication) getApplication()).getComponent(KeyguardViewMediator.class);
49     }
50 
51     @Override
onBind(Intent intent)52     public IBinder onBind(Intent intent) {
53         return mBinder;
54     }
55 
checkPermission()56     void checkPermission() {
57         // Avoid deadlock by avoiding calling back into the system process.
58         if (Binder.getCallingUid() == Process.SYSTEM_UID) return;
59 
60         // Otherwise,explicitly check for caller permission ...
61         if (getBaseContext().checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) {
62             Log.w(TAG, "Caller needs permission '" + PERMISSION + "' to call " + Debug.getCaller());
63             throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
64                     + ", must have permission " + PERMISSION);
65         }
66     }
67 
68     private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {
69 
70         @Override // Binder interface
71         public void addStateMonitorCallback(IKeyguardStateCallback callback) {
72             checkPermission();
73             mKeyguardViewMediator.addStateMonitorCallback(callback);
74         }
75 
76         @Override // Binder interface
77         public void verifyUnlock(IKeyguardExitCallback callback) {
78             Trace.beginSection("KeyguardService.mBinder#verifyUnlock");
79             checkPermission();
80             mKeyguardViewMediator.verifyUnlock(callback);
81             Trace.endSection();
82         }
83 
84         @Override // Binder interface
85         public void setOccluded(boolean isOccluded, boolean animate) {
86             Trace.beginSection("KeyguardService.mBinder#setOccluded");
87             checkPermission();
88             mKeyguardViewMediator.setOccluded(isOccluded, animate);
89             Trace.endSection();
90         }
91 
92         @Override // Binder interface
93         public void dismiss(IKeyguardDismissCallback callback) {
94             checkPermission();
95             mKeyguardViewMediator.dismiss(callback);
96         }
97 
98         @Override // Binder interface
99         public void onDreamingStarted() {
100             checkPermission();
101             mKeyguardViewMediator.onDreamingStarted();
102         }
103 
104         @Override // Binder interface
105         public void onDreamingStopped() {
106             checkPermission();
107             mKeyguardViewMediator.onDreamingStopped();
108         }
109 
110         @Override // Binder interface
111         public void onStartedGoingToSleep(int reason) {
112             checkPermission();
113             mKeyguardViewMediator.onStartedGoingToSleep(reason);
114         }
115 
116         @Override // Binder interface
117         public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) {
118             checkPermission();
119             mKeyguardViewMediator.onFinishedGoingToSleep(reason, cameraGestureTriggered);
120         }
121 
122         @Override // Binder interface
123         public void onStartedWakingUp() {
124             Trace.beginSection("KeyguardService.mBinder#onStartedWakingUp");
125             checkPermission();
126             mKeyguardViewMediator.onStartedWakingUp();
127             Trace.endSection();
128         }
129 
130         @Override // Binder interface
131         public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
132             Trace.beginSection("KeyguardService.mBinder#onScreenTurningOn");
133             checkPermission();
134             mKeyguardViewMediator.onScreenTurningOn(callback);
135             Trace.endSection();
136         }
137 
138         @Override // Binder interface
139         public void onScreenTurnedOn() {
140             Trace.beginSection("KeyguardService.mBinder#onScreenTurnedOn");
141             checkPermission();
142             mKeyguardViewMediator.onScreenTurnedOn();
143             Trace.endSection();
144         }
145 
146         @Override // Binder interface
147         public void onScreenTurnedOff() {
148             checkPermission();
149             mKeyguardViewMediator.onScreenTurnedOff();
150         }
151 
152         @Override // Binder interface
153         public void setKeyguardEnabled(boolean enabled) {
154             checkPermission();
155             mKeyguardViewMediator.setKeyguardEnabled(enabled);
156         }
157 
158         @Override // Binder interface
159         public void onSystemReady() {
160             Trace.beginSection("KeyguardService.mBinder#onSystemReady");
161             checkPermission();
162             mKeyguardViewMediator.onSystemReady();
163             Trace.endSection();
164         }
165 
166         @Override // Binder interface
167         public void doKeyguardTimeout(Bundle options) {
168             checkPermission();
169             mKeyguardViewMediator.doKeyguardTimeout(options);
170         }
171 
172         @Override // Binder interface
173         public void setSwitchingUser(boolean switching) {
174             checkPermission();
175             mKeyguardViewMediator.setSwitchingUser(switching);
176         }
177 
178         @Override // Binder interface
179         public void setCurrentUser(int userId) {
180             checkPermission();
181             mKeyguardViewMediator.setCurrentUser(userId);
182         }
183 
184         @Override
185         public void onBootCompleted() {
186             checkPermission();
187             mKeyguardViewMediator.onBootCompleted();
188         }
189 
190         @Override
191         public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
192             Trace.beginSection("KeyguardService.mBinder#startKeyguardExitAnimation");
193             checkPermission();
194             mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration);
195             Trace.endSection();
196         }
197 
198         @Override
199         public void onShortPowerPressedGoHome() {
200             checkPermission();
201             mKeyguardViewMediator.onShortPowerPressedGoHome();
202         }
203     };
204 }
205 
206