package com.jiuqi.njt.son;
import com.jiuqi.mobile.nigo.comeclose.bean.app.msg.SmsMassBean;
import com.jiuqi.mobile.nigo.comeclose.bean.base.UserBean;
import com.jiuqi.mobile.nigo.comeclose.manager.app.ISmsMassManager;
import com.jiuqi.mobile.nigo.comeclose.manager.base.ILoginManager;
import com.jiuqi.mobile.nigo.comeclose.manager.master.IUserManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.ReturnObject;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.ValidateRule;
import com.jiuqi.njt.util.Validator;
import com.jiuqi.njt.widget.ProgressDialogStyle;
import com.jiuqi.util.UIUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ResetPasswordActivity extends Activity implements OnClickListener {
protected MyApp application;
private static OptsharepreInterface sharePre;
int role = -1;
private EditText userName;
private EditText yanzhengma;
private Button btnyanzhengma;
private EditText newpwd;
private EditText newpwdOk;
private Button submit;
private String account;
private String msg;
private int time;
private TextView alertMessage;
private String telephone;
private View alertLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
application = (MyApp) this.getApplication();
sharePre = new OptsharepreInterface(ResetPasswordActivity.this);
account = sharePre.getPres("account");
}
private void initWidgets() {
setContentView(R.layout.activity_reset_password);
ViewStub rlTitleBar = (ViewStub) findViewById(R.id.titleBarStub);
TitleBarUtil.createTitleBar(this, rlTitleBar, "找回密码",
new OnClickListener() {
@Override
public void onClick(View v) {
ResetPasswordActivity.this.finish();
}
});
userName = (EditText) findViewById(R.id.userName);
yanzhengma = (EditText) findViewById(R.id.yanzhengma);
btnyanzhengma = (Button) findViewById(R.id.getyanzhengma);
newpwd = (EditText) findViewById(R.id.newpwd);
newpwdOk = (EditText) findViewById(R.id.newpwdOk);
submit = (Button) findViewById(R.id.submit);
alertLayout = findViewById(R.id.alertLayout);
alertMessage = (TextView) findViewById(R.id.alertMessage);
}
private void initListeners() {
btnyanzhengma.setOnClickListener(this);
submit.setOnClickListener(this);
}
private void initUI() {
userName.setText(account);
}
private String checkParams() {
String toastMsg = "";
String yzm = yanzhengma.getText().toString();
String password = newpwd.getText().toString();
String passwordOk = newpwdOk.getText().toString();
TextView[] widgets = new TextView[] { yanzhengma, newpwd, newpwdOk };
Validator validator = null;
ReturnObject ro = null;
// 验证这些控件是不是都有内容了
validator = new Validator();
validator.addRules(ValidateRule.IS_NOT_EMPTY,
ValidateRule.IS_REACH_MAX_LENGTH);
for (TextView w : widgets) {
ro = validator.val(w.getText());
if (!ro.isSuccess) {
toastMsg = w.getHint().toString();
w.requestFocus();
return toastMsg;
}
}
// 验证这些控件是不是都是纯数字
validator.clearRules();
validator.addRules(ValidateRule.IS_DIGITS_ONLY);
widgets = new TextView[] { yanzhengma };
for (TextView w : widgets) {
ro = validator.val(w.getText());
if (!ro.isSuccess) {
UIUtil.showMsg(this, ro.getErrorMessage());
toastMsg = ro.getErrorMessage();
w.requestFocus();
return toastMsg;
}
}
// if("".equals(yzm)){
// toastMsg = "验证码不能为空";
// }
// if("".equals(password)||"".equals(passwordOk)){
// toastMsg = "密码不能为空";
// }
if (!password.equals(passwordOk)) {
toastMsg = "两次输入的密码不一致";
}
// if (time < 2) {
// toastMsg = "短信验证码已失效,请重新获取";
// }
// if (!msg.equals(yzm)) {
// toastMsg = "短信验证码错误";
// }
return toastMsg;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.getyanzhengma:
if (!"".equals(userName.getText().toString())) {
btnyanzhengma.setBackgroundResource(R.drawable.btn_bg_small_greay);
UIUtil.setWidgetEnabled(btnyanzhengma, false);
new SendMsgTask().execute();
} else {
UIUtil.showMsg(this, "账号不能为空");
}
break;
case R.id.submit:
String message = checkParams();
if ("".equals(message)) {
new ReSetPasswordTask(yanzhengma.getText().toString())
.execute();
} else {
Toast.makeText(ResetPasswordActivity.this, message,
Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
btnyanzhengma.setText("点击获取短信验证码");
btnyanzhengma.setBackgroundResource(R.drawable.btn_bg_small_orange);
UIUtil.setWidgetEnabled(btnyanzhengma, true);
}
@Override
public void onTick(long millisUntilFinished) {
btnyanzhengma.setText("(" + millisUntilFinished / 1000 + ")秒后重新获取");
time = (int) (millisUntilFinished / 1000);
}
}
class SendMsgTask extends AsyncTask<SmsMassBean, Void, Boolean> {
private String Message;
private Dialog pd;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Message = "";
pd = myshowDialog(ResetPasswordActivity.this, 3);
}
@Override
protected Boolean doInBackground(SmsMassBean... arg0) {
// TODO Auto-generated method stub
ClientContext context = application.getClientContext();
try {
if (null == context) {
context = ClientContext.getClientContext(
Constants.SERVER_URL, Constants.ANONYMOUS,
Constants.ANONYMOUS);
application.setClientContext(context);
}
// msg = getDxyz();
ILoginManager loginManager = context
.getManager(ILoginManager.class);
String account = userName.getText().toString();
UserBean userbean = loginManager.findByAccount(account);
if (null != userbean) {
UserBean admin = loginManager.findByAccount("admin");
telephone = userbean.getMobileNumber();
ISmsMassManager manager = context
.getManager(ISmsMassManager.class);
String mobile[] = { telephone };
// SmsMassBean bean = new SmsMassBean(admin);
// String mobile[] = { telephone };
// bean.setFlag(2);
// bean.setFeeType(1);
// bean.setToMobileNumbers(mobile);
// bean.setSendMsg("验证码为:" + msg + ",来自全国农机化生产信息管理服务平台。");
// String m = telephone.substring(0, 3) + "****"
// + telephone.substring(7, 11);
// manager.send(bean);
manager.sendYZM(mobile);
Message = "验证成功";
return true;
} else {
Message = "输入的账号不存在";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Message = "失败";
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
myremoveDialog(pd);
if (!result) {
UIUtil.setWidgetEnabled(btnyanzhengma, true);
UIUtil.showMsg(ResetPasswordActivity.this, Message);
} else {
yanzhengma.requestFocus();
alertLayout.setVisibility(View.VISIBLE);
alertMessage.setText("短信已经发送到您的手机"+com.jiuqi.njt.util.UIUtil.getPhonenumber(telephone)+",如在120秒\n之内还没收到短信验证码,请重新获取验证码");
MyCount count = new MyCount(120000, 1000);
count.start();
}
}
}
class ReSetPasswordTask extends AsyncTask<UserBean, Void, String> {
private String yzm;
private String message = "";
private Dialog pd;
public ReSetPasswordTask(String yzm) {
super();
this.yzm = yzm;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = myshowDialog(ResetPasswordActivity.this, 4);
}
@Override
protected String doInBackground(UserBean... arg0) {
// TODO Auto-generated method stub
ClientContext context = application.getClientContext();
try {
if (null == context) {
context = ClientContext.getClientContext(
Constants.SERVER_URL, Constants.ANONYMOUS,
Constants.ANONYMOUS);
application.setClientContext(context);
}
// msg = getDxyz();
IUserManager manager = context.getManager(IUserManager.class);
ILoginManager loginManager = context
.getManager(ILoginManager.class);
String account = userName.getText().toString();
UserBean userbean = loginManager.findByAccount(account);
ISmsMassManager smsManager = context
.getManager(ISmsMassManager.class);
message = smsManager
.compareYZM(yzm, userbean.getMobileNumber());
if ("ok".equals(message)) {
if (manager.resetPassWord(userbean, newpwdOk.getText()
.toString())) {
message = "密码重置成功";
} else {
message = "密码重置失败";
}
} else if ("timeOut".equals(message)) {
message = "验证码超时,已经失效,请重新获取";
} else {
message = "验证码输入错误";
}
return message;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
message = "密码重置失败";
}
return message;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
myremoveDialog(pd);
Toast.makeText(ResetPasswordActivity.this, result,
Toast.LENGTH_SHORT).show();
if ("密码重置成功".equals(result)) {
ResetPasswordActivity.this.finish();
}
}
}
private String getDxyz() {
String dxyz = "";
for (int i = 0; i < 6; i++) {
dxyz = dxyz + (int) (Math.random() * 10);
}
// Map<String, Object> session =
// ActionContext.getContext().getSession();
// session.put("dxyz", dxyz);
return dxyz;
}
public static Dialog myshowDialog(Context context, int i) {
Dialog pd = null;
switch (i) {
case 1:
pd = ProgressDialogStyle.createLoadingDialog(context, "正在定位...");
pd.show();
break;
case 2:
pd = ProgressDialogStyle.createLoadingDialog(context, "正在获取天气...");
pd.show();
break;
case 3:
pd = ProgressDialogStyle.createLoadingDialog(context, "正在验证账号...");
pd.show();
break;
case 4:
pd = ProgressDialogStyle.createLoadingDialog(context, "正在提交数据...");
pd.show();
break;
default:
break;
}
return pd;
}
public static void myremoveDialog(Dialog pd) {
if (pd != null) {
pd.dismiss();
}
}
}