예제 #1
0
  private void refreshCampaigns() {
    mSharedPreferencesHelper.setLastCampaignRefreshTime(System.currentTimeMillis());
    new CampaignReadTask(this) {

      @Override
      protected void onPreExecute() {
        super.onPreExecute();
        getActionBar().setProgressVisible(true);
      }

      @Override
      protected void onPostExecute(CampaignReadResponse response) {
        if (response == null) return;
        super.onPostExecute(response);
        getActionBar().setProgressVisible(false);
      }
    }.execute(mSharedPreferencesHelper.getUsername(), mSharedPreferencesHelper.getHashedPassword());
  }
  private void loginFinished(String username, String hashedPassword) {

    // save creds
    mPreferencesHelper.putUsername(username);
    mPreferencesHelper.putHashedPassword(hashedPassword);

    // clear related notifications
    // NotificationHelper.cancel(LoginActivity.this, NotificationHelper.NOTIFY_LOGIN_FAIL);
    // makes more sense to clear notification on launch, so moved to oncreate

    // start services
    // set alarms
    // register receivers
    // BackgroundManager.initAuthComponents(this);

    boolean isFirstRun = mPreferencesHelper.isFirstRun();

    if (isFirstRun) {
      Log.i(TAG, "this is the first run");

      // Funf Ohmage
      // BackgroundManager.initComponents(this);

      // cancel get started notification. this works regardless of how we start the app
      // (notification or launcher)
      // NotificationHelper.cancel(this, NotificationHelper.NOTIFY_GET_STARTED, null);

      // show intro dialog
      // showDialog(DIALOG_FIRST_RUN);
      mPreferencesHelper.setFirstRun(false);
      mPreferencesHelper.putLoginTimestamp(System.currentTimeMillis());
    } else {
      Log.i(TAG, "this is not the first run");
    }

    if (mUpdateCredentials) finish();
    else startActivityForResult(new Intent(this, MainActivity.class), LOGIN_FINISHED);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");
    setContentView(R.layout.login);

    // first see if they are already logged in
    final SharedPreferencesHelper preferencesHelper = new SharedPreferencesHelper(this);

    // Funf Ohmage
    /*
    if (preferencesHelper.isUserDisabled()) {
    	((OhmageApplication) getApplication()).resetAll();
    }*/

    mUpdateCredentials = getIntent().getBooleanExtra(EXTRA_UPDATE_CREDENTIALS, false);

    // if they are, redirect them to the dashboard
    if (preferencesHelper.isAuthenticated() && !mUpdateCredentials) {
      startActivityForResult(new Intent(this, MainActivity.class), LOGIN_FINISHED);
      return;
    }

    mLoginButton = (Button) findViewById(R.id.login);
    mUsernameEdit = (EditText) findViewById(R.id.login_username);
    mPasswordEdit = (EditText) findViewById(R.id.login_password);
    mVersionText = (TextView) findViewById(R.id.version);

    try {
      mVersionText.setText("v" + getPackageManager().getPackageInfo("org.ohmage", 0).versionName);
    } catch (Exception e) {
      Log.e(TAG, "unable to retrieve version", e);
      mVersionText.setText(" ");
    }

    mLoginButton.setOnClickListener(mClickListener);

    mPreferencesHelper = new SharedPreferencesHelper(this);

    if (savedInstanceState == null) {
      Log.i(TAG, "creating from scratch");

      // clear login fail notification (if such notification existed)
      // NotificationHelper.cancel(this, NotificationHelper.NOTIFY_LOGIN_FAIL, null);
      // move this down so notification is only cleared if login is successful???

      // the following block is commented out to support single user lock-in, implemented in the
      // code below
      /*String username = mPreferencesHelper.getUsername();
           if (username.length() > 0) {
           	Log.i(TAG, "saved credentials exist");
      mUsernameEdit.setText(username);
           	mPasswordEdit.setText(DUMMY_PASSWORD);
           	//launch main activity and finish
           	//WE CAN'T DO THIS BECAUSE IF THE ACTIVITY IS LAUNCHED AFTER AN HTTP ERROR NOTIFICATION, CREDS WILL STILL BE STORED
           	//startActivity(new Intent(LoginActivity.this, MainActivity.class));
           	//finish();
           	//OR do Login?
           	doLogin();


           	//what if this works the other way around? make main activity the starting point, and launch login if needed?
           } else {
           	Log.i(TAG, "saved credentials do not exist");
           }*/

      // Funf Ohmage
      /*
      if (mPreferencesHelper.isUserDisabled()) {
      	((OhmageApplication) getApplication()).resetAll();
      }
      */

      // code for single user lock-in
      String username = mPreferencesHelper.getUsername();
      if (username.length() > 0) {
        Log.i(TAG, "saved username exists");
        mUsernameEdit.setText(username);
        mUsernameEdit.setEnabled(false);
        mUsernameEdit.setFocusable(false);

        //    			String hashedPassword = mPreferencesHelper.getHashedPassword();
        //    			if (hashedPassword.length() > 0) {
        //    				Log.i(TAG, "saved password exists");
        //    				mPasswordEdit.setText(DUMMY_PASSWORD);
        //    				//doLogin();
        //    			} else {
        //    				Log.i(TAG, "no saved password, must have had a login problem");
        //    			}

      } else {
        Log.i(TAG, "no saved username");
      }
    }

    // Funf-Ohmage
    // Since Campaighns not needed
    /*
    mCampaignDownloadTask = (CampaignReadTask) getSupportLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<CampaignReadResponse>()
    {
    	@Override
    	public Loader<CampaignReadResponse> onCreateLoader(int id, Bundle args) {
    		return new CampaignReadTask(OhmageLoginActivity.this, null, null);
    	}

    	@Override
    	public void onLoadFinished(Loader<CampaignReadResponse> loader, CampaignReadResponse data)
    	{
    		String urn = Campaign.getSingleCampaign(OhmageLoginActivity.this);
    		if(urn == null) {
    			// Downloading the campaign failed so we should reset the username and password
    			mPreferencesHelper.clearCredentials();
    			Toast.makeText(OhmageLoginActivity.this, R.string.login_error_downloading_campaign, Toast.LENGTH_LONG).show();
    		} else {
    			loginFinished(((CampaignReadTask) loader).getUsername(), ((CampaignReadTask) loader).getHashedPassword());
    		}
    		dismissDialog(DIALOG_DOWNLOADING_CAMPAIGNS);
    	}

    	@Override
    	public void onLoaderReset(Loader<CampaignReadResponse> loader)
    	{
    	}
    });*/
  }
  private void onLoginTaskDone(OhmageApi.AuthenticateResponse response, final String username) {

    try {
      dismissDialog(DIALOG_LOGIN_PROGRESS);
    } catch (IllegalArgumentException e) {
      Log.e(TAG, "Attempting to dismiss dialog that had not been shown.");
      e.printStackTrace();
    }

    switch (response.getResult()) {
      case SUCCESS:
        Log.i(TAG, "login success");
        final String hashedPassword = response.getHashedPassword();

        // Funf Ohmage: Since no camplaigns needed
        /*
        if(Config.IS_SINGLE_CAMPAIGN) {
        	// Download the single campaign
        	showDialog(DIALOG_DOWNLOADING_CAMPAIGNS);

        	// Temporarily set the user credentials. If the download fails, they will be removed
        	// We need to set this so the ResponseSyncService can use them once the campaign is downloaded
        	mPreferencesHelper.putUsername(username);
        	mPreferencesHelper.putHashedPassword(hashedPassword);

        	mCampaignDownloadTask.setCredentials(username, hashedPassword);
        	mCampaignDownloadTask.forceLoad();
        } else {
        	loginFinished(username, hashedPassword);
        }*/
        loginFinished(username, hashedPassword);
        break;
      case FAILURE:
        Log.e(TAG, "login failure");
        for (String s : response.getErrorCodes()) {
          Log.e(TAG, "error code: " + s);
        }

        // clear creds
        // mPreferencesHelper.clearCredentials();
        // just clear password, keep username for single user lock-in
        // FAISAL: commenting this out so the user gets a chance to back out of a password change
        // attempt
        /*mPreferencesHelper.putHashedPassword("");*/

        // clear password so user will re-enter it
        mPasswordEdit.setText("");

        // show error dialog
        if (Arrays.asList(response.getErrorCodes()).contains("0201")) {
          mPreferencesHelper.setUserDisabled(true);
          showDialog(DIALOG_USER_DISABLED);
        } else {
          showDialog(DIALOG_LOGIN_ERROR);
        }
        break;
      case HTTP_ERROR:
        Log.e(TAG, "login http error");

        // show error dialog
        showDialog(DIALOG_NETWORK_ERROR);
        break;
      case INTERNAL_ERROR:
        Log.e(TAG, "login internal error");

        // show error dialog
        showDialog(DIALOG_INTERNAL_ERROR);
        break;
    }
  }