ElectronicCardValidationActivity.java 16.4 KB
package com.hjx.personalcenter.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjx.personalcenter.R;
import com.hjx.personalcenter.db.Content;
import com.hjx.personalcenter.db.SaveParam;
import com.hjx.personalcenter.http.HttpCode;
import com.hjx.personalcenter.http.HttpManager;
import com.hjx.personalcenter.util.AlertUtils;
import com.hjx.personalcenter.util.GetDevicesUtil;
import com.hjx.personalcenter.util.PhoneNumCheckUtils;

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

import java.util.TimerTask;

/**
 * Created by h on 2017/8/9.
 */

public class ElectronicCardValidationActivity extends FragmentActivity implements View.OnClickListener {
    private Button btn_card_valiyanzhen, get_authcode;
    private EditText phonenumber, anth_6num, activation_code;//;//密码
    private ImageButton phone_delete_btn, code_delete_btn, activation_code_delete_btn;
    private TextView menu_title;
    private ImageView cancel;
    private int delayTime = 60;
    private LinearLayout rl_authcode, activation_code_layout;
    private java.util.Timer Delaytimer;
    private String sourceStr = "android";
    private String typeStr = "general";
    String authcode = "";
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            JSONObject jsonObject;
            String status;
            switch (msg.what) {
                case HttpCode.AUTHCODE_SUCESS:
                    jsonObject = (JSONObject) msg.obj;
                    status = jsonObject.optString("status");
                    authcode = jsonObject.optString("jsessionid");
                    if (status.equals("100")) {
                        AlertUtils.showToast(ElectronicCardValidationActivity.this, "验证码发送成功");
                    }

                    break;
                case HttpCode.AUTHCODE_FAIL:
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "验证码发送失败");
                    break;
                case 21:
                    if (delayTime <= 0) {
                        if (Delaytimer != null) {
                            Delaytimer.cancel();
                        }
                        get_authcode.setEnabled(true);
                        get_authcode.setClickable(true);
                        get_authcode.setText("获取验证码");
                        delayTime = 60;
                    } else {
                        get_authcode.setEnabled(false);
                        get_authcode.setClickable(false);
                        get_authcode.setText(delayTime + " s");
                    }
                    break;
                case HttpCode.ACTIVATION_SUCCESS:
                    jsonObject = (JSONObject) msg.obj;
                    status = jsonObject.optString("status");
                    if(status.equals("1")){
                        AlertUtils.showToast(ElectronicCardValidationActivity.this, "激活成功");
                        String phone = phonenumber.getText().toString().trim();
                        Intent intent = new Intent();
                        intent.putExtra(SaveParam.CARDPHONE, phone);
                        intent.setClass(ElectronicCardValidationActivity.this, ElectronicCardEditInfoActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.rightin, R.anim.rightout);
                    }else if(status.equals("5001")){
                        AlertUtils.showToast(ElectronicCardValidationActivity.this, "激活码无效");
                    }else if(status.equals("0")){
                        AlertUtils.showToast(ElectronicCardValidationActivity.this, "无法链接到服务器,请检查您的网络或稍后重试");
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electroniccard_validation);
        SysApplication.getInstance().addActivity(this);
        initView();
        initData();
        initLister();
        initTextWatch();
    }

    private void initView() {
        btn_card_valiyanzhen = (Button) findViewById(R.id.btn_card_valiyanzhen);
        get_authcode = (Button) findViewById(R.id.btn_authcode);
        anth_6num = (EditText) findViewById(R.id.et_authcode);
        phonenumber = (EditText) findViewById(R.id.et_phonenumber);
        activation_code = (EditText) findViewById(R.id.activation_code);
        menu_title = (TextView) findViewById(R.id.menu_title);
        cancel = (ImageView) findViewById(R.id.cancel);
        code_delete_btn = (ImageButton) findViewById(R.id.code_delete_btn);
        phone_delete_btn = (ImageButton) findViewById(R.id.phone_delete_btn);
        activation_code_delete_btn = (ImageButton) findViewById(R.id.activation_code_delete_btn);
        rl_authcode = (LinearLayout) findViewById(R.id.rl_authcode);
        activation_code_layout = (LinearLayout) findViewById(R.id.activation_code_layout);
    }

    private void initData() {
        phonenumber.setText(SaveParam.getInstance().getLoginParam(this, "username"));
        if (Content.authcodeflag == 1) {
            menu_title.setText("更换绑定");
            btn_card_valiyanzhen.setText("确定");
            phonenumber.setText("");
            rl_authcode.setVisibility(View.VISIBLE);
        } else if (Content.authcodeflag == 2) {
            menu_title.setText("修改保卡信息");
            btn_card_valiyanzhen.setText("确定");
            rl_authcode.setVisibility(View.VISIBLE);
            anth_6num.requestFocus();
        } else {
            if (Build.MODEL.toUpperCase().equals("T5 PLUS")) {//T5 PLUS需要输入注册激活码
                menu_title.setText("激活机器并确认绑定保卡的手机号");
                activation_code_layout.setVisibility(View.VISIBLE);
            } else {
                menu_title.setText("确认绑定保卡的手机号");
                activation_code_layout.setVisibility(View.GONE);
            }
            btn_card_valiyanzhen.setText("下一步");
            anth_6num.requestFocus();
        }


    }

    private void initLister() {
        get_authcode.setOnClickListener(this);
        btn_card_valiyanzhen.setOnClickListener(this);
        cancel.setOnClickListener(this);
        phone_delete_btn.setOnClickListener(this);
        code_delete_btn.setOnClickListener(this);
        activation_code_delete_btn.setOnClickListener(this);

    }

    /**
     * 输入框改变的监听
     */
    private void initTextWatch() {
        phonenumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() == 0) {
                    phone_delete_btn.setVisibility(View.INVISIBLE);
                } else {
                    phone_delete_btn.setVisibility(View.VISIBLE);
                }
                if (Build.MODEL.toUpperCase().equals("T5 PLUS")) {
                    if (!menu_title.getText().toString().equals("激活机器并确认绑定保卡的手机号") || s.toString().length() == 0 || !s.toString().equals(SaveParam.getInstance().getLoginParam(ElectronicCardValidationActivity.this, "username"))) {
                        rl_authcode.setVisibility(View.VISIBLE);
                    } else {
                        rl_authcode.setVisibility(View.GONE);
                    }
                } else {
                    if (!menu_title.getText().toString().equals("确认绑定保卡的手机号") || s.toString().length() == 0 || !s.toString().equals(SaveParam.getInstance().getLoginParam(ElectronicCardValidationActivity.this, "username"))) {
                        rl_authcode.setVisibility(View.VISIBLE);
                    } else {
                        rl_authcode.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        phonenumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!phonenumber.getText().toString().isEmpty()) {
                        phone_delete_btn.setVisibility(View.VISIBLE);
                    }
                } else {

                    phone_delete_btn.setVisibility(View.INVISIBLE);
                }
            }
        });
        anth_6num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() == 0) {

                    code_delete_btn.setVisibility(View.INVISIBLE);
                } else {
                    code_delete_btn.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        anth_6num.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!anth_6num.getText().toString().isEmpty()) {
                        code_delete_btn.setVisibility(View.VISIBLE);
                    }
                } else {
                    code_delete_btn.setVisibility(View.INVISIBLE);
                }
            }
        });
        activation_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() == 0) {

                    activation_code_delete_btn.setVisibility(View.INVISIBLE);
                } else {
                    activation_code_delete_btn.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        activation_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!activation_code.getText().toString().isEmpty()) {
                        activation_code_delete_btn.setVisibility(View.VISIBLE);
                    }
                } else {
                    activation_code_delete_btn.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_card_valiyanzhen:
                String userID22 = GetDevicesUtil.getDevicesInfo(this).getDeviceNumber();
                String usernameStr = phonenumber.getText().toString().trim();
                String authcodeStr = anth_6num.getText().toString().trim();
                String activationStr = activation_code.getText().toString().trim();
                if (TextUtils.isEmpty(usernameStr)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "手机号不能为空");
                    return;
                } else if (activation_code_layout.getVisibility() == View.VISIBLE && TextUtils.isEmpty(activationStr)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "注册激活码不能为空");
                    return;
                }else if (rl_authcode.getVisibility() == View.VISIBLE && TextUtils.isEmpty(authcodeStr)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "验证码不能为空");
                    return;
                } else if (!PhoneNumCheckUtils.isPhone(usernameStr)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "手机号码输入错误");

                } else if (!authcode.equals(authcodeStr)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "您输入的验证码不正确");
                } else {
                    String userID = SaveParam.getInstance().getLoginParam(this, "userId");
                    Long auserID = null;
                    try {
                        auserID = Long.parseLong(userID);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                    if (Content.authcodeflag == 1) {
                        //修改账户手机号
                        HttpManager.getInstance().changeaccountphone(this,
                                auserID, usernameStr, authcodeStr);

                    } else if (Content.authcodeflag == 2) {
                        //修改保卡手机号

                        HttpManager.getInstance().changecardinfophone(this,
                                userID22, usernameStr, authcodeStr);
                    } else {
                        if(Build.MODEL.toUpperCase().equals("T5 PLUS")){
                            HttpManager.getInstance().submitEcard(this,userID22,activationStr,handler);
                        } else{
                            String phone = phonenumber.getText().toString().trim();
//                        SaveParam.getInstance().saveCustomizeParam(this, SaveParam.CARDPHONE, phone);
                            Intent intent = new Intent();
                            intent.putExtra(SaveParam.CARDPHONE, phone);
                            intent.setClass(ElectronicCardValidationActivity.this, ElectronicCardEditInfoActivity.class);
                            startActivity(intent);
                            overridePendingTransition(R.anim.rightin, R.anim.rightout);
                        }
                    }
//                    SaveParam.getInstance().saveLoginParam(this,SaveParam.CARDPHONE,  phonenumber.getText().toString().trim());
//                    Intent intent = new Intent();
//                    intent.setClass(ElectronicCardValidationActivity.this, ElectronicCardEditInfoActivity.class);
//                    startActivity(intent);
//                    overridePendingTransition(R.anim.rightin, R.anim.rightout);
                }
                break;
            case R.id.btn_authcode:
                String usernameStr1 = phonenumber.getText().toString().trim();
                if (!PhoneNumCheckUtils.isPhone(usernameStr1)) {
                    AlertUtils.showToast(ElectronicCardValidationActivity.this, "请输入正确的手机号");
                } else {
                    anth_6num.requestFocus();
                    HttpManager.getInstance().authCode(typeStr, usernameStr1, handler, ElectronicCardValidationActivity.this);
                    valiDelay();

                }

                break;
            case R.id.cancel:
                finish();
                break;
            case R.id.phone_delete_btn:
                phonenumber.setText("");
                break;
            case R.id.code_delete_btn:
                anth_6num.setText("");
                break;
            case R.id.activation_code_delete_btn:
                activation_code.setText("");
                break;
        }
    }

    // 线程类 定时器
    public void valiDelay() {

        Delaytimer = new java.util.Timer(true);


        TimerTask task = new TimerTask() {
            public void run() {
                //每次需要执行的代码放到这里面。
                delayTime--;
                handler.sendEmptyMessage(21);
            }
        };
        Delaytimer.schedule(task, 1000, 1000);
    }
}