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;
}
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;
}
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));
}
}
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;
}
Aggregations