|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 The CyanogenMod Project (Jens Doll) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package android.service.gesture; |
| 17 | + |
| 18 | +import android.os.Handler; |
| 19 | +import android.os.IBinder; |
| 20 | +import android.os.Looper; |
| 21 | +import android.os.RemoteException; |
| 22 | +import android.os.ServiceManager; |
| 23 | +import android.service.gesture.IEdgeGestureActivationListener; |
| 24 | +import android.service.gesture.IEdgeGestureHostCallback; |
| 25 | +import android.service.gesture.IEdgeGestureService; |
| 26 | +import android.util.Slog; |
| 27 | + |
| 28 | +import com.android.internal.util.gesture.EdgeGesturePosition; |
| 29 | + |
| 30 | +/** |
| 31 | + * This is a simple Manager class for edge gesture service on the application side. The application need |
| 32 | + * {@code INJECT_EVENTS} permission to register {@code EdgeGestureActivationListener}s.<br> |
| 33 | + * See {@link android.service.gesture.IEdgeGestureService} for more information. |
| 34 | + * |
| 35 | + * @see android.service.gesture.IEdgeGestureService |
| 36 | + * @hide |
| 37 | + */ |
| 38 | +public class EdgeGestureManager { |
| 39 | + public static final String TAG = "EdgeGestureManager"; |
| 40 | + public static final boolean DEBUG = false; |
| 41 | + |
| 42 | + private static EdgeGestureManager sInstance; |
| 43 | + |
| 44 | + private final IEdgeGestureService mPs; |
| 45 | + |
| 46 | + public static abstract class EdgeGestureActivationListener { |
| 47 | + private Handler mHandler; |
| 48 | + private IEdgeGestureHostCallback mCallback; |
| 49 | + |
| 50 | + private class Delegator extends IEdgeGestureActivationListener.Stub { |
| 51 | + public void onEdgeGestureActivation(final int touchX, final int touchY, final int positionIndex, final int flags) |
| 52 | + throws RemoteException { |
| 53 | + mHandler.post(new Runnable() { |
| 54 | + public void run() { |
| 55 | + EdgeGestureActivationListener.this.onEdgeGestureActivation(touchX, touchY, EdgeGesturePosition.values()[positionIndex], flags); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + private Delegator mDelegator; |
| 61 | + |
| 62 | + public EdgeGestureActivationListener() { |
| 63 | + this(Looper.getMainLooper()); |
| 64 | + } |
| 65 | + |
| 66 | + public EdgeGestureActivationListener(Looper looper) { |
| 67 | + mHandler = new Handler(looper); |
| 68 | + mDelegator = new Delegator(); |
| 69 | + } |
| 70 | + |
| 71 | + /* package */ void setHostCallback(IEdgeGestureHostCallback hostCallback) { |
| 72 | + mCallback = hostCallback; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Override this to receive activations from the edge gesture service. |
| 77 | + * |
| 78 | + * @param touchX the last X position a touch event was registered. |
| 79 | + * @param touchY the last Y position a touch event was registered. |
| 80 | + * @param position the position of the activation. |
| 81 | + * @param flags currently 0. |
| 82 | + * @see IEdgeGestureActivationListener#onEdgeGestureActivation(int, int, int, int) |
| 83 | + */ |
| 84 | + public abstract void onEdgeGestureActivation(int touchX, int touchY, EdgeGesturePosition position, int flags); |
| 85 | + |
| 86 | + /** |
| 87 | + * After being activated, this allows the edge gesture control to steal focus from the current |
| 88 | + * window. |
| 89 | + * |
| 90 | + * @see IEdgeGestureHostCallback#gainTouchFocus(IBinder) |
| 91 | + */ |
| 92 | + public boolean gainTouchFocus(IBinder applicationWindowToken) { |
| 93 | + try { |
| 94 | + return mCallback.gainTouchFocus(applicationWindowToken); |
| 95 | + } catch (RemoteException e) { |
| 96 | + Slog.w(TAG, "gainTouchFocus failed: " + e.getMessage()); |
| 97 | + /* fall through */ |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + public boolean dropEventsUntilLift() { |
| 103 | + try { |
| 104 | + return mCallback.dropEventsUntilLift(); |
| 105 | + } catch (RemoteException e) { |
| 106 | + Slog.w(TAG, "dropNextEvents failed: " + e.getMessage()); |
| 107 | + /* fall through */ |
| 108 | + } |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Turns listening for edge gesture activation gestures on again, after it was disabled during |
| 114 | + * the call to the listener. |
| 115 | + * |
| 116 | + * @see IEdgeGestureHostCallback#restoreListenerState() |
| 117 | + */ |
| 118 | + public void restoreListenerState() { |
| 119 | + if (DEBUG) { |
| 120 | + Slog.d(TAG, "restore listener state: " + Thread.currentThread().getName()); |
| 121 | + } |
| 122 | + try { |
| 123 | + mCallback.restoreListenerState(); |
| 124 | + } catch (RemoteException e) { |
| 125 | + Slog.w(TAG, "restoreListenerState failed: " + e.getMessage()); |
| 126 | + /* fall through */ |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private EdgeGestureManager(IEdgeGestureService ps) { |
| 132 | + mPs = ps; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Gets an instance of the edge gesture manager. |
| 137 | + * |
| 138 | + * @return The edge gesture manager instance. |
| 139 | + * @hide |
| 140 | + */ |
| 141 | + public static EdgeGestureManager getInstance() { |
| 142 | + synchronized (EdgeGestureManager.class) { |
| 143 | + if (sInstance == null) { |
| 144 | + IBinder b = ServiceManager.getService("edgegestureservice"); |
| 145 | + sInstance = new EdgeGestureManager(IEdgeGestureService.Stub.asInterface(b)); |
| 146 | + } |
| 147 | + return sInstance; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Checks if the edge gesture service is present. |
| 153 | + * <p> |
| 154 | + * Since the service is only started at boot time and is bound to the system server, this |
| 155 | + * is constant for the devices up time. |
| 156 | + * |
| 157 | + * @return {@code true} when the edge gesture service is running on this device. |
| 158 | + * @hide |
| 159 | + */ |
| 160 | + public boolean isPresent() { |
| 161 | + return mPs != null; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Register a listener for edge gesture activation gestures. Initially the listener |
| 166 | + * is set to listen for no position. Use updateedge gestureActivationListener() to |
| 167 | + * bind the listener to positions. |
| 168 | + * |
| 169 | + * @param listener is the activation listener. |
| 170 | + * @return {@code true} if the registration was successful. |
| 171 | + * @hide |
| 172 | + */ |
| 173 | + public boolean setEdgeGestureActivationListener(EdgeGestureActivationListener listener) { |
| 174 | + if (DEBUG) { |
| 175 | + Slog.d(TAG, "Set edge gesture activation listener"); |
| 176 | + } |
| 177 | + if (mPs == null) { |
| 178 | + Slog.e(TAG, "Failed to set edge gesture activation listener: Service not present"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + try { |
| 182 | + IEdgeGestureHostCallback callback = mPs.registerEdgeGestureActivationListener(listener.mDelegator); |
| 183 | + listener.setHostCallback(callback); |
| 184 | + return true; |
| 185 | + } catch (RemoteException e) { |
| 186 | + Slog.e(TAG, "Failed to set edge gesture activation listener: " + e.getMessage()); |
| 187 | + return false; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Update the listener to react on gestures in the given positions. |
| 193 | + * |
| 194 | + * @param listener is a already registered listener. |
| 195 | + * @param positions is a bit mask describing the positions to listen to. |
| 196 | + * @hide |
| 197 | + */ |
| 198 | + public void updateEdgeGestureActivationListener(EdgeGestureActivationListener listener, int positions) { |
| 199 | + if (DEBUG) { |
| 200 | + Slog.d(TAG, "Update edge gesture activation listener: 0x" + Integer.toHexString(positions)); |
| 201 | + } |
| 202 | + if (mPs == null) { |
| 203 | + Slog.e(TAG, "Failed to update edge gesture activation listener: Service not present"); |
| 204 | + return; |
| 205 | + } |
| 206 | + try { |
| 207 | + mPs.updateEdgeGestureActivationListener(listener.mDelegator.asBinder(), positions); |
| 208 | + } catch (RemoteException e) { |
| 209 | + Slog.e(TAG, "Failed to update edge gesture activation listener: " + e.getMessage()); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments