Newer
Older
dxcgt / app / src / main / java / com / smartdot / cgt / activity / ModPwdActivity.java
wangxitong on 6 Apr 2021 7 KB first commit
package com.smartdot.cgt.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smartdot.cgt.R;
import com.smartdot.cgt.request.ApiClent;
import com.smartdot.cgt.request.Request;
import com.smartdot.cgt.util.ApplicationMain;
import com.smartdot.cgt.util.BaseThread;
import com.smartdot.cgt.util.HandlerStatus;
import com.smartdot.cgt.util.Msg;
import com.smartdot.cgt.util.RsaUtils;
import com.smartdot.cgt.view.FootBar;
import com.smartdot.cgt.view.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.PublicKey;



/**
 * Created by W530 on 2019/9/25.
 */

public class ModPwdActivity extends BaseActivity{

    private TitleBar titlebar;
    private BaseThread thread;
    private FootBar footbar;

    @Override
    protected void setLayout()
    {
        initActivity();
         setContentView(R.layout.updatepwd);
         titlebar = (TitleBar) this.findViewById(R.id.titlebar);
         titlebar.setTitleText("修改密码");
        footbar = (FootBar) this.findViewById(R.id.footbar);
        footbar.setChoose(4);
    }

    @Override
    protected void addEventListener() {
        findViewById(R.id.submit_bt).setOnClickListener(btnOnClick);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
    }
    private View.OnClickListener btnOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.submit_bt:
                    try {
                        ModPwd();
                    } catch (Exception e) {
                        toastMessage("修改失败!");
                    }
                    break;
            }
        }
    };
    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字
        boolean isLetter = false;//定义一个boolean值,用来表示是否包含字母
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            } else if (Character.isLetter(str.charAt(i))) {  //用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }
        String regex = "^[a-zA-Z0-9]{6,12}$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;
    }

    public void ModPwd(){
        EditText editText = (EditText) findViewById(R.id.oldPwd);
        final String OldPwd = editText.getText().toString().trim();
        editText = (EditText) findViewById(R.id.newPwd);
        final String NewPwd1 = editText.getText().toString().trim();
        editText = (EditText) findViewById(R.id.newPwdConfirm);
        final String NewPwd2 = editText.getText().toString().trim();
        if(OldPwd.length() == 0){
            toastMessage("请输入旧密码");
            return;
        } else if(NewPwd1.length() == 0){
            toastMessage("请输入新密码");
            return;
        } else if(NewPwd2.length() == 0){
            toastMessage("请再次输入新密码");
            return;
        } else if(!NewPwd1.equals(NewPwd2)){
            toastMessage("新密码和确认密码输入不一致,请重新输入");
            return ;
        }else if(!isLetterDigit(NewPwd1)){
            toastMessage("新密码请输入6-12位的数字字母组合");
            return ;
        } else {
            thread = new BaseThread(baseHandler)
            {
                @Override
                public void runThread()
                {
                    final Message msg = thread.obtainMessage();
                    if (msg != null)
                    {
                        msg.what = HandlerStatus.REQUEST_LIST;
                        String publicStr = ApplicationMain.getInstance().getCgtConfig().getpublicKey();
                        PublicKey publicKey = RsaUtils.keyStrToPublicKey(publicStr);
                        String oldpassword = RsaUtils.encryptDataByPublicKey(OldPwd.getBytes(), publicKey);
                        String newpassword = RsaUtils.encryptDataByPublicKey(NewPwd1.getBytes(), publicKey);
                        ApiClent.changePwd(oldpassword, newpassword, new ApiClent.ClientCallback() {
                            @Override
                            public void onSuccess(Object data)
                            {
                                try {
                                    JSONObject jsonObject = new JSONObject(data.toString());
                                    msg.obj = jsonObject;
                                    msg.arg1 = HandlerStatus.HANDLE_OK;
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                    Log.i("list_error", e.getMessage());
                                    msg.arg1 = HandlerStatus.HANDLE_FAIL;
                                }
                            }
                            @Override
                            public void onFailure(String message) {
                                msg.arg1 = HandlerStatus.HANDLE_FAIL;
                            }
                            @Override
                            public void onError(Exception e) {
                                msg.arg1 = HandlerStatus.HANDLE_ERROR;
                            }
                        });
                        this.sendMessage(msg);
                    }
                }
            };
            thread.start();
        }
    }

    public void toastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void handleMessage(Message msg)
    {
        if(msg.what ==HandlerStatus.REQUEST_LIST){
            if (msg.arg1 == HandlerStatus.HANDLE_OK) {
                Toast.makeText(ModPwdActivity.this, "修改密码成功!", Toast.LENGTH_SHORT).show();
                ModPwdActivity.this.finish();
            }
            else if (msg.arg1 == HandlerStatus.HANDLE_FAIL)
            {
                Msg.showInfo(ModPwdActivity.this, "修改密码失败!");
            }
            else
            {
                Msg.showInfo(ModPwdActivity.this, "修改密码时出现错误!");
            }
            checkIsNeedCloseProgress();
        }
    }
    private boolean checkIsNeedCloseProgress()
    {
        boolean canCloseProgress = true;
        if (canCloseProgress) {
            try {
                closeProgress();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return canCloseProgress;
    }

    @Override
    protected void onCancelProgress(DialogInterface arg0) {

    }

}