Search in sources :

Example 11 with FutureCallback

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.");
        }
    });
}
Also used : BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Logo(com.nightonke.saver.model.Logo) Bitmap(android.graphics.Bitmap) BmobQuery(cn.bmob.v3.BmobQuery) BmobFile(cn.bmob.v3.datatype.BmobFile) File(java.io.File) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Example 12 with FutureCallback

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?
            }
        }
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) URL(java.net.URL) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) CertificateEncodingException(java.security.cert.CertificateEncodingException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) FileOutputStream(java.io.FileOutputStream) Future(com.koushikdutta.async.future.Future) File(java.io.File) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Aggregations

FutureCallback (com.koushikdutta.async.future.FutureCallback)12 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)8 Bitmap (android.graphics.Bitmap)6 BmobQuery (cn.bmob.v3.BmobQuery)6 BmobUser (cn.bmob.v3.BmobUser)6 Logo (com.nightonke.saver.model.Logo)6 User (com.nightonke.saver.model.User)6 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)5 BmobFile (cn.bmob.v3.datatype.BmobFile)3 Future (com.koushikdutta.async.future.Future)2 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Paint (android.graphics.Paint)1 Point (android.graphics.Point)1 BmobProFile (com.bmob.BmobProFile)1 JsonArray (com.google.gson.JsonArray)1