• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Dependency;
35 import com.android.systemui.SystemUIApplication;
36 
37 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
38 
39 public class KeyguardService extends Service {
40     static final String TAG = "KeyguardService";
41     static final String PERMISSION = android.Manifest.permission.CONTROL_KEYGUARD;
42 
43     private KeyguardViewMediator mKeyguardViewMediator;
44     private KeyguardLifecyclesDispatcher mKeyguardLifecyclesDispatcher;
45 
46     @Override
onCreate()47     public void onCreate() {
48         ((SystemUIApplication) getApplication()).startServicesIfNeeded();
49         mKeyguardViewMediator =
50                 ((SystemUIApplication) getApplication()).getComponent(KeyguardViewMediator.class);
51         mKeyguardLifecyclesDispatcher = new KeyguardLifecyclesDispatcher(
52                 Dependency.get(ScreenLifecycle.class),
53                 Dependency.get(WakefulnessLifecycle.class));
54 
55     }
56 
57     @Override
onBind(Intent intent)58     public IBinder onBind(Intent intent) {
59         return mBinder;
60     }
61 
checkPermission()62     void checkPermission() {
63         // Avoid deadlock by avoiding calling back into the system process.
64         if (Binder.getCallingUid() == Process.SYSTEM_UID) return;
65 
66         // Otherwise,explicitly check for caller permission ...
67         if (getBaseContext().checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) {
68             Log.w(TAG, "Caller needs permission '" + PERMISSION + "' to call " + Debug.getCaller());
69             throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
70                     + ", must have permission " + PERMISSION);
71         }
72     }
73 
74     private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {
75 
76         @Override // Binder interface
77         public void addStateMonitorCallback(IKeyguardStateCallback callback) {
78             checkPermission();
79             mKeyguardViewMediator.addStateMonitorCallback(callback);
80         }
81 
82         @Override // Binder interface
83         public void verifyUnlock(IKeyguardExitCallback callback) {
84             Trace.beginSection("KeyguardService.mBinder#verifyUnlock");
85             checkPermission();
86             mKeyguardViewMediator.verifyUnlock(callback);
87             Trace.endSection();
88         }
89 
90         @Override // Binder interface
91         public void setOccluded(boolean isOccluded, boolean animate) {
92             Trace.beginSection("KeyguardService.mBinder#setOccluded");
93             checkPermission();
94             mKeyguardViewMediator.setOccluded(isOccluded, animate);
95             Trace.endSection();
96         }
97 
98         @Override // Binder interface
99         public void dismiss(IKeyguardDismissCallback callback) {
100             checkPermission();
101             mKeyguardViewMediator.dismiss(callback);
102         }
103 
104         @Override // Binder interface
105         public void onDreamingStarted() {
106             checkPermission();
107             mKeyguardViewMediator.onDreamingStarted();
108         }
109 
110         @Override // Binder interface
111         public void onDreamingStopped() {
112             checkPermission();
113             mKeyguardViewMediator.onDreamingStopped();
114         }
115 
116         @Override // Binder interface
117         public void onStartedGoingToSleep(int reason) {
118             checkPermission();
119             mKeyguardViewMediator.onStartedGoingToSleep(reason);
120             mKeyguardLifecyclesDispatcher.dispatch(
121                     KeyguardLifecyclesDispatcher.STARTED_GOING_TO_SLEEP);
122         }
123 
124         @Override // Binder interface
125         public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) {
126             checkPermission();
127             mKeyguardViewMediator.onFinishedGoingToSleep(reason, cameraGestureTriggered);
128             mKeyguardLifecyclesDispatcher.dispatch(
129                     KeyguardLifecyclesDispatcher.FINISHED_GOING_TO_SLEEP);
130         }
131 
132         @Override // Binder interface
133         public void onStartedWakingUp() {
134             Trace.beginSection("KeyguardService.mBinder#onStartedWakingUp");
135             checkPermission();
136             mKeyguardViewMediator.onStartedWakingUp();
137             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.STARTED_WAKING_UP);
138             Trace.endSection();
139         }
140 
141         @Override // Binder interface
142         public void onFinishedWakingUp() {
143             Trace.beginSection("KeyguardService.mBinder#onFinishedWakingUp");
144             checkPermission();
145             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.FINISHED_WAKING_UP);
146             Trace.endSection();
147         }
148 
149         @Override // Binder interface
150         public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
151             Trace.beginSection("KeyguardService.mBinder#onScreenTurningOn");
152             checkPermission();
153             mKeyguardViewMediator.onScreenTurningOn(callback);
154             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNING_ON);
155             Trace.endSection();
156         }
157 
158         @Override // Binder interface
159         public void onScreenTurnedOn() {
160             Trace.beginSection("KeyguardService.mBinder#onScreenTurnedOn");
161             checkPermission();
162             mKeyguardViewMediator.onScreenTurnedOn();
163             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNED_ON);
164             Trace.endSection();
165         }
166 
167         @Override // Binder interface
168         public void onScreenTurningOff() {
169             checkPermission();
170             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNING_OFF);
171         }
172 
173         @Override // Binder interface
174         public void onScreenTurnedOff() {
175             checkPermission();
176             mKeyguardViewMediator.onScreenTurnedOff();
177             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNED_OFF);
178         }
179 
180         @Override // Binder interface
181         public void setKeyguardEnabled(boolean enabled) {
182             checkPermission();
183             mKeyguardViewMediator.setKeyguardEnabled(enabled);
184         }
185 
186         @Override // Binder interface
187         public void onSystemReady() {
188             Trace.beginSection("KeyguardService.mBinder#onSystemReady");
189             checkPermission();
190             mKeyguardViewMediator.onSystemReady();
191             Trace.endSection();
192         }
193 
194         @Override // Binder interface
195         public void doKeyguardTimeout(Bundle options) {
196             checkPermission();
197             mKeyguardViewMediator.doKeyguardTimeout(options);
198         }
199 
200         @Override // Binder interface
201         public void setSwitchingUser(boolean switching) {
202             checkPermission();
203             mKeyguardViewMediator.setSwitchingUser(switching);
204         }
205 
206         @Override // Binder interface
207         public void setCurrentUser(int userId) {
208             checkPermission();
209             mKeyguardViewMediator.setCurrentUser(userId);
210         }
211 
212         @Override
213         public void onBootCompleted() {
214             checkPermission();
215             mKeyguardViewMediator.onBootCompleted();
216         }
217 
218         @Override
219         public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
220             Trace.beginSection("KeyguardService.mBinder#startKeyguardExitAnimation");
221             checkPermission();
222             mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration);
223             Trace.endSection();
224         }
225 
226         @Override
227         public void onShortPowerPressedGoHome() {
228             checkPermission();
229             mKeyguardViewMediator.onShortPowerPressedGoHome();
230         }
231     };
232 }
233 
234