use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class CodenameOneImplementation method downloadImageToStorage.
/**
* Downloads an image to storage. This will *not* first check to see if the image is located in storage
* already. It will download and overwrite any existing image at the provided location.
*
* <p>Some platforms may override this method to use platform-level caching. E.g. Javascript will use
* the browser cache for downloading the image.</p>
*
* @param url The URL of the image to download.
* @param fileName The storage key to be used to store the image.
* @param onSuccess Callback on success. Will be executed on EDT.
* @param onFail Callback on failure. Will be executed on EDT.
*/
public void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setReadResponseForErrors(false);
cr.setDuplicateSupported(true);
cr.setUrl(url);
cr.downloadImageToStorage(fileName, onSuccess, onFail);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class CodenameOneImplementation method registerPollingFallback.
/**
* Registers a polling thread to simulate push notification
*/
protected static void registerPollingFallback() {
if (pollingThreadRunning || callback == null) {
return;
}
pollingThreadRunning = true;
final long pushId = Preferences.get("push_id", (long) -1);
if (pushId > -1) {
new CodenameOneThread(new Runnable() {
public void run() {
String lastReq = Preferences.get("last_push_req", "0");
while (pollingThreadRunning) {
try {
ConnectionRequest cr = new ConnectionRequest();
cr.setUrl(Display.getInstance().getProperty("cloudServerURL", "https://codename-one.appspot.com/") + "pollManualPush");
cr.setPost(false);
cr.setFailSilently(true);
cr.addArgument("i", "" + pushId);
cr.addArgument("last", lastReq);
NetworkManager.getInstance().addToQueueAndWait(cr);
if (cr.getResponseCode() != 200) {
callback.pushRegistrationError("Server registration error", 1);
} else {
DataInputStream di = new DataInputStream(new ByteArrayInputStream(cr.getResponseData()));
if (di.readBoolean()) {
byte type = di.readByte();
String message = di.readUTF();
lastReq = "" + di.readLong();
Preferences.set("last_push_req", lastReq);
callback.push(message);
}
}
} catch (IOException ex) {
Log.e(ex);
}
try {
synchronized (callback) {
callback.wait(pollingMillis);
}
} catch (Throwable t) {
Log.e(t);
}
}
}
}, "Polling Thread").start();
}
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class Ads method setAd.
/**
* HTML ad received from the server
* @param ad the ad to set
*/
public void setAd(String ad) {
HTMLComponent html = new HTMLComponent(new AsyncDocumentRequestHandlerImpl() {
protected ConnectionRequest createConnectionRequest(DocumentInfo docInfo, IOCallback callback, Object[] response) {
ConnectionRequest req = super.createConnectionRequest(docInfo, callback, response);
req.setFailSilently(true);
req.addResponseCodeListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do nothing, just make sure the html won't throw an error
}
});
return req;
}
});
html.setSupressExceptions(true);
html.setHTMLCallback(this);
html.setBodyText("<html><body><div align='center'>" + ad + "</div></body></html>");
replace(getComponentAt(0), html, null);
revalidate();
html.setPageUIID("Container");
html.getStyle().setBgTransparency(0);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class FaceBookAccess method getUsersDetails.
/**
* Gets users requested details ((this method uses the legacy rest api see http://developers.facebook.com/docs/reference/rest/))
*
* @param usersIds the users to query
* @param fields which fields to query on the users see http://developers.facebook.com/docs/reference/rest/users.getInfo/
* @param callback the result will call the callback with the result
* to extrct the data preform the following:
* public void actionPerformed(ActionEvent evt) {
* Vector data = (Vector) ((NetworkEvent) evt).getMetaData();
* Vector users = (Vector) data.elementAt(0);
* }
*/
public void getUsersDetails(String[] usersIds, String[] fields, final ActionListener callback) throws IOException {
checkAuthentication();
final FacebookRESTService con = new FacebookRESTService(token, "https://api.facebook.com/method/users.getInfo", false);
String ids = usersIds[0];
int ulen = usersIds.length;
for (int i = 1; i < ulen; i++) {
ids += "," + usersIds[i];
}
con.addArgumentNoEncoding("uids", ids);
String fieldsStr = fields[0];
int flen = fields.length;
for (int i = 1; i < flen; i++) {
fieldsStr += "," + fields[i];
}
con.addArgumentNoEncoding("fields", fieldsStr);
con.addArgument("format", "json");
con.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!con.isAlive()) {
return;
}
if (callback != null) {
callback.actionPerformed(evt);
}
}
});
if (slider != null) {
SliderBridge.bindProgress(con, slider);
}
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
current = con;
NetworkManager.getInstance().addToQueue(con);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class FaceBookAccess method getUserNotifications.
/**
* Gets the user notifications (this method uses the legacy rest api see http://developers.facebook.com/docs/reference/rest/)
*
* @param userId the user id
* @param startTime Indicates the earliest time to return a notification.
* This equates to the updated_time field in the notification FQL table. If not specified, this call returns all available notifications.
* @param includeRead Indicates whether to include notifications that have already been read.
* By default, notifications a user has read are not included.
* @param notifications store notifications results into the given model,
* each entry is an Hashtable Object contaning the Object data
* @param callback the callback that should be updated when the data arrives
*/
public void getUserNotifications(String userId, String startTime, boolean includeRead, DefaultListModel notifications, final ActionListener callback) throws IOException {
checkAuthentication();
final FacebookRESTService con = new FacebookRESTService(token, "https://api.facebook.com/method/notifications.getList", false);
con.addArgument("start_time", startTime);
con.addArgument("include_read", new Boolean(includeRead).toString());
con.addArgument("format", "json");
con.setResponseDestination(notifications);
con.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!con.isAlive()) {
return;
}
if (callback != null) {
callback.actionPerformed(evt);
}
}
});
if (slider != null) {
SliderBridge.bindProgress(con, slider);
}
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
current = con;
NetworkManager.getInstance().addToQueue(con);
}
Aggregations