Please be patient to read this module because of its large content and length.
My module is divided into four parts
- [ ] My interface
- [ ] Set up interface
- [x] Modify password interface
- [x] Set Secret Security and Retrieve Password
1. Modify Password
1. Create a password modification interface
In the com.boxuegu.activity package, create a java class named ModifyPswActivity.Create a layout file named activity_modify_psw under the res/layout folder.
2. Modify password interface code--activity_modify_psw.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/register_bg"> <include layout="@layout/main_title_bar"/> <EditText android:id="@+id/et_original_psw" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="35dp" android:background="@drawable/register_user_name_bg" android:drawableLeft="@drawable/psw_icon" android:drawablePadding="10dp" android:gravity="center_vertical" android:hint="Please enter the original password" android:inputType="textPassword" android:paddingLeft="8dp" android:textColor="#000000" android:textColorHint="#a3a3a3" android:textSize="14sp"/> <EditText android:id="@+id/et_new_psw" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:background="@drawable/register_psw_bg" android:drawableLeft="@drawable/psw_icon" android:drawablePadding="10dp" android:hint="Please enter a new password" android:inputType="textPassword" android:paddingLeft="8dp" android:singleLine="true" android:textColor="#000000" android:textColorHint="#a3a3a3" android:textSize="14sp"/> <EditText android:id="@+id/et_new_psw_again" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:background="@drawable/register_psw_again_bg" android:drawableLeft="@drawable/psw_icon" android:drawablePadding="10dp" android:hint="Please enter the new password again" android:inputType="textPassword" android:paddingLeft="8dp" android:singleLine="true" android:textColor="#000000" android:textColorHint="#a3a3a3" android:textSize="14sp"/> <Button android:id="@+id/btn_save" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="15dp" android:background="@drawable/register_selector" android:text="Preservation" android:textColor="@android:color/white" android:textSize="18sp"/> </LinearLayout>
3. Modify password interface logic code--ModifyPswActivity.java
package com.boxuegu.activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import com.boxuegu.R; import com.boxuegu.utils.AnalysisUtils; import com.boxuegu.utils.MD5Utils; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ModifyPswActivity extends AppCompatActivity { private TextView tv_main_title; private TextView tv_back; private EditText et_original_psw, et_new_psw, et_new_psw_again; private Button btn_save; private String originalPsw, newPsw, newPswAgain; private String userName; @Override protected void onCreate(Bundle savedIntanceState){ super.onCreate(savedIntanceState); //Set up interface layout setContentView(R.layout.activity_modify_psw); //Set interface to vertical screen setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); init(); userName = AnalysisUtils.readLoginUserName(this); } //Get interface controls and handle click events for related controls private void init(){ tv_main_title = (TextView) findViewById(R.id.tv_main_title); tv_main_title.setText("Change Password"); tv_back = (TextView) findViewById(R.id.tv_back); et_original_psw = (EditText) findViewById(R.id.et_original_psw); et_new_psw = (EditText) findViewById(R.id.et_new_psw); et_new_psw_again = (EditText) findViewById(R.id.et_new_psw_again); btn_save = (Button) findViewById(R.id.btn_save); tv_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ModifyPswActivity.this.finish(); } }); //Save button click events btn_save.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { getEditString(); if (TextUtils.isEmpty(originalPsw)) { Toast.makeText(ModifyPswActivity.this, "Please enter the original password", Toast.LENGTH_SHORT).show(); return; }else if (!MD5Utils.md5(originalPsw).equals(readPsw())) { Toast.makeText(ModifyPswActivity.this, "The password entered does not match the original password", Toast.LENGTH_SHORT).show(); return; }else if (MD5Utils.md5(newPsw).equals(readPsw())) { Toast.makeText(ModifyPswActivity.this, "The new password you entered does not match the original password", Toast.LENGTH_SHORT).show(); return; }else if (TextUtils.isEmpty(newPsw)) { Toast.makeText(ModifyPswActivity.this, "Please enter a new password", Toast.LENGTH_SHORT).show(); return; }else if (TextUtils.isEmpty(newPswAgain)) { Toast.makeText(ModifyPswActivity.this, "Please enter the new password again", Toast.LENGTH_SHORT).show(); return; }else if (!newPsw.equals(newPswAgain)) { Toast.makeText(ModifyPswActivity.this, "The new password entered twice is inconsistent", Toast.LENGTH_SHORT).show(); return; } else { Toast.makeText(ModifyPswActivity.this,"New password set successfully", Toast.LENGTH_SHORT).show(); modifyPsw(newPsw); Intent intent = new Intent(ModifyPswActivity.this, LoginActivity.class); startActivity(intent); SettingActivity.instance.finish(); //Close Setup Interface ModifyPswActivity.this.finish(); //Close this interface } } }); } //Gets the string on the control private void getEditString(){ originalPsw = et_original_psw.getText().toString().trim(); newPsw = et_new_psw.getText().toString().trim(); newPswAgain=et_new_psw_again.getText().toString().trim(); } //Modify the password saved in SharedPreferences upon successful login private void modifyPsw(String newPsw){ String md5Psw = MD5Utils.md5(newPsw); SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); //Get Editor editor.putString(userName,md5Psw); //Save New Password editor.commit(); //Submit modifications } //Read the original password from SharedPreferences private String readPsw(){ SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE); String spPsw = sp.getString(userName, ""); return spPsw; } }
4. Modify Setup Interface Code
Find the init method in the SettingActivity.java file and add the following code below the comment//jump to the interface where the password is changed
Intent intent = new Intent(SettingActivity.this,ModifyPswActivity.class); startActivity(intent);
2. Setting up secret protection and retrieving passwords
1. Create a password-setting and password-retrieving interface
In the com.boxuegu.activity package, create a java class named FindPswActivity.Create a layout file named activity_find_psw under the res/layout folder.Import the desired picture find_psw_icon.png into the drawable folder.
2. Create an interface code for setting secret and retrieving passwords--activity_find_psw.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login_bg"> <include layout="@layout/main_title_bar"/> <TextView android:id="@+id/tv_user_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="35dp" android:text="Your username is?" android:textColor="@android:color/white" android:textSize="18sp" android:visibility="gone"/> <EditText android:id="@+id/et_user_name" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="10dp" android:background="@drawable/find_psw_icon" android:hint="Please enter your user name" android:paddingLeft="8dp" android:singleLine="true" android:textColor="#000000" android:textColorHint="#a3a3a3" android:visibility="gone"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="15dp" android:text="Your name is?" android:textColor="@android:color/white" android:textSize="18sp"/> <EditText android:id="@+id/et_validate_name" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="10dp" android:background="@drawable/find_psw_icon" android:hint="Please enter a name to verify" android:paddingLeft="8dp" android:singleLine="true" android:textColor="#000000" android:textColorHint="#a3a3a3"/> <TextView android:id="@+id/tv_reset_psw" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="10dp" android:gravity="center_vertical" android:textColor="@android:color/white" android:textSize="15sp" android:visibility="gone"/> <Button android:id="@+id/btn_validate" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="35dp" android:layout_marginRight="35dp" android:layout_marginTop="15dp" android:background="@drawable/register_selector" android:text="Verification" android:textColor="@android:color/white" android:textSize="18sp"/> </LinearLayout>
3. Set Secret Protection and Retrieve Password Interface Logic Code
package com.boxuegu.activity; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.boxuegu.R; import com.boxuegu.utils.AnalysisUtils; import com.boxuegu.utils.MD5Utils; public class FindPswActivity extends AppCompatActivity { //Jump from confidentiality settings when form is security, otherwise jump from login private String from; private TextView tv_main_title; private TextView tv_back; private Button btn_validate; private EditText et_validate_name; private TextView tv_reset_psw; private EditText et_user_name; private TextView tv_user_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set up interface layout setContentView(R.layout.activity_find_psw); //Set interface to vertical screen setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); from = getIntent().getStringExtra("from"); init(); } //Get interface controls and handle click events for corresponding controls private void init() { tv_main_title = (TextView) findViewById(R.id.tv_main_title); tv_back = (TextView) findViewById(R.id.tv_back); et_validate_name = (EditText) findViewById(R.id.et_validate_name); btn_validate = (Button) findViewById(R.id.btn_validate); tv_reset_psw = (TextView) findViewById(R.id.tv_reset_psw); et_user_name = (EditText) findViewById(R.id.et_user_name); tv_user_name = (TextView) findViewById(R.id.tv_user_name); if ("security".equals(from)){ tv_main_title.setText("Set Secret Security"); }else{ tv_main_title.setText("Retrieve password"); tv_user_name.setVisibility(View.VISIBLE); et_user_name.setVisibility(View.VISIBLE); } tv_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FindPswActivity.this.finish(); } }); btn_validate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String validateName = et_validate_name.getText().toString().trim(); if ("security".equals(from)){ //Set Secret Security if (TextUtils.isEmpty(validateName)){ Toast.makeText(FindPswActivity.this,"Please enter a name to verify", Toast.LENGTH_SHORT).show(); return; }else{ Toast.makeText(FindPswActivity.this,"Secret security setup succeeded", Toast.LENGTH_SHORT).show(); //Save Secret to SharedPreferences saveSecurity(validateName); FindPswActivity.this.finish(); } }else{ //Retrieve password String userName = et_user_name.getText().toString().trim(); String sp_security = readSecurity(userName); if (TextUtils.isEmpty(userName)){ Toast.makeText(FindPswActivity.this,"Please enter your user name", Toast.LENGTH_SHORT).show(); return; }else if (!isExistUserName(userName)){ Toast.makeText(FindPswActivity.this,"The username you entered does not exist", Toast.LENGTH_SHORT).show(); return; }else if (TextUtils.isEmpty(validateName)){ Toast.makeText(FindPswActivity.this,"Please enter a name to verify", Toast.LENGTH_SHORT).show(); return; }if (!validateName.equals(sp_security)){ Toast.makeText(FindPswActivity.this,"Incorrect secret protection entered", Toast.LENGTH_SHORT).show(); return; }else{ //The password entered is correct and the user is given a new password: 123456 tv_reset_psw.setVisibility(View.VISIBLE); tv_reset_psw.setText("Initial password: 123456"); savePsw(userName); } } } }); } //Save Initialized Password private void savePsw(String userName){ String md5Psw = MD5Utils.md5("123456"); //Password MD5 Encryption SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); //Get Editor editor.putString(userName,md5Psw); editor.commit(); //Submit modifications } private void saveSecurity(String validateName){ SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); //Get Editor //Secret security deposited in the corresponding account editor.putString(AnalysisUtils.readLoginUserName(this)+"_security",validateName); editor.commit(); //Submit modifications } //Reading Secret Keys from SharedPreferences private String readSecurity(String userName){ SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); String security = sp.getString(userName+"_security",""); return security; } //Use the user name entered by the user in SharedPreferences to determine if the user is there private boolean isExistUserName(String userName){ boolean hasUserName = false; SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); String spPsw = sp.getString(userName,""); if (!TextUtils.isEmpty(spPsw)){ hasUserName=true; } return hasUserName; } }
4. Modify Code
(1) Modify the login interface
Find the init method in the LoginActivity.java folder.Add the following code under Comment//Jump to the Retrieve Password interface
Intent intent = new Intent(LoginActivity.this, FindPswActivity.class); startActivity(intent);
(2) Modify the setup interface
Find the init method in the SettingActivity.java folder.Add the following code under Comment//Jump to Set Secret Interface
Intent intent = new Intent(SettingActivity.this,FindPswActivity.class); intent.putExtra("from","security"); startActivity(intent);