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