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