Search in sources :

Example 1 with BoxDownload

use of com.box.androidsdk.content.models.BoxDownload 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

BoxException (com.box.androidsdk.content.BoxException)1 BoxFutureTask (com.box.androidsdk.content.BoxFutureTask)1 BoxDownload (com.box.androidsdk.content.models.BoxDownload)1 BoxRequestsFile (com.box.androidsdk.content.requests.BoxRequestsFile)1 BoxAvatarView (com.box.androidsdk.content.views.BoxAvatarView)1 File (java.io.File)1 IOException (java.io.IOException)1 WeakReference (java.lang.ref.WeakReference)1