android 添加全局TouchPanel响应范例

alps\frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManager.java

package com.android.internal.policy.impl;
public class PhoneWindowManager implements WindowManagerPolicy {
    private static final int MSG_ENABLE_POINTER_LOCATION = 1;
    private static final int MSG_DISABLE_POINTER_LOCATION = 2;

//Add nav down by cbk
    private static final int MSG_ENABLE_NAV_BAR_PULL_UP_GESTURE = 7;
    private static final int MSG_DISABLE_NAV_BAR_PULL_UP_GESTURE = 8;
//end nav down by cbk

    private class PolicyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_ENABLE_POINTER_LOCATION:
                    enablePointerLocation();
                    break;
                case MSG_DISABLE_POINTER_LOCATION:
                    disablePointerLocation();
                    break;
//Add nav down by cbk
                case MSG_ENABLE_NAV_BAR_PULL_UP_GESTURE:
                    enableNavBarPullupGesture();
                    break;
                case MSG_DISABLE_NAV_BAR_PULL_UP_GESTURE:
                    disableNavBarPullupGesture();
                    break;
//end nav down by cbk
            }
        }
    }

    class SettingsObserver extends ContentObserver {
        SettingsObserver(Handler handler) {
            super(handler);
        }

        void observe() {
            // Observe all users' changes
            ContentResolver resolver = mContext.getContentResolver();
//Add nav down by cbk
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.MULTI_TOUCH_GESTURE), false, this,
                    UserHandle.USER_ALL);
//end nav down by cbk
            updateSettings();
        }

        @Override public void onChange(boolean selfChange) {
            updateSettings();
            updateRotation(false);
        }
    }
    public void updateSettings() {
        ContentResolver resolver = mContext.getContentResolver();
        boolean updateRotation = false;
        synchronized (mLock) {
//Add nav down by cbk
                if (FeatureOption.K19_NCX_ASSISTIVE_TOUCH_NAV_BAR) {
                    /*int NavBarPullupGesture = Settings.System.getIntForUser(resolver,
                        Settings.System.MULTI_TOUCH_GESTURE, 0, UserHandle.USER_CURRENT);
                    if (mNavBarPullupGestureMode != NavBarPullupGesture) {
                        mNavBarPullupGestureMode = NavBarPullupGesture;
                        mHandler.sendEmptyMessage(NavBarPullupGesture != 0 ?
                                MSG_ENABLE_NAV_BAR_PULL_UP_GESTURE: MSG_DISABLE_NAV_BAR_PULL_UP_GESTURE);
                    }*/
                    if (mNavBarPullupGestureMode != 1) {
                        mNavBarPullupGestureMode = 1;
                        mHandler.sendEmptyMessage(MSG_ENABLE_NAV_BAR_PULL_UP_GESTURE);
                    }
                }
//end nav down by cbk

            }
    }

