3

I have a widget placed programatically on the Home screen, and I made it possible for the user to drag it to the place he wishes. I have added an OnTouchListener to the view to get motion events (so that I can remember view position on screen for next time), and it fires wherever I click on the Home screen, even when this is outside of my view.

What I would like to achieve is to forward the MotionEvent.ACTION_UP to the Launcher app so that, if user clicked on an app icon, that app will be launched. Or alternatively, NOT to receive this event when clicked outside of my view. Here's what I have done:

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 inflatedViewWidget = inflater.inflate(R.layout.mtc_appwidget, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        0,
                        PixelFormat.TRANSLUCENT);
final WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
wm.addView(inflatedViewWidget, params);

OnTouchListener:

inflatedViewWidget.setClickable(true);
inflatedViewWidget.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {

                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_UP:
                                //how to forward event to Launcher app?
                                break;
michaelsmith
  • 1,011
  • 1
  • 16
  • 35
  • How about launching the app manually rather than forwarding event to the Launcher app? See this answer : https://stackoverflow.com/a/11262904/9167710 – cgb_pandey Sep 01 '20 at 00:53
  • Are you implying that you've created custom launcher app UI and there you place widgets and other app icons programmatically? – Jeel Vankhede Sep 02 '20 at 07:15

1 Answers1

0

AFAIK, doing a click in another app is not allowed, that would allow overlay apps to make you click on a pay button for example, you would need root or debugging capabilities for that.

So the trick is to play with the window size. Your widget's window size and location should always match the clickable area. You can do this with the updateViewLayout method on the WindowManager.

A good example can be found here: https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

francis duvivier
  • 2,056
  • 11
  • 17