Search in sources :

Example 1 with BoxUser

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));
    }
}
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 2 with BoxUser

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);
}
Also used : Context(android.content.Context) BoxUser(com.box.androidsdk.content.models.BoxUser) BoxException(com.box.androidsdk.content.BoxException)

Example 3 with BoxUser

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;
}
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 4 with BoxUser

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);
}
Also used : FutureTask(java.util.concurrent.FutureTask) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) BoxUser(com.box.androidsdk.content.models.BoxUser) BoxException(com.box.androidsdk.content.BoxException)

Aggregations

BoxUser (com.box.androidsdk.content.models.BoxUser)4 BoxFutureTask (com.box.androidsdk.content.BoxFutureTask)3 BoxException (com.box.androidsdk.content.BoxException)2 Context (android.content.Context)1 BoxApiUser (com.box.androidsdk.content.BoxApiUser)1 BoxCollaborator (com.box.androidsdk.content.models.BoxCollaborator)1 BoxSession (com.box.androidsdk.content.models.BoxSession)1 File (java.io.File)1 FutureTask (java.util.concurrent.FutureTask)1