//Add nav down by cbk
    int mNavBarPullupGestureMode = 0;
    NavBarPullupGestureInputEventReceiver mNavBarPullupGestureInputEventReceiver;
    NavBarPullupGesturePanel mNavBarPullupGestureView;
    InputChannel mNavBarPullupGestureInputChannel;

    static final String TAG_NAV_BAR_PULL_UP_GESTURE = "NavBarPullupGesture";
    static boolean DEBUG_NAV_BAR_PULL_UP_GESTURE = true;

    private int mPosX =0;
    private int mPosY =0;
    private int mCurrentPosX =0;
    private int mCurrentPosY =0;
    private boolean mNavBarPullUpValid = false;

    private static final class NavBarPullupGestureInputEventReceiver extends InputEventReceiver {
        private final NavBarPullupGesturePanel mView;

        public NavBarPullupGestureInputEventReceiver(InputChannel inputChannel, Looper looper,
                NavBarPullupGesturePanel view) {
            super(inputChannel, looper);
            mView = view;
        }

        @Override
        public void onInputEvent(InputEvent event) {
            boolean handled = false;
            try {
                if (event instanceof MotionEvent
                        && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
                    final MotionEvent motionEvent = (MotionEvent)event;
                    mView.onTouchEvent(motionEvent);
                    handled = true;
                }
            } finally {
                finishInputEvent(event, handled);
            }
        }
    }

    public void NavBarDisplayGesture() {
        IStatusBarService statusBarService = IStatusBarService.Stub.asInterface(
                ServiceManager.getService("statusbar"));
        try {
            if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
   Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,"NavBarDisplayGesture ()");

            statusBarService.EnableNavBarDisplay();//toggleNavBarDisplay();
        } catch (RemoteException e) {
            Log.e(TAG, "Error toggling recent apps.");
        }
    }

    public class NavBarPullupGesturePanel extends FrameLayout{
        public NavBarPullupGesturePanel(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
  if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
   Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,
    "NavBarPullupGesturePanel event.getAction() =" +event.getAction() );

  /*if(MotionEvent.ACTION_DOWN==event.getAction()){
   mPosX = (int)event.getX();
   mPosY = (int)event.getY();

   if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
    Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,
     "NavBarPullupGesturePanel tp_down mPosX =" +mPosX
     +",mPosY ="+mPosY);

  }else if (MotionEvent.ACTION_MOVE == event.getAction()) {
   mCurrentPosX = (int)event.getX();
   mCurrentPosY = (int)event.getY();

   if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
    Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,
     "NavBarPullupGesturePanel tp_move mCurrentPosX =" +mCurrentPosX
     +",mCurrentPosY ="+mCurrentPosY);
  }else{
   mCurrentPosX = 0;
   mCurrentPosY = 0;
   mPosX = 0;
   mPosY = 0;
  }

  if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
   Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,
    "NavBarPullupGesturePanel mPosX =" +mPosX
    + ",mPosY ="+mPosY
    + ",mCurrentPosX ="+mCurrentPosX
    + ",mCurrentPosY ="+mCurrentPosY );

  if (mCurrentPosY - mPosY < -200 && Math.abs(mCurrentPosX - mPosX) < 300)
  {
   NavBarDisplayGesture();
   Log.d(TAG_NAV_BAR_PULL_UP_GESTURE, "向上的按下位置"+mPosX+"移动位置"+mCurrentPosX);
  }*/

  if(MotionEvent.ACTION_DOWN==event.getAction()){
   mPosX = (int)event.getX();
   mPosY = (int)event.getY();

          int orientation = mContext.getResources().getConfiguration().orientation;
          Log.d(TAG,"current config orienation " + orientation);

   if (DEBUG_NAV_BAR_PULL_UP_GESTURE)
    Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,
     "NavBarPullupGesturePanel tp_down mPosX =" +mPosX
     +",mPosY ="+mPosY
     +",orientation =" +orientation);

          if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
               if(mPosY >=540 && mPosY <=600){
     NavBarDisplayGesture();
               }
          }else if(orientation == Configuration.ORIENTATION_PORTRAIT) {
               if(mPosY >=940 && mPosY <=1024){
     NavBarDisplayGesture();
               }
   }
  }

            return true;
        }

        @Override
        public boolean onGenericMotionEvent(MotionEvent event) {
            final int source = event.getSource();
            /*if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
                mGestureRecognizer.onTouchEvent(event);
            }*/
            return true;
        }
    }

    private void enableNavBarPullupGesture() {
        if (mNavBarPullupGestureView == null) {
            mNavBarPullupGestureView = new NavBarPullupGesturePanel(mContext);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT);
            lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
            lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            lp.format = PixelFormat.TRANSLUCENT;
            lp.setTitle("NavBarPullupGesturePanel");
            WindowManager wm = (WindowManager)
                    mContext.getSystemService(Context.WINDOW_SERVICE);
            wm.addView(mNavBarPullupGestureView, lp);

            if (DEBUG_NAV_BAR_PULL_UP_GESTURE) Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,"enableNavBarPullupGesture");

            mNavBarPullupGestureInputChannel =
                    mWindowManagerFuncs.monitorInput("NavBarPullupGesture");
            mNavBarPullupGestureInputEventReceiver =
                    new NavBarPullupGestureInputEventReceiver(mNavBarPullupGestureInputChannel,
                            Looper.myLooper(), mNavBarPullupGestureView);
        }
    }

    private void disableNavBarPullupGesture() {
        if (DEBUG_NAV_BAR_PULL_UP_GESTURE) Log.d(TAG_NAV_BAR_PULL_UP_GESTURE,"disableNavBarPullupGesture");

        if (mNavBarPullupGestureInputEventReceiver != null) {
            mNavBarPullupGestureInputEventReceiver.dispose();
            mNavBarPullupGestureInputEventReceiver = null;
        }

        if (mNavBarPullupGestureInputChannel != null) {
            mNavBarPullupGestureInputChannel.dispose();
            mNavBarPullupGestureInputChannel = null;
        }

        if (mNavBarPullupGestureView != null) {
            WindowManager wm = (WindowManager)
                    mContext.getSystemService(Context.WINDOW_SERVICE);
            wm.removeView(mNavBarPullupGestureView);
            mNavBarPullupGestureView = null;
        }
    }
