Android PhoneWindowManager listening screen slides to the left on the right side to realize the return function

  • 2021-11-30 01:36:25
  • OfStack

Because the project does not have a design return key, once you enter other applications, you cannot return to the desktop. You can only return to the desktop by entering input keyevent 4 (return key) through serial port. In order to facilitate debugging, you can monitor gestures in Framework to realize the return function.

There is one such code in frameworks\ base\ services\ core\ java\ com\ android\ server\ policy\ PhoneWindowManager


mSystemGestures = new SystemGesturesPointerEventListener(context,
        new SystemGesturesPointerEventListener.Callbacks() {
          @Override
          public void onSwipeFromTop() {
   Log.i("gyx","onSwipeFromTop");
            if (mStatusBar != null) {
              requestTransientBars(mStatusBar);
            }
          }
          @Override
          public void onSwipeFromBottom() {
   Log.i("gyx","onSwipeFromBottom");
            if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_BOTTOM) {
              requestTransientBars(mNavigationBar);
            }
          }
          @Override
          public void onSwipeFromRight() {
   Log.i("gyx","onSwipeFromRight");
   sendKeyCode(4);
   Log.i("gyx","sendKeyCode 4");
 
            if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_RIGHT) {
              requestTransientBars(mNavigationBar);
            }
          }
          @Override
          public void onSwipeFromLeft() {
   Log.i("gyx","onSwipeFromLeft");
            if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_LEFT) {
              requestTransientBars(mNavigationBar);
            }
          }
          @Override
          public void onFling(int duration) {
            if (mPowerManagerInternal != null) {
              mPowerManagerInternal.powerHint(
                  PowerHint.INTERACTION, duration);
            }
          }
          @Override
          public void onDebug() {
            // no-op
          }
          @Override
          public void onDown() {
            mOrientationListener.onTouchStart();
          }
          @Override
          public void onUpOrCancel() {
            mOrientationListener.onTouchEnd();
          }
          @Override
          public void onMouseHoverAtTop() {
            mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
            Message msg = mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS);
            msg.arg1 = MSG_REQUEST_TRANSIENT_BARS_ARG_STATUS;
            mHandler.sendMessageDelayed(msg, 500);
          }
          @Override
          public void onMouseHoverAtBottom() {
            mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
            Message msg = mHandler.obtainMessage(MSG_REQUEST_TRANSIENT_BARS);
            msg.arg1 = MSG_REQUEST_TRANSIENT_BARS_ARG_NAVIGATION;
            mHandler.sendMessageDelayed(msg, 500);
          }
          @Override
          public void onMouseLeaveFromEdge() {
            mHandler.removeMessages(MSG_REQUEST_TRANSIENT_BARS);
          }
        });

The code listens for gesture operations, where onSwipeFromRight() It is triggered when sliding from the right side of the screen to the left, as long as the return function is added here;

The function code to realize the return button is as follows


private void sendKeyCode(final int keyCode) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          //  Create 1 A Instrumentation Object 
          Instrumentation inst = new Instrumentation();
          //  Call inst Key-press simulation method of object 
          inst.sendKeyDownUpSync(keyCode);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }).start();
  }

Summarize


Related articles: