use of com.koushikdutta.async.future.FutureCallback in project CoCoin by Nightonke.
the class AccountBookSettingActivity method downloadLogoFromServer.
// download logo to local///////////////////////////////////////////////////////////////////////////
private void downloadLogoFromServer() {
User user = getCurrentUser();
if (user.getLogoObjectId() == null) {
// the user has no logo
return;
}
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/////////////////////////////////////////////////////////
Log.d("Saver", "There is an old logo");
String url = object.get(0).getFile().getUrl();
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) {
Bitmap bitmap = BitmapFactory.decodeFile(CoCoinApplication.getAppContext().getFilesDir() + CoCoinUtil.LOGO_NAME);
if (bitmap == null) {
Log.d("Saver", "Logo misses");
} else {
logo.setImageBitmap(bitmap);
}
SettingManager.getInstance().setHasLogo(true);
}
});
SettingManager.getInstance().setTodayViewLogoShouldChange(true);
}
@Override
public void onError(int code, String msg) {
// the picture is lost
Log.d("Saver", "Can't find the old logo in server.");
}
});
}
use of com.koushikdutta.async.future.FutureCallback in project aware-client by denzilferreira.
the class SSLManager method downloadCertificate.
/**
* Classic method: Download certificate unconditionally. This is the old
* method of certificate fetching. This method blocks if the block parameter is true,
* otherwise is nonblocking.
*
* @param context app contexto
* @param hostname Hostname to download.
* @param block If true, block until certificate retrieved, otherwise do not.
*/
public static void downloadCertificate(Context context, String hostname, boolean block) {
// Fixed: make sure we have a valid hostname
if (hostname == null || hostname.length() == 0)
return;
// api.awareframework.com is an exception: we download from a different host.
// cert_host is the host from which we actually download.
String cert_host;
if (hostname.contains("api.awareframework.com")) {
cert_host = "awareframework.com";
} else
cert_host = hostname;
File root_folder;
if (context.getApplicationContext().getResources().getBoolean(R.bool.internalstorage)) {
root_folder = new File(context.getFilesDir(), "/credentials/" + hostname);
} else if (!context.getApplicationContext().getResources().getBoolean(R.bool.standalone)) {
// sdcard/AWARE/ (shareable, does not delete when uninstalling)
root_folder = new File(Environment.getExternalStoragePublicDirectory("AWARE"), "/credentials/" + hostname);
} else {
// sdcard/Android/<app_package_name>/AWARE/ (not shareable, deletes when uninstalling package)
root_folder = new File(ContextCompat.getExternalFilesDirs(context, null)[0], "/AWARE/credentials/" + hostname);
}
root_folder.mkdirs();
try {
X509Certificate certificate = retrieveRemoteCertificate(new URL(hostname));
Log.d(Aware.TAG, "Certificate info: " + certificate.toString());
byte[] certificate_data = certificate.getEncoded();
FileOutputStream outputStream = new FileOutputStream(new File(root_folder.toString() + "/server.crt"));
outputStream.write(certificate_data);
outputStream.close();
} catch (CertificateEncodingException | IOException | NullPointerException e) {
Ion.getDefault(context.getApplicationContext()).getConscryptMiddleware().enable(false);
Future https = Ion.with(context.getApplicationContext()).load("http://" + cert_host + "/public/server.crt").noCache().write(new File(root_folder.toString() + "/server.crt")).setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File result) {
if (e != null) {
Log.d(Aware.TAG, "ERROR SSL certificate: " + e.getMessage());
}
}
});
if (block) {
try {
https.get();
} catch (java.lang.InterruptedException | ExecutionException j) {
// What to do here?
}
}
}
}
Aggregations