Search in sources :

Example 1 with BoxFutureTask

use of com.box.androidsdk.content.BoxFutureTask in project box-android-sdk by box.

the class BoxRequestBatch method onSend.

@Override
public BoxResponseBatch onSend() throws BoxException {
    BoxResponseBatch responses = new BoxResponseBatch();
    if (mExecutor != null) {
        ArrayList<BoxFutureTask<BoxObject>> tasks = new ArrayList<BoxFutureTask<BoxObject>>();
        for (BoxRequest req : mRequests) {
            BoxFutureTask task = req.toTask();
            mExecutor.submit(task);
            tasks.add(task);
        }
        for (BoxFutureTask<BoxObject> task : tasks) {
            try {
                BoxResponse<BoxObject> response = task.get();
                responses.addResponse(response);
            } catch (InterruptedException e) {
                throw new BoxException(e.getMessage(), e);
            } catch (ExecutionException e) {
                throw new BoxException(e.getMessage(), e);
            }
        }
    } else {
        for (BoxRequest req : mRequests) {
            BoxObject value = null;
            Exception ex = null;
            try {
                value = req.send();
            } catch (Exception e) {
                ex = e;
            }
            BoxResponse<BoxObject> response = new BoxResponse<BoxObject>(value, ex, req);
            responses.addResponse(response);
        }
    }
    return responses;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) ArrayList(java.util.ArrayList) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) ExecutionException(java.util.concurrent.ExecutionException) BoxException(com.box.androidsdk.content.BoxException) BoxObject(com.box.androidsdk.content.models.BoxObject) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with BoxFutureTask

use of com.box.androidsdk.content.BoxFutureTask in project box-android-sdk by box.

the class BoxAuthentication method doUserRefresh.

private BoxFutureTask<BoxUser> doUserRefresh(final Context context, final BoxAuthenticationInfo info) {
    BoxSession tempSession = new BoxSession(context, info.accessToken(), null);
    BoxApiUser apiUser = new BoxApiUser(tempSession);
    BoxFutureTask<BoxUser> task = apiUser.getCurrentUserInfoRequest().setFields(BoxUser.ALL_FIELDS).toTask();
    task.addOnCompletedListener(new BoxFutureTask.OnCompletedListener<BoxUser>() {

        @Override
        public void onCompleted(BoxResponse<BoxUser> response) {
            if (response.isSuccess()) {
                info.setUser(response.getResult());
                BoxAuthentication.getInstance().onAuthenticated(info, context);
            } else {
                BoxAuthentication.getInstance().onAuthenticationFailure(info, response.getException());
            }
        }
    });
    AUTH_EXECUTOR.execute(task);
    return task;
}
Also used : BoxApiUser(com.box.androidsdk.content.BoxApiUser) BoxSession(com.box.androidsdk.content.models.BoxSession) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) BoxUser(com.box.androidsdk.content.models.BoxUser)

Example 3 with BoxFutureTask

use of com.box.androidsdk.content.BoxFutureTask in project box-android-sdk by box.

the class BoxAvatarView method updateAvatar.

