Help: OnTouch Callback

135 views
Skip to first unread message

Tenz WH

unread,
Jun 27, 2017, 8:34:17β€―AM6/27/17
to DroidScript
I make my own plugin, but can't respond OnTouch callback, how the sample data returned to be responded as an object like DS ImageView OnTouch result

Symbroson

unread,
Jun 27, 2017, 8:38:41β€―AM6/27/17
to DroidScript
what do you have tried for now? What is your current implementation

Symbroson

unread,
Jun 27, 2017, 8:40:35β€―AM6/27/17
to DroidScript
normally the OnTouch callback function have to be called with passing an event object including x,y,X,Y,count,action and _source

Tenz WH

unread,
Jun 27, 2017, 8:47:43β€―AM6/27/17
to DroidScript
My callback method was called, but the app stopped.
send this data from my java plugin

json:{\\\"id\\\": \\\"#"+getId()+"\\\"}";

Tenz WH

unread,
Jun 27, 2017, 8:52:29β€―AM6/27/17
to DroidScript
String result = "json:{\\\"id\\\" : \\\"#"+getId()+"\\\"";

if (!act.equals("LongTouch") && !act.equals("Click")) {
float[][] pointer = getXY(event);
result += ", \\\"action\\\" : \\\""+act+"\\\""+
", \\\"count\\\" : "+cnt+
", \\\"X\\\" : "+pointer[0][0]+
", \\\"Y\\\" : "+pointer[0][1]+
", \\\"x\\\" : ["+pointer[0][0]+", "+pointer[1][0]+", "+pointer[2][0]+"], "+
"\\\"y\\\" : ["+pointer[0][1]+", "+pointer[1][1]+", "+pointer[2][1]+"]";
}
result += "}";
main.responseCallback(cb, result);

Symbroson

unread,
Jun 27, 2017, 9:38:27β€―AM6/27/17
to DroidScript
Im not sure where the problem is cauz I dont know what the variables contains and how the tesult string was handled after that. Have you looked at what result is at the end?

Tenz WH

unread,
Jun 27, 2017, 10:52:25β€―AM6/27/17
to DroidScript
just show as string. but when I touch my img the app stuck. maybe DS Plugin manager get wrong data from my java. <br>
<br>
function myTouchCallback(event) {
app.ShowPopup(JSON.stringify(event));
}
Screenshot_2017-06-27-20-32-47.png

Symbroson

unread,
Jun 27, 2017, 1:48:07β€―PM6/27/17
to DroidScript
sorry the popup is not visible in your screenshot. try alert() instead of app.ShowPopup()

Dave Smart

unread,
Jun 27, 2017, 2:01:02β€―PM6/27/17
to DroidScript
Here is an example snippet from our Bluetooth plugin which might help you:-


//Fire callback in script.
Bundle b = new Bundle();
b.putString( "cmd", m_OnDeviceFound );
b.putString( "p1", (device.getName()==null ? "" : device.getName().toString() ) );
b.putString( "p2", device.getAddress() );
b.putBoolean( "p3", (device.getBondState()==BluetoothDevice.BOND_BONDED) );
b.putInt( "p4", rssi );

if( dd.typeString.equals("iBeacon") )Β 
{
b.putString( "p5", "json:{type:\"" + dd.typeString + "\",txPower:" + dd.txPowerΒ 
+ ",Id:\"" + dd.proximityId + "\",major:" + dd.major + ",minor:" + dd.minor + "}" );
}

CallScript( b );

Tenz WH

unread,
Jun 27, 2017, 2:05:46β€―PM6/27/17
to DroidScript
yes. That's the point. I did not get anything in the callback because the app stops when I pressing the image. I guess it's because the data from my java is being processed by the DroidScript Plugin.class. and whatever happened there. But it looks like there is constant repetition. Probably because the data format from my java is wrong. So I need the correct data format so that the returned results as objects like event objects of OnTouch result in DroidScript ImageView

Tenz WH

unread,
Jun 28, 2017, 1:57:56β€―AM6/28/17
to DroidScript
I think my problem is my onTouch method. can you fix this.


@Override
public boolean onTouch(View view, MotionEvent event)
{

// Check for clickable state and do nothing if disabled
if(!this.isClickable()) {
this.isSelected = false;
return super.onTouchEvent(event);
}


// Set selected state based on Motion Event
String act = "";
String cb = "";
int index = event.getActionIndex();
int pid = event.getPointerId(index);
int cnt = event.getPointerCount();
switch(event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
PointF p = new PointF();
p.x = event.getX();
p.y = event.getY();
activePointers.put(pid, p);
lastX = event.getX();
lastY = event.getY();
isSelected = true;
ctime = System.currentTimeMillis();
if (touchdown_cb != null) {
act = "Down";
cb = touchdown_cb;
}
else if (touch_cb != null) {
act = "Down";
cb = touch_cb;
}
else
act = "";
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
isSelected = false;
activePointers.remove(pid);
if (touchup_cb != null){
act = "Up";
cb = touchup_cb;
}
else if (touch_cb != null) {
act = "Up";
cb = touch_cb;
}
else act = "";
ctime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
isSelected = true;
if (touchmove_cb != null) {
act = "Move";
cb = touchmove_cb;
}
else if (touch_cb != null) {
act = "Move";
cb = touch_cb;
}
else act = "";
break;
case MotionEvent.ACTION_SCROLL:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
//ctime = System.currentTimeMillis();
isSelected = false;
act = "";
break;
}
if ( !cb.equals("") && !act.equals("") ) {
String result = "json:{\"id\":\"#"+pid+"\", \"action\":\""+act+"\"";


if (!act.equals("LongTouch") && !act.equals("Click")) {

result += ",\"count\":"+cnt+
",\"X\":"+event.getX()+
",\"Y\":"+event.getY()+
",\"x\":["+getTouched(0)+"]"+
",\"y\":["+getTouched(1)+"]";
}
result += "}";

//result = "testtt";
main.responseCallback(cb, result);
}

// Redraw image and return super type
this.invalidate();
return super.onTouchEvent(event);
}

Tenz WH

unread,
Jun 28, 2017, 2:09:23β€―AM6/28/17
to DroidScript
this my getTouched method


private String getTouched(int type) {
String res = "";
if ( activePointers.size() < 1)
return "";
for (int i = 0; i<activePointers.size(); i++) {
int k = activePointers.keyAt(i);
PointF p = activePointers.get(k);
if (type == 0)
res += p.x+", ";
else
res += p.y+", ";
}
res = res.substring(0, res.length()-2);
return res;
}

Tenz WH

unread,
Jun 28, 2017, 9:58:24β€―AM6/28/17
to DroidScript
FIXED

Symbroson

unread,
Jun 28, 2017, 10:17:27β€―AM6/28/17
to DroidScript
good to know :)
good luck with your following progress

Tenz WH

unread,
Jun 28, 2017, 11:42:50β€―AM6/28/17
to DroidScript
πŸ‘πŸ˜„
Screenshot_2017-06-28-22-30-01.png
Reply all
Reply to author
Forward
0 new messages