use of com.koushikdutta.async.future.FutureCallback in project AndroidAsync by koush.
the class SocketIOConnection method reconnect.
void reconnect(final DependentCancellable child) {
if (isConnected()) {
return;
}
// if a connection is in progress, just wait.
if (connecting != null && !connecting.isDone() && !connecting.isCancelled()) {
if (child != null)
child.setParent(connecting);
return;
}
request.logi("Reconnecting socket.io");
connecting = httpClient.executeString(request, null).then(new TransformFuture<SocketIOTransport, String>() {
@Override
protected void transform(String result) throws Exception {
String[] parts = result.split(":");
final String sessionId = parts[0];
if (!"".equals(parts[1]))
heartbeat = Integer.parseInt(parts[1]) / 2 * 1000;
else
heartbeat = 0;
String transportsLine = parts[3];
String[] transports = transportsLine.split(",");
HashSet<String> set = new HashSet<String>(Arrays.asList(transports));
final SimpleFuture<SocketIOTransport> transport = new SimpleFuture<SocketIOTransport>();
if (set.contains("websocket")) {
final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("websocket").appendPath(sessionId).build().toString();
httpClient.websocket(sessionUrl, null, null).setCallback(new FutureCallback<WebSocket>() {
@Override
public void onCompleted(Exception e, WebSocket result) {
if (e != null) {
transport.setComplete(e);
return;
}
transport.setComplete(new WebSocketTransport(result, sessionId));
}
});
} else if (set.contains("xhr-polling")) {
final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("xhr-polling").appendPath(sessionId).build().toString();
XHRPollingTransport xhrPolling = new XHRPollingTransport(httpClient, sessionUrl, sessionId);
transport.setComplete(xhrPolling);
} else {
throw new SocketIOException("transport not supported");
}
setComplete(transport);
}
}).setCallback(new FutureCallback<SocketIOTransport>() {
@Override
public void onCompleted(Exception e, SocketIOTransport result) {
if (e != null) {
reportDisconnect(e);
return;
}
reconnectDelay = request.config.reconnectDelay;
SocketIOConnection.this.transport = result;
attach();
}
});
if (child != null)
child.setParent(connecting);
}
use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookTagViewActivity method loadLogo.
private void loadLogo() {
User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
if (user != null) {
try {
File logoFile = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (!logoFile.exists()) {
// the local logo file is missed
// try to get from the server
BmobQuery<Logo> bmobQuery = new BmobQuery();
bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {
@Override
public void onSuccess(List<Logo> object) {
// there has been an old logo in the server/////////////////////////////////////////////////////////
String url = object.get(0).getFile().getFileUrl(CoCoinApplication.getAppContext());
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Logo in server: " + url);
Ion.with(CoCoinApplication.getAppContext()).load(url).write(new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME)).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File file) {
profileImage.setImageBitmap(BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME));
}
});
}
@Override
public void onError(int code, String msg) {
// the picture is lost
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Can't find the old logo in server.");
}
});
} else {
// the user logo is in the storage
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(logoFile));
profileImage.setImageBitmap(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
// use the default logo
profileImage.setImageResource(R.drawable.default_user_logo);
}
}
use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookTodayViewActivity method loadLogo.
private void loadLogo() {
User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
if (user != null) {
try {
File logoFile = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (!logoFile.exists()) {
// the local logo file is missed
// try to get from the server
BmobQuery<Logo> bmobQuery = new BmobQuery();
Log.d("CoCoin", user.getLogoObjectId());
bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {
@Override
public void onSuccess(List<Logo> object) {
// there has been an old logo in the server/////////////////////////////////////////////////////////
String url = object.get(0).getFile().getFileUrl(CoCoinApplication.getAppContext());
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Logo in server: " + url);
Ion.with(CoCoinApplication.getAppContext()).load(url).write(new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME)).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File file) {
profileImage.setImageBitmap(BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME));
}
});
}
@Override
public void onError(int code, String msg) {
// the picture is lost
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Can't find the old logo in server.");
}
});
} else {
// the user logo is in the storage
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(logoFile));
profileImage.setImageBitmap(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
// use the default logo
profileImage.setImageResource(R.drawable.default_user_logo);
}
}
use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookSettingActivity method loadLogo.
// Load logo from local/////////////////////////////////////////////////////////////////////////////
private void loadLogo() {
User user = BmobUser.getCurrentUser(CoCoinApplication.getAppContext(), User.class);
if (user != null) {
try {
File logoFile = new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (!logoFile.exists()) {
// the local logo file is missed
// try to get from the server
BmobQuery<Logo> bmobQuery = new BmobQuery();
bmobQuery.addWhereEqualTo("objectId", user.getLogoObjectId());
bmobQuery.findObjects(CoCoinApplication.getAppContext(), new FindListener<Logo>() {
@Override
public void onSuccess(List<Logo> object) {
// there has been an old logo in the server/////////////////////////////////////////////////////////
String url = object.get(0).getFile().getFileUrl(CoCoinApplication.getAppContext());
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Logo in server: " + url);
Ion.with(CoCoinApplication.getAppContext()).load(url).write(new File(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME)).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File file) {
logo.setImageBitmap(BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME));
}
});
}
@Override
public void onError(int code, String msg) {
// the picture is lost
if (BuildConfig.DEBUG)
Log.d("CoCoin", "Can't find the old logo in server.");
}
});
} else {
// the user logo is in the storage
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(logoFile));
logo.setImageBitmap(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
// use the default logo
logo.setImageResource(R.drawable.default_user_logo);
}
}
use of com.koushikdutta.async.future.FutureCallback in project ion by koush.
the class BitmapCallback method report.
protected void report(final Exception e, final BitmapInfo info) {
AsyncServer.post(Ion.mainHandler, new Runnable() {
@Override
public void run() {
BitmapInfo result = info;
if (result == null) {
// cache errors, unless they were cancellation exceptions
result = new BitmapInfo(key, null, null, new Point());
result.exception = e;
if (!(e instanceof CancellationException))
ion.getBitmapCache().put(result);
} else if (put()) {
ion.getBitmapCache().put(result);
} else {
ion.getBitmapCache().putSoft(result);
}
final ArrayList<FutureCallback<BitmapInfo>> callbacks = ion.bitmapsPending.remove(key);
if (callbacks == null || callbacks.size() == 0) {
onReported();
return;
}
for (FutureCallback<BitmapInfo> callback : callbacks) {
callback.onCompleted(e, result);
}
onReported();
}
});
// attempt to smart cache stuff to disk
if (info == null || info.originalSize == null || info.decoder != null || // don't cache anything that requests not to be cached
!put || // don't cache dead bitmaps
info.bitmap == null || // don't cache gifs
info.gifDecoder != null || // too big
info.sizeOf() > 512 * 512 * 4) {
return;
}
saveBitmapSnapshot(ion, info);
}
Aggregations