//end nav down by cbk
}
时间: 2024-10-24 23:49:49

android 添加全局TouchPanel响应范例的相关文章

Android添加button并响应

1.首先要在activity_main.xml文件里新建两个button标签. 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_hei

SpringBoot2.0系列教程(五)Springboot框架添加全局异常处理

Hello大家好,本章我们添加全局异常处理.另求各路大神指点,感谢 一:为什么需要定义全局异常 在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,所以我们需要对异常进行捕获,然后给予相应的处理,来减少程序异常对用户体验的影响 二:添加业务类异常 在前面说过的ret文件夹下创建ServiceException package com.example.demo.core.ret; import java.io.Seriali

android添加账户源码浅析

上篇粗略的分析android添加账号的流程,本篇深入的解析下执行步骤.先来看图片,取自深入理解android卷2: 上图详细的分析了addAccount的流程,下面我们结合源码来理解它 1.addAccount:其实这里省略了一步,应该是客户端addAccount——>AddAccountSettings.addAccount——>AccountManager.addAccount.我们看下setting是如何到AccountManager: private void addAccount(S

Android添加快捷方式(Shortcut)到手机桌面

Android添加快捷方式(Short)到手机桌面 权限 要在手机桌面上添加快捷方式,首先需要在manifest中添加权限. <!-- 添加快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <!-- 移除快捷方式 --> <uses-permission android:name="com.andro

Android 添加数据到本地Excel表中

由于项目需要,今天学习了一下如何将程序里的数据添加到本地的Excel表中. 下面为学习笔记: 先上效果图: 首先,需要导入jxl.jar包到libs文件夹内. 然后创建Excel表,并往表里添加表头. // 创建excel表. public void createExcel(File file) { WritableSheet ws = null; try { if (!file.exists()) { // 创建表 wwb = Workbook.createWorkbook(file); //

android添加edittext后布局就不好用

在布局添加控件手动添加还是拖的添加,添加edittext后布局就不好用,其他控件好用,然后就说下面这段话 Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V Exception details are logged in Window > Show View > Error Log Check the "Android version to use when rendering layouts

android 添加手机短信,获取手机短信,删除手机短信和修改手机短信

注意添加权限: <uses-permission android:name="android.permission.READ_SMS"></uses-permission> <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission> 代码如下: //更新短信 private void UpdateSMS() { /* u

为DELL 2900添加全局热备磁盘

我们在规划服务器本地存储的时候,一般是为服务器配置RAID,并在机房放置一块同样容量.大小的硬盘做为备用.并且定期巡检机房,一旦看到磁盘报警(红灯或黄灯),应该用备用硬盘替换故障磁盘,此时RAID卡会检测到硬盘替换,并在新替换的磁盘上重建数据.这种方式是目前大多数单位使用的方法,但是,服务器一般都能正常使用三.五年不出问题,开始的时候,可能还会定期检查服务器,之后可能管理员就很少去机房检查,即使去机房检查也很少一一查看服务器的硬盘指示灯.在配置RAID5时,如果阵列中只有一块硬盘出现故障,系统会

android中activity可以响应外部的action的例子(可以用这个来导入外部文件)

如果我们在文件浏览器中点击一个文件,然后点击它,会弹出一个列表给你选择 如果使自己的应用也出现在这个列表上,必须在menifest的这个activity下加入: <activity android:name="com.example.clickabletextview.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name