1 /* 2 * Copyright (C) 2011 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.server.accessibility; 18 19 import com.android.server.wm.InputFilter; 20 21 import android.content.Context; 22 import android.util.Slog; 23 import android.view.InputDevice; 24 import android.view.InputEvent; 25 import android.view.MotionEvent; 26 import android.view.WindowManagerPolicy; 27 28 /** 29 * Input filter for accessibility. 30 * 31 * Currently just a stub but will eventually implement touch exploration, etc. 32 */ 33 public class AccessibilityInputFilter extends InputFilter { 34 private static final String TAG = "AccessibilityInputFilter"; 35 private static final boolean DEBUG = false; 36 37 private final Context mContext; 38 39 /** 40 * This is an interface for explorers that take a {@link MotionEvent} 41 * stream and perform touch exploration of the screen content. 42 */ 43 public interface Explorer { 44 /** 45 * Handles a {@link MotionEvent}. 46 * 47 * @param event The event to handle. 48 * @param policyFlags The policy flags associated with the event. 49 */ 50 public void onMotionEvent(MotionEvent event, int policyFlags); 51 52 /** 53 * Requests that the explorer clears its internal state. 54 * 55 * @param event The last received event. 56 * @param policyFlags The policy flags associated with the event. 57 */ 58 public void clear(MotionEvent event, int policyFlags); 59 60 /** 61 * Requests that the explorer clears its internal state. 62 */ 63 public void clear(); 64 } 65 66 private TouchExplorer mTouchExplorer; 67 private int mTouchscreenSourceDeviceId; 68 69 public AccessibilityInputFilter(Context context) { 70 super(context.getMainLooper()); 71 mContext = context; 72 } 73 74 @Override 75 public void onInstalled() { 76 if (DEBUG) { 77 Slog.d(TAG, "Accessibility input filter installed."); 78 } 79 mTouchExplorer = new TouchExplorer(this, mContext); 80 super.onInstalled(); 81 } 82 83 @Override 84 public void onUninstalled() { 85 if (DEBUG) { 86 Slog.d(TAG, "Accessibility input filter uninstalled."); 87 } 88 mTouchExplorer.clear(); 89 super.onUninstalled(); 90 } 91 92 @Override 93 public void onInputEvent(InputEvent event, int policyFlags) { 94 if (DEBUG) { 95 Slog.d(TAG, "Received event: " + event + ", policyFlags=0x" 96 + Integer.toHexString(policyFlags)); 97 } 98 if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) { 99 MotionEvent motionEvent = (MotionEvent) event; 100 int deviceId = event.getDeviceId(); 101 if (mTouchscreenSourceDeviceId != deviceId) { 102 mTouchscreenSourceDeviceId = deviceId; 103 mTouchExplorer.clear(motionEvent, policyFlags); 104 } 105 if ((policyFlags & WindowManagerPolicy.FLAG_PASS_TO_USER) != 0) { 106 mTouchExplorer.onMotionEvent(motionEvent, policyFlags); 107 } else { 108 mTouchExplorer.clear(motionEvent, policyFlags); 109 } 110 } else { 111 super.onInputEvent(event, policyFlags); 112 } 113 } 114 } 115