use of com.box.androidsdk.content.models.BoxUser 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.models.BoxUser in project box-android-sdk by box.
the class BoxAuthentication method logout.
/**
* Log out current BoxSession. After logging out, the authentication information related to the Box user in this session will be gone.
* @param session session to logout user from
*/
public synchronized void logout(final BoxSession session) {
BoxUser user = session.getUser();
if (user == null) {
return;
}
session.clearCache();
Context context = session.getApplicationContext();
String userId = user.getId();
getAuthInfoMap(session.getApplicationContext());
BoxAuthenticationInfo info = mCurrentAccessInfo.get(userId);
Exception ex = null;
try {
BoxApiAuthentication api = new BoxApiAuthentication(session);
BoxApiAuthentication.BoxRevokeAuthRequest request = api.revokeOAuth(info.refreshToken(), session.getClientId(), session.getClientSecret());
request.send();
} catch (Exception e) {
ex = e;
BoxLogUtils.e(TAG, "logout", e);
// Do nothing as we want to continue wiping auth info
}
mCurrentAccessInfo.remove(userId);
String lastUserId = authStorage.getLastAuthentictedUserId(context);
if (lastUserId != null && userId.equals(userId)) {
authStorage.storeLastAuthenticatedUserId(null, context);
}
authStorage.storeAuthInfoMap(mCurrentAccessInfo, context);
onLoggedOut(info, ex);
}
use of com.box.androidsdk.content.models.BoxUser 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.models.BoxUser in project box-android-sdk by box.
the class BoxAuthentication method refresh.
/**
* Refresh the OAuth in the given BoxSession. This method is called when OAuth token expires.
* @param session a box session with all the necessary information to authenticate the user for the first time.
* @return a future task allowing monitoring of the api call.
* @throws BoxException thrown if there are any errors in refreshing this session.
*/
public synchronized FutureTask<BoxAuthenticationInfo> refresh(BoxSession session) throws BoxException {
BoxUser user = session.getUser();
if (user == null) {
return doRefresh(session, session.getAuthInfo());
}
// Fetch auth info map from storage if not present.
getAuthInfoMap(session.getApplicationContext());
BoxAuthenticationInfo info = mCurrentAccessInfo.get(user.getId());
if (info == null) {
// session has info that we do not. ? is there any other situation we want to update our info based on session info? we can do checks against
// refresh time.
mCurrentAccessInfo.put(user.getId(), session.getAuthInfo());
info = mCurrentAccessInfo.get(user.getId());
}
// No need to refresh if we have already refreshed within 15 seconds or have a newer access token already.
if (session.getAuthInfo().accessToken() == null || (!session.getAuthInfo().accessToken().equals(info.accessToken()) && info.getRefreshTime() != null && System.currentTimeMillis() - info.getRefreshTime() < 15000)) {
final BoxAuthenticationInfo latestInfo = info;
// this session is probably using old information. Give it our information.
BoxAuthenticationInfo.cloneInfo(session.getAuthInfo(), info);
FutureTask task = new FutureTask<BoxAuthenticationInfo>(new Callable<BoxAuthenticationInfo>() {
@Override
public BoxAuthenticationInfo call() throws Exception {
return latestInfo;
}
});
AUTH_EXECUTOR.execute(task);
return task;
}
FutureTask task = mRefreshingTasks.get(user.getId());
if (task != null && !(task.isCancelled() || task.isDone())) {
// We already have a refreshing task for this user. No need to do anything.
return task;
}
// create the task to do the refresh and put it in mRefreshingTasks and execute it.
return doRefresh(session, info);
}
Aggregations