blob: 1ea920de435fa85382021eed46ccae8553c2a128 [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
Jim Millercaf24fc2013-09-10 18:37:01 -070029import com.android.internal.policy.IKeyguardService;
30
John Spurlockdfc63192014-01-13 11:27:25 -050031import java.util.ArrayList;
Adrian Roos5b0acf02014-01-10 12:58:22 -080032import java.util.List;
33
Jim Millercaf24fc2013-09-10 18:37:01 -070034
35/**
36 * Facilitates event communication between navigation bar and keyguard. Currently used to
37 * control WidgetPager in keyguard to expose the camera widget.
38 *
39 */
40public class KeyguardTouchDelegate {
41 // TODO: propagate changes to these to {@link KeyguardServiceDelegate}
42 static final String KEYGUARD_PACKAGE = "com.android.keyguard";
43 static final String KEYGUARD_CLASS = "com.android.keyguard.KeyguardService";
44
Jim Miller6c9df502013-09-19 15:50:11 -070045 private static KeyguardTouchDelegate sInstance;
Adrian Roos5b0acf02014-01-10 12:58:22 -080046 private static final List<OnKeyguardConnectionListener> sConnectionListeners =
John Spurlockdfc63192014-01-13 11:27:25 -050047 new ArrayList<OnKeyguardConnectionListener>();
Jim Miller6c9df502013-09-19 15:50:11 -070048
49 private volatile IKeyguardService mService;
Jim Millercaf24fc2013-09-10 18:37:01 -070050
51 protected static final boolean DEBUG = false;
52 protected static final String TAG = "KeyguardTouchDelegate";
53
54 private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
55 @Override
56 public void onServiceConnected(ComponentName name, IBinder service) {
Jim Miller6c9df502013-09-19 15:50:11 -070057 Slog.v(TAG, "Connected to keyguard");
Jim Millercaf24fc2013-09-10 18:37:01 -070058 mService = IKeyguardService.Stub.asInterface(service);
59
Adrian Roos5b0acf02014-01-10 12:58:22 -080060 for (int i = 0; i < sConnectionListeners.size(); i++) {
61 OnKeyguardConnectionListener listener = sConnectionListeners.get(i);
62 listener.onKeyguardServiceConnected(KeyguardTouchDelegate.this);
63 }
Jim Millercaf24fc2013-09-10 18:37:01 -070064 }
65
66 @Override
67 public void onServiceDisconnected(ComponentName name) {
Jim Miller6c9df502013-09-19 15:50:11 -070068 Slog.v(TAG, "Disconnected from keyguard");
Jim Millercaf24fc2013-09-10 18:37:01 -070069 mService = null;
Jim Miller6c9df502013-09-19 15:50:11 -070070 sInstance = null; // force reconnection if this goes away
Adrian Roos5b0acf02014-01-10 12:58:22 -080071
72 for (int i = 0; i < sConnectionListeners.size(); i++) {
73 OnKeyguardConnectionListener listener = sConnectionListeners.get(i);
74 listener.onKeyguardServiceDisconnected(KeyguardTouchDelegate.this);
75 }
Jim Millercaf24fc2013-09-10 18:37:01 -070076 }
77
78 };
79
Jim Miller6c9df502013-09-19 15:50:11 -070080 private KeyguardTouchDelegate(Context context) {
Jim Millercaf24fc2013-09-10 18:37:01 -070081 Intent intent = new Intent();
82 intent.setClassName(KEYGUARD_PACKAGE, KEYGUARD_CLASS);
83 if (!context.bindServiceAsUser(intent, mKeyguardConnection,
84 Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
Jim Miller6c9df502013-09-19 15:50:11 -070085 if (DEBUG) Slog.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS);
Jim Millercaf24fc2013-09-10 18:37:01 -070086 } else {
Jim Miller6c9df502013-09-19 15:50:11 -070087 if (DEBUG) Slog.v(TAG, "*** Keyguard started");
Jim Millercaf24fc2013-09-10 18:37:01 -070088 }
89 }
90
Jim Miller6c9df502013-09-19 15:50:11 -070091 public static KeyguardTouchDelegate getInstance(Context context) {
Jim Millera999d462013-10-30 13:58:11 -070092 KeyguardTouchDelegate instance = sInstance;
93 if (instance == null) {
94 instance = sInstance = new KeyguardTouchDelegate(context);
Jim Miller6c9df502013-09-19 15:50:11 -070095 }
Jim Millera999d462013-10-30 13:58:11 -070096 return instance;
Jim Miller6c9df502013-09-19 15:50:11 -070097 }
98
Jim Millercaf24fc2013-09-10 18:37:01 -070099 public boolean isSecure() {
Jim Miller6c9df502013-09-19 15:50:11 -0700100 final IKeyguardService service = mService;
101 if (service != null) {
Jim Millercaf24fc2013-09-10 18:37:01 -0700102 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700103 return service.isSecure();
Jim Millercaf24fc2013-09-10 18:37:01 -0700104 } catch (RemoteException e) {
Jim Miller6c9df502013-09-19 15:50:11 -0700105 Slog.e(TAG, "RemoteException calling keyguard.isSecure()!", e);
Jim Millercaf24fc2013-09-10 18:37:01 -0700106 }
107 } else {
Jim Miller6c9df502013-09-19 15:50:11 -0700108 Slog.w(TAG, "isSecure(): NO SERVICE!");
Jim Millercaf24fc2013-09-10 18:37:01 -0700109 }
Jim Miller6c9df502013-09-19 15:50:11 -0700110 return false;
Jim Millercaf24fc2013-09-10 18:37:01 -0700111 }
112
113 public boolean dispatch(MotionEvent event) {
Jim Miller6c9df502013-09-19 15:50:11 -0700114 final IKeyguardService service = mService;
115 if (service != null) {
Jim Millercaf24fc2013-09-10 18:37:01 -0700116 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700117 service.dispatch(event);
118 return true;
Jim Millercaf24fc2013-09-10 18:37:01 -0700119 } catch (RemoteException e) {
120 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700121 Slog.e(TAG, "RemoteException sending event to keyguard!", e);
Jim Millercaf24fc2013-09-10 18:37:01 -0700122 }
Jim Millercaf24fc2013-09-10 18:37:01 -0700123 } else {
Jim Miller6c9df502013-09-19 15:50:11 -0700124 Slog.w(TAG, "dispatch(event): NO SERVICE!");
125 }
126 return false;
127 }
128
129 public boolean isInputRestricted() {
130 final IKeyguardService service = mService;
131 if (service != null) {
132 try {
133 return service.isInputRestricted();
134 } catch (RemoteException e) {
135 Slog.w(TAG , "Remote Exception", e);
136 }
137 } else {
138 Slog.w(TAG, "isInputRestricted(): NO SERVICE!");
139 }
140 return false;
141 }
142
143 public boolean isShowingAndNotHidden() {
144 final IKeyguardService service = mService;
145 if (service != null) {
146 try {
147 return service.isShowingAndNotHidden();
148 } catch (RemoteException e) {
149 Slog.w(TAG , "Remote Exception", e);
150 }
151 } else {
152 Slog.w(TAG, "isShowingAndNotHidden(): NO SERVICE!");
Jim Millercaf24fc2013-09-10 18:37:01 -0700153 }
154 return false;
155 }
156
Jim Miller138f25d2013-09-25 13:46:58 -0700157 public void showAssistant() {
Jim Miller6c9df502013-09-19 15:50:11 -0700158 final IKeyguardService service = mService;
159 if (service != null) {
Jim Miller138f25d2013-09-25 13:46:58 -0700160 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700161 service.showAssistant();
Jim Miller138f25d2013-09-25 13:46:58 -0700162 } catch (RemoteException e) {
163 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700164 Slog.e(TAG, "RemoteException launching assistant!", e);
Jim Miller138f25d2013-09-25 13:46:58 -0700165 }
166 } else {
Jim Miller6c9df502013-09-19 15:50:11 -0700167 Slog.w(TAG, "showAssistant(event): NO SERVICE!");
Jim Miller138f25d2013-09-25 13:46:58 -0700168 }
169 }
170
171 public void launchCamera() {
Jim Miller6c9df502013-09-19 15:50:11 -0700172 final IKeyguardService service = mService;
173 if (service != null) {
Jim Miller138f25d2013-09-25 13:46:58 -0700174 try {
Jim Miller6c9df502013-09-19 15:50:11 -0700175 service.launchCamera();
Jim Miller138f25d2013-09-25 13:46:58 -0700176 } catch (RemoteException e) {
177 // What to do?
Jim Miller6c9df502013-09-19 15:50:11 -0700178 Slog.e(TAG, "RemoteException launching camera!", e);
Jim Miller138f25d2013-09-25 13:46:58 -0700179 }
180 } else {
Jim Millera999d462013-10-30 13:58:11 -0700181 Slog.w(TAG, "launchCamera(): NO SERVICE!");
182 }
183 }
184
185 public void dismiss() {
186 final IKeyguardService service = mService;
187 if (service != null) {
188 try {
189 service.dismiss();
190 } catch (RemoteException e) {
191 // What to do?
192 Slog.e(TAG, "RemoteException dismissing keyguard!", e);
193 }
194 } else {
195 Slog.w(TAG, "dismiss(): NO SERVICE!");
Jim Miller138f25d2013-09-25 13:46:58 -0700196 }
197 }
198
Adrian Roos5b0acf02014-01-10 12:58:22 -0800199 public static void addListener(OnKeyguardConnectionListener listener) {
200 sConnectionListeners.add(listener);
201 }
202
203 public interface OnKeyguardConnectionListener {
204
205 void onKeyguardServiceConnected(KeyguardTouchDelegate keyguardTouchDelegate);
206 void onKeyguardServiceDisconnected(KeyguardTouchDelegate keyguardTouchDelegate);
207 }
Jim Millercaf24fc2013-09-10 18:37:01 -0700208}