| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984 |
- package com.jiaju.activity;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.reflect.Method;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.json.JSONObject;
- import com.jiaju.net.WebClient;
- import com.jiaju.utils.Base64Utils;
- import com.jiaju.utils.CommonProgressDialog;
- import com.jiaju.utils.CommonUtil;
- import com.jiaju.utils.Constants;
- import com.jiaju.utils.CustomToast;
- import com.jiaju.utils.Installation;
- import com.jiaju.utils.Md5Utils;
- import com.jiaju.utils.ProcessDialogUtils;
- import com.jiaju.utils.RSAUtils;
- import com.jiaju.utils.UpgradeDialog;
- import android.Manifest;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.content.pm.PackageManager;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.os.Message;
- import android.provider.Settings;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.text.method.PasswordTransformationMethod;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- public class LoginActivity extends Activity implements OnClickListener {
- private TextView autologintext;
- public EditText login_zhangtao, login_yonghu, login_mima;
- private ImageView auto_login;
- private ImageView remember_password;
- private Button login_login;
- private Button cancel;
- private SharedPreferences preferences, preferencesjizhu;
- private CheckBox jizhumima;
- static String YES = "yes";
- static String NO = "no";
- int contentlength;
- private SharedPreferences sp = null;// 声明一个SharedPreferences
- private String name = "", password = "", zhangtao = "";
- private String ServerAddress_ip, ServerAddress_duankou;
- private String result = null;
- // private Map<String,String> params=new HashMap<String,String>();
- private boolean IsAutoLogin = false;
- private boolean IsRememberPassword = false;
- private Button setting;
- // private ImageView setting;
- private WebClient client;
- // 版本更新
- private String newVerName = "";// 新版本名称
- private int newVerCode = -1;// 新版本号
- private String verName = "";// 当前版本号
- private int verCode = -1;// 当前版本号
- private ProgressBar mProgress;
- private Dialog downloadDialog;
- private static String savePath = "/updateapk/";
- private static String saveFileName = "FanJiaJuProject.apk";
- private static final int GET_SERVER_VERSION = 2;
- private static final int DOWN_UPDATE = 3;
- private static final int DOWN_OVER = 4;
- private boolean autologinflag = false;
- private String installFilePath;
- private UpgradeDialog dialog = null;
- public static final int MAX_RETRY = 10;
- private URL url;
- private HttpURLConnection connection;
- private InputStream input;
- private long cur_pos; // 用于决定seek方法中是否执行文件定位
- private int connect_timeout = 5000 * 2;
- private int read_timeout = 5000 * 2;
- FileOutputStream output = null;
- OutputStream outputStream = null;
- private CommonProgressDialog mDialog = null;
- private String serverApp;
- private String localVersion;
- private TextView rememberPasswordTxt;
- private HandlerThread thread;
- private Handler mHandler;
- private String userName;
- private static final int PERMISSON_REQUESTCODE = 0;
- private boolean isNeedCheck = true;
- private Object obj = new Object();
- Handler handler = new Handler();
- protected String[] needPermissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- preferencesjizhu = getSharedPreferences("user2", Context.MODE_PRIVATE);
- preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
- SysApplication.getInstance().addActivity(this);
- initview();
- ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
- ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
- if (TextUtils.isEmpty(ServerAddress_duankou)) {
- ServerAddress_duankou = "80";
- }
- zhangtao = preferences.getString("AccountCode", "");
- name = preferences.getString("UserCode", "").toUpperCase();
- password = preferences.getString("UserPassword", "");
- login_zhangtao.setText(zhangtao);
- login_yonghu.setText(name);
- login_zhangtao.setSelection(login_zhangtao.length());
- login_yonghu.setSelection(login_yonghu.length());
- String changeFlag = preferencesjizhu.getString("changeuser", "");
- if (changeFlag.equals("1")) {
- Editor edit = preferencesjizhu.edit();
- edit.putString("changeuser", "0");
- edit.commit();
- login_yonghu.setFocusable(true);
- login_yonghu.requestFocus();
- } else {
- if (login_zhangtao.getText().toString().equals("") && login_yonghu.getText().toString().equals("")
- && login_mima.getText().toString().equals("")) {
- login_zhangtao.setFocusable(true);
- login_zhangtao.requestFocus();
- }
- if (!login_zhangtao.getText().toString().equals("") && !login_yonghu.getText().toString().equals("")) {
- login_mima.setFocusable(true);
- login_mima.requestFocus();
- String password = login_mima.getText().toString().trim();
- if (!TextUtils.isEmpty(password)) {
- login_mima.setSelection(password.length());
- }
- }
- }
- if (preferencesjizhu.getBoolean("iszidong", false)) {
- auto_login.setBackgroundResource(R.drawable.check_pressed);
- login_zhangtao.setText(preferencesjizhu.getString("AccountCode", ""));
- login_yonghu.setText(preferencesjizhu.getString("UserCode", "").toUpperCase());
- if (preferencesjizhu.getBoolean("isrememberpassword", false)) {
- IsRememberPassword = true;
- remember_password.setBackgroundResource(R.drawable.check_pressed);
- String encryptPassword = preferencesjizhu.getString("UserPassword", "");
- PrivateKey privateKey;
- try {
- InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
- privateKey = RSAUtils.loadPrivateKey(inPrivate);
- byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
- String decryptPassword = new String(decryptByte);
- if (TextUtils.isEmpty(decryptPassword)) {
- CustomToast.showToast(getApplicationContext(), "请输入密码", 2000);
- login_mima.setFocusable(true);
- login_mima.requestFocus();
- return;
- }
- login_mima.setText(decryptPassword);
- login_mima.setSelection(decryptPassword.length());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- login(true);
- } else {
- if (preferencesjizhu.getBoolean("isrememberpassword", false)) {
- IsRememberPassword = true;
- remember_password.setBackgroundResource(R.drawable.check_pressed);
- String encryptPassword = preferencesjizhu.getString("UserPassword", "");
- PrivateKey privateKey;
- try {
- InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
- privateKey = RSAUtils.loadPrivateKey(inPrivate);
- byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
- String decryptPassword = new String(decryptByte);
- login_mima.setText(decryptPassword);
- login_mima.setSelection(decryptPassword.length());
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- IsRememberPassword = false;
- remember_password.setBackgroundResource(R.drawable.check);
- login_mima.setText("");
- }
- auto_login.setBackgroundResource(R.drawable.check);
- }
- setoncick();
- }
- @Override
- protected void onResume() {
- if (Build.VERSION.SDK_INT >= 23
- && getApplicationInfo().targetSdkVersion >= 23) {
- if (isNeedCheck) {
- checkPermissions(needPermissions);
- }
- }
- super.onResume();
- ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
- ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
- if (TextUtils.isEmpty(ServerAddress_duankou)) {
- ServerAddress_duankou = "80";
- }
- }
- /**
- *
- * @param permissions
- * @since 2.5.0 requestPermissions方法是请求某一权限,
- */
- private void checkPermissions(String... permissions) {
- try {
- if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) {
- List<String> needRequestPermissonList = findDeniedPermissions(permissions);
- if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) {
- String[] array = needRequestPermissonList.toArray(new String[needRequestPermissonList.size()]);
- Method method = getClass().getMethod("requestPermissions",
- new Class[] { String[].class, int.class });
- method.invoke(this, array, PERMISSON_REQUESTCODE);
- }
- }
- } catch (Throwable e) {
- }
- }
- /**
- * 获取权限集中需要申请权限的列表
- *
- * @param permissions
- * @return
- * @since 2.5.0 checkSelfPermission方法是在用来判断是否app已经获取到某一个权限
- * shouldShowRequestPermissionRationale方法用来判断是否
- * 显示申请权限对话框,如果同意了或者不在询问则返回false
- */
- private List<String> findDeniedPermissions(String[] permissions) {
- List<String> needRequestPermissonList = new ArrayList<String>();
- if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) {
- try {
- for (String perm : permissions) {
- Method checkSelfMethod = getClass().getMethod("checkSelfPermission", String.class);
- Method shouldShowRequestPermissionRationaleMethod = getClass()
- .getMethod("shouldShowRequestPermissionRationale", String.class);
- if ((Integer) checkSelfMethod.invoke(this, perm) != PackageManager.PERMISSION_GRANTED
- || (Boolean) shouldShowRequestPermissionRationaleMethod.invoke(this, perm)) {
- needRequestPermissonList.add(perm);
- }
- }
- } catch (Throwable e) {
- }
- }
- return needRequestPermissonList;
- }
- /**
- * 检测是否所有的权限都已经授权
- *
- * @param grantResults
- * @return
- * @since 2.5.0
- *
- */
- private boolean verifyPermissions(int[] grantResults) {
- for (int result : grantResults) {
- if (result != PackageManager.PERMISSION_GRANTED) {
- return false;
- }
- }
- return true;
- }
- /**
- * 申请权限结果的回调方法
- */
- // @TargetApi(23)
- // public void onRequestPermissionsResult(int requestCode,
- // String[] permissions, int[] paramArrayOfInt) {
- // if (requestCode == PERMISSON_REQUESTCODE) {
- // if (!verifyPermissions(paramArrayOfInt)) {
- // showMissingPermissionDialog();
- // isNeedCheck = false;
- // }
- // }
- // }
- /**
- * 显示提示信息
- *
- * @since 2.5.0
- *
- */
- private void showMissingPermissionDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("权限申请");
- builder.setMessage("缺少相关权限");
- // 拒绝, 退出应用
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- });
- builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- startAppSettings();
- }
- });
- builder.setCancelable(false);
- builder.show();
- }
- /**
- * 启动应用的设置
- *
- * @since 2.5.0
- *
- */
- private void startAppSettings() {
- Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- intent.setData(Uri.parse("package:" + getPackageName()));
- startActivity(intent);
- }
- private void initview() {
- login_zhangtao = (EditText) findViewById(R.id.login_zhangtao);
- login_yonghu = (EditText) findViewById(R.id.login_yonghu);
- login_mima = (EditText) findViewById(R.id.login_mima);
- auto_login = (ImageView) findViewById(R.id.auto_login);
- remember_password = (ImageView) findViewById(R.id.remember_password);
- login_login = (Button) findViewById(R.id.login_login);
- cancel = (Button) findViewById(R.id.login_set);
- autologintext = (TextView) findViewById(R.id.logint1);
- rememberPasswordTxt = (TextView) findViewById(R.id.rememberPasswordTxt);
- login_mima.setTransformationMethod(PasswordTransformationMethod.getInstance());
- setting = (Button) findViewById(R.id.login_set);
- }
- private void setoncick() {
- auto_login.setOnClickListener(this);
- login_login.setOnClickListener(this);
- cancel.setOnClickListener(this);
- setting.setOnClickListener(this);
- autologintext.setOnClickListener(this);
- remember_password.setOnClickListener(this);
- rememberPasswordTxt.setOnClickListener(this);
- login_yonghu.addTextChangedListener(new TextWatcher() {
- public void onTextChanged(CharSequence text, int start, int before, int count) {
- userName = login_yonghu.getText().toString();
- // 判断输入的文字是否是小写
- for (int i = 0; i < userName.length(); i++) {
- if (userName.charAt(i) - 0 >= 97 && userName.charAt(i) - 0 <= 122) {
- handler.postDelayed(UpperCase, 300);
- }
- }
- }
- @Override
- public void beforeTextChanged(CharSequence text, int start, int count, int after) {
- }
- @Override
- public void afterTextChanged(Editable edit) {
- }
- });
- }
- Handler h = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- Bundle b = msg.getData();
- String content = b.getString("loginResult");
- login_login.setEnabled(true);
- autologinflag = b.getBoolean("autoLoginFlag");
- try {
- JSONObject jsonObject = new JSONObject(content);
- JSONObject jo = jsonObject.getJSONObject("d");
- int status = jo.getInt("Status");
- contentlength = jo.getInt("ApkSize");
- String message = jo.getString("Message");
- String validityEnd = jo.optString("ValidityEnd");
- String serverName = jo.optString("ServerName");
- if (mHandler != null) {
- mHandler.removeCallbacksAndMessages(null);
- mHandler.getLooper().quit();
- }
- if (status == 0) {
- Editor editor = preferences.edit();
- editor.putString("AccountCode", login_zhangtao.getText().toString());
- editor.putString("UserCode", login_yonghu.getText().toString());
- editor.putInt("ContentLength", contentlength);
- editor.putString("ValidityEnd", validityEnd);
- editor.putString("ServiceName", serverName);
- String passwd = login_mima.getText().toString().trim();
- String md5Password = Md5Utils.encode(passwd);
- editor.putString("UserPassword", md5Password);
- editor.putString("AccountID", jo.getInt("AccountID") + "");
- editor.putString("UserID", jo.getInt("UserID") + "");
- editor.putString("UserName", jo.getString("UserName"));
- editor.putString("SessionKey", jo.getString("SessionKey"));
- editor.putString("IsGroutingWorker", jo.optString("IsGroutingWorker"));
- editor.commit();
- if (!autologinflag) {
- Editor editor2 = preferencesjizhu.edit();
- editor2.putString("changeuser", "0");
- editor2.putBoolean("isrememberpassword", IsRememberPassword);
- editor2.putBoolean("iszidong", IsAutoLogin);
- editor2.putString("AccountCode", login_zhangtao.getText().toString());
- editor2.putString("UserCode", login_yonghu.getText().toString());
- String password = login_mima.getText().toString().trim();
- InputStream inPublic = getResources().getAssets().open("rsa_public_key.pem");
- PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
- // 加密
- byte[] encryptByte = RSAUtils.encryptData(password.getBytes(), publicKey);
- // 为了方便观察吧加密后的数据用base64加密转一下,要不然看起来是乱码,所以解密是也是要用Base64先转换
- String encryptPassword = Base64Utils.encode(encryptByte);
- editor2.putString("UserPassword", encryptPassword);
- editor2.putString("AccountID", jo.getInt("AccountID") + "");
- editor2.putString("UserID", jo.getInt("UserID") + "");
- editor2.putString("UserName", jo.getString("UserName"));
- editor2.putString("SessionKey", jo.getString("SessionKey"));
- editor2.commit();
- }
- serverApp = jo.getString("AppVersion");
- localVersion = CommonUtil.getVersionName(LoginActivity.this);
- Pattern pa = Pattern.compile(Constants.ServerVersionReg);
- if (!TextUtils.isEmpty(serverApp) && serverApp.length() > 0) {
- Matcher ma = pa.matcher(serverApp);
- boolean matchresult = ma.matches();
- if (matchresult) {
- boolean downloadFlag = CommonUtil.CompareServerVersion(localVersion, serverApp);
- if (downloadFlag) {
- if (contentlength == -1) {
- // if (contentlength != -1) {
- Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
- startActivity(intent);
- } else {
- dialog = new UpgradeDialog(LoginActivity.this, R.style.MyDialog);
- dialog.setDialog(R.layout.upgrade_layout, LoginActivity.this);
- dialog.tip.setText("发现新版本,请立刻升级?");
- dialog.versionTitle.setText("当前版本:");
- dialog.versionDesc.setText(localVersion);
- dialog.serverTitle.setText("最新版本:");
- dialog.serverDesc.setText(serverApp);
- dialog.versionUpgradeInfoTitle.setText("新版本介绍:");
- dialog.versionUpgradeInfoTxt.setText(R.string.versionDesc);
- dialog.ok.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ShowDownloadDialog();
- }
- });
- dialog.cancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- });
- dialog.showDialog();
- }
- } else {
- Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
- startActivity(intent);
- }
- }
- } else {
- Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
- startActivity(intent);
- }
- ProcessDialogUtils.closeProgressDilog();
- }
- else {
- ProcessDialogUtils.closeProgressDilog();
- CommonUtil.showAlertDialog(status, message, LoginActivity.this);
- if (status == 1) {
- login_zhangtao.setFocusable(true);
- login_zhangtao.requestFocus();
- return;
- } else if (status == 2) {
- login_yonghu.setFocusable(true);
- login_yonghu.requestFocus();
- return;
- } else if (status == 3) {
- login_mima.setFocusable(true);
- login_mima.requestFocus();
- return;
- }
- return;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- break;
- case 2:
- ProcessDialogUtils.closeProgressDilog();
- Bundle b3 = msg.getData();
- String message3 = b3.getString("message");
- CustomToast.showToast(getApplicationContext(), message3, 2000);
- if (mHandler != null) {
- mHandler.removeCallbacksAndMessages(null);
- mHandler.getLooper().quit();
- }
- break;
- case DOWN_UPDATE:
- Bundle bu = msg.getData();
- int progress = bu.getInt("process");
- mDialog.mProgress.setProgress(progress);
- mDialog.setProgressNumber();
- mDialog.setProgressPercent();
- break;
- case DOWN_OVER:
- dialog.dismiss();
- if (mHandler != null) {
- mHandler.removeCallbacksAndMessages(null);
- mHandler.getLooper().quit();
- }
- CommonUtil.installApk(installFilePath, LoginActivity.this);
- break;
- }
- };
- };
- private void ShowDownloadDialog() {
- mDialog = new CommonProgressDialog(this);
- mDialog.setIndeterminate(false);
- mDialog.setCanceledOnTouchOutside(false);
- mDialog.setCancelable(false);
- mDialog.show();
- thread = new HandlerThread("updateThread", 5);
- thread.start();
- mHandler = new Handler(thread.getLooper());
- UpdateRunnable updateRunnable = new UpdateRunnable();
- mHandler.post(updateRunnable);
- }
- Runnable UpperCase = new Runnable() {
- @Override
- public void run() {
- // 小写转大写
- login_yonghu.setText(userName.toUpperCase());
- // 设置EditText光标位置
- login_yonghu.setSelection(userName.length());
- }
- };
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.auto_login:
- if (IsAutoLogin) {
- IsAutoLogin = false;
- v.setBackgroundResource(R.drawable.check);
- } else {
- IsAutoLogin = true;
- IsRememberPassword = true;
- remember_password.setBackgroundResource(R.drawable.check_pressed);
- v.setBackgroundResource(R.drawable.check_pressed);
- }
- break;
- case R.id.remember_password:
- if (IsRememberPassword) {
- IsRememberPassword = false;
- v.setBackgroundResource(R.drawable.check);
- IsAutoLogin = false;
- auto_login.setBackgroundResource(R.drawable.check);
- } else {
- IsRememberPassword = true;
- v.setBackgroundResource(R.drawable.check_pressed);
- }
- break;
- case R.id.login_login:
- if (login_zhangtao.getText().toString().equals("")) {
- CustomToast.showToast(getApplicationContext(), "请输入帐套", 2000);
- login_zhangtao.setFocusable(true);
- login_zhangtao.requestFocus();
- return;
- } else {
- if (login_yonghu.getText().toString().equals("")) {
- // Toast.makeText(getApplicationContext(), "请输入用户名",
- // Toast.LENGTH_SHORT).show();
- CustomToast.showToast(getApplicationContext(), "请输入用户名", 2000);
- login_yonghu.setFocusable(true);
- login_yonghu.requestFocus();
- return;
- } else {
- if (login_mima.getText().toString().equals("")) {
- CustomToast.showToast(getApplicationContext(), "请输入密码", 2000);
- login_mima.setFocusable(true);
- login_mima.requestFocus();
- return;
- } else if (TextUtils.isEmpty(ServerAddress_ip)) {
- CustomToast.showToast(getApplicationContext(), "请输入IP", 2000);
- return;
- } else if (TextUtils.isEmpty(ServerAddress_duankou)) {
- // Toast.makeText(getApplicationContext(), "请输入端口号",
- // Toast.LENGTH_SHORT).show();
- CustomToast.showToast(getApplicationContext(), "请输入端口号", 2000);
- return;
- } else {
- login(false);
- }
- }
- }
- break;
- case R.id.login_set:
- Intent i = new Intent();
- i.setClass(LoginActivity.this, SetActivity.class);
- startActivity(i);
- break;
- case R.id.logint1:
- if (IsAutoLogin) {
- IsAutoLogin = false;
- auto_login.setBackgroundResource(R.drawable.check);
- } else {
- IsAutoLogin = true;
- IsRememberPassword = true;
- remember_password.setBackgroundResource(R.drawable.check_pressed);
- auto_login.setBackgroundResource(R.drawable.check_pressed);
- }
- break;
- case R.id.rememberPasswordTxt:
- if (IsRememberPassword) {
- IsRememberPassword = false;
- remember_password.setBackgroundResource(R.drawable.check);
- IsAutoLogin = false;
- auto_login.setBackgroundResource(R.drawable.check);
- } else {
- IsRememberPassword = true;
- remember_password.setBackgroundResource(R.drawable.check_pressed);
- }
- break;
- default:
- break;
- }
- }
- public class UpdateRunnable implements Runnable {
- @Override
- public void run() {
- String ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
- String ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
- if (TextUtils.isEmpty(ServerAddress_duankou)) {
- ServerAddress_duankou = "80";
- }
- String accountCode = preferences.getString("AccountCode", "");
- String login_userCode = preferences.getString("UserCode", "");
- String password = preferences.getString("UserPassword", "");
- String sessionkey = preferences.getString("SessionKey", "");
- String pPath = CommonUtil.getRootFilePath();
- String fileDir = pPath + Constants.DATA_CACHE_PATH;
- File file = new File(fileDir);
- if (!file.exists()) {
- file.mkdir();
- }
- String filep = "ibossprd.apk";
- File f = new File(file.getAbsolutePath(), filep);
- if (!f.exists()) {
- try {
- f.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try {
- installFilePath = f.getAbsolutePath();
- JSONObject params = new JSONObject();
- params.put("accountCode", accountCode);
- params.put("userCode", login_userCode);
- params.put("userPassword", password);
- params.put("sdk", Build.VERSION.SDK_INT);
- downloadFileStream("http://" + ServerAddress_ip + ":" + ServerAddress_duankou + Constants.serverAction
- + "/AutoUpgradeNew", installFilePath, params.toString());
- } catch (Exception e) {
- e.printStackTrace();
- Message m = new Message();
- m.what = 2;
- Bundle b = new Bundle();
- b.putString("message", "网络连接异常");
- m.setData(b);
- h.sendMessage(m);
- return;
- }
- }
- }
- public void downloadFileStream(String url, String filePath, String content) {
- try {
- int retry = 0;
- while (retry < MAX_RETRY)
- try {
- seek(0, url, content);
- break;
- } catch (Exception e) {
- retry++;
- }
- File wdFile = new File(filePath);
- output = new FileOutputStream(wdFile);
- input = connection.getInputStream();
- // 这将是有用的,这样你可以显示一个典型的0-100%的进度条
- // int fileLength = connection.getContentLength();
- // 下载文件
- mDialog.mProgress.setMax(contentlength);
- byte data[] = new byte[1024];
- int count = 0;
- do {
- int numread = input.read(data);
- count += numread;
- Message m = new Message();
- Bundle b = new Bundle();
- b.putInt("process", count);
- m.what = DOWN_UPDATE;
- m.setData(b);
- h.sendMessage(m);
- if (numread <= 0) {
- // 下载完成通知安装
- h.sendEmptyMessage(DOWN_OVER);
- break;
- }
- output.write(data, 0, numread);
- } while (true);
- output.flush();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- finally {
- try {
- output.close();
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void seek(long start_pos, String conurl, String content) throws IOException {
- if (start_pos == cur_pos && input != null)
- return;
- if (connection != null) {
- connection.disconnect();
- connection = null;
- }
- if (input != null) {
- input.close();
- input = null;
- }
- url = new URL(conurl);
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestProperty("connection", "Keep-Alive");
- connection.setRequestMethod("POST");
- connection.setUseCaches(false);
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setDoInput(true);
- connection.setConnectTimeout(180 * 1000);
- connection.setReadTimeout(180 * 1000);
- connection.setDoOutput(true);
- connection.setRequestProperty("Accept-Encoding", "identity");
- System.setProperty("http.keepAlive", "false");
- connection.connect();
- outputStream = connection.getOutputStream();
- outputStream.write(content.getBytes());
- outputStream.close();
- // httpConnection.setRequestProperty("Connection", "Keep-Alive");
- int responseCode = connection.getResponseCode();
- if (responseCode < 200 || responseCode >= 300) {
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- throw new IOException("HTTP responseCode=" + responseCode);
- }
- input = connection.getInputStream();
- cur_pos = start_pos;
- }
- private void login(final boolean iszidong11) {
- ProcessDialogUtils.showProcessDialog(LoginActivity.this);
- // String Ip = CommonUtil.getIpAddress(LoginActivity.this);
- // String Mac = CommonUtil.getMacFromWifi(LoginActivity.this);
- String Mac = "00:00:00:00:00:00";
- String Ip = "0.0.0.0";
- JSONObject params = new JSONObject();
- try {
- if (iszidong11) {
- params.put("accountCode", preferencesjizhu.getString("AccountCode", "").trim());
- params.put("userCode", preferencesjizhu.getString("UserCode", "").trim().toUpperCase());
- String encryptPassword = preferencesjizhu.getString("UserPassword", "").trim();
- // 从文件中得到私钥
- InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
- PrivateKey privateKey = RSAUtils.loadPrivateKey(inPrivate);
- // 因为RSA加密后的内容经Base64再加密转换了一下,所以先Base64解密回来再给RSA解密
- byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
- String decryptPassword = new String(decryptByte);
- params.put("userPassword", Md5Utils.encode(decryptPassword));
- } else {
- params.put("accountCode", login_zhangtao.getText().toString());
- params.put("userCode", login_yonghu.getText().toString());
- params.put("userPassword", Md5Utils.encode(login_mima.getText().toString().trim()));
- }
- params.put("macAddress", Mac);
- params.put("ipAddress", Ip);
- params.put("phoneCode", "android" );//Installation.id(LoginActivity.this) 2024年6月17日 安卓14
- params.put("phoneType", android.os.Build.MODEL);
- params.put("appVersion", verName);
- params.put("systemType", "Android");
- params.put("systemVersion", android.os.Build.VERSION.RELEASE);
- thread = new HandlerThread("loginThread", 5);
- thread.start();
- mHandler = new Handler(thread.getLooper());
- LoginRunnable loginRunnable = new LoginRunnable(iszidong11, obj, params);
- mHandler.post(loginRunnable);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public class LoginRunnable implements Runnable {
- private JSONObject param;
- private boolean autologinflag;
- private Object obj;
- public LoginRunnable(boolean loginflag, Object obj, JSONObject param) {
- autologinflag = loginflag;
- this.obj = obj;
- this.param = param;
- }
- @Override
- public void run() {
- synchronized (obj) {
- try {
- client = new WebClient();
- result = client.doPost("http://" + ServerAddress_ip + ":" + ServerAddress_duankou
- + Constants.serverAction + "/DoPDALogin", param.toString(), "application/json");
- Message m = new Message();
- m.what = 1;
- Bundle b = new Bundle();
- b.putBoolean("autoLoginFlag", autologinflag);
- b.putString("loginResult", result);
- m.setData(b);
- h.sendMessage(m);
- } catch (Exception e) {
- e.printStackTrace();
- Message m = new Message();
- m.what = 2;
- Bundle b = new Bundle();
- b.putString("message", "网络连接异常");
- m.setData(b);
- h.sendMessage(m);
- }
- }
- }
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- View view = getCurrentFocus();
- CommonUtil.hideKeyboard(ev, view, LoginActivity.this);// 调用方法判断是否需要隐藏键盘
- break;
- default:
- break;
- }
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public Resources getResources() {
- Resources res = super.getResources();
- Configuration config = new Configuration();
- config.setToDefaults();
- res.updateConfiguration(config, res.getDisplayMetrics());
- return res;
- }
- }
|