3

I need to record all touch points in an activity so I have added an overlay view in window manager and set the touch listener on the overlay view and I am getting all touch point but I am facing two issues after adding the overlay

  1. I can open the options menu but not able to select any menu item
  2. AlertDialog.show() stopped showing the alertbox I can get all touch points by overriding onTouchEvent method inside activity but app requirement does not permit that.

It will be great if any one can help me in this or share some link which explains about WindowManager, window and DecorView etc . here is my code, please let me know if more information is required.

 WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.TYPE_PHONE,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
    PixelFormat.TRANSLUCENT);


WindowManager wm = (WindowManager) activityContext
    .getSystemService(Context.WINDOW_SERVICE);

View mView = new View(activityContext);

wm.addView(mView, params);

mView.setOnTouchListener(new OnTouchListener() {

@Overrideenter code here
public boolean onTouch(View v, MotionEvent event) {
    Log.d("touch", "event occured");
    activity.getWindow().superDispatchTouchEvent(event);    
 // or   
   // activity.dispatchTouchEvent(event);

 return false;
}
});
44kksharma
  • 2,740
  • 27
  • 32
  • This should not be possible on Android 4.0+. If your app intercepts touch events, they should not be passed along to the underlying app. – CommonsWare Oct 14 '14 at 13:31
  • Thanks a lot for your quick replying , using above code I am able to log touch event on android 4.1 as well as on android 4.4.4 but facing above explained issue – 44kksharma Oct 14 '14 at 14:05
  • please let me know if there is any alternate way to do the same – 44kksharma Oct 14 '14 at 14:37
  • `Is your overlay able to get the touch event and is your overlay consuming the touchevents?` – Nadeem Iqbal Oct 14 '14 at 14:42
  • yes overlay is getting all the touch event and then I am able to dispatch touch events to activity using activity.getWindow().superDispatchTouchEvent(event); or activity.dispatchTouchEvent(event); I have also modified the above please have a look at ontouch method. – 44kksharma Oct 14 '14 at 16:16

1 Answers1

8

finally I did it by setting a callback on window's activity

android.view.Window.Callback mCallBack = activity.getWindow().getCallback();

activity.getWindow().setCallback(new MyWindowCallBacks(mCallBack, act)); 

and here is my MyWindowCallBacks class

public class MyWindowCallBacks implements Window.Callback {
        private Window.Callback mCallBack;
        private Activity mActivity;

        public MyWindowCallBacks(Window.Callback mCallBack, Activity mActivity) {
            this.mCallBack = mCallBack;
            this.mActivity = mActivity;
        }

        @Override
        public boolean dispatchGenericMotionEvent(MotionEvent event) {

            return mCallBack.dispatchGenericMotionEvent(event);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {

            return mCallBack.dispatchKeyEvent(event);
        }

        @Override
        public boolean dispatchKeyShortcutEvent(KeyEvent event) {

            return mCallBack.dispatchKeyShortcutEvent(event);
        }

        @Override
        public boolean dispatchPopulateAccessibilityEvent(
                AccessibilityEvent event) {

            return mCallBack.dispatchPopulateAccessibilityEvent(event);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {

          // Here I can get all touch events

            return mCallBack.dispatchTouchEvent(event);
        }

        @Override
        public boolean dispatchTrackballEvent(MotionEvent event) {

            return mCallBack.dispatchTrackballEvent(event);
        }

        @Override
        public void onActionModeFinished(ActionMode mode) {

            mCallBack.onActionModeFinished(mode);
        }

        @Override
        public void onActionModeStarted(ActionMode mode) {

            mCallBack.onActionModeStarted(mode);
        }

        @Override
        public void onAttachedToWindow() {

            mCallBack.onAttachedToWindow();
        }

        @Override
        public void onContentChanged() {

            mCallBack.onContentChanged();
        }

        @Override
        public boolean onCreatePanelMenu(int featureId, Menu menu) {

            return mCallBack.onCreatePanelMenu(featureId, menu);
        }

        @Override
        public View onCreatePanelView(int featureId) {

            return mCallBack.onCreatePanelView(featureId);
        }

        @SuppressLint("MissingSuperCall")
        @Override
        public void onDetachedFromWindow() {


            mCallBack.onDetachedFromWindow();
        }

        @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {

            return mCallBack.onMenuItemSelected(featureId, item);
        }

        @Override
        public boolean onMenuOpened(int featureId, Menu menu) {

            return mCallBack.onMenuOpened(featureId, menu);
        }

        @Override
        public void onPanelClosed(int featureId, Menu menu) {

            mCallBack.onPanelClosed(featureId, menu);
        }

        @Override
        public boolean onPreparePanel(int featureId, View view, Menu menu) {

            return mCallBack.onPreparePanel(featureId, view, menu);
        }

        @Override
        public boolean onSearchRequested() {

            return mCallBack.onSearchRequested();
        }

        @Override
        public void onWindowAttributesChanged(LayoutParams attrs) {

            mCallBack.onWindowAttributesChanged(attrs);
        }

        @Override
        public void onWindowFocusChanged(boolean hasFocus) {

            mCallBack.onWindowFocusChanged(hasFocus);
        }

        @Override
        public ActionMode onWindowStartingActionMode(Callback callback) {

            return mCallBack.onWindowStartingActionMode(callback);
        }

    }
44kksharma
  • 2,740
  • 27
  • 32