Android Open Source - trivial-password Password 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;
//from  w ww . j a  v  a 2 s  .c o m
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PasswordFragment extends Fragment {
  private static final String TAG = "PasswordFragment";

  PasswordManager mPasswordManager = null;
  private View mView = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    
    mPasswordManager = PasswordManager.get(getActivity());
    setHasOptionsMenu(false);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    Log.i(TAG, "onCreateView");

    final boolean enterMode;
    if (mPasswordManager.havePasswordFile()){
      getActivity().setTitle("Enter Password");
      enterMode = true;
    }
    else{
      getActivity().setTitle("New Password");
      enterMode = false;
    }

    mView = inflater.inflate(R.layout.fragment_password, parent, false);

    final EditText pw = (EditText)mView.findViewById(R.id.password_edit);
    final EditText eHint = (EditText)mView.findViewById(R.id.password_hint_edit);
    final TextView tHint = (TextView)mView.findViewById(R.id.password_hint_text);
    final Button b = (Button)mView.findViewById(R.id.ok_button);

    if (enterMode){
      b.setEnabled(false);
      eHint.setVisibility(View.INVISIBLE);
      ((ViewManager)eHint.getParent()).removeView(eHint);
      tHint.setText(mPasswordManager.getHint());
    }
    else{
      tHint.setVisibility(View.INVISIBLE);
      ((ViewManager)tHint.getParent()).removeView(tHint);
    }
    
    b.setOnClickListener(new View.OnClickListener(){
      public void onClick(View vw){
        String hint;
        if (enterMode){
          hint = tHint.getText().toString();
        }
        else{
          hint = eHint.getText().toString();
        }
        mPasswordManager.savePassword(pw.getText().toString(), hint);
        if (NavUtils.getParentActivityName(getActivity()) != null){
          NavUtils.navigateUpFromSameTask(getActivity());
        }
      }
    });

      pw.addTextChangedListener(new TextWatcher(){
          public void afterTextChanged(Editable s){
            if (enterMode){
              if (mPasswordManager.isPassword(s.toString())){
                b.setEnabled(true);
              }
              else{
                b.setEnabled(false);
              }
            }
          }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
          public void onTextChanged(CharSequence s, int start, int before, int count){}
      });

    return mView;
  }
}




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