|
package com.electric.chargingpile.activity;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.blankj.utilcode.util.PhoneUtils;
import com.electric.chargingpile.R;
import com.electric.chargingpile.application.MainApplication;
import com.electric.chargingpile.data.Register;
import com.electric.chargingpile.manager.ProfileManager;
import com.electric.chargingpile.util.ActivityManagerApplication;
import com.electric.chargingpile.util.BarColorUtil;
import com.electric.chargingpile.util.CheckMobileNum;
import com.electric.chargingpile.util.DES3;
import com.electric.chargingpile.util.JsonUtils;
import com.electric.chargingpile.util.NetUtil;
import com.electric.chargingpile.util.OkHttpUtil;
import com.electric.chargingpile.util.ToastUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.umeng.analytics.MobclickAgent;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONObject;
import java.net.URLEncoder;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.Call;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
public class RegisterActivity extends Activity implements OnClickListener {
private static final String TAG = "RegisterActivity";
private ImageView ivBack, ivRight;
private EditText etCode, etPhone;
private TextView tvGetCode, tvSure, tv_xieyi;
LinearLayout ll_tongyi;
ImageView iv_tongyi;
private int time = 90;
private Timer timer = new Timer();
private TimerTask task;
private String imei;
InputMethodManager imm;
// private Login login;
private EditText password, confirmPassword, et_input_shareno;
private Register register;
private boolean getCode = false;
private static String JSPSESSID = null;
private Button username_clear, password_claer;
private ImageView point;
private CheckBox check;
Context context;
String no;
private Bitmap download_bmp;
private Boolean flag = true;
private android.view.animation.Animation animation;
private static final int RC_CAMERA_PERM = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
BarColorUtil.initStatusBarColor(RegisterActivity.this);
initView();
cameraTask();
// getTpye();
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
private void initView() {
iv_tongyi = (ImageView) findViewById(R.id.iv_tongyi);
ll_tongyi = (LinearLayout) findViewById(R.id.ll_tongyi);
ll_tongyi.setOnClickListener(this);
tv_xieyi = (TextView) findViewById(R.id.tv_xieyi);
tv_xieyi.setOnClickListener(this);
animation = AnimationUtils.loadAnimation(RegisterActivity.this, R.anim.nn);
point = (ImageView) findViewById(R.id.tv_one);
check = (CheckBox) findViewById(R.id.check);//取得CheckBox组件
check.setOnClickListener(this);
et_input_shareno = (EditText) findViewById(R.id.et_input_shareno);
ivRight = (ImageView) findViewById(R.id.iv_right);
ivBack = (ImageView) findViewById(R.id.iv_back);
ivBack.setOnClickListener(this);
Picasso.with(this).load("http://img0.imgtn.bdimg.com/it/u=1359105123,3423006123&fm=21&gp=0.jpg").into(ivRight);
tvSure = (TextView) findViewById(R.id.tv_make_sure);
tvSure.setOnClickListener(this);
etCode = (EditText) findViewById(R.id.et_input_code);
etCode.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) {
}
@Override
public void afterTextChanged(Editable s) {
if (password.getText().toString().equals("") || etPhone.getText().toString().equals("") || etCode.getText().toString().equals("")) {
tvSure.setEnabled(false);
tvSure.setBackgroundResource(R.drawable.textview_1green);
}
if (!password.getText().toString().equals("") && !etPhone.getText().toString().equals("") && !etCode.getText().toString().equals("")) {
tvSure.setEnabled(true);
tvSure.setBackgroundResource(R.drawable.textview_greenstyle);
}
}
});
etPhone = (EditText) findViewById(R.id.et_input_phone);
etPhone.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) {
}
@Override
public void afterTextChanged(Editable s) {
if (!etPhone.getText().toString().equals("") && password.getText().toString() != null) {
username_clear.setVisibility(View.VISIBLE);
} else {
username_clear.setVisibility(View.GONE);
}
if (password.getText().toString().equals("") || etPhone.getText().toString().equals("") || etCode.getText().toString().equals("")) {
tvSure.setEnabled(false);
tvSure.setBackgroundResource(R.drawable.textview_1green);
}
if (!password.getText().toString().equals("") && !etPhone.getText().toString().equals("") && !etCode.getText().toString().equals("")) {
tvSure.setEnabled(true);
tvSure.setBackgroundResource(R.drawable.textview_greenstyle);
}
}
});
tvGetCode = (TextView) findViewById(R.id.tv_getcode);
tvGetCode.setOnClickListener(this);
password = (EditText) findViewById(R.id.et_password);
password.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) {
}
@Override
public void afterTextChanged(Editable s) {
if (!password.getText().toString().equals("") && password.getText().toString() != null) {
password_claer.setVisibility(View.VISIBLE);
} else {
password_claer.setVisibility(View.GONE);
}
if (password.getText().toString().equals("") || etPhone.getText().toString().equals("") || etCode.getText().toString().equals("")) {
tvSure.setEnabled(false);
tvSure.setBackgroundResource(R.drawable.textview_1green);
}
if (!password.getText().toString().equals("") && !etPhone.getText().toString().equals("") && !etCode.getText().toString().equals("")) {
tvSure.setEnabled(true);
tvSure.setBackgroundResource(R.drawable.textview_greenstyle);
}
}
});
if (etPhone.getText().toString().equals("") || password.getText().toString().equals("") || etCode.getText().toString().equals("")) {
tvSure.setBackgroundResource(R.drawable.textview_1green);
tvSure.setEnabled(false);
}
username_clear = (Button) findViewById(R.id.username_clear);
username_clear.setOnClickListener(this);
password_claer = (Button) findViewById(R.id.btn_clear);
password_claer.setOnClickListener(this);
}
private boolean checkText() {
String phoneNum = etPhone.getText().toString().trim();
if (TextUtils.isEmpty(phoneNum)) {
// Toast.makeText(this, "请输入您的电话", Toast.LENGTH_SHORT).show();
return false;
}
if (CheckMobileNum.isMobileNum(phoneNum) == false) {
// Toast.makeText(this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private boolean checkTextSubmit() {
String phoneNum = etPhone.getText().toString().trim();
String passwordd = password.getText().toString().trim();
String code = etCode.getText().toString().trim();
if (TextUtils.isEmpty(phoneNum)) {
Toast.makeText(this, "请输入您的电话", Toast.LENGTH_SHORT).show();
return false;
}
if (TextUtils.isEmpty(passwordd)) {
Toast.makeText(this, "请输入您的电话", Toast.LENGTH_SHORT).show();
return false;
}
if (!validatPassword(passwordd)) {
ToastUtil.showToast(getApplicationContext(), "密码格式为字母或数字,请检查您输入的内容", Toast.LENGTH_SHORT);
return false;
}
if (TextUtils.isEmpty(code)) {
Toast.makeText(this, "请输入您的电话", Toast.LENGTH_SHORT).show();
return false;
}
if (CheckMobileNum.isMobileNum(phoneNum) == false) {
Toast.makeText(this, "手机号码错误,您输入的是一个无效手机号码", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private boolean validatPassword(String s) {
String strRegex = "^[A-Za-z0-9]+$";
Pattern pattern = Pattern.compile(strRegex);
Matcher matcher = pattern.matcher(s);
return matcher.find();
}
@Override
public void onClick(View v) {
// if (imm.isActive()) {
// View view = getWindow().peekDecorView();
// if (view != null) {
// imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
// }
// }
switch (v.getId()) {
case R.id.check:
if (check.isChecked()) { //当CheckBox被选中
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setSelection(password.getText().length());
check.setBackgroundResource(R.drawable.icon_bi_password1215);
// password.setTransformationMethod(PasswordTransformationMethod
// .getInstance()); //以密文显示,以.代替
} else {
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
password.setSelection(password.getText().length());
check.setBackgroundResource(R.drawable.icon_password1215);
// password.setTransformationMethod(HideReturnsTransformationMethod
// .getInstance()); //密码以明文显示
}
break;
case R.id.iv_back:
finish();
break;
case R.id.tv_getcode:
if (checkText()) {
MobclickAgent.onEvent(getApplicationContext(), "0902");
tvGetCode.setEnabled(false);
getlogin();
task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (time <= 0) {
tvGetCode.setEnabled(true);
if (getCode) {
tvGetCode.setText("重新获取");
} else {
tvGetCode.setText("获取验证码");
}
task.cancel();
} else {
tvGetCode.setText("" + time);
}
time--;
}
});
}
};
time = 90;
timer.schedule(task, 0, 1000);
} else {
if (etPhone.getText().toString().equals("")) {
Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.tv_make_sure:
// SharedPreferences mySharedPreferences = getSharedPreferences(imei,
// Activity.MODE_PRIVATE);
// no = mySharedPreferences.getString(imei, "0");
// Log.e("@@@",no);
// if (Integer.parseInt(no) < 2){
if (checkTextSubmit()) {
if (!flag) {
// Toast.makeText(this, "请勾选保密协议", Toast.LENGTH_SHORT).show();
tvSure.setEnabled(false);
tvSure.setBackgroundResource(R.drawable.textview_1green);
} else {
MobclickAgent.onEvent(getApplicationContext(), "0901");
tvSure.setEnabled(true);
tvSure.setBackgroundResource(R.drawable.textview_greenstyle);
resetsubmit();
}
}
// }else{
// Toast.makeText(getApplication(),"该设备已注册过5个账号",Toast.LENGTH_SHORT).show();
// }
// String imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
// .getDeviceId();
// Log.d("imei","imei:"+imei);
// SharedPreferences sp = getSharedPreferences(imei,
// Activity.MODE_PRIVATE);
// SharedPreferences.Editor editor =sp.edit();
// editor.putString(imei, String.valueOf(1));
// editor.commit();
break;
case R.id.btn_clear:
password.setText("");
break;
case R.id.username_clear:
etPhone.setText("");
break;
case R.id.tv_xieyi:
Intent intent = new Intent(this, XieYiActivity.class);
startActivity(intent);
// this.startActivity(intent); //启动浏览器
break;
case R.id.ll_tongyi:
if (flag) {
iv_tongyi.setImageResource(R.drawable.icon_login_select);
flag = false;
tvSure.setBackgroundResource(R.drawable.textview_1green);
// tvSure.setTextColor(getResources().getColor(R.color.lvse));
tvSure.setEnabled(false);
} else {
iv_tongyi.setImageResource(R.drawable.icon_login_selected);
flag = true;
if (!etPhone.getText().toString().equals("") && !etCode.getText().toString().equals("")
&& !password.getText().toString().equals("")) {
tvSure.setBackgroundResource(R.drawable.textview_greenstyle);
tvSure.setEnabled(true);
}
}
break;
}
}
private void getlogin() {
if (!NetUtil.CheckNetwork(this)) {
Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
return;
}
final String phoneNum = etPhone.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
try {
long appTime1 = System.currentTimeMillis() / 1000;
Log.i("appTime(long)---", appTime1 + "");
long updatetime = appTime1 - MainMapActivity.cha - 1;
Log.i("updatetime(long)---", updatetime + "");
Log.i("cha---", MainMapActivity.cha + "");
String token = String.valueOf(updatetime);
String replace = URLEncoder.encode(DES3.encode(token));
Log.i("token---", token);
Log.d("token----", token);
String url = null;
url = MainApplication.url + "/zhannew/basic/web/index.php/tpsms/getsms?phone=" + phoneNum + "&token=" + replace;
login(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
// private void getTpye() {
// if (!NetUtil.CheckNetwork(this)) {
// Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
// return;
// }
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// String url="http://www.d1ev.com/public/getSaleCar";
// getType(url);
// }
// }).start();
// }
private void resetsubmit() {
if (!NetUtil.CheckNetwork(this)) {
Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
return;
}
if (hasCameraPermission()) {
String imei = PhoneUtils.getIMEI();
final String url = MainApplication.url + "/zhannew/basic/web/index.php/tpmember/add?phone=" + etPhone.getText().toString().trim() + "&sms=" + etCode.getText().toString().trim() + "&password=" + password.getText().toString().trim() + "&yqm=" + et_input_shareno.getText().toString() + "&imei=" + "android_" + imei + "®istration_id=" + MainApplication.push_id;
new Thread(new Runnable() {
@Override
public void run() {
submit(url);
}
}).start();
} else {
EasyPermissions.requestPermissions(
this, null,
RC_CAMERA_PERM,
Manifest.permission.READ_PHONE_STATE);
}
}
@AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
if (hasCameraPermission()) {
// Have permission, do the thing!
// Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
} else {
// Ask for one permission
// EasyPermissions.requestPermissions(
// this,null,
// RC_CAMERA_PERM,
// Manifest.permission.READ_PHONE_STATE);
EasyPermissions.requestPermissions(
this,
"注册功能需要开启相关权限,是否开启?",
RC_CAMERA_PERM,
Manifest.permission.READ_PHONE_STATE);
}
}
private boolean hasCameraPermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.READ_PHONE_STATE);
}
private Handler handler = new Handler() {
// 主线程通过这个方法处理消息
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
try {
getCode = true;
// JSONArray jsonArray = new JSONArray(msg.obj.toString());
JSONObject jsonObj = new JSONObject(msg.obj.toString());
String rtnCode = jsonObj.getString("rtnCode");
String rtnMsg = jsonObj.opt("rtnMsg").toString();
if (rtnCode.equals("01")) {
// JSPSESSID=jsonObj.getString("sid");
Toast.makeText(RegisterActivity.this, rtnMsg, Toast.LENGTH_SHORT).show();
} else if (rtnCode.equals("03")) {
Toast.makeText(RegisterActivity.this, "获取验证码超时", Toast.LENGTH_SHORT).show();
// if(task!=null){
// task.cancel();
// 15010185513
// tvGetCode.setEnabled(true);
// if (getCode) {
// tvGetCode.setText("重新获取");
// } else {
// tvGetCode.setText("获取验证码");
// }
// }
if (task != null) {
task.cancel();
tvGetCode.setEnabled(true);
if (getCode) {
tvGetCode.setText("重新获取");
} else {
tvGetCode.setText("获取验证码");
}
}
} else if (rtnCode.equals("02")) {
Toast.makeText(RegisterActivity.this, "此手机账号已注册", Toast.LENGTH_SHORT).show();
if (task != null) {
task.cancel();
tvGetCode.setEnabled(true);
if (getCode) {
tvGetCode.setText("重新获取");
} else {
tvGetCode.setText("获取验证码");
}
}
} else {
Toast.makeText(RegisterActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
return;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
JSONObject jsonObj = new JSONObject(msg.obj.toString());
String rtnCode = jsonObj.getString("rtnCode");
String rtnMsg = jsonObj.getString("rtnMsg");
if (rtnCode.equals("01")) {
MobclickAgent.onProfileSignIn(MainApplication.userId);
// ProfileManager.getInstance().setUserid(RegisterActivity.this, rtnMsg);
MobclickAgent.onEvent(getApplicationContext(), "0903");
getLoginData();
// userGrowUp();
} else if (rtnCode.equals("03")) {
Toast.makeText(RegisterActivity.this, "验证码不正确,请重新输入", Toast.LENGTH_SHORT).show();
} else if (rtnCode.equals("02")) {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
} else {
return;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
Toast.makeText(RegisterActivity.this, "服务器处理错误", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
private void getLoginData() {
try {
long appTime1 = System.currentTimeMillis() / 1000;
long updatetime = appTime1 - MainMapActivity.cha - 3;
String token = String.valueOf(updatetime);
final String replace = URLEncoder.encode(DES3.encode(token));
String url = MainApplication.url + "/zhannew/basic/web/index.php/tpmember/login?phone="
+ etPhone.getText().toString() + "&password=" + URLEncoder.encode(password.getText().toString()) + "&token=" + replace
+ "&imei=" + "android_" + MainApplication.imei + "®istration_id=" + MainApplication.push_id;
OkHttpUtils.get().url(url).build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
// Log.e(TAG, "onResponse: "+response );
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if ("01".equals(rtnCode)) {
String data = JsonUtils.getKeyResult(response, "data");
saveUserInfo(data);
MobclickAgent.onProfileSignIn(MainApplication.userId);
CreditActivity.canFresh = true;
MainApplication.ok = "1";
ProfileManager.getInstance().setCount(getApplicationContext(), 0);
MainApplication.count = 0;
new Handler().postDelayed(new Runnable() {
public void run() {
ActivityManagerApplication.destoryActivity("login");
startActivity(new Intent(RegisterActivity.this, SkipUserInfoActivity.class));
finish();
}
}, 1200);
} else {
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveUserInfo(String info) {
// Log.e(TAG, "saveUserInfo: "+info );
String userid = JsonUtils.getKeyResult(info, "userid");
String nickname = JsonUtils.getKeyResult(info, "nickname");
String tel = JsonUtils.getKeyResult(info, "tel");
String userpic = JsonUtils.getKeyResult(info, "userpic");
String chexing = JsonUtils.getKeyResult(info, "chexing");
String userpassword = password.getText().toString();
//保存用户id
MainApplication.userId = userid;
ProfileManager.getInstance().setUserid(getApplicationContext(), userid);
//保存用户昵称
MainApplication.userNickname = nickname;
ProfileManager.getInstance().setNickname(getApplicationContext(), nickname);
//保存用户手机号
MainApplication.userPhone = tel;
ProfileManager.getInstance().setUsername(getApplicationContext(), tel);
//保存用户头像
MainApplication.userIcon = MainApplication.pic_url + userpic;
ProfileManager.getInstance().setUsericon(getApplicationContext(), MainApplication.userIcon);
//保存用户车型
MainApplication.userCar = chexing;
ProfileManager.getInstance().setUsercar(getApplicationContext(), chexing);
//保存用户密码
MainApplication.userPassword = userpassword;
ProfileManager.getInstance().setKeyUserpassword(getApplicationContext(), userpassword);
ProfileManager.getInstance().setPreviousUser(getApplicationContext(), tel);
}
private void submit(String sms) {
Log.e("url", sms);
Request request = new Request.Builder().url(sms).build();
Response response = null;
try {
if (request == null) {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
return;
}
response = OkHttpUtil.execute(request);
if (response.code() == 200) {
String json = "";
try {
json = response.body().string();
Message msg = new Message();
msg.obj = json;
msg.what = 2;
handler.sendMessage(msg);
Log.e("url", json);
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
private void login(String sms) {
Log.e("url", sms);
Request request = new Request.Builder().url(sms).build();
Response response = null;
try {
if (request == null) {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
return;
}
response = OkHttpUtil.execute(request);
if (response.code() == 200) {
String json = "";
try {
json = response.body().string();
Message msg = new Message();
msg.obj = json;
msg.what = 1;
handler.sendMessage(msg);
Log.e("url", json);
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
@Override
protected void onResume() {
super.onResume();
// requestManager.addOnRequestFinishedListener(this);
MobclickAgent.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
// requestManager.removeOnRequestFinishedListener(this);
MobclickAgent.onPause(this);
}
}
|