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.IKeyguardExitCallback; 29 import com.android.internal.policy.IKeyguardService; 30 import com.android.internal.policy.IKeyguardShowCallback; 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 43 public void onCreate() { 44 ((SystemUIApplication) getApplication()).startServicesIfNeeded(); 45 mKeyguardViewMediator = 46 ((SystemUIApplication) getApplication()).getComponent(KeyguardViewMediator.class); 47 } 48 49 @Override 50 public IBinder onBind(Intent intent) { 51 return mBinder; 52 } 53 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 mKeyguardViewMediator.keyguardDone(authenticated, wakeup); 84 } 85 86 @Override // Binder interface 87 public void setOccluded(boolean isOccluded) { 88 checkPermission(); 89 mKeyguardViewMediator.setOccluded(isOccluded); 90 } 91 92 @Override // Binder interface 93 public void dismiss() { 94 checkPermission(); 95 mKeyguardViewMediator.dismiss(); 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 onScreenTurnedOff(int reason) { 112 checkPermission(); 113 mKeyguardViewMediator.onScreenTurnedOff(reason); 114 } 115 116 @Override // Binder interface 117 public void onScreenTurnedOn(IKeyguardShowCallback callback) { 118 checkPermission(); 119 mKeyguardViewMediator.onScreenTurnedOn(callback); 120 } 121 122 @Override // Binder interface 123 public void setKeyguardEnabled(boolean enabled) { 124 checkPermission(); 125 mKeyguardViewMediator.setKeyguardEnabled(enabled); 126 } 127 128 @Override // Binder interface 129 public void onSystemReady() { 130 checkPermission(); 131 mKeyguardViewMediator.onSystemReady(); 132 } 133 134 @Override // Binder interface 135 public void doKeyguardTimeout(Bundle options) { 136 checkPermission(); 137 mKeyguardViewMediator.doKeyguardTimeout(options); 138 } 139 140 @Override // Binder interface 141 public void setCurrentUser(int userId) { 142 checkPermission(); 143 mKeyguardViewMediator.setCurrentUser(userId); 144 } 145 146 @Override 147 public void onBootCompleted() { 148 checkPermission(); 149 mKeyguardViewMediator.onBootCompleted(); 150 } 151 152 @Override 153 public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) { 154 checkPermission(); 155 mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration); 156 } 157 158 @Override 159 public void onActivityDrawn() { 160 checkPermission(); 161 mKeyguardViewMediator.onActivityDrawn(); 162 } 163 }; 164 } 165 166