public static synchronized Simperium configureSimperium(final Context context, String token) { // Create a new instance of Simperium if it doesn't exist yet. // In any case, authorize the user. if (mSimperium == null) { mSimperium = Simperium.newClient( BuildConfig.SIMPERIUM_APP_NAME, BuildConfig.SIMPERIUM_APP_SECRET, context); try { mNotesBucket = mSimperium.bucket(new Note.Schema()); mMetaBucket = mSimperium.bucket(META_BUCKET_NAME); mSimperium.setUserStatusChangeListener( new User.StatusChangeListener() { @Override public void onUserStatusChange(User.Status status) { switch (status) { case AUTHORIZED: startBuckets(); break; case NOT_AUTHORIZED: mNotesBucket.stop(); mMetaBucket.stop(); EventBus.getDefault().post(new NotificationEvents.SimperiumNotAuthorized()); break; default: AppLog.d(AppLog.T.SIMPERIUM, "User not authorized yet"); break; } } }); } catch (BucketNameInvalid e) { AppLog.e(AppLog.T.SIMPERIUM, e.getMessage()); } } authorizeUser(mSimperium, token); return mSimperium; }
private static void authorizeUser(Simperium simperium, String token) { User user = simperium.getUser(); String tokenFormat = "WPCC/%s/%s"; String wpccToken = String.format(tokenFormat, BuildConfig.SIMPERIUM_APP_SECRET, StringUtils.notNullStr(token)); user.setAccessToken(wpccToken); // we'll assume the user is AUTHORIZED, and catch NOT_AUTHORIZED if something goes wrong. user.setStatus(User.Status.AUTHORIZED); }
public static void resetBucketsAndDeauthorize() { if (mNotesBucket != null) { mNotesBucket.reset(); mNotesBucket = null; } if (mMetaBucket != null) { mMetaBucket.reset(); mMetaBucket = null; } // Reset user status if (mSimperium != null) { mSimperium.getUser().setStatus(User.Status.UNKNOWN); mSimperium = null; } }
public static boolean isUserAuthorized() { return mSimperium != null && mSimperium.getUser() != null && mSimperium.getUser().getStatus() == User.Status.AUTHORIZED; }