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