1/*
2 * Copyright (C) 2013 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.keyguard;
18
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21
22import android.app.Service;
23import android.content.Intent;
24import static android.content.pm.PackageManager.PERMISSION_GRANTED;
25
26import android.os.Binder;
27import android.os.Bundle;
28import android.os.Debug;
29import android.os.IBinder;
30import android.util.Log;
31import android.view.MotionEvent;
32
33import com.android.internal.policy.IKeyguardService;
34import com.android.internal.policy.IKeyguardExitCallback;
35import com.android.internal.policy.IKeyguardShowCallback;
36import com.android.internal.widget.LockPatternUtils;
37
38public class KeyguardService extends Service {
39    static final String TAG = "KeyguardService";
40    static final String PERMISSION = android.Manifest.permission.CONTROL_KEYGUARD;
41    private KeyguardViewMediator mKeyguardViewMediator;
42
43    @Override
44    public void onCreate() {
45        if (mKeyguardViewMediator == null) {
46            mKeyguardViewMediator = new KeyguardViewMediator(
47                    KeyguardService.this, new LockPatternUtils(KeyguardService.this));
48        }
49        Log.v(TAG, "onCreate()");
50    }
51
52    @Override
53    public IBinder onBind(Intent intent) {
54        return mBinder;
55    }
56
57    @Override
58    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
59        // TODO
60    }
61
62    void checkPermission() {
63        if (getBaseContext().checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) {
64            Log.w(TAG, "Caller needs permission '" + PERMISSION + "' to call " + Debug.getCaller());
65            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
66                    + ", must have permission " + PERMISSION);
67        }
68    }
69
70    private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {
71        public boolean isShowing() {
72            return mKeyguardViewMediator.isShowing();
73        }
74        public boolean isSecure() {
75            return mKeyguardViewMediator.isSecure();
76        }
77        public boolean isShowingAndNotHidden() {
78            return mKeyguardViewMediator.isShowingAndNotHidden();
79        }
80        public boolean isInputRestricted() {
81            return mKeyguardViewMediator.isInputRestricted();
82        }
83        public void verifyUnlock(IKeyguardExitCallback callback) {
84            mKeyguardViewMediator.verifyUnlock(callback);
85        }
86        public void keyguardDone(boolean authenticated, boolean wakeup) {
87            checkPermission();
88            mKeyguardViewMediator.keyguardDone(authenticated, wakeup);
89        }
90        public void setHidden(boolean isHidden) {
91            checkPermission();
92            mKeyguardViewMediator.setHidden(isHidden);
93        }
94        public void dismiss() {
95            mKeyguardViewMediator.dismiss();
96        }
97        public void onDreamingStarted() {
98            checkPermission();
99            mKeyguardViewMediator.onDreamingStarted();
100        }
101        public void onDreamingStopped() {
102            checkPermission();
103            mKeyguardViewMediator.onDreamingStopped();
104        }
105        public void onScreenTurnedOff(int reason) {
106            checkPermission();
107            mKeyguardViewMediator.onScreenTurnedOff(reason);
108        }
109        public void onScreenTurnedOn(IKeyguardShowCallback callback) {
110            checkPermission();
111            mKeyguardViewMediator.onScreenTurnedOn(callback);
112        }
113        public void setKeyguardEnabled(boolean enabled) {
114            checkPermission();
115            mKeyguardViewMediator.setKeyguardEnabled(enabled);
116        }
117        public boolean isDismissable() {
118            return mKeyguardViewMediator.isDismissable();
119        }
120        public void onSystemReady() {
121            checkPermission();
122            mKeyguardViewMediator.onSystemReady();
123        }
124        public void doKeyguardTimeout(Bundle options) {
125            checkPermission();
126            mKeyguardViewMediator.doKeyguardTimeout(options);
127        }
128        public void setCurrentUser(int userId) {
129            checkPermission();
130            mKeyguardViewMediator.setCurrentUser(userId);
131        }
132        public void showAssistant() {
133            checkPermission();
134            mKeyguardViewMediator.showAssistant();
135        }
136        public void dispatch(MotionEvent event) {
137            checkPermission();
138            mKeyguardViewMediator.dispatch(event);
139        }
140        public void launchCamera() {
141            checkPermission();
142            mKeyguardViewMediator.launchCamera();
143        }
144        public void onBootCompleted() {
145            checkPermission();
146            mKeyguardViewMediator.onBootCompleted();
147        }
148    };
149
150}
151
152