protected void updateAvatar() {
    if (mUser == null || mAvatarController == null) {
        return;
    }
    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
        post(new Runnable() {

            @Override
            public void run() {
                updateAvatar();
            }
        });
        return;
    }
    final File avatarFile = mAvatarController.getAvatarFile(mUser.getId());
    if (avatarFile.exists()) {
        // load avatar file into view.
        mAvatar.setImageDrawable(Drawable.createFromPath(avatarFile.getAbsolutePath()));
        mAvatar.setVisibility(View.VISIBLE);
        mInitials.setVisibility(View.GONE);
    } else {
        String name = DEFAULT_NAME;
        if (mUser instanceof BoxCollaborator) {
            name = mUser.getName();
        } else if (SdkUtils.isBlank(name) && mUser instanceof BoxUser) {
            name = ((BoxUser) mUser).getLogin();
        }
        int numberOfCollab = 0;
        try {
            numberOfCollab = Integer.parseInt(name);
        } catch (NumberFormatException ex) {
        // do nothing
        }
        if (numberOfCollab == 0) {
            SdkUtils.setInitialsThumb(getContext(), mInitials, name);
        } else {
            SdkUtils.setCollabNumberThumb(getContext(), mInitials, numberOfCollab);
        }
        mAvatar.setVisibility(View.GONE);
        mInitials.setVisibility(View.VISIBLE);
        mAvatarDownloadTaskRef = new WeakReference<BoxFutureTask<BoxDownload>>(mAvatarController.executeAvatarDownloadRequest(mUser.getId(), this));
    }
}
Also used : File(java.io.File) BoxCollaborator(com.box.androidsdk.content.models.BoxCollaborator) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) BoxUser(com.box.androidsdk.content.models.BoxUser)

Example 4 with BoxFutureTask

use of com.box.androidsdk.content.BoxFutureTask in project box-android-sdk by box.

the class DefaultAvatarController method executeAvatarDownloadRequest.

@Override
public BoxFutureTask<BoxDownload> executeAvatarDownloadRequest(final String userId, BoxAvatarView avatarView) {
    final WeakReference<BoxAvatarView> avatarViewWeakReference = new WeakReference<BoxAvatarView>(avatarView);
    try {
        final File avatarFile = getAvatarFile(userId);
        if (mUnavailableAvatars.contains(avatarFile.getAbsolutePath())) {
            // no point trying if we tried before and it was unavailable.
            return null;
        }
        final BoxFutureTask<BoxDownload> avatarDownloadTask = getApiUser().getDownloadAvatarRequest(getAvatarDir(userId), userId).toTask();
        avatarDownloadTask.addOnCompletedListener(new BoxFutureTask.OnCompletedListener<BoxDownload>() {

            @Override
            public void onCompleted(BoxResponse<BoxDownload> response) {
                if (response.isSuccess()) {
                    BoxAvatarView avatarView = avatarViewWeakReference.get();
                    if (avatarView != null) {
                        avatarView.updateAvatar();
                    }
                } else {
                    if (response.getException() instanceof BoxException) {
                        if (((BoxException) response.getException()).getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
                            mUnavailableAvatars.add(getAvatarFile(userId).getAbsolutePath());
                        }
                    }
                    if (avatarFile != null) {
                        avatarFile.delete();
                    }
                }
            }
        });
        executeTask(avatarDownloadTask);
        return avatarDownloadTask;
    } catch (IOException e) {
        BoxLogUtils.e("unable to createFile ", e);
    }
    return null;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxAvatarView(com.box.androidsdk.content.views.BoxAvatarView) BoxDownload(com.box.androidsdk.content.models.BoxDownload) WeakReference(java.lang.ref.WeakReference) IOException(java.io.IOException) File(java.io.File) BoxRequestsFile(com.box.androidsdk.content.requests.BoxRequestsFile) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask)

Aggregations

BoxFutureTask (com.box.androidsdk.content.BoxFutureTask)4 BoxException (com.box.androidsdk.content.BoxException)2 BoxUser (com.box.androidsdk.content.models.BoxUser)2 File (java.io.File)2 BoxApiUser (com.box.androidsdk.content.BoxApiUser)1 BoxCollaborator (com.box.androidsdk.content.models.BoxCollaborator)1 BoxDownload (com.box.androidsdk.content.models.BoxDownload)1 BoxObject (com.box.androidsdk.content.models.BoxObject)1 BoxSession (com.box.androidsdk.content.models.BoxSession)1 BoxRequestsFile (com.box.androidsdk.content.requests.BoxRequestsFile)1 BoxAvatarView (com.box.androidsdk.content.views.BoxAvatarView)1 IOException (java.io.IOException)1 WeakReference (java.lang.ref.WeakReference)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1