/
CordovaCallback.java
69 lines (57 loc) · 1.67 KB
/
CordovaCallback.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codename1.cordova;
import ca.weblite.codename1.json.JSONException;
import ca.weblite.codename1.json.JSONObject;
import com.codename1.ui.Display;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
/**
*
* @author Chen
*/
public class CordovaCallback {
private JSONObject response;
private ActionListener listener;
public CordovaCallback() {
}
public CordovaCallback(ActionListener listener) {
this.listener = listener;
}
public void onError(JSONObject json){
this.response = json;
}
public void onSuccess(JSONObject json){
this.response = json;
if(listener != null){
Display.getInstance().callSerially(new Runnable(){
public void run(){
listener.actionPerformed(new ActionEvent(json));
}
});
}
}
public void sendResult(JSONObject json){
this.response = json;
if(listener != null){
Display.getInstance().callSerially(new Runnable(){
public void run(){
listener.actionPerformed(new ActionEvent(json));
}
});
}
}
public JSONObject getResponse() {
return response;
}
public boolean isError(){
try {
return response != null && response.getString("error") != null;
} catch (JSONException ex) {
}
return false;
}
}