How to log all touch events in an activity from a background service

I need to write all touchpoints to the action, so I added an overlay view in the window manager and set a touch listener in the overlay view and I get all the touchpoints, but there are two questions before them after adding the overlay

  • I can open the options menu, but I cannot select any menu item
  • AlertDialog.show () stops showing alert I can get all touch points by overriding the onTouchEvent method inside the activity, but the application requirement does not allow it.

It would be great if anyone can help me on this or share some kind of link that explains 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;
}
});

      

+3


source to share


1 answer


Finally I did it by setting a window activity callback

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

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

      



and here is my class MyWindowCallBacks

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);
        }

    }

      

+7


source







All Articles