AccessibilityInputFilter.java revision c9c9a48e7bafae63cb35a9aa69255e80aba83988
10029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown/*
20029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Copyright (C) 2011 The Android Open Source Project
30029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown *
40029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
50029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * you may not use this file except in compliance with the License.
60029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * You may obtain a copy of the License at
70029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown *
80029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
90029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown *
100029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Unless required by applicable law or agreed to in writing, software
110029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
120029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * See the License for the specific language governing permissions and
140029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * limitations under the License.
150029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown */
160029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
170029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownpackage com.android.server.accessibility;
180029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
190029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownimport android.content.Context;
20d1ff736d01cebaee70b76f012b660ae549c252b9Svetoslav Ganovimport android.os.PowerManager;
210029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownimport android.util.Slog;
22736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganovimport android.view.InputDevice;
230029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownimport android.view.InputEvent;
24c9c9a48e7bafae63cb35a9aa69255e80aba83988Svetoslav Ganovimport android.view.InputFilter;
250029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownimport android.view.MotionEvent;
260029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownimport android.view.WindowManagerPolicy;
2786783474fdec98a22bc22e224462767eab13e273Svetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
280029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
290029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown/**
300029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Input filter for accessibility.
310029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown *
320029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Currently just a stub but will eventually implement touch exploration, etc.
330029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown */
340029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brownpublic class AccessibilityInputFilter extends InputFilter {
350029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    private static final String TAG = "AccessibilityInputFilter";
36736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    private static final boolean DEBUG = false;
370029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
380029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    private final Context mContext;
390029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
40d1ff736d01cebaee70b76f012b660ae549c252b9Svetoslav Ganov    private final PowerManager mPm;
41d1ff736d01cebaee70b76f012b660ae549c252b9Svetoslav Ganov
42e15ccb93add99ebb9cd7aec03a04faa37f45b39dSvetoslav Ganov    private final AccessibilityManagerService mAms;
434213804541a8b05cd0587b138a2fd9a3b7fd9350Svetoslav Ganov
44736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    /**
45736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov     * This is an interface for explorers that take a {@link MotionEvent}
46736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov     * stream and perform touch exploration of the screen content.
47736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov     */
48736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    public interface Explorer {
49736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        /**
50736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * Handles a {@link MotionEvent}.
51736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         *
52736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * @param event The event to handle.
53736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * @param policyFlags The policy flags associated with the event.
54736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         */
55736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        public void onMotionEvent(MotionEvent event, int policyFlags);
56736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
57736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        /**
58736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * Requests that the explorer clears its internal state.
59736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         *
60736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * @param event The last received event.
61736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         * @param policyFlags The policy flags associated with the event.
62736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov         */
63736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        public void clear(MotionEvent event, int policyFlags);
643e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov
653e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov        /**
663e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov         * Requests that the explorer clears its internal state.
673e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov         */
683e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov        public void clear();
69736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    }
70736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
71736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    private TouchExplorer mTouchExplorer;
724213804541a8b05cd0587b138a2fd9a3b7fd9350Svetoslav Ganov
73736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov    private int mTouchscreenSourceDeviceId;
74736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov
75e15ccb93add99ebb9cd7aec03a04faa37f45b39dSvetoslav Ganov    public AccessibilityInputFilter(Context context, AccessibilityManagerService service) {
760029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        super(context.getMainLooper());
770029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        mContext = context;
78e15ccb93add99ebb9cd7aec03a04faa37f45b39dSvetoslav Ganov        mAms = service;
79d1ff736d01cebaee70b76f012b660ae549c252b9Svetoslav Ganov        mPm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
800029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
810029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
820029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    @Override
830029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    public void onInstalled() {
840029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        if (DEBUG) {
850029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown            Slog.d(TAG, "Accessibility input filter installed.");
860029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        }
87e15ccb93add99ebb9cd7aec03a04faa37f45b39dSvetoslav Ganov        mTouchExplorer = new TouchExplorer(this, mContext, mAms);
880029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        super.onInstalled();
890029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
900029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
910029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    @Override
920029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    public void onUninstalled() {
930029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        if (DEBUG) {
940029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown            Slog.d(TAG, "Accessibility input filter uninstalled.");
950029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        }
963e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov        mTouchExplorer.clear();
970029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        super.onUninstalled();
980029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
990029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown
1000029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    @Override
1010029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    public void onInputEvent(InputEvent event, int policyFlags) {
1020029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        if (DEBUG) {
103736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            Slog.d(TAG, "Received event: " + event + ", policyFlags=0x"
104736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                    + Integer.toHexString(policyFlags));
1050029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        }
106736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
107736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            MotionEvent motionEvent = (MotionEvent) event;
108736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            int deviceId = event.getDeviceId();
109736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if (mTouchscreenSourceDeviceId != deviceId) {
110736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                mTouchscreenSourceDeviceId = deviceId;
1113e4e4af45216aee4d4b009fe842c0324610918ebSvetoslav Ganov                mTouchExplorer.clear(motionEvent, policyFlags);
1120029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown            }
113736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            if ((policyFlags & WindowManagerPolicy.FLAG_PASS_TO_USER) != 0) {
114d1ff736d01cebaee70b76f012b660ae549c252b9Svetoslav Ganov                mPm.userActivity(event.getEventTime(), false);
115736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                mTouchExplorer.onMotionEvent(motionEvent, policyFlags);
116736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            } else {
117736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov                mTouchExplorer.clear(motionEvent, policyFlags);
118736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            }
119736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov        } else {
120736c2756bf3c14ae9fef7255c119057f7a2be1edSvetoslav Ganov            super.onInputEvent(event, policyFlags);
1210029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown        }
1220029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown    }
12386783474fdec98a22bc22e224462767eab13e273Svetoslav Ganov
12486783474fdec98a22bc22e224462767eab13e273Svetoslav Ganov    public void onAccessibilityEvent(AccessibilityEvent event) {
125ee33ad24cdc31ed0d7f99e110e041b0a63c9b0f1Svetoslav Ganov        if (mTouchExplorer != null) {
126ee33ad24cdc31ed0d7f99e110e041b0a63c9b0f1Svetoslav Ganov            mTouchExplorer.onAccessibilityEvent(event);
127ee33ad24cdc31ed0d7f99e110e041b0a63c9b0f1Svetoslav Ganov        }
12886783474fdec98a22bc22e224462767eab13e273Svetoslav Ganov    }
1290029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown}
130