Search in sources :

Example 1 with FutureCallback

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);
}
Also used : WebSocket(com.koushikdutta.async.http.WebSocket) SocketIOTransport(com.koushikdutta.async.http.socketio.transport.SocketIOTransport) WebSocketTransport(com.koushikdutta.async.http.socketio.transport.WebSocketTransport) XHRPollingTransport(com.koushikdutta.async.http.socketio.transport.XHRPollingTransport) FutureCallback(com.koushikdutta.async.future.FutureCallback) HashSet(java.util.HashSet) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 2 with FutureCallback

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

Example 3 with FutureCallback

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

Example 4 with FutureCallback

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);
    }
}
Also used : BmobUser(cn.bmob.v3.BmobUser) User(com.nightonke.saver.model.User) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) 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 5 with FutureCallback

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);
}
Also used : CancellationException(java.util.concurrent.CancellationException) ArrayList(java.util.ArrayList) Point(android.graphics.Point) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) FutureCallback(com.koushikdutta.async.future.FutureCallback)

Aggregations

FutureCallback (com.koushikdutta.async.future.FutureCallback)11 FileNotFoundException (java.io.FileNotFoundException)7 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 File (java.io.File)6 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)4 BmobFile (cn.bmob.v3.datatype.BmobFile)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)2 Point (android.graphics.Point)1 BmobProFile (com.bmob.BmobProFile)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 AsyncSSLException (com.koushikdutta.async.AsyncSSLException)1 Future (com.koushikdutta.async.future.Future)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 WebSocket (com.koushikdutta.async.http.WebSocket)1