package com.jiuqi.njt.ui;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import com.jiuqi.mobile.nigo.comeclose.bean.base.UserBean;
import com.jiuqi.mobile.nigo.comeclose.bean.base.UserClientModuleBean;
import com.jiuqi.mobile.nigo.comeclose.exception.LoginException;
import com.jiuqi.mobile.nigo.comeclose.manager.base.ILoginManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.mobile.nigo.comeclose.ws.server.Session;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.CheckStateInterface;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.register.NewRegister;
import com.jiuqi.njt.register.PerfectInformation;
import com.jiuqi.njt.register.RegisterUtils;
import com.jiuqi.njt.register.SelectRole;
import com.jiuqi.njt.son.ResetPasswordActivity;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jqyd.android.module.lbs.util.CheckState_interface;
public class LoginActivityNew extends Activity implements OnClickListener {
private Button btnLogin;
private Button btnPhoneCall;
private EditText etAccountName;
private EditText etPassword;
private TextView loadText;
private TextView login_info;
private TableLayout info_tab;
private RelativeLayout check_relay;
private LinearLayout load_begin;
private View load_Layout;
private MyApp application;
private static OptsharepreInterface sharePre;
private CheckBox remPwd;
private CheckBox isAutoLogin;
private String cooper_guid;
// 监听的服务名
// final String ServiceName = "com.jiuqi.njt.service.LxsbService";
// final String ScreenServiceName = "com.jiuqi.njt.service.ScreenService";
final String UpPlanServiceName = "com.jiuqi.njt.service.UpPlanService";
final String UpLocServiceName = "com.jiuqi.njt.service.UpLocationService";
final String RetransServiceName = "com.jiuqi.njt.service.RetransmissionService";
// 判断服务是否开启
boolean isupPlanStart = false;
boolean isupLocStart = false;
boolean isRetransStart = false;
private Button btnForgetPassword;
// private Intent UpPlanServiceIntent;
// private Intent UpLocServiceIntent;
// private Intent RetransServiceIntent;
// public int isPerfect = 0 ;//信息是否完整(0、未完善 1、部分完善 2、已完善)
private final String[] strsPoiTypes = Constants.strAllRoleTypes;
private int selectedRoleIndex = 0;
private Dialog dialog;
private List<UserClientModuleBean> userModules;
// private Handler handler = new Handler() {
//
// @Override
// public void handleMessage(Message msg) {
// super.handleMessage(msg);
// switch (msg.what) {
// case 100:
//// UIUtil.showView(load_Layout);
// new RegisterUtils().loginSuccess(LoginActivityNew.this);
// break;
// default:
// break;
// }
//
// }
//
// };
private boolean ischangeUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private <T> void initParam() {
application = (MyApp) getApplication();
sharePre = new OptsharepreInterface(LoginActivityNew.this);
ischangeUser = getIntent().getBooleanExtra("changeUser", false);
}
private void initWidgets() {
setContentView(R.layout.login_activity);
ViewStub rlTitleBar = (ViewStub) findViewById(R.id.titleBarStub);
TitleBarUtil.createTitleBar(this, rlTitleBar, "登录",
Constants.BTN_BACK_TEXT, new OnClickListener() {
@Override
public void onClick(View v) {
LoginActivityNew.this.finish();
}
}, "注册", new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// intent.setClass(LoginActivity.this, Register.class);
intent.setClass(LoginActivityNew.this,NewRegister.class);
startActivity(intent);
}
});
btnLogin = (Button) findViewById(R.id.btnLogin);
etAccountName = (EditText) findViewById(R.id.etAccountName);
etPassword = (EditText) findViewById(R.id.etPassword);
remPwd = (CheckBox) this.findViewById(R.id.remPwd);
load_begin = (LinearLayout) this.findViewById(R.id.load_begin);
load_Layout = this.findViewById(R.id.load_Layout);
loadText = (TextView) this.findViewById(R.id.load);
info_tab = (TableLayout) this.findViewById(R.id.info_tablay);
check_relay = (RelativeLayout) this.findViewById(R.id.check_relay);
login_info = (TextView) this.findViewById(R.id.login_info);
btnPhoneCall = (Button) findViewById(R.id.btnPhoneCall);
btnForgetPassword = (Button) findViewById(R.id.btnForgetPassword);
isAutoLogin = (CheckBox) this.findViewById(R.id.isAutoLogin);
btnForgetPassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
btnForgetPassword.getPaint().setAntiAlias(true);
isRemPwd();// 检测记忆密码
isAutoLogin();// 检测选择自动登录
String userName = sharePre.getPres("account");
if (!userName.equals("")) {
etAccountName.setText(userName);
}
}
/**
* 检测是否记住密码
*/
public boolean isRemPwd() {
String i = sharePre.getPres("isRemPwd");
if (i.equals("1")) {
remPwd.setChecked(true);
String pwd = sharePre.getPres("password");
etPassword.setText(pwd);
return true;
}
return false;
}
/**
* 检测是否选择自动登录
*/
public boolean isAutoLogin() {
String i = sharePre.getPres("isAutoLogin");
if(!ischangeUser){
if (i.equals("1")) {
UIUtil.showView(load_Layout);
final String name = sharePre.getPres("account");
final String password = sharePre.getPres("password");
new LoginTask(name, password).execute();
isAutoLogin.setChecked(true);
return true;
}
}
return false;
}
private void initListeners() {
btnLogin.setOnClickListener(this);
remPwd.setOnClickListener(this);
btnPhoneCall.setOnClickListener(this);
isAutoLogin.setOnClickListener(this);
btnForgetPassword.setOnClickListener(this);
}
private void initUI() {
btnPhoneCall.setText("客服专线:" + getString(R.string.hotlineText));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
if (UIUtil.isFastDoubleClick()) {
return;
}
final String name = etAccountName.getText().toString();
final String password = etPassword.getText().toString();
if ("".equals(name)) {
UIUtil.alert(LoginActivityNew.this, "请输入用户名!", null);
} else if ("".equals(password)) {
UIUtil.alert(LoginActivityNew.this, "请输入密码!", null);
} else {
if (new CheckState_interface(LoginActivityNew.this).checkConnection()) {
// RegisterUtils.getData(LoginActivityNew.this, ILoginManager.class, new ManagerInterface() {
//
// @Override
// public <T> void getManager(T manager) {
// // TODO Auto-generated method stub
// String sessionId = ((ILoginManager)manager).loginNew(name, password);
// ((ILoginManager)manager).getUser(sessionId);
// }
// });
new LoginTask(name, password).execute();
} else {
UIUtil.showMsg(LoginActivityNew.this,
Constants.NETWORK_STATE_ERROR);
}
}
break;
case R.id.btnPhoneCall:
UIUtil.tryToDial(this, getString(R.string.hotlineText));
break;
case R.id.btnForgetPassword:
Intent in = new Intent();
in.setClass(LoginActivityNew.this, ResetPasswordActivity.class);
startActivity(in);
break;
default:
break;
}
}
class LoginTask extends AsyncTask<String, Integer, String> {
private String msg = "";
private String name;
private String pwd;
public LoginTask(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
@Override
protected void onPreExecute() {
UIUtil.showView(load_Layout);
}
@Override
protected String doInBackground(String... params) {
try {
ClientContext context = application.getClientContext();
if (null!= context ) {
try {
String sessionId = context.getSessionID();
ILoginManager loginManager = context.getManager(ILoginManager.class,5000);
UserBean user = context.getUser();
if(user ==null){
user = new UserBean();
user.setAccount(Constants.ANONYMOUS);
}
if(sessionId!=null){
loginManager.loginOut(sessionId,user, 2);
}
} catch (Exception e) {
}
}
context = ClientContext.getClientContext(Constants.SERVER_URL,name, pwd);
application.setClientContext(context);
UserBean userBean = context.getUser();
if (null == userBean) {
msg = "登录验证失败!";
} else {
sharePre.putPres("isAutoLogin", "1");
sharePre.putPres("isRemPwd", "1");
sharePre.putPres("account", name);
sharePre.putPres("password", pwd);
// IRegistManager manager = context
// .getManager(IRegistManager.class);
// msg = manager.decideRegistAndLogin(name,
// context.getSessionID());
// context = ClientContext.getClientContext(Constants.SERVER_URL, context.getSessionID());
application.setClientContext(context);
// userBean = context.getUser();
msg = context.getDetail();
RegisterUtils.saveLoginData(LoginActivityNew.this,userBean);
}
} catch (Exception e) {
if (e instanceof UndeclaredThrowableException) {
msg = "连接服务器异常";
} else if (e instanceof LoginException) {
msg = e.getMessage();
} else {
msg = "登录失败";
}
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// if (!"登录成功,正在进入……".equals(msg)) {
// UIUtil.hideView(load_Layout);
if ("index".equals(msg)) {
UIUtil.setPushMsgTags(LoginActivityNew.this);//设置推送的标签
application.setIsLogin(true);
Intent jump = new Intent();
jump.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
jump.setClass(LoginActivityNew.this, NjtMainActivity.class);
startActivity(jump);
finish();
} else if ("chooserole".equals(msg)) {
UIUtil.setPushMsgTags(LoginActivityNew.this);//设置推送的标签
Intent jump = new Intent();
jump.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
jump.setClass(LoginActivityNew.this, SelectRole.class);
jump.putExtra("sessionId", application.getClientContext()
.getSessionID());
jump.putExtra("UserBean", application.getClientContext()
.getUser());
startActivity(jump);
finish();
} else if ("perfectinfo".equals(msg)) {
// loginSuccess();
// handler.sendEmptyMessage(100);//
UIUtil.setPushMsgTags(LoginActivityNew.this);//设置推送的标签
Intent jump = new Intent();
jump.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
jump.setClass(LoginActivityNew.this,
PerfectInformation.class);
jump.putExtra("sessionId", application.getClientContext()
.getSessionID());
jump.putExtra("UserBean", application.getClientContext()
.getUser());
startActivity(jump);
finish();
} else {
// load_begin.setVisibility(LinearLayout.GONE);
UIUtil.hideView(load_Layout);
if (!TextUtils.isEmpty(msg)) {
UIUtil.showMsg(LoginActivityNew.this, msg);
}
}
// /*} else {
// handler.sendEmptyMessage(100);//
// UIUtil.setPushMsgTags(LoginActivityNew.this);//设置推送的标签
// }*/
// saveLoginState(msg);
}
}
/**
* 启动上报服务
*/
// private void closeService() {
// getServiceState();
// if (isupPlanStart) {
// stopService(new Intent(LoginActivity.this, UpPlanService.class));
// Log.i(this.getClass().getSimpleName(), "关闭更新任务服务");
// }
//
// if (isRetransStart) {
// stopService(new Intent(LoginActivity.this, RetransmissionService.class));
// Log.i(this.getClass().getSimpleName(), "关闭重传服务");
// }
//
// if (isupLocStart) {
// stopService(new Intent(LoginActivity.this, UpLocationService.class));
// Log.i(this.getClass().getSimpleName(), "关闭位置上报服务");
// }
//
// }
private void getServiceState() {
isupLocStart = CheckStateInterface.isServiceRunning(
LoginActivityNew.this, UpLocServiceName);
isRetransStart = CheckStateInterface.isServiceRunning(
LoginActivityNew.this, RetransServiceName);
isupPlanStart = CheckStateInterface.isServiceRunning(
LoginActivityNew.this, UpPlanServiceName);
}
@Override
protected void onResume() {
super.onResume();
UIUtil.hideView(load_begin);
}
}