Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppWidgetHostView can't handle onClick/onLongClick properly

I am developing a launcher, now I am working with app widgets, I follow the tutorial from here: AppWigetHost tutrial - Leonardo Fischer Everything goes well until I tried adding onLongCick listener on the AppWidgetHostView

private void createAppWidget(Intent data) {
    Bundle extras = data.getExtras();
    final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    LinearLayout.LayoutParams params = createLayoutParams(appWidgetInfo); 
    rootLayout.addView(hostView, params);
    hostView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(getApplicationContext(), "Long click " + appWidgetId, Toast.LENGTH_LONG).show();
            return true;
        }
    });
}

When I added a widget on the host (The default Google Analog Clock) - and tried to long click on it, it just doesn't work ! After the long click it just opens the Clock application (default action for click).

Can anyone tell me how to fix it ? Here is my full code: http://pastebin.com/61TkuLvx

Added I've just checked again:
----If I long click on the bound of the Clock widget, the longClick event is fired !
----I added my custom appwidget (which has no onclick handler): so the longclick event is always fired properly.
So I guess, it must have something with touchevent handler/dispatcher to do.

Added I've just tried to set onLongClick on all children of the hostview like this:

private void createAppWidget(Intent data) {
...
... 
    setChildrenViewLongClick(hostView, new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(getApplicationContext(), "Long click " + appWidgetId, Toast.LENGTH_LONG).show();
            return true;
        }
    });
}

protected void setChildrenViewLongClick(View view, OnLongClickListener listener) {
    view.setOnLongClickListener(listener);
    String name = view.getClass().getName();
    Logger.logInfo("Classname: " + name);
    if ( view instanceof ViewGroup ) {
        ViewGroup vg = (ViewGroup) view;
        for(int i = 0 ; i < vg.getChildCount() ; i++ ) {
            setViewLongClick(vg.getChildAt(i), listener);
        }
    }
}

It just doesn't work :(

The weird thing is: I did another experiment on a another activity, which has a Linearlayout LL( has onLongClick handler ) contains 2 buttons bt1,bt2 - (each button has onClick handler) And apply the method setChildrenViewLongClick(LL,onLongClickListener)- then long click on bt2 - Woop, it works, the action inside onLongClickListener is called !
So which is the problem ? The AppWidgetHostView or the event/handler dispatcher ? Or me (my codes)

I am so confused, please help me !

Tks

like image 371
hungson175 Avatar asked Jun 06 '14 04:06

hungson175


2 Answers

Hungson175's answer was great but it didn't get me there all the way. Since I'm using AppWidgetHost to create the AppWidgetHostView's I needed to extend both AppWidgetHost, and AppWidgetHostView. Luckily this is fairly simple to do and doesn't require too much overriding of default android methods.

WidgetHost

public class WidgetHost extends AppWidgetHost {

    public WidgetHost(Context context, int hostId) {
        super(context, hostId);
    }

    @Override
    protected AppWidgetHostView onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget) {
        // pass back our custom AppWidgetHostView
        return new WidgetView(context);
    }
}

WidgetView

public class WidgetView extends AppWidgetHostView {

    private OnLongClickListener longClick;
    private long down;

    public WidgetView(Context context) {
        super(context);
    }

    public WidgetView(Context context, int animationIn, int animationOut) {
        super(context, animationIn, animationOut);
    }

    @Override
    public void setOnLongClickListener(OnLongClickListener l) {
        this.longClick = l;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch(MotionEventCompat.getActionMasked( ev ) ) {
            case MotionEvent.ACTION_DOWN:
                down = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                boolean upVal = System.currentTimeMillis() - down > 300L;
                if( upVal ) {
                    longClick.onLongClick( WidgetView.this );
                }
                break;
        }

        return false;
    }
}

Hope it helps someone, because dealing with AppWidget's is difficult enough.

like image 185
Chris Sullivan Avatar answered Sep 26 '22 22:09

Chris Sullivan


after some days without any answer from SO, I tried to read the source code of Trebuchet-launcher It turns out very simple: extends the AppWidgetHostView and override the method onInterceptTouchEvent() like this source code - I haven't tried it yet, but I guess it will work :).

Hope this helps anyone like me :)

like image 30
hungson175 Avatar answered Sep 25 '22 22:09

hungson175