package com.bigdo.app;
import org.json.JSONException;
import org.json.JSONObject;
import com.bigdo.common.RConfig;
import com.bigdo.common.RState;
import com.bigdo.util.MD5;
import com.bigdo.util.Pub;
import com.bigdo.util.WSCheck;
import com.bigdo.util.WSHelper;
import com.bigdo.util.WSResult;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
public class UpdatePwd extends BaseActivity {
private Button send;
private Handler waitSMS = null;
private int waitTime = 0;
private String vcode = "";
private Runnable run = null;
private boolean isSending = false, isSubmiting = false;
TextView right_tools = null, title_name = null;
LinearLayout pwd_host;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_pwd_activity);
View com_title = (View) this.findViewById(R.id.bd_title_host);
title_name = (TextView) com_title.findViewById(R.id.bd_title_name);
title_name.setText("找回密码");
right_tools = (TextView) com_title.findViewById(R.id.bd_title_operate);
right_tools.setVisibility(View.VISIBLE);
right_tools.setText("下一步");
right_tools.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPwdHost();
}
});
ImageButton bd_title_back = (ImageButton) com_title
.findViewById(R.id.bd_title_back);
bd_title_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
send = (Button) findViewById(R.id.sendvc);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
pwd_host = (LinearLayout) findViewById(R.id.pwd_host);
}
private void sendSMS() {
if (!isSending && waitTime <= 0) {
EditText p = (EditText) this.findViewById(R.id.telcode);
String phone = p.getText().toString();
if (Pub.isMobilePhone(phone)) {
isSending = true;
vcode = "";
showProgress("发送中..");
send.setText("发送中...");
send.setOnClickListener(null);
String url = RConfig.sendSMS(this);
JSONObject jparams = new JSONObject();
try {
jparams.put("telcode", phone);
jparams.put("type", "changepwd");
} catch (JSONException e) {
messageBox("准备发送短信信息发生错误");
isSubmiting = false;
return;
}
String data = jparams.toString();
WSHelper ws = new WSHelper(this, "", false, WsHandler, url, "",
WSHelper.getStringEntity(data), 1, 1, 0, 0,
WSHelper.POST);
ws.start();
Log.e("sendSMS", url);
Log.e("sendSMS-data", data);
} else {
p.setError("手机号有误");
p.requestFocus();
showInfo("手机号有误");
}
} else {
this.showInfo("已经在发送中...");
}
}
private void waitVC() {
if (vcode != null && !vcode.equals("") && waitTime > 0) {
if (send == null) {
send = (Button) this.findViewById(R.id.sendvc);
}
if (waitTime >= 60) {
waitTime--;
send.setText("获取验证码:(" + waitTime + ")");
}
send.setBackgroundResource(R.drawable.reg_button_bg_disabled);
send.setOnClickListener(null);
if (waitSMS == null) {
waitSMS = new Handler();
}
run = new Runnable() {
@Override
public void run() {
waitTime--;
if (waitTime <= 0) {
clearWait();
} else {
send.setText("获取验证码 (" + waitTime + ")");
waitVC();
}
}
};
waitSMS.postDelayed(run, 1000);
} else {
clearWait();
}
}
private void clearWait() {
vcode = "";
waitTime = 0;
if (send != null && run != null) {
send.removeCallbacks(run);
run = null;
}
send.setText("获取验证码");
send.setBackgroundResource(R.drawable.reg_button_bg);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
}
private void showPwdHost() {
EditText p = (EditText) this.findViewById(R.id.telcode);
String phone = p.getText().toString();
if (Pub.isMobilePhone(phone)) {
if (vcode == null || vcode.equals("")) {
showInfo("请先获取验证码");
return;
}
EditText _vc = (EditText) this.findViewById(R.id.vcode);
String _vcode = _vc.getText().toString();
if (_vcode == null || _vcode.equals("")) {
_vc.setError("请输入短信验证码");
_vc.requestFocus();
showInfo("请输入短信验证码");
return;
}
_vcode = MD5.encode(_vcode + phone);
if (!vcode.equals(_vcode)) {
// _vc.setError("短信验证码不对 或 手机号已更改");
_vc.requestFocus();
showInfo("短信验证码不对 或 手机号已更改");
return;
}
} else {
p.setError("手机号有误");
p.requestFocus();
showInfo("手机号有误");
return;
}
clearWait();
LinearLayout phone_host = (LinearLayout) findViewById(R.id.phone_host);
phone_host.setVisibility(LinearLayout.GONE);
pwd_host.setVisibility(LinearLayout.VISIBLE);
right_tools.setText("提 交");
title_name.setText("重置密码");
right_tools.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changePwd();
}
});
}
private void changePwd() {
if (!isSubmiting) {
isSubmiting = true;
EditText pwd = (EditText) findViewById(R.id.pwd);
EditText config_pwd = (EditText) findViewById(R.id.config_pwd);
String pwd_v = pwd.getText().toString();
String config_pwd_v = config_pwd.getText().toString();
if (pwd_v == null || pwd_v.equals("")) {
pwd.setError("密码 不能为空");
pwd.requestFocus();
showInfo("密码 不能为空");
isSubmiting = false;
return;
}
if (pwd_v.length() < 4) {
pwd.setError("密码长度必需大于 4 位");
pwd.requestFocus();
showInfo("密码长度必需大于 4 位");
isSubmiting = false;
return;
}
if (!pwd_v.equals(config_pwd_v)) {
config_pwd.setError("两次密码不一样");
config_pwd.requestFocus();
showInfo("两次密码不一样");
isSubmiting = false;
return;
}
EditText p = (EditText) this.findViewById(R.id.telcode);
String telcode = p.getText().toString();
if (!Pub.isMobilePhone(telcode)) {
showInfo("手机号有误");
isSubmiting = false;
return;
}
JSONObject jparams = new JSONObject();
try {
jparams.put("newpwd", pwd_v);
jparams.put("user", telcode);
} catch (JSONException e) {
isSubmiting = false;
messageBox("准备注册信息发生错误");
return;
}
String data = jparams.toString();
String url = RConfig.changePwd2(this);
showProgress("重置密码中..");
WSHelper ws = new WSHelper(this, "", false, WsHandler, url, "",
WSHelper.getStringEntity(data), 2, 2, 0, 0, WSHelper.POST);
ws.start();
Log.e("register", url);
Log.e("register-data", data);
} else {
showInfo("重置密码中..");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
EditText p = (EditText) this.findViewById(R.id.telcode);
outState.putString("phone", p.getText().toString());
outState.putString("vcode", vcode);
outState.putInt("waittime", waitTime);
}
@Override
public void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
EditText p = (EditText) this.findViewById(R.id.telcode);
String phone = outState.getString("phone");
p.setText(phone);
vcode = outState.getString("vcode");
waitTime = outState.getInt("waittime");
waitVC();
}
/**
* 事件通知
*/
@SuppressLint("HandlerLeak")
Handler WsHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1 == RState.WSCall) {
WSResult result = (WSResult) msg.getData().getParcelable(
WSResult.ResultKey);
if (result.RequestCode == 1) {
if (send != null) {
send.setText("获取验证码");
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
}
checkSMS(result);
hiddenProgress();
isSending = false;
} else if (result.RequestCode == 2) {
checkRegister(result);
hiddenProgress();
}
}
}
};
private void checkSMS(WSResult result) {
JSONObject jresult = WSCheck.Json(this, result);
if (jresult != null) {
try {
vcode = jresult.getString("vcode");
if (vcode != null && !vcode.equals("")) {
showInfo("验证码发送成功");
waitTime = 60;
waitVC();
return;
}
} catch (Exception e) {
}
messageBox("发送验证码失败.");
}
clearWait();
}
private void checkRegister(WSResult result) {
JSONObject jresult = WSCheck.Json(this, result);
if (jresult != null && pwd_host != null) {
messageBox("重置密码成功!", new OnDismissListener() {
@Override
public void onDismiss(DialogInterface arg0) {
finish();
}
});
}
}
@Override
public void onClearData() {
// TODO Auto-generated method stub
}
@Override
public void onVideoBroadcast(Intent intent) {
// TODO Auto-generated method stub
}
}