Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.UserHandle; |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 27 | import android.view.MotionEvent; |
| 28 | |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 29 | import com.android.internal.policy.IKeyguardService; |
| 30 | |
John Spurlock | dfc6319 | 2014-01-13 11:27:25 -0500 | [diff] [blame] | 31 | import java.util.ArrayList; |
Adrian Roos | 5b0acf0 | 2014-01-10 12:58:22 -0800 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 34 | |
| 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 | */ |
| 40 | public 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 Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 45 | private static KeyguardTouchDelegate sInstance; |
Adrian Roos | 5b0acf0 | 2014-01-10 12:58:22 -0800 | [diff] [blame] | 46 | private static final List<OnKeyguardConnectionListener> sConnectionListeners = |
John Spurlock | dfc6319 | 2014-01-13 11:27:25 -0500 | [diff] [blame] | 47 | new ArrayList<OnKeyguardConnectionListener>(); |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 48 | |
| 49 | private volatile IKeyguardService mService; |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 50 | |
| 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 Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 57 | Slog.v(TAG, "Connected to keyguard"); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 58 | mService = IKeyguardService.Stub.asInterface(service); |
| 59 | |
Adrian Roos | 5b0acf0 | 2014-01-10 12:58:22 -0800 | [diff] [blame] | 60 | for (int i = 0; i < sConnectionListeners.size(); i++) { |
| 61 | OnKeyguardConnectionListener listener = sConnectionListeners.get(i); |
| 62 | listener.onKeyguardServiceConnected(KeyguardTouchDelegate.this); |
| 63 | } |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void onServiceDisconnected(ComponentName name) { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 68 | Slog.v(TAG, "Disconnected from keyguard"); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 69 | mService = null; |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 70 | sInstance = null; // force reconnection if this goes away |
Adrian Roos | 5b0acf0 | 2014-01-10 12:58:22 -0800 | [diff] [blame] | 71 | |
| 72 | for (int i = 0; i < sConnectionListeners.size(); i++) { |
| 73 | OnKeyguardConnectionListener listener = sConnectionListeners.get(i); |
| 74 | listener.onKeyguardServiceDisconnected(KeyguardTouchDelegate.this); |
| 75 | } |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | }; |
| 79 | |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 80 | private KeyguardTouchDelegate(Context context) { |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 81 | 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 Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 85 | if (DEBUG) Slog.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 86 | } else { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 87 | if (DEBUG) Slog.v(TAG, "*** Keyguard started"); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 91 | public static KeyguardTouchDelegate getInstance(Context context) { |
Jim Miller | a999d46 | 2013-10-30 13:58:11 -0700 | [diff] [blame] | 92 | KeyguardTouchDelegate instance = sInstance; |
| 93 | if (instance == null) { |
| 94 | instance = sInstance = new KeyguardTouchDelegate(context); |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 95 | } |
Jim Miller | a999d46 | 2013-10-30 13:58:11 -0700 | [diff] [blame] | 96 | return instance; |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 99 | public boolean isSecure() { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 100 | final IKeyguardService service = mService; |
| 101 | if (service != null) { |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 102 | try { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 103 | return service.isSecure(); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 104 | } catch (RemoteException e) { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 105 | Slog.e(TAG, "RemoteException calling keyguard.isSecure()!", e); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 106 | } |
| 107 | } else { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 108 | Slog.w(TAG, "isSecure(): NO SERVICE!"); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 109 | } |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 110 | return false; |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public boolean dispatch(MotionEvent event) { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 114 | final IKeyguardService service = mService; |
| 115 | if (service != null) { |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 116 | try { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 117 | service.dispatch(event); |
| 118 | return true; |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 119 | } catch (RemoteException e) { |
| 120 | // What to do? |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 121 | Slog.e(TAG, "RemoteException sending event to keyguard!", e); |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 122 | } |
Jim Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 123 | } else { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 124 | 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 Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 157 | public void showAssistant() { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 158 | final IKeyguardService service = mService; |
| 159 | if (service != null) { |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 160 | try { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 161 | service.showAssistant(); |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 162 | } catch (RemoteException e) { |
| 163 | // What to do? |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 164 | Slog.e(TAG, "RemoteException launching assistant!", e); |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 165 | } |
| 166 | } else { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 167 | Slog.w(TAG, "showAssistant(event): NO SERVICE!"); |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | public void launchCamera() { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 172 | final IKeyguardService service = mService; |
| 173 | if (service != null) { |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 174 | try { |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 175 | service.launchCamera(); |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 176 | } catch (RemoteException e) { |
| 177 | // What to do? |
Jim Miller | 6c9df50 | 2013-09-19 15:50:11 -0700 | [diff] [blame] | 178 | Slog.e(TAG, "RemoteException launching camera!", e); |
Jim Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 179 | } |
| 180 | } else { |
Jim Miller | a999d46 | 2013-10-30 13:58:11 -0700 | [diff] [blame] | 181 | 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 Miller | 138f25d | 2013-09-25 13:46:58 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Adrian Roos | 5b0acf0 | 2014-01-10 12:58:22 -0800 | [diff] [blame] | 199 | 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 Miller | caf24fc | 2013-09-10 18:37:01 -0700 | [diff] [blame] | 208 | } |