private User getUserFromPreferenceFile(Site site) {
    SharedPreferences preferenceFile = getSharedPreferences(PREFERENCE_FILE, MODE_PRIVATE);
    String authToken = preferenceFile.getString(site.mName + AUTH_TOKEN_KEY, null);
    String username = preferenceFile.getString(site.mName + USERNAME_KEY, null);
    int userid = preferenceFile.getInt(site.mName + USERID_KEY, 0);
    User user = null;

    if (username != null && authToken != null) {
      user = new User();
      user.setAuthToken(authToken);
      user.setUsername(username);
      user.setUserid(userid);
    }

    return user;
  }
  /** Logs the given user in by writing it to SharedPreferences and setting mUser. */
  public void login(User user) {
    final SharedPreferences prefs = getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
    Editor editor = prefs.edit();
    editor.putString(mSite.mName + AUTH_TOKEN_KEY, user.getAuthToken());
    editor.putString(mSite.mName + USERNAME_KEY, user.getUsername());
    editor.putInt(mSite.mName + USERID_KEY, user.getUserid());
    editor.commit();
    mUser = user;

    getBus().post(new LoginEvent.Login(mUser));

    setIsLoggingIn(false);

    /** Execute pending API call if one exists. */
    Intent pendingApiCall = APIService.getAndRemovePendingApiCall(this);
    if (pendingApiCall != null) {
      startService(pendingApiCall);
    }
  }
Beispiel #3
0
  /** Login parsing info */
  public static User parseLoginInfo(String json) throws JSONException {
    JSONObject jUser = new JSONObject(json);

    User user = new User();
    user.setUserid(jUser.getInt("userid"));
    user.setUsername(jUser.getString("username"));

    if (!jUser.isNull("image")) user.setAvatar(parseImage(jUser.getJSONObject("image"), null));

    user.setAboutRaw(jUser.getString("about_raw"));
    user.setAboutRendered(jUser.getString("about_rendered"));

    if (!jUser.isNull("summary")) user.setSummary(jUser.getString("summary"));
    if (!jUser.isNull("location")) user.setLocation(jUser.getString("location"));
    if (!jUser.isNull("join_date")) user.setJoinDate(jUser.getInt("join_date"));

    user.setBadges(parseBadges(jUser.getJSONObject("badge_counts")));
    user.setReputation(jUser.getInt("reputation"));
    user.setCertificationCount(jUser.getInt("certification_count"));
    user.setAuthToken(jUser.getString("authToken"));

    return user;
  }