Beispiel #1
0
  private Boolean registerUser() {
    // new user
    Log.d(TAG, "new user");
    PutCurrentUserRequest pUser = new PutCurrentUserRequest();
    pUser.setIsVisible(true);
    try {
      myApiService.langeoAPI().putCurrentUser(pUser).execute();
    } catch (IOException e) {
      Log.d(TAG, "IOException " + e.getMessage());
      return false;
    }

    // successful registration
    Log.d(TAG, "successful registration");
    LocalUser.getInstance().fill(eMail, true, true);
    fillPreferences(eMail, true, true);

    return true;
  }
Beispiel #2
0
  @Override
  protected Void doInBackground(String... params) {
    eMail = params[0];

    if (myApiService == null) { // Only do this once
      Langeo.Builder builder;
      GoogleAccountCredential credential =
          GoogleAccountCredential.usingAudience(mContext, AUDIENCE);
      credential.setSelectedAccountName(eMail);
      if (BuildConfig.DEBUG) {
        builder =
            new Langeo.Builder(
                    AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential)
                .setRootUrl(
                    mContext.getResources().getString(R.string.local_server_ip)
                        + mContext.getResources().getString(R.string.local_server_port))
                .setApplicationName(mContext.getResources().getString(R.string.app_name))
                .setGoogleClientRequestInitializer(
                    new GoogleClientRequestInitializer() {
                      @Override
                      public void initialize(
                          AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
                          throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                      }
                    });
      } else {
        builder =
            new Langeo.Builder(
                    AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential)
                .setRootUrl(SERVER_ADDRESS);
      }
      myApiService = builder.build();
    }

    mPrefs = mContext.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
    mPrefsEditor = mPrefs.edit();

    eMailLocal = mPrefs.getString(SP_EMAIL, SP_UNDEFINED);
    if (eMailLocal.equals("u")) {
      Log.d(TAG, "undefined login");
      GetCurrentUserResponse gUser;
      try {
        gUser = myApiService.langeoAPI().getCurrentUser().execute();
      } catch (IOException e) {
        Log.d(TAG, "IOException " + e.getMessage());
        e.printStackTrace();
        gUser = null;
      }

      if (gUser != null) {
        // old user on a new device
        Log.d(TAG, "old user on a new device");
        LocalUser.getInstance().fill(eMail, false, gUser.getIsVisible());
        fillPreferences(eMail, false, gUser.getIsVisible());
        LocalUser.getInstance().setInitialized(true);
      } else {
        // new user
        LocalUser.getInstance().setInitialized(registerUser());
      }
    } else {
      Log.d(TAG, "Some email is stored on device!");
      if (eMail.equals(eMailLocal)) {
        // standard user login
        LocalUser.getInstance()
            .fill(
                mPrefs.getString(SP_EMAIL, SP_UNDEFINED),
                mPrefs.getBoolean(SP_SLIDESHOW, false),
                mPrefs.getBoolean(SP_IS_VISIBLE, true));
        Log.d(TAG, "standard user log in");
        LocalUser.getInstance().setInitialized(true);
      } else {
        // re-login with a new user
        Log.d(TAG, "re-login with a new user - " + eMail + " != " + eMailLocal);
        GetCurrentUserResponse gUser;
        try {
          gUser = myApiService.langeoAPI().getCurrentUser().execute();
        } catch (IOException e) {
          Log.d(TAG, "IOException " + e.getMessage());
          clearPreferences();
          gUser = null;
        }
        if (gUser != null) {
          LocalUser.getInstance().fill(gUser.getEmail(), false, gUser.getIsVisible());
          fillPreferences(gUser.getEmail(), false, gUser.getIsVisible());
          LocalUser.getInstance().setInitialized(true);
        } else {
          // new user
          LocalUser.getInstance().setInitialized(registerUser());
        }
      }
    }
    return null;
  }