350 lines
12 KiB
Java
350 lines
12 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.text.Editable;
|
|
import android.text.TextUtils;
|
|
import android.text.TextWatcher;
|
|
import android.view.KeyEvent;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.DialogUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.GetLoginDataUtils;
|
|
import com.gh.common.util.LoginUtils;
|
|
import com.gh.common.util.PatternUtils;
|
|
import com.gh.gamecenter.entity.UserInfoEntity;
|
|
import com.gh.gamecenter.eventbus.EBReuse;
|
|
import com.gh.gamecenter.personal.PersonalFragment;
|
|
import com.lightgame.utils.Util_System_Keyboard;
|
|
import com.lightgame.utils.Utils;
|
|
import com.tencent.connect.common.Constants;
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
/**
|
|
* Created by khy on 19/06/17.
|
|
*/
|
|
|
|
public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnLoginDataListener,
|
|
LoginUtils.onCaptchaCallBackListener, LoginUtils.onLoginCallBackListener {
|
|
|
|
|
|
@BindView(R.id.login_phone_et)
|
|
EditText mLoginPhoneEt;
|
|
@BindView(R.id.login_pass_et)
|
|
EditText mLoginPassEt;
|
|
@BindView(R.id.login_captcha)
|
|
TextView mLoginCaptcha;
|
|
@BindView(R.id.login_phone_btn)
|
|
TextView mLoginBtn;
|
|
@BindView(R.id.login_qq_btn)
|
|
LinearLayout mLoginQqBtn;
|
|
@BindView(R.id.login_wechat_btn)
|
|
LinearLayout mLoginWechatBtn;
|
|
@BindView(R.id.login_weibo_btn)
|
|
LinearLayout mLoginWeiboBtn;
|
|
@BindView(R.id.login_bottom)
|
|
LinearLayout mLoginBottom;
|
|
@BindView(R.id.login_bottom_olduser)
|
|
LinearLayout mLoginBottomOlduser;
|
|
|
|
ImageView mOldUserIcon;
|
|
|
|
private Dialog mLoginDialog;
|
|
|
|
private String mServiceId;
|
|
private boolean mIsOldUserPage;
|
|
|
|
Handler mHandler = new Handler() {
|
|
@Override
|
|
public void handleMessage(Message msg) {
|
|
super.handleMessage(msg);
|
|
if (msg.what == 0) { // 验证码倒计时
|
|
int arg1 = msg.arg1;
|
|
arg1--;
|
|
if (arg1 >= 0) {
|
|
Message message = new Message();
|
|
message.what = 0;
|
|
message.arg1 = arg1;
|
|
mHandler.sendMessageDelayed(message, 1000);
|
|
mLoginCaptcha.setText(String.valueOf(arg1));
|
|
} else {
|
|
mLoginCaptcha.setText("重新获取");
|
|
mLoginCaptcha.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.title));
|
|
mLoginCaptcha.setBackgroundResource(R.drawable.border_black_bg);
|
|
mLoginCaptcha.setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
if (requestCode == Constants.REQUEST_LOGIN) { // QQ Login callback
|
|
GetLoginDataUtils.getInstance(this).onQQCallback(requestCode, resultCode, data);
|
|
} else if (requestCode == 32973) {
|
|
GetLoginDataUtils.getInstance(this).onWeiboCallback(requestCode, resultCode, data);
|
|
}
|
|
}
|
|
|
|
@NonNull
|
|
public static Intent getIntent(Context context, boolean isOldUser) {
|
|
Intent intent = new Intent(context, LoginActivity.class);
|
|
intent.putExtra("isOldUser", isOldUser);
|
|
return intent;
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_login;
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
initTitle("登录光环助手");
|
|
|
|
// 添加老用户入口
|
|
mOldUserIcon = new ImageView(this);
|
|
mOldUserIcon.setImageResource(R.drawable.login_olduser_icon);
|
|
mOldUserIcon.setPadding(DisplayUtils.dip2px(this, 13), DisplayUtils.dip2px(this, 11),
|
|
DisplayUtils.dip2px(this, 11), DisplayUtils.dip2px(this, 13));
|
|
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
|
|
DisplayUtils.dip2px(this, 48), ViewGroup.LayoutParams.MATCH_PARENT);
|
|
params.addRule(RelativeLayout.CENTER_VERTICAL);
|
|
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
|
RelativeLayout reuseActionbar = (RelativeLayout) mContentView.findViewById(R.id.reuse_actionbar);
|
|
reuseActionbar.addView(mOldUserIcon, params);
|
|
|
|
mLoginPassEt.addTextChangedListener(new LoginTextWatcher(mLoginPassEt));
|
|
mLoginPhoneEt.addTextChangedListener(new LoginTextWatcher(mLoginPhoneEt));
|
|
|
|
mOldUserIcon.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
changeUserLogin(true);
|
|
}
|
|
});
|
|
|
|
findViewById(R.id.actionbar_rl_back).setOnClickListener(
|
|
new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (mIsOldUserPage) {
|
|
changeUserLogin(false);
|
|
} else {
|
|
finish();
|
|
}
|
|
}
|
|
});
|
|
|
|
String syncDeviceID = PreferenceManager.getDefaultSharedPreferences(this).getString("syncDeviceID", null);
|
|
if (TextUtils.isEmpty(syncDeviceID)) {
|
|
mOldUserIcon.setVisibility(View.GONE);
|
|
}
|
|
|
|
if (getIntent() != null) {
|
|
boolean isOldUser = getIntent().getBooleanExtra("isOldUser", false);
|
|
if (isOldUser) {
|
|
changeUserLogin(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
@OnClick({R.id.login_captcha, R.id.login_phone_btn, R.id.login_qq_btn,
|
|
R.id.login_wechat_btn, R.id.login_weibo_btn})
|
|
public void onViewClicked(View view) {
|
|
switch (view.getId()) {
|
|
case R.id.login_captcha:
|
|
final String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
|
if (PatternUtils.isPhoneNum(phoneNum) && phoneNum.length() == 11) {
|
|
DialogUtils.checkPhoneNumDialog(this, phoneNum, new DialogUtils.ConfirmListener() {
|
|
@Override
|
|
public void onConfirm() {
|
|
if (mIsOldUserPage) {
|
|
LoginUtils.checkPhoneNum(LoginActivity.this, phoneNum, LoginActivity.this);
|
|
} else {
|
|
LoginUtils.getPhoneCaptcha(LoginActivity.this, phoneNum, LoginActivity.this);
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
toast("请输入正确的手机号");
|
|
}
|
|
break;
|
|
case R.id.login_phone_btn:
|
|
loginByMobile();
|
|
break;
|
|
case R.id.login_qq_btn:
|
|
GetLoginDataUtils.getInstance(this).QQLogin(this, this);
|
|
break;
|
|
case R.id.login_weibo_btn:
|
|
GetLoginDataUtils.getInstance(this).WeiBoLogin(this, this);
|
|
break;
|
|
case R.id.login_wechat_btn:
|
|
GetLoginDataUtils.getInstance(this).WCLogin(this);
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
mHandler.removeCallbacksAndMessages(null);
|
|
}
|
|
|
|
// 登录成功返回
|
|
@Override
|
|
public void OnLoginData(JSONObject content, LoginUtils.LoginTag loginTag) {
|
|
|
|
LoginUtils.login(this, content, loginTag, this);
|
|
}
|
|
|
|
// 登录
|
|
private void loginByMobile() {
|
|
String code = mLoginPassEt.getText().toString().trim();
|
|
String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
|
|
|
if (TextUtils.isEmpty(phoneNum)) {
|
|
toast("手机号码不能为空");
|
|
return;
|
|
} else if (TextUtils.isEmpty(code)) {
|
|
toast("验证码不能为空");
|
|
return;
|
|
} else if (TextUtils.isEmpty(mServiceId)) {
|
|
toast("验证码错误,请重新获取");
|
|
return;
|
|
}
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("mobile", phoneNum);
|
|
params.put("code", code);
|
|
params.put("service_id", mServiceId);
|
|
|
|
LoginUtils.LoginTag phone;
|
|
|
|
if (mIsOldUserPage) {
|
|
phone = LoginUtils.LoginTag.oldUserPhone;
|
|
mLoginDialog = DialogUtils.showWaitDialog(this, "登录中...");
|
|
} else {
|
|
phone = LoginUtils.LoginTag.phone;
|
|
}
|
|
LoginUtils.login(LoginActivity.this, new JSONObject(params), phone, LoginActivity.this);
|
|
}
|
|
|
|
private void changeUserLogin(boolean isOldUser) {
|
|
mIsOldUserPage = isOldUser;
|
|
if (isOldUser) {
|
|
mLoginBottom.setVisibility(View.GONE);
|
|
mLoginBottomOlduser.setVisibility(View.VISIBLE);
|
|
mOldUserIcon.setVisibility(View.GONE);
|
|
initTitle("老用户登录");
|
|
} else {
|
|
mLoginBottom.setVisibility(View.VISIBLE);
|
|
mLoginBottomOlduser.setVisibility(View.GONE);
|
|
mOldUserIcon.setVisibility(View.VISIBLE);
|
|
initTitle("登录光环助手");
|
|
}
|
|
Util_System_Keyboard.hideSoftKeyboard(this);
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
if (keyCode == KeyEvent.KEYCODE_BACK && mIsOldUserPage) {
|
|
changeUserLogin(false);
|
|
return true;
|
|
}
|
|
return super.onKeyDown(keyCode, event);
|
|
}
|
|
|
|
@Override
|
|
public void onCaptcha(String serviceId) {
|
|
if (!TextUtils.isEmpty(serviceId)) {
|
|
mServiceId = serviceId;
|
|
|
|
// 启动验证码倒计时
|
|
Message message = new Message();
|
|
message.what = 0;
|
|
message.arg1 = 60; // 倒数时间
|
|
mHandler.sendMessage(message);
|
|
mLoginCaptcha.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.hint));
|
|
mLoginCaptcha.setBackgroundResource(R.drawable.border_captcha_bg);
|
|
mLoginCaptcha.setEnabled(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onLogin(UserInfoEntity entity, LoginUtils.LoginTag loginTag) { // 登录成功
|
|
Utils.log("======登录成功" + entity.getIcon() + "==" + entity.getName());
|
|
if (mIsOldUserPage && mLoginDialog != null) {
|
|
mLoginDialog.dismiss();
|
|
}
|
|
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGIN_TAG));
|
|
finish();
|
|
}
|
|
|
|
private class LoginTextWatcher implements TextWatcher {
|
|
private EditText mEditText;
|
|
|
|
public LoginTextWatcher(EditText e) {
|
|
mEditText = e;
|
|
}
|
|
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
Drawable drawable;
|
|
int tvCount = s.length();
|
|
if (mEditText == mLoginPassEt) {
|
|
if (tvCount > 0) {
|
|
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_password_focused);
|
|
} else {
|
|
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_password_unfocused);
|
|
}
|
|
} else {
|
|
if (tvCount > 0) {
|
|
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_phone_focused);
|
|
} else {
|
|
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_phone_unfocused);
|
|
}
|
|
}
|
|
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
|
|
mEditText.setCompoundDrawables(drawable, null, null, null);
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
}
|
|
}
|