blob: c1646bac974f54820f09ab3ce3408a4f2e792f69 [file] [log] [blame]
Jim Millercaf24fc2013-09-10 18:37:01 -07001/*
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.systemui.statusbar.phone;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.os.UserHandle;
Jim Miller6c9df502013-09-19 15:50:11 -070026import android.util.Slog;
Jim Millercaf24fc2013-09-10 18:37:01 -070027import android.view.MotionEvent;
28
29import com.android.internal.policy.IKeyguardExitCallback;
30import com.android.internal.policy.IKeyguardShowCallback;
31import com.android.internal.policy.IKeyguardService;
32
33
34/**
35 * Facilitates event communication between navigation bar and keyguard. Currently used to
36 * control WidgetPager in keyguard to expose the camera widget.
37 *
38 */
39public class KeyguardTouchDelegate {
40 // TODO: propagate changes to these to {@link KeyguardServiceDelegate}
41 static final String KEYGUARD_PACKAGE = "com.android.keyguard";
42 static final String KEYGUARD_CLASS = "com.android.keyguard.KeyguardService";
43
Jim Miller6c9df502013-09-19 15:50:11 -070044 private static KeyguardTouchDelegate sInstance;
45
46 private volatile IKeyguardService mService;
Jim Millercaf24fc2013-09-10 18:37:01 -070047
48 protected static final boolean DEBUG = false;
49 protected static final String TAG = "KeyguardTouchDelegate";
50
51 private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
52 @Override
53 public void onServiceConnected(ComponentName name, IBinder service) {
Jim Miller6c9df502013-09-19 15:50:11 -070054 Slog.v(TAG, "Connected to keyguard");
Jim Millercaf24fc2013-09-10 18:37:01 -070055 mService = IKeyguardService.Stub.asInterface(service);
56
57 }
58
59 @Override
60 public void onServiceDisconnected(ComponentName name) {
Jim Miller6c9df502013-09-19 15:50:11 -070061 Slog.v(TAG, "Disconnected from keyguard");
Jim Millercaf24fc2013-09-10 18:37:01 -070062 mService = null;
Jim Miller6c9df502013-09-19 15:50:11 -070063 sInstance = null; // force reconnection if this goes away
Jim Millercaf24fc2013-09-10 18:37:01 -070064 }
65
66 };
67
Jim Miller6c9df502013-09-19 15:50:11 -070068 private KeyguardTouchDelegate(Context context) {
Jim Millercaf24fc2013-09-10 18:37:01 -070069 Intent intent = new Intent();
70 intent.setClassName(KEYGUARD_PACKAGE, KEYGUARD_CLASS);
71 if (!context.bindServiceAsUser(intent, mKeyguardConnection,
72 Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
Jim Miller6c9df502013-09-19 15:50:11 -070073 if (DEBUG) Slog.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS);
Jim Millercaf24fc2013-09-10 18:37:01 -070074 } else {
Jim Miller6c9df502013-09-19 15:50:11 -070075 if (DEBUG) Slog.v(TAG, "*** Keyguard started");
Jim Millercaf24fc2013-09-10 18:37:01 -070076 }
77 }
78
Jim Miller6c9df502013-09-19 15:50:11 -070079 public static KeyguardTouchDelegate getInstance(Context context) {
Jim Millera999d462013-10-30 13:58:11 -070080 KeyguardTouchDelegate instance = sInstance;
81 if (instance == null) {
82 instance = sInstance = new KeyguardTouchDelegate(context);
Jim Miller6c9df502013-09-19 15:50:11 -070083 }
Jim Millera999d462013-10-30 13:58:11 -070084 return instance;
Jim Miller6c9df502013-09-19 15:50:11 -070085 }
86
Jim Millercaf24fc2013-09-10 18:37:01 -070087 public boolean isSecure() {
Jim Miller6c9df502013-09-19 15:50:11 -070088 final IKeyguardService service = mService;
89 if (service != null) {
Jim Millercaf24fc2013-09-10 18:37:01 -070090 try {
Jim Miller6c9df502013-09-19 15:50:11 -070091 return service.isSecure();
Jim Millercaf24fc2013-09-10 18:37:01 -070092 } catch (RemoteException e) {
Jim Miller6c9df502013-09-19 15:50:11 -070093 Slog.e(TAG, "RemoteException calling keyguard.isSecure()!", e);
Jim Millercaf24fc2013-09-10 18:37:01 -070094 }
95 } else {
Jim Miller6c9df502013-09-19 15:50:11 -070096 Slog.w(TAG, "isSecure(): NO SERVICE!");
Jim Millercaf24fc2013-09-10 18:37:01 -070097 }
Jim Miller6c9df502013-09-19 15:50:11 -070098 return false;
Jim Millercaf24fc2013-09-10 18:37:01 -070099 }
100
101 public boolean dispatch(MotionEvent event) {
Jim Miller6c9df502013-09-19 15:50:11 -0700102 final IKeyguardService service = mService;
103 if (service != null) {
Jim Millercaf24fc2013-09-10 18:37:01 -0700104 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700105 service.dispatch(event);
106 return true;
Jim Millercaf24fc2013-09-10 18:37:01 -0700107 } catch (RemoteException e) {
108 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700109 Slog.e(TAG, "RemoteException sending event to keyguard!", e);
Jim Millercaf24fc2013-09-10 18:37:01 -0700110 }
Jim Millercaf24fc2013-09-10 18:37:01 -0700111 } else {
Jim Miller6c9df502013-09-19 15:50:11 -0700112 Slog.w(TAG, "dispatch(event): NO SERVICE!");
113 }
114 return false;
115 }
116
117 public boolean isInputRestricted() {
118 final IKeyguardService service = mService;
119 if (service != null) {
120 try {
121 return service.isInputRestricted();
122 } catch (RemoteException e) {
123 Slog.w(TAG , "Remote Exception", e);
124 }
125 } else {
126 Slog.w(TAG, "isInputRestricted(): NO SERVICE!");
127 }
128 return false;
129 }
130
131 public boolean isShowingAndNotHidden() {
132 final IKeyguardService service = mService;
133 if (service != null) {
134 try {
135 return service.isShowingAndNotHidden();
136 } catch (RemoteException e) {
137 Slog.w(TAG , "Remote Exception", e);
138 }
139 } else {
140 Slog.w(TAG, "isShowingAndNotHidden(): NO SERVICE!");
Jim Millercaf24fc2013-09-10 18:37:01 -0700141 }
142 return false;
143 }
144
Jim Miller138f25d2013-09-25 13:46:58 -0700145 public void showAssistant() {
Jim Miller6c9df502013-09-19 15:50:11 -0700146 final IKeyguardService service = mService;
147 if (service != null) {
Jim Miller138f25d2013-09-25 13:46:58 -0700148 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700149 service.showAssistant();
Jim Miller138f25d2013-09-25 13:46:58 -0700150 } catch (RemoteException e) {
151 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700152 Slog.e(TAG, "RemoteException launching assistant!", e);
Jim Miller138f25d2013-09-25 13:46:58 -0700153 }
154 } else {
Jim Miller6c9df502013-09-19 15:50:11 -0700155 Slog.w(TAG, "showAssistant(event): NO SERVICE!");
Jim Miller138f25d2013-09-25 13:46:58 -0700156 }
157 }
158
159 public void launchCamera() {
Jim Miller6c9df502013-09-19 15:50:11 -0700160 final IKeyguardService service = mService;
161 if (service != null) {
Jim Miller138f25d2013-09-25 13:46:58 -0700162 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700163 service.launchCamera();
Jim Miller138f25d2013-09-25 13:46:58 -0700164 } catch (RemoteException e) {
165 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700166 Slog.e(TAG, "RemoteException launching camera!", e);
Jim Miller138f25d2013-09-25 13:46:58 -0700167 }
168 } else {
Jim Millera999d462013-10-30 13:58:11 -0700169 Slog.w(TAG, "launchCamera(): NO SERVICE!");
170 }
171 }
172
173 public void dismiss() {
174 final IKeyguardService service = mService;
175 if (service != null) {
176 try {
177 service.dismiss();
178 } catch (RemoteException e) {
179 // What to do?
180 Slog.e(TAG, "RemoteException dismissing keyguard!", e);
181 }
182 } else {
183 Slog.w(TAG, "dismiss(): NO SERVICE!");
Jim Miller138f25d2013-09-25 13:46:58 -0700184 }
185 }
186
Jim Millercaf24fc2013-09-10 18:37:01 -0700187}