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

Java tutorial

Introduction

Here is the source code for com.app.jdy.ui.LoginActivity.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 java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
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.activity.MainActivity;
import com.app.jdy.context.MyApplication;
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.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.SendAuth;
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 LoginActivity extends Activity implements OnClickListener {
    /**
     * 
     */
    private ImageView mBackImg;
    /**
     * 
     */
    private TextView title;
    /**
     * ??editText
     */
    private EditText username;
    /**
     * ??textview
     */
    // private TextView username_message;
    /**
     * ?editText
     */
    private EditText password;
    /**
     * ?textview
     */
    // private TextView password_message;
    /**
     * ?
     */
    private Button sumbitLogin;
    // private ImageView logoImage;
    private TextView find_password;
    private TextView regirster;
    private TextView detail_product;
    private final int PROGRESS_DIALOG = 0x112;
    private String dataJson;// json?
    private List<NameValuePair> params = new ArrayList<NameValuePair>();
    private String jdycode;
    private String ID;// ID?
    private MyApplication myApplication;

    private View loginOther, loginWeixin;
    private MyApplication data;
    private String wxUnionId = null;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_login);
        data = (MyApplication) getApplication();
        initView();
        intiAnimation();
        intiClick();

        mBackImg.setOnClickListener(this);
        find_password.setOnClickListener(this);
    }

    /**
     * 
     * @author Owater
     * @createtime 2015-1-13 ?6:04:53
     * @Decription ??
     * 
     */
    public void initView() {
        mBackImg = (ImageView) findViewById(R.id.back_img);
        mBackImg.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title_tv);
        title.setText("");
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);

        // username_message = (TextView) findViewById(R.id.username_message);
        // password_message = (TextView) findViewById(R.id.password_message);
        find_password = (TextView) findViewById(R.id.find_password);

        sumbitLogin = (Button) findViewById(R.id.sumbitLogin);
        loginOther = findViewById(R.id.login_other);
        loginWeixin = findViewById(R.id.login_weixin);
        // logoImage = (ImageView) findViewById(R.id.image_login_logo);
        find_password = (TextView) findViewById(R.id.find_password);
        regirster = (TextView) findViewById(R.id.regirster);
    }

    /**
     * 
     * @author Owater
     * @createtime 2015-1-13 ?6:05:11
     * @Decription ??
     * 
     */
    public void intiAnimation() {
        // 
        // Animation translateAnimation = AnimationUtils.loadAnimation(this,
        // R.anim.translate_username);
        Animation translateAnimation1 = new TranslateAnimation(0, 0, 3000, 0);
        translateAnimation1.setDuration(1200);// ?
        // username_message.startAnimation(translateAnimation1);

        Animation translateAnimation2 = new TranslateAnimation(0, 0, 3000, 0);
        translateAnimation2.setDuration(1400);// ?
        // password_message.startAnimation(translateAnimation2);

        Animation translateAnimation3 = new TranslateAnimation(0, 0, 3000, 0);
        translateAnimation3.setDuration(1600);// ?
        sumbitLogin.startAnimation(translateAnimation3);
        loginOther.startAnimation(translateAnimation3);
        loginWeixin.startAnimation(translateAnimation3);

        Animation translateAnimation4 = new TranslateAnimation(0, 0, 3000, 0);
        translateAnimation4.setDuration(1800);// ?
        find_password.startAnimation(translateAnimation4);
        regirster.startAnimation(translateAnimation4);

        // ??
        // Animation animation =AnimationUtils.loadAnimation(this,
        // R.anim.animation_image_logo);
        Animation animation = new AlphaAnimation(0F, 1.0F);
        animation.setDuration(5000);
        // logoImage.startAnimation(animation);
        animation.startNow();

        /*
         * Animation scaleAnimation = new ScaleAnimation(1.0f, 1.0f, 4.0f, 1.0f,
         * Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f);
         * scaleAnimation.setDuration(2000);
         * logoImage.startAnimation(scaleAnimation); scaleAnimation.startNow();
         */
    }

    /**
     * 
     * @author Owater
     * @createtime 2015-1-13 ?6:07:23
     * @Decription ?
     * 
     */
    public void intiClick() {
        loginWeixin.setOnClickListener(this);
        regirster.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startActivityForResult(new Intent(LoginActivity.this, RegirsterActivity.class), 2);
            }
        });

        sumbitLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // ?????
                String un = username.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                if (un == null || un.length() == 0) {
                    Toast.makeText(LoginActivity.this, "???", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pwd == null || pwd.length() == 0) {
                    Toast.makeText(LoginActivity.this, "??", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (un.length() != 11) {
                    Toast.makeText(LoginActivity.this, "????", Toast.LENGTH_SHORT).show();
                    return;
                }

                WaitingDialog.showDialog(LoginActivity.this);
                LoginAysnTask task = new LoginAysnTask();
                task.execute();
            }
        });
    }

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

        @Override
        protected String doInBackground(Void... p) {
            params.add(new BasicNameValuePair("userName", username.getText().toString()));
            params.add(new BasicNameValuePair("password", password.getText().toString()));
            params.add(new BasicNameValuePair("wxUnionId", wxUnionId));
            String json = null;
            json = HttpUtils.login(getApplicationContext(), params, URLs.LOGIN_URL, null);
            return json;
        }

        @Override
        protected void onPostExecute(String json) {
            if (StringUtils.isNotEmpty(json)) {
                JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject(json);
                    boolean success = jsonObject.optBoolean("success");
                    if (success) {
                        JSONObject attributesObject = jsonObject.optJSONObject("attributes");
                        jdycode = attributesObject.optString("jdycode");
                        ID = attributesObject.getString("ID");
                        saveUserInfo(jdycode, ID);
                        rollLogin();

                        Toast.makeText(mContext, "?", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();
                        intent.setAction("org.jdy.action.finshActivityReceiver");
                        sendBroadcast(intent);
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "?,?", Toast.LENGTH_LONG)
                                .show();
                    }
                    WaitingDialog.dismissDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * 
     * @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
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.back_img:
            finish();
            break;
        case R.id.find_password:
            startActivity(new Intent(LoginActivity.this, CallBackPasswordActivity.class));
            break;
        case R.id.login_weixin:
            WxCustUtil.SendAuth(this);
            break;
        default:
            break;
        }
    }

    @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:
                    String token = resp.token;
                    ReqWXAuthAysnTask task = new ReqWXAuthAysnTask();
                    task.execute(token);
                    break;
                // 
                case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
                    break;
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Toast.makeText(this, R.string.errcode_cancel, Toast.LENGTH_LONG).show();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Toast.makeText(this, R.string.errcode_deny, Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
        }
    }

    /**
     * code,?access_token??unionId
     * 
     */
    private class ReqWXAuthAysnTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String 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");

                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);
                wxUnionId = jsonObject2.optString("unionid");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return wxUnionId;
        }

        @Override
        protected void onPostExecute(String result) {
            if (StringUtils.isNotEmpty(result)) {
                // ?,unionid
                WaitingDialog.showDialog(LoginActivity.this);
                LoginAysnTask task = new LoginAysnTask();
                task.execute();
            } else {
                // ?
                Toast.makeText(getApplicationContext(), "?", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (resultCode == 2) {
                finish();
            }
        }
    }

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

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