AccessibilityInputFilter.java revision 736c2756bf3c14ae9fef7255c119057f7a2be1ed
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
17package com.android.server.accessibility;
18
19import com.android.server.wm.InputFilter;
20
21import android.content.Context;
22import android.util.Slog;
23import android.view.InputDevice;
24import android.view.InputEvent;
25import android.view.MotionEvent;
26import android.view.WindowManagerPolicy;
27
28/**
29 * Input filter for accessibility.
30 *
31 * Currently just a stub but will eventually implement touch exploration, etc.
32 */
33public 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    private TouchExplorer mTouchExplorer;
62    private int mTouchscreenSourceDeviceId;
63
64    public AccessibilityInputFilter(Context context) {
65        super(context.getMainLooper());
66        mContext = context;
67    }
68
69    @Override
70    public void onInstalled() {
71        if (DEBUG) {
72            Slog.d(TAG, "Accessibility input filter installed.");
73        }
74        super.onInstalled();
75    }
76
77    @Override
78    public void onUninstalled() {
79        if (DEBUG) {
80            Slog.d(TAG, "Accessibility input filter uninstalled.");
81        }
82        super.onUninstalled();
83    }
84
85    @Override
86    public void onInputEvent(InputEvent event, int policyFlags) {
87        if (DEBUG) {
88            Slog.d(TAG, "Received event: " + event + ", policyFlags=0x"
89                    + Integer.toHexString(policyFlags));
90        }
91        if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
92            MotionEvent motionEvent = (MotionEvent) event;
93            int deviceId = event.getDeviceId();
94            if (mTouchscreenSourceDeviceId != deviceId) {
95                mTouchscreenSourceDeviceId = deviceId;
96                if (mTouchExplorer != null) {
97                    mTouchExplorer.clear(motionEvent, policyFlags);
98                } else {
99                    mTouchExplorer = new TouchExplorer(this, mContext);
100                }
101            }
102            if ((policyFlags & WindowManagerPolicy.FLAG_PASS_TO_USER) != 0) {
103                mTouchExplorer.onMotionEvent(motionEvent, policyFlags);
104            } else {
105                mTouchExplorer.clear(motionEvent, policyFlags);
106            }
107        } else {
108            super.onInputEvent(event, policyFlags);
109        }
110    }
111}
112