com.app.jdy.ui.RegirsterActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.app.jdy.ui.RegirsterActivity.java

Source

/**
 * Copyright (c) 2015
 *
 * Licensed under the UCG License, Version 1.0 (the "License");
 */
package com.app.jdy.ui;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.jdy.R;
import com.app.jdy.context.MyApplication;
import com.app.jdy.utils.CommonUtils;
import com.app.jdy.utils.Constants;
import com.app.jdy.utils.HttpUtils;
import com.app.jdy.utils.StringUtils;
import com.app.jdy.utils.URLs;
import com.app.jdy.utils.WxCustUtil;
import com.app.jdy.widget.WaitingDialog;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.PropertyValuesHolder;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.SendAuth;
import com.tencent.mm.sdk.openapi.SendAuth.Resp;
import com.umeng.analytics.MobclickAgent;

/**
 * description :
 * 
 * @version 1.0
 * @author zhoufeng
 * @createtime : 2015-1-12 ?10:39:44
 * 
 *             ?:    --------------- -------------------
 *             ----------------------------------- zhoufeng 2015-1-12 ?10:39:44
 * 
 */
public class RegirsterActivity extends Activity implements OnClickListener {
    /**
     * 
     */
    private ImageView mBackImg;
    /**
     * 
     */
    private TextView title;
    /**
     * ??
     */
    private EditText new_username;
    /**
     * ?
     */
    private EditText new_password;
    /**
     * ?textview
     */
    // private TextView new_password_message;

    /**
     * ??
     */
    private EditText regirster_code;
    /**
     * ??textview
     */
    // private TextView regirster_code_message;
    /**
     * ???
     */
    private Button get_regirster_code;
    /**
     * ??
     */
    private Button sumbit_regirster;
    /**
     * TextView
     */
    // private TextView back_login;
    /**
     * 60
     */
    private int recLen = 60;

    /**
     * ?
     */
    private final int PROGRESS_DIALOG = 0x112;
    /**
     * ???
     */
    private final int GETSUCC = 4;
    /**
     * ???
     */
    private final int GETFALL = 0x110;
    /**
     * json?
     */
    private String dataJson;
    /**
     * ???ArrayList???
     */
    private ArrayList<NameValuePair> params;
    /**
     * ?????ArrayList???
     */
    private ArrayList<NameValuePair> phone;
    private String jdycode;
    private String ID;// ID?
    private MyApplication data;// ??
    public String username;
    public String password;
    // private String repassword;
    public String checkCode;
    private int animatorStart = 800;// 
    private String WX_OPENID; // openId
    private String WX_UNIONID;// UNIONID
    private Activity mContext; // 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regirster);
        data = (MyApplication) getApplication();
        // ?
        init();

        // ?
        initAnimation();

        mBackImg.setOnClickListener(this);

        // ????
        get_regirster_code.setOnClickListener(this);

        // ?
        sumbit_regirster.setOnClickListener(this);

        // ??
        // back_login.setOnClickListener(this);

        // ?????
        new_username.addTextChangedListener(watcher);

        // get_regirster_code.setVisibility(View.INVISIBLE);
        get_regirster_code.setClickable(false);
        get_regirster_code.setBackgroundResource(R.drawable.regirster_code_false);
        get_regirster_code.setTextColor(getResources().getColor(R.color.text_hint));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SendAuth.Resp resp = (SendAuth.Resp) data.getSendResp();
        if (resp != null && data.getWxFlag()) {
            switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                switch (resp.getType()) {
                // ?
                case ConstantsAPI.COMMAND_SENDAUTH:
                    reqWXAuthRegister(resp);
                    break;
                // 
                case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
                    break;
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                // Toast.makeText(this, R.string.errcode_register_cancel,
                // Toast.LENGTH_LONG).show();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                // Toast.makeText(this, R.string.errcode_register_cancel,
                // Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
        }

    }

    /**
     * ???
     * 
     * @param resp
     */
    private void reqWXAuthRegister(Resp resp) {
        String code = resp.token;
        ReqWXAuthAysnTask task = new ReqWXAuthAysnTask();
        task.execute(code);
    }

    /**
     * code,?access_token??unionId
     * 
     */
    private class ReqWXAuthAysnTask extends AsyncTask<String, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            String code = params[0];
            String ac_url = URLs.WX_ACCESS_TOKEN.replace("CODE", code);
            String jsonString = HttpUtils.request(null, ac_url);
            String access_token = null;
            String openId = null;
            try {
                JSONObject jsonObject = new JSONObject(jsonString);
                access_token = jsonObject.optString("access_token");
                openId = jsonObject.optString("openid");
                WX_OPENID = openId;

                String getuser_url = URLs.WX_GETUSERINFO.replace("ACCESS_TOKEN", access_token).replace("OPENID",
                        openId);
                String jsonString2 = HttpUtils.request(null, getuser_url);
                JSONObject jsonObject2 = new JSONObject(jsonString2);
                WX_UNIONID = jsonObject2.optString("unionid");
                if (StringUtils.isNotEmpty(WX_UNIONID)) {
                    return true;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                // ?,unionid
                WaitingDialog.showDialog(RegirsterActivity.this).setText(",?");
                ReqRegisterTask reqRegisterTask = new ReqRegisterTask();
                reqRegisterTask.execute();
            } else {
                // ?
                Toast.makeText(getApplicationContext(), "?,?,",
                        Toast.LENGTH_LONG).show();
            }
        }
    }

    private class ReqRegisterTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... paramArgs) {
            String toastText = "";
            params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("phone", username));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("verifyCode", checkCode));
            params.add(new BasicNameValuePair("wcSDKOpenId", WX_OPENID));
            params.add(new BasicNameValuePair("unionId", WX_UNIONID));

            dataJson = null;
            dataJson = HttpUtils.request(params, URLs.REGIRSTER_URL);
            try {
                JSONObject dataObj = new JSONObject(dataJson);
                Boolean regResult = dataObj.optBoolean("success");
                // ?
                if (regResult) {
                    toastText = "?,?,?";

                    params.clear();
                    params.add(new BasicNameValuePair("userName", username));
                    params.add(new BasicNameValuePair("password", password));
                    dataJson = null;
                    dataJson = HttpUtils.login(getApplicationContext(), params, URLs.LOGIN_URL, null);
                    JSONObject jsonObject = new JSONObject(dataJson);
                    Boolean loginResult = jsonObject.getBoolean("success");
                    if (loginResult) {
                        jdycode = jsonObject.optJSONObject("attributes").optString("jdycode");
                        String ID = jsonObject.optJSONObject("attributes").optString("ID");
                        saveUserInfo(jdycode, ID);
                        rollLogin();
                        mContext.setResult(2);
                        Intent intent = new Intent("com.app.jdy.sender.ResetMainPage_BroadCast");
                        mContext.sendBroadcast(intent);
                        mContext.finish();
                    } else {
                        toastText = "";
                    }
                } else {
                    toastText = "";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return toastText;
        }

        @Override
        protected void onPostExecute(String result) {
            WaitingDialog.dismissDialog();
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
    }

    private class ToastMessageTask extends AsyncTask<String, String, String> {
        String toastMessage;

        @Override
        protected String doInBackground(String... params) {
            toastMessage = params[0];
            return toastMessage;
        }

        // GUIcontext
        protected void onPostExecute(String result) {
            Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    /**
     * 
     * @author zhoufeng
     * @createtime 2015-1-13 ?6:24:12
     * @Decription ?
     * 
     */
    boolean pwdShow = false;
    ImageView pwdEye;

    public void init() {
        mContext = this;
        mBackImg = (ImageView) findViewById(R.id.back_img);
        mBackImg.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title_tv);
        title.setText("");
        new_username = (EditText) findViewById(R.id.new_username);
        new_password = (EditText) findViewById(R.id.new_password);
        regirster_code = (EditText) findViewById(R.id.regirster_code);

        pwdEye = (ImageView) findViewById(R.id.regist_eye);
        pwdEye.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (pwdShow) {
                    new_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdEye.setImageResource(R.drawable.register_eye_false);
                } else {
                    new_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdEye.setImageResource(R.drawable.register_eye);
                }
                pwdShow = !pwdShow;
            }
        });

        // new_password_message = (TextView)
        // findViewById(R.id.new_password_message);
        // new_repassword_message = (TextView)
        // findViewById(R.id.new_repassword_message);
        // regirster_code_message = (TextView)
        // findViewById(R.id.regirster_code_message);

        // back_login = (TextView) findViewById(R.id.back_login);
        get_regirster_code = (Button) findViewById(R.id.get_regirster_code);
        get_regirster_code.setBackgroundResource(R.drawable.regirster_code_false);
        get_regirster_code.setTextColor(getResources().getColor(R.color.text_hint));

        sumbit_regirster = (Button) findViewById(R.id.sumbit_regirster);

    }

    /**
     * 
     * @author zhoufeng
     * @createtime 2015-1-13 ?6:29:17
     * @Decription ?
     * 
     */
    public void initAnimation() {
        // ?
        PropertyValuesHolder pvh = PropertyValuesHolder.ofFloat("translationY", 3000f, 0f);
        ObjectAnimator.ofPropertyValuesHolder(get_regirster_code, pvh).setDuration(animatorStart).start();
        ObjectAnimator.ofPropertyValuesHolder(new_username, pvh).setDuration(animatorStart).start();
        // ?
        ObjectAnimator.ofFloat(regirster_code, "translationY", 3000f, 0f).setDuration(animatorStart + 200).start();
        ObjectAnimator.ofFloat(new_password, "translationY", 3000f, 0f).setDuration(animatorStart + 400).start();
        ObjectAnimator.ofFloat(pwdEye, "translationY", 3000f, 0f).setDuration(animatorStart + 400).start();

        Animation translateAnimation7 = new TranslateAnimation(0, 0, 3000f, 0f);
        translateAnimation7.setDuration(animatorStart + 600);// ?
        // ?
        ObjectAnimator.ofFloat(sumbit_regirster, "translationY", 3000f, 0f).setDuration(animatorStart + 800)
                .start();
    }

    /**
     * ???11????
     */
    TextWatcher watcher = new TextWatcher() {
        private CharSequence temp;

        @Override
        public void onTextChanged(CharSequence c, int arg1, int arg2, int arg3) {
            temp = c;
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            if (temp.length() == 11) {
                /**
                 * ??????
                 */
                get_regirster_code.setClickable(true);
                get_regirster_code.setBackgroundResource(R.drawable.regirster_code);
                get_regirster_code.setTextColor(getResources().getColor(R.color.title));
            } else {
                get_regirster_code.setClickable(false);
                get_regirster_code.setBackgroundResource(R.drawable.regirster_code_false);
                get_regirster_code.setTextColor(getResources().getColor(R.color.text_hint));
            }

        }
    };

    /**
     * 
     * ?: onClick|??:?
     * 
     * @param v
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.back_img:
            finish();
            break;
        case R.id.get_regirster_code:
            getCheckCode();
            break;
        case R.id.sumbit_regirster:
            clickRegirster();
            break;
        case R.id.back_login:
            startActivity(new Intent(RegirsterActivity.this, LoginActivity.class));
            break;
        default:
            break;
        }
    }

    /**
     * handler
     */
    private Handler handler = new Handler() { // handle
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject(dataJson);
                    if (jsonObject.getString("success").equals("true")) {
                        Message msg1 = new Message();
                        msg1.what = 4;
                        handler.sendMessage(msg1);
                    } else {
                        Toast.makeText(RegirsterActivity.this, "???", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                break;
            case 2:
                // pd.dismiss();
                Toast.makeText(RegirsterActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_LONG).show();
                break;
            case 3:
                recLen--;
                if (recLen == 0) {
                    get_regirster_code.setText("???");
                    get_regirster_code.setClickable(true);
                    get_regirster_code.setBackgroundResource(R.drawable.regirster_code);
                    get_regirster_code.setTextColor(getResources().getColor(R.color.title));
                    recLen = 60;
                } else {
                    get_regirster_code.setBackgroundResource(R.drawable.regirster_code_false);
                    get_regirster_code.setTextColor(getResources().getColor(R.color.text_hint));
                    get_regirster_code.setText(recLen + "???");
                }
                break;
            case 4:
                Toast.makeText(RegirsterActivity.this, "????", Toast.LENGTH_LONG).show();
                /**
                 * ??60
                 */
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        for (int i = 0; i < 60; i++) {
                            try {
                                Thread.sleep(1000); // sleep 1000ms
                                Message message = new Message();
                                message.what = 3;
                                handler.sendMessage(message);
                            } catch (Exception e) {
                            }
                        }
                    }
                });
                thread.start();
                break;
            }
        };
    };

    /**
     * 
     * @author zhoufeng
     * @createtime 2015-1-13 ?1:35:16
     * @Decription ???
     * 
     */
    public void getCheckCode() {

        get_regirster_code.setClickable(false);
        get_regirster_code.setBackgroundResource(R.drawable.regirster_code_false);
        get_regirster_code.setTextColor(getResources().getColor(R.color.text_hint));

        String username = new_username.getText().toString().trim();

        phone = new ArrayList<NameValuePair>();
        phone.add(new BasicNameValuePair("phone", username));
        Log.i("aaaa", "phone===" + username);

        /**
         * ????
         */
        Thread code_thread = new Thread(new Runnable() {

            @Override
            public void run() {
                dataJson = HttpUtils.request(phone, URLs.CHECH_PHONE);
                Message msg = new Message();
                if (HttpUtils.isNetworkConnected(mContext)) {
                    msg.what = 1;
                } else {
                    msg.what = 2;
                }
                handler.sendMessage(msg);
            }
        });
        code_thread.start();
    }

    /**
     * 
     * @author zhoufeng
     * @createtime 2015-1-13 ?11:56:16
     * @Decription ?????
     * 
     */
    public void getCode() {
        // pd.dismiss();//ProgressDialog?
        // ?uidsessionid??
        try {
            JSONObject jsonObject = new JSONObject(dataJson);

            if (jsonObject.getString("success").equals("true")) {
                /**
                 * ??
                 */
                // Thread thread = new Thread(new Runnable() {
                //
                // @Override
                // public void run() {
                // dataJson = HttpUtils.request(phone,
                // URLs.GET_REGIRSTER_CODE_URL);
                // Log.i("???json", "datajson===" + dataJson);
                // Message msg = new Message();
                // if (dataJson.length() != 0 && !dataJson.equals("0x110")) {
                // msg.what = GETSUCC;
                // } else {
                // msg.what = GETFALL;
                // }
                // handler.sendMessage(msg);
                // }
                // });
                // thread.start();
            } else {
                Toast.makeText(RegirsterActivity.this, "???", Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @author zhoufeng
     * @createtime 2015-1-13 ?1:37:57
     * @Decription ??
     * 
     */
    public void clickRegirster() {
        username = new_username.getText().toString().trim();
        password = new_password.getText().toString().trim();
        checkCode = regirster_code.getText().toString().trim();

        // TODO ?1???
        if (username.equals("") || password.equals("") || checkCode.equals("")) {
            Toast.makeText(RegirsterActivity.this, "??", Toast.LENGTH_SHORT).show();
        } else if (username.length() != 11) {
            Toast.makeText(RegirsterActivity.this, "???", Toast.LENGTH_SHORT).show();
            // } else if (!password.equals(repassword)) {
            // Toast.makeText(RegirsterActivity.this, "????",
            // Toast.LENGTH_SHORT).show();
        } else if (password.length() < 6) {
            Toast.makeText(RegirsterActivity.this, "??6?", Toast.LENGTH_SHORT).show();
        } else {
            // ??
            WxCustUtil.SendAuth(this);
        }

    }

    /**
     * 
     * @author Owater
     * @createtime 2015-1-14 ?1:08:51
     * @Decription ?Android??SSIONID
     * 
     */
    public void rollLogin() {
        final ArrayList<NameValuePair> tmpParams = new ArrayList<NameValuePair>();
        tmpParams.add(new BasicNameValuePair("jdycode", jdycode));
        new Thread(new Runnable() {

            @Override
            public void run() {
                int i = 0;
                while (true) {
                    i++;
                    try {
                        HttpUtils.login(getApplicationContext(), tmpParams, URLs.LOGIN_URL, jdycode);
                        Log.i("Thread", "  == " + i);
                        Thread.sleep(Constants.JSESSIONID_TIME);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    /**
     * 
     * @author Owater
     * @createtime 2015-1-14 ?12:56:54
     * @Decription ??
     * 
     */
    public void saveUserInfo(String jdycode, String ID) {
        SharedPreferences userPreferences;
        userPreferences = getSharedPreferences("umeng_general_config", Context.MODE_PRIVATE);
        Editor editor = userPreferences.edit();
        editor.putString("jdycode", jdycode);
        editor.putString("ID", ID);
        editor.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}