package com.rachel.settingpwddemo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.text.method.NumberKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
String fileName = "syspass.txt";
public boolean fileIsExists(){
try{
File f = new File(fileName);
if(f.exists()==true){
return false;
}else{
return true;
}
}catch(Exception e){
return false;
}
}
String syspassword = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputTitleDialog();
}
private void inputTitleDialog() {//获取初始密码,判断是否正确
if(!fileIsExists()==false){
FileInputStream is = null;
ByteArrayOutputStream os2 = null;
try{
is = openFileInput(fileName);
os2 = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1){
os2.write(buffer,0,len);
}
}catch(IOException e){
Log.i("SettingsActivity","error");
}finally{
try{
if(os2!=null){
os2.close();
}
if(is!=null){
is.close();
}
}catch(IOException e){
Log.i("SettingsActivity","error");
}
}
if(os2!=null){
byte[] content_byte = os2.toByteArray();
syspassword = new String(content_byte);
}
}else{
syspassword=null;
}
final String syspass = syspassword;
if(syspass==null){
showSetDialog();
}else{
final EditText passEdit = new EditText(this);
numericOnlyListener(passEdit);
AlertDialog builder = new AlertDialog.Builder(this)
.setTitle("键入密码")
.setIcon(R.drawable.ic_launcher)
.setView(passEdit)
.setOnKeyListener(new OnKeyListener(){
public boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
MainActivity.this.finish();
return false;
}
return false;
}
})
.setNegativeButton(getString(R.string.preference_change_password_title),//change the password
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String inputPassWord = passEdit.getText().toString();
if(!(inputPassWord.equals(syspass)) || inputPassWord==null){
Toast.makeText(MainActivity.this,"旧密码错误,请重试",Toast.LENGTH_SHORT).show();
// SettingsActivity.this.finish();
passEdit.setText("");
try{
//The following three sentences control the closing of the frame
//这三句代码是设置dialog是否关闭
Field field=dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog,false);//true:Close the dialog box false:The dialog box is not closed
}catch(Exception e){
e.printStackTrace();
}
}else{
showSetDialog();
}
}
})
.setPositiveButton(getString(R.string.lockpassword_ok_label),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String inputPassWord = passEdit.getText().toString();
if(!(inputPassWord.equals(syspass)) ){
Toast.makeText(MainActivity.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_SHORT).show();
MainActivity.this.finish();
}
}
})
.show();
builder.setCanceledOnTouchOutside(false);
}
}
public void showSetDialog() { //设置初始密码
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.editTextPass);
final EditText editTextrePass = (EditText)textEntryView.findViewById(R.id.editTextrePass);
numericOnlyListener(editTextrePass);
numericOnlyListener(editTextPass);
AlertDialog ad1 = new AlertDialog.Builder(this)
.setTitle("设置密码:")
.setIcon(R.drawable.ic_launcher)
.setView(textEntryView)
.setOnKeyListener(new OnKeyListener(){
public boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
MainActivity.this.finish();
return false;
}
return false;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
// Prompt two passwords do not matchdestv
if(!(editTextPass.getText().toString().equals(editTextrePass.getText().toString())) || editTextPass.getText().toString().equals("") || editTextrePass.getText().toString().equals("")){//
Toast.makeText(MainActivity.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_LONG).show();
MainActivity.this.finish();
}else{
FileOutputStream os = null;
try{
os = openFileOutput(fileName,Context.MODE_PRIVATE);
os.write((editTextPass.getText().toString()).getBytes());
}catch(IOException e){
Log.i("MainActivity","error");
}finally{
try{
os.close();
}catch(IOException e){
Log.i("MainActivity","error");
}
}
}
}
})
.show();
ad1.setCanceledOnTouchOutside(false);
}
/**
* 设置输入框输入密码的类型
* 这里只能输入小写字母和数字,输入以外的都无效
* 可以根据自己的需要设置
* @param v
*/
public void numericOnlyListener(EditText v){
v.setKeyListener(new NumberKeyListener() {
@Override
public int getInputType() {
// TODO Auto-generated method stub
return android.text.InputType.TYPE_CLASS_TEXT;
}
@Override
protected char[] getAcceptedChars() {
// TODO Auto-generated method stub
char numberChars[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'};
return numberChars;
}
});
}
}