对input分发的拦截

一,创建一个单例模式的帮助类

package android.view;

import android.net.Uri;
import android.provider.Settings;
import android.content.Intent;
import android.content.Context;
import android.content.ContentResolver;
import android.os.Handler;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.AndroidRuntimeException;
import android.database.ContentObserver;
import android.util.ArraySet;
import android.util.Log;
import android.hardware.display.DisplayManager;
import android.view.Display;
import android.view.DisplayInfo;
import android.app.KeyguardManager;
import android.view.ViewConfiguration;
/**
 * @hide
 */
public class TranslationHelper  {
    private static final String TAG = "TranslationHelper ";
    private Context mContext;
    private boolean mIntercept = false;
    
    public TranslationHelper(Context context) {
        mContext = context.getApplicationContext();
        if(mContext == null){
            return ;
        }
        //init();
    }

    public boolean getInterceptState(){
        
        return mIntercept;
    }
   
    public boolean interceptEvent(MotionEvent event) {
        final int action = event.getAction();
        if(event.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER &&  event.getTouchMajor() < 20)  {  
            if (action == MotionEvent.ACTION_DOWN) {
                mIntercept = true;
                
                return true;
            } else if (action == MotionEvent.ACTION_UP) {
                mIntercept = false;
                
                return false;
            } else if (action == MotionEvent.ACTION_MOVE){
                mIntercept = true;
                
                return true;
            }            
        }else{
            return false;
        }            
        return false;
    }
}

 二,在WindowManagerGlobal.java中 new TranslationHelper

    public static TranslationHelper getTranslationHelper(Context context) {
        synchronized (XpenTranslationHelper.class) {
            if (sTranslationHelper == null) {
                sTranslationHelper = new TranslationHelper(context);
                Log.i(TAG, "TranslationHelper = " + TranslationHelper);
            }
            return sXpenTranslationHelper;
        }
    }

三,在ViewRootImpl中得到touch状态,判断是否拦截

      public ViewRootImpl(Context context, Display display) {
        mContext = context;
        ......
        mTranslationHelper = WindowManagerGlobal.getTranslationHelper(context);
        ......
      }
  void enqueueInputEvent(InputEvent event,
            InputEventReceiver receiver, int flags, boolean processImmediately) {
     ......
             if (event instanceof MotionEvent && mXpenTranslationHelper != null) {
            mTranslationHelper.interceptEvent((MotionEvent)event);
        }
     ......
  }

四,DecorView中拦截input事件

mTranslationHelper = WindowManagerGlobal.getTranslationHelper(mContext);

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        final Window.Callback cb = mWindow.getCallback();

        if (mTranslationHelper != null && mTranslationHelper.getInterceptState()) {
            Log.i("lzq", "dispatchTouchEvent");
            return false;
        }
        
        return cb != null && !mWindow.isDestroyed() && mFeatureId < 0
                ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev);
    }

 

 

 

你可能感兴趣的:(Android,input)