327 lines
15 KiB
Plaintext
Executable File
327 lines
15 KiB
Plaintext
Executable File
package com.example.administrator.ximengjianyu.activity;
|
||
|
||
import android.app.Activity;
|
||
import android.app.ActivityManager;
|
||
import android.app.Dialog;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.ActivityInfo;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.graphics.drawable.AnimationDrawable;
|
||
import android.support.v7.app.AppCompatActivity;
|
||
import android.os.Bundle;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.view.WindowManager;
|
||
import android.widget.Button;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
import com.cm.utils.net.HttpUtils;
|
||
import com.cm.utils.net.response.GsonResponseHandler;
|
||
import com.example.administrator.ximengjianyu.R;
|
||
import com.example.administrator.ximengjianyu.activity.home.HomeActivity;
|
||
import com.example.administrator.ximengjianyu.activity.home.NewPwdActivity;
|
||
import com.example.administrator.ximengjianyu.beans.AppCriminalUserBean;
|
||
import com.example.administrator.ximengjianyu.global.Constants;
|
||
import com.example.administrator.ximengjianyu.global.UserInfo;
|
||
import com.example.administrator.ximengjianyu.mqtt.push.MQTTService;
|
||
import com.example.administrator.ximengjianyu.utils.AndroidManager;
|
||
import com.example.administrator.ximengjianyu.utils.CacheUtils;
|
||
import com.example.administrator.ximengjianyu.utils.EdittextUtils;
|
||
import com.example.administrator.ximengjianyu.utils.PinYinUtils;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
//import de.eclipsemagazin.mqtt.push.BlackIceActivity;
|
||
//import de.eclipsemagazin.mqtt.push.MQTTService;
|
||
|
||
|
||
public class LoginActivity extends AppCompatActivity {
|
||
public static final String SERVICE_CLASSNAME = "com.example.administrator.ximengjianyu.mqtt.push.MQTTService";
|
||
private EditText mLoginUsername;
|
||
private EditText mLoginPassword;
|
||
private Button btn_login;
|
||
public Activity mActivity;
|
||
private AnimationDrawable animationDrawable;
|
||
private Map<String, String> tuichumap;
|
||
/**
|
||
* 该变量表明是否是被挤下线跳转
|
||
*/
|
||
|
||
//sp中的用户名密码
|
||
String password, username;
|
||
private String TAG = "LoginActivity";
|
||
/**
|
||
* 声明一个集合,内部装入点击登录按钮,当联网失败或者成功后,从该集合中移除请求
|
||
*/
|
||
private List<String> clickList;
|
||
|
||
PackageInfo packageInfo = null;
|
||
private boolean first = true;
|
||
private ImageView mIvUsernameDelete;
|
||
private ImageView mIvPasswordDelete;
|
||
private View.OnClickListener on;
|
||
private Intent intent;
|
||
private Dialog dialog;
|
||
public Dialog baseDialog;
|
||
private String msg;
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||
setContentView(R.layout.activity_login);
|
||
getSupportActionBar().hide();//隐藏标题栏
|
||
//初始化视图
|
||
initView();
|
||
//数据
|
||
initData();
|
||
//通知栏
|
||
// updateButton();
|
||
}
|
||
|
||
//
|
||
//
|
||
public boolean serviceIsRunning() {
|
||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||
if (SERVICE_CLASSNAME.equals(service.service.getClassName())) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
private void initView() {
|
||
mLoginUsername = (EditText) findViewById(R.id.login_name);
|
||
mLoginPassword = (EditText) findViewById(R.id.login_pwd);
|
||
mIvUsernameDelete = (ImageView) findViewById(R.id.iv_username_delete);
|
||
mIvPasswordDelete = (ImageView) findViewById(R.id.iv_password_delete);
|
||
btn_login = (Button) findViewById(R.id.btn_login);
|
||
}
|
||
|
||
private void initData() {
|
||
|
||
clickList = new ArrayList<>();
|
||
// 获取sp缓存中的用户名密码
|
||
// password = CacheUtils.getCache(this, Constants.LOGIN_PASSWORD_SP);
|
||
// username = CacheUtils.getCache(this, Constants.LOGIN_USERNAME_SP);
|
||
//点击登录按钮
|
||
btn_login.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View view) {
|
||
|
||
if (clickList.size() == 0) { //长度为0,说明没有点击过,才可以走登录的网络请求
|
||
// intDialog("正在登陆...");
|
||
clickList.add("hasClick"); //
|
||
final String user = mLoginUsername.getText().toString().trim(); //用户名
|
||
final String password = mLoginPassword.getText().toString().trim(); //密码
|
||
clickLoginButton(user, password);
|
||
} else {
|
||
Toast.makeText(LoginActivity.this, getResources().getText(R.string.LoginHasClicked),
|
||
Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
});
|
||
|
||
mIvUsernameDelete.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View view) {
|
||
mLoginUsername.setText(null);
|
||
mIvUsernameDelete.setVisibility(View.GONE);
|
||
}
|
||
});
|
||
mIvPasswordDelete.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View view) {
|
||
mLoginPassword.setText(null);
|
||
mIvPasswordDelete.setVisibility(View.GONE);
|
||
}
|
||
});
|
||
|
||
|
||
//为登录页输入框添加数据
|
||
EdittextUtils edittextUtils = EdittextUtils.getEdittext();
|
||
if (username != null) {
|
||
mLoginUsername.setText(username);
|
||
edittextUtils.clearEdittext(mLoginUsername, mIvUsernameDelete);
|
||
EdittextUtils.getEdittext().makeEdittextToLast(mLoginUsername);
|
||
} else {
|
||
edittextUtils.clearEdittext(mLoginUsername, mIvUsernameDelete);
|
||
EdittextUtils.getEdittext().makeEdittextToLast(mLoginUsername);
|
||
}
|
||
if (password != null) {
|
||
mLoginUsername.setText(password);
|
||
edittextUtils.clearEdittext(mLoginPassword, mIvPasswordDelete);
|
||
EdittextUtils.getEdittext().makeEdittextToLast(mLoginPassword);
|
||
} else {
|
||
edittextUtils.clearEdittext(mLoginPassword, mIvPasswordDelete);
|
||
EdittextUtils.getEdittext().makeEdittextToLast(mLoginPassword);
|
||
}
|
||
try {
|
||
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
|
||
} catch (PackageManager.NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 该方法用来实现点击登录按钮的功能
|
||
*/
|
||
private void clickLoginButton(final String username, final String password) {
|
||
|
||
//要判断用户名和密码是否为空
|
||
if (TextUtils.isEmpty(username)) { //用户名为空
|
||
Toast.makeText(this, getResources().getText(R.string.LoginUsernameIsNull), Toast.LENGTH_SHORT).show();
|
||
EdittextUtils.getEdittext().showSoftInputFromWindow(this, mLoginUsername);
|
||
removeFromClicklist();
|
||
} else if (TextUtils.isEmpty(password)) { //密码为空
|
||
Toast.makeText(this, getResources().getText(R.string.LoginPasswordIsNull), Toast.LENGTH_SHORT).show();
|
||
EdittextUtils.getEdittext().showSoftInputFromWindow(this, mLoginPassword);
|
||
removeFromClicklist();
|
||
} else {
|
||
if (PinYinUtils.isContainChinese(username)) { //如果是中文
|
||
Toast.makeText(this, getResources().getText(R.string.LoginUsernameIsChinese), Toast.LENGTH_SHORT).show();
|
||
removeFromClicklist();
|
||
} else {
|
||
Map<String, String> map = new HashMap<>();
|
||
map.put("username", username);
|
||
map.put("password", password);
|
||
map.put("version", packageInfo.versionName);
|
||
map.put("appkey", this.getResources().getString(R.string.appKey));
|
||
|
||
HttpUtils.getInstance().post(Constants.LOGIN_USER_URL, map, new GsonResponseHandler<AppCriminalUserBean>() {
|
||
@Override
|
||
public void onSuccess(int statusCode, AppCriminalUserBean response) {
|
||
Log.e("--Login--", "onSuccess: " + response.toString());
|
||
Log.e("--Main--", "登陆:" + response.toString());
|
||
if (password.equals("123456") && Integer.parseInt(response.getState()) == 200) {
|
||
UserInfo.setToken(LoginActivity.this, response.getData().getToken());
|
||
Log.e("Login_Token", response.getData().getToken());
|
||
Intent intent1 = new Intent(LoginActivity.this, NewPwdActivity.class);
|
||
startActivity(intent1);
|
||
finish();
|
||
}else
|
||
if (Integer.parseInt(response.getState()) == 200) {
|
||
// dismissDialog();
|
||
//保存用户的uid,姓名,用户名,密码,组织名称
|
||
CacheUtils.setCache(LoginActivity.this, Constants.LOGIN_USERNAME_SP, username);
|
||
CacheUtils.setCache(LoginActivity.this, Constants.LOGIN_PASSWORD_SP, password);
|
||
UserInfo.setToken(LoginActivity.this, response.getData().getToken());
|
||
Log.e("Login_Token", response.getData().getToken());
|
||
msg = response.getMsg();
|
||
UserInfo.setName(LoginActivity.this, response.getData().getName());
|
||
UserInfo.setUserid(LoginActivity.this, response.getData().getCriminalUserId());
|
||
UserInfo.setId(LoginActivity.this, response.getData().getCriminalId());
|
||
UserInfo.setIcon(LoginActivity.this, response.getData().getFrontPhoto());
|
||
//分机号
|
||
UserInfo.setExtensionNumber(LoginActivity.this, response.getData().getExtensionNumber());
|
||
|
||
UserInfo.setSurplusCount(LoginActivity.this, response.getData().getSurplusCount());
|
||
// if (response.getData().getFamilylovePhoneList().size() > 0){
|
||
// //亲友姓名
|
||
// UserInfo.setDeptname(LoginActivity.this, response.getData().getFamilylovePhoneList().get(0).getName());
|
||
// //亲友电话
|
||
// UserInfo.setTel(LoginActivity.this, response.getData().getFamilylovePhoneList().get(0).getPhone());
|
||
// //亲友关系
|
||
// UserInfo.setFax(LoginActivity.this, response.getData().getFamilylovePhoneList().get(0).getRelation());
|
||
// }
|
||
// for (int i = 0; i <response.getData().getFamilylovePhoneList().size() ; i++) {
|
||
// //亲友姓名
|
||
// UserInfo.setDeptname(LoginActivity.this, response.getData().getFamilylovePhoneList().get(i).getName());
|
||
// //亲友电话
|
||
// UserInfo.setTel(LoginActivity.this, response.getData().getFamilylovePhoneList().get(i).getPhone());
|
||
// Log.e("亲友电话",response.getData().getFamilylovePhoneList().get(i).getPhone());
|
||
// //亲友关系
|
||
// UserInfo.setFax(LoginActivity.this, response.getData().getFamilylovePhoneList().get(i).getRelation());
|
||
// }
|
||
|
||
//剩余次数
|
||
UserInfo.setBirthday(LoginActivity.this, response.getData().getSurplusCount() + "");
|
||
Toast.makeText(LoginActivity.this, response.getMsg(), Toast.LENGTH_SHORT).show();
|
||
removeFromClicklist();
|
||
// //调用通知启动方法
|
||
// Intent intent = new Intent(LoginActivity.this, MQTTService.class);
|
||
//// intent.setAction(MQTTService.ACTION_START);
|
||
// LoginActivity.this.startService(intent);
|
||
// MQTTService.actionStart(LoginActivity.this);
|
||
intent = new Intent(LoginActivity.this, HomeActivity.class);
|
||
startActivity(intent);
|
||
finish();
|
||
}else {
|
||
Toast.makeText(LoginActivity.this, response.getMsg(), Toast.LENGTH_SHORT).show();
|
||
removeFromClicklist();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(int statusCode, String error_msg) {
|
||
Log.e("--Login--", "onFailure: " + error_msg);
|
||
Toast.makeText(LoginActivity.this, "网络超时", Toast.LENGTH_SHORT).show();
|
||
removeFromClicklist();
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 该方法是用来从集合中移除已点击登录按钮
|
||
*/
|
||
private void removeFromClicklist() {
|
||
if (clickList.size() > 0) {
|
||
clickList.remove(0); //请求失败,移除集合中的已点击
|
||
}
|
||
}
|
||
|
||
|
||
// /**
|
||
// * 呼叫Dialog
|
||
// *
|
||
// * @param content
|
||
// * @return
|
||
// */
|
||
// public Dialog intDialog(String content) {
|
||
// Log.i("------------", mActivity.toString());
|
||
// baseDialog = new Dialog(mActivity);
|
||
// //隐藏Dialog标题
|
||
// baseDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
// View view = View.inflate(mActivity, R.layout.dialog_load, null);
|
||
// baseDialog.setContentView(view);
|
||
// TextView text = view.findViewById(R.id.progress_message);
|
||
// ImageView imge = view.findViewById(R.id.progress_view);
|
||
// animationDrawable = (AnimationDrawable) imge.getDrawable();
|
||
// if (animationDrawable != null) {
|
||
// animationDrawable.setOneShot(false);
|
||
// animationDrawable.start();
|
||
// }
|
||
// baseDialog.setCanceledOnTouchOutside(false);
|
||
// text.setText(content);
|
||
// baseDialog.show();
|
||
// //设置Dialog窗口大小
|
||
// WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
|
||
// params.width = 500;
|
||
// params.height = 200;
|
||
// dialog.getWindow().setAttributes(params);
|
||
// return baseDialog;
|
||
// }
|
||
//
|
||
// public void dismissDialog() {
|
||
// if (baseDialog != null) {
|
||
// baseDialog.dismiss();
|
||
// }
|
||
// }
|
||
|
||
} |