Android Open Source - trivial-password Password Change Fragment






From Project

Back to project page trivial-password.

License

The source code is released under:

MIT License

If you think the Android project trivial-password listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.hbabcock.trivialpassword;
/*ww w.j  a  v  a 2s  . co  m*/
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PasswordChangeFragment extends DialogFragment {
  private static final String TAG = "PasswordChangeFragment";

  private AlertDialog mAlertDialog;
  private Button mButtonYes;
  private EditText mConfirmPassword;  
  private EditText mCurrentPassword;
  private EditText mNewPassword;
  private PasswordManager mPasswordManager;
  private View mView;

  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState){
    mView = (View)getActivity().getLayoutInflater().inflate(R.layout.dialog_password_change, null);

    mPasswordManager = PasswordManager.get(getActivity());

    ((EditText)mView.findViewById(R.id.password_hint)).setText(mPasswordManager.getHint());

    mCurrentPassword = (EditText)mView.findViewById(R.id.current_password);
    mNewPassword = (EditText)mView.findViewById(R.id.new_password);
    mConfirmPassword = (EditText)mView.findViewById(R.id.confirm_password);
        
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(mView);
    builder.setTitle(R.string.password_change);
    builder.setPositiveButton(
            android.R.string.ok,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                mPasswordManager.savePassword(((EditText)mView.findViewById(R.id.confirm_password)).getText().toString(),
                                ((EditText)mView.findViewById(R.id.password_hint)).getText().toString());                                
              }
            });
    mAlertDialog = builder.create();
    
    return mAlertDialog;
  }
  
  @Override
  public void onResume(){
    super.onResume();
    
    mButtonYes = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    updateDisplay();

      mCurrentPassword.addTextChangedListener(new TextWatcher(){
          public void afterTextChanged(Editable s){
            updateDisplay();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
          public void onTextChanged(CharSequence s, int start, int before, int count){}
      });

      mNewPassword.addTextChangedListener(new TextWatcher(){
          public void afterTextChanged(Editable s){
            updateDisplay();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
          public void onTextChanged(CharSequence s, int start, int before, int count){}
      });    

      mConfirmPassword.addTextChangedListener(new TextWatcher(){
          public void afterTextChanged(Editable s){
            updateDisplay();
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
          public void onTextChanged(CharSequence s, int start, int before, int count){}
      });    
  }
  
  private void updateDisplay(){
      if (mConfirmPassword.getText().toString().equals(mNewPassword.getText().toString())){
        mNewPassword.setTextColor(Color.rgb(0, 100, 0));
        mConfirmPassword.setTextColor(Color.rgb(0, 100, 0));
        if (mPasswordManager.isPassword(mCurrentPassword.getText().toString())){
          mButtonYes.setEnabled(true);      
        }
        else{
          mButtonYes.setEnabled(false);
        }
      }
      else{
        mNewPassword.setTextColor(Color.rgb(150, 0, 0));
        mConfirmPassword.setTextColor(Color.rgb(150, 0, 0));
        mButtonYes.setEnabled(false);
      }
  }

}




Java Source Code List

org.hbabcock.trivialpassword.AccountEditorFragment.java
org.hbabcock.trivialpassword.AccountFragment.java
org.hbabcock.trivialpassword.AccountListActivity.java
org.hbabcock.trivialpassword.AccountListFragment.java
org.hbabcock.trivialpassword.AccountManager.java
org.hbabcock.trivialpassword.AccountPagerActivity.java
org.hbabcock.trivialpassword.Account.java
org.hbabcock.trivialpassword.PasswordActivity.java
org.hbabcock.trivialpassword.PasswordChangeFragment.java
org.hbabcock.trivialpassword.PasswordFragment.java
org.hbabcock.trivialpassword.PasswordManager.java
org.hbabcock.trivialpassword.SingleFragmentActivity.java
org.hbabcock.trivialpassword.TrivialPasswordJSONSerializer.java