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.util.Log; 26 import android.view.MotionEvent; 27 28 import com.android.internal.policy.IKeyguardExitCallback; 29 import com.android.internal.policy.IKeyguardService; 30 import com.android.internal.policy.IKeyguardServiceConstants; 31 import com.android.internal.policy.IKeyguardShowCallback; 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 if (getBaseContext().checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) { 56 Log.w(TAG, "Caller needs permission '" + PERMISSION + "' to call " + Debug.getCaller()); 57 throw new SecurityException("Access denied to process: " + Binder.getCallingPid() 58 + ", must have permission " + PERMISSION); 59 } 60 } 61 62 private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() { 63 64 private boolean mIsOccluded; 65 66 @Override 67 public boolean isShowing() { 68 return mKeyguardViewMediator.isShowing(); 69 } 70 71 @Override 72 public boolean isSecure() { 73 return mKeyguardViewMediator.isSecure(); 74 } 75 76 @Override 77 public boolean isShowingAndNotOccluded() { 78 return mKeyguardViewMediator.isShowingAndNotOccluded(); 79 } 80 81 @Override 82 public boolean isInputRestricted() { 83 return mKeyguardViewMediator.isInputRestricted(); 84 } 85 86 @Override 87 public void verifyUnlock(IKeyguardExitCallback callback) { 88 checkPermission(); 89 mKeyguardViewMediator.verifyUnlock(callback); 90 } 91 92 @Override 93 public void keyguardDone(boolean authenticated, boolean wakeup) { 94 checkPermission(); 95 mKeyguardViewMediator.keyguardDone(authenticated, wakeup); 96 } 97 98 @Override 99 public int setOccluded(boolean isOccluded) { 100 checkPermission(); 101 synchronized (this) { 102 int result; 103 if (isOccluded && mKeyguardViewMediator.isShowing() 104 && !mIsOccluded) { 105 result = IKeyguardServiceConstants 106 .KEYGUARD_SERVICE_SET_OCCLUDED_RESULT_UNSET_FLAGS; 107 } else if (!isOccluded && mKeyguardViewMediator.isShowing() 108 && mIsOccluded) { 109 result = IKeyguardServiceConstants 110 .KEYGUARD_SERVICE_SET_OCCLUDED_RESULT_SET_FLAGS; 111 } else { 112 result = IKeyguardServiceConstants.KEYGUARD_SERVICE_SET_OCCLUDED_RESULT_NONE; 113 } 114 if (mIsOccluded != isOccluded) { 115 mKeyguardViewMediator.setOccluded(isOccluded); 116 117 // Cache the value so we always have a fresh view in whether Keyguard is occluded. 118 // If we would just call mKeyguardViewMediator.isOccluded(), this might be stale 119 // because that value gets updated in another thread. 120 mIsOccluded = isOccluded; 121 } 122 return result; 123 } 124 } 125 126 @Override 127 public void dismiss() { 128 checkPermission(); 129 mKeyguardViewMediator.dismiss(); 130 } 131 132 @Override 133 public void onDreamingStarted() { 134 checkPermission(); 135 mKeyguardViewMediator.onDreamingStarted(); 136 } 137 138 @Override 139 public void onDreamingStopped() { 140 checkPermission(); 141 mKeyguardViewMediator.onDreamingStopped(); 142 } 143 144 @Override 145 public void onScreenTurnedOff(int reason) { 146 checkPermission(); 147 mKeyguardViewMediator.onScreenTurnedOff(reason); 148 } 149 150 @Override 151 public void onScreenTurnedOn(IKeyguardShowCallback callback) { 152 checkPermission(); 153 mKeyguardViewMediator.onScreenTurnedOn(callback); 154 } 155 156 @Override 157 public void setKeyguardEnabled(boolean enabled) { 158 checkPermission(); 159 mKeyguardViewMediator.setKeyguardEnabled(enabled); 160 } 161 162 @Override 163 public boolean isDismissable() { 164 return mKeyguardViewMediator.isDismissable(); 165 } 166 167 @Override 168 public void onSystemReady() { 169 checkPermission(); 170 mKeyguardViewMediator.onSystemReady(); 171 } 172 173 @Override 174 public void doKeyguardTimeout(Bundle options) { 175 checkPermission(); 176 mKeyguardViewMediator.doKeyguardTimeout(options); 177 } 178 179 @Override 180 public void setCurrentUser(int userId) { 181 checkPermission(); 182 mKeyguardViewMediator.setCurrentUser(userId); 183 } 184 185 @Override 186 public void showAssistant() { 187 checkPermission(); 188 } 189 190 @Override 191 public void dispatch(MotionEvent event) { 192 checkPermission(); 193 } 194 195 @Override 196 public void launchCamera() { 197 checkPermission(); 198 } 199 200 @Override 201 public void onBootCompleted() { 202 checkPermission(); 203 mKeyguardViewMediator.onBootCompleted(); 204 } 205 206 @Override 207 public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) { 208 checkPermission(); 209 mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration); 210 } 211 212 @Override 213 public void onActivityDrawn() { 214 checkPermission(); 215 mKeyguardViewMediator.onActivityDrawn(); 216 } 217 }; 218 } 219 220