LoginActivity.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. package com.jiaju.activity;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.lang.reflect.Method;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.security.PrivateKey;
  11. import java.security.PublicKey;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. import org.json.JSONObject;
  17. import com.jiaju.net.WebClient;
  18. import com.jiaju.utils.Base64Utils;
  19. import com.jiaju.utils.CommonProgressDialog;
  20. import com.jiaju.utils.CommonUtil;
  21. import com.jiaju.utils.Constants;
  22. import com.jiaju.utils.CustomToast;
  23. import com.jiaju.utils.Installation;
  24. import com.jiaju.utils.Md5Utils;
  25. import com.jiaju.utils.ProcessDialogUtils;
  26. import com.jiaju.utils.RSAUtils;
  27. import com.jiaju.utils.UpgradeDialog;
  28. import android.Manifest;
  29. import android.app.Activity;
  30. import android.app.AlertDialog;
  31. import android.app.Dialog;
  32. import android.content.Context;
  33. import android.content.DialogInterface;
  34. import android.content.Intent;
  35. import android.content.SharedPreferences;
  36. import android.content.SharedPreferences.Editor;
  37. import android.content.pm.PackageManager;
  38. import android.content.res.Configuration;
  39. import android.content.res.Resources;
  40. import android.net.Uri;
  41. import android.os.Build;
  42. import android.os.Bundle;
  43. import android.os.Handler;
  44. import android.os.HandlerThread;
  45. import android.os.Message;
  46. import android.provider.Settings;
  47. import android.text.Editable;
  48. import android.text.TextUtils;
  49. import android.text.TextWatcher;
  50. import android.text.method.PasswordTransformationMethod;
  51. import android.view.MotionEvent;
  52. import android.view.View;
  53. import android.view.View.OnClickListener;
  54. import android.widget.Button;
  55. import android.widget.CheckBox;
  56. import android.widget.EditText;
  57. import android.widget.ImageView;
  58. import android.widget.ProgressBar;
  59. import android.widget.TextView;
  60. public class LoginActivity extends Activity implements OnClickListener {
  61. private TextView autologintext;
  62. public EditText login_zhangtao, login_yonghu, login_mima;
  63. private ImageView auto_login;
  64. private ImageView remember_password;
  65. private Button login_login;
  66. private Button cancel;
  67. private SharedPreferences preferences, preferencesjizhu;
  68. private CheckBox jizhumima;
  69. static String YES = "yes";
  70. static String NO = "no";
  71. int contentlength;
  72. private SharedPreferences sp = null;// 声明一个SharedPreferences
  73. private String name = "", password = "", zhangtao = "";
  74. private String ServerAddress_ip, ServerAddress_duankou;
  75. private String result = null;
  76. // private Map<String,String> params=new HashMap<String,String>();
  77. private boolean IsAutoLogin = false;
  78. private boolean IsRememberPassword = false;
  79. private Button setting;
  80. // private ImageView setting;
  81. private WebClient client;
  82. // 版本更新
  83. private String newVerName = "";// 新版本名称
  84. private int newVerCode = -1;// 新版本号
  85. private String verName = "";// 当前版本号
  86. private int verCode = -1;// 当前版本号
  87. private ProgressBar mProgress;
  88. private Dialog downloadDialog;
  89. private static String savePath = "/updateapk/";
  90. private static String saveFileName = "FanJiaJuProject.apk";
  91. private static final int GET_SERVER_VERSION = 2;
  92. private static final int DOWN_UPDATE = 3;
  93. private static final int DOWN_OVER = 4;
  94. private boolean autologinflag = false;
  95. private String installFilePath;
  96. private UpgradeDialog dialog = null;
  97. public static final int MAX_RETRY = 10;
  98. private URL url;
  99. private HttpURLConnection connection;
  100. private InputStream input;
  101. private long cur_pos; // 用于决定seek方法中是否执行文件定位
  102. private int connect_timeout = 5000 * 2;
  103. private int read_timeout = 5000 * 2;
  104. FileOutputStream output = null;
  105. OutputStream outputStream = null;
  106. private CommonProgressDialog mDialog = null;
  107. private String serverApp;
  108. private String localVersion;
  109. private TextView rememberPasswordTxt;
  110. private HandlerThread thread;
  111. private Handler mHandler;
  112. private String userName;
  113. private static final int PERMISSON_REQUESTCODE = 0;
  114. private boolean isNeedCheck = true;
  115. private Object obj = new Object();
  116. Handler handler = new Handler();
  117. protected String[] needPermissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE,
  118. Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA };
  119. @Override
  120. protected void onCreate(Bundle savedInstanceState) {
  121. super.onCreate(savedInstanceState);
  122. setContentView(R.layout.activity_login);
  123. preferencesjizhu = getSharedPreferences("user2", Context.MODE_PRIVATE);
  124. preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
  125. SysApplication.getInstance().addActivity(this);
  126. initview();
  127. ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
  128. ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
  129. if (TextUtils.isEmpty(ServerAddress_duankou)) {
  130. ServerAddress_duankou = "80";
  131. }
  132. zhangtao = preferences.getString("AccountCode", "");
  133. name = preferences.getString("UserCode", "").toUpperCase();
  134. password = preferences.getString("UserPassword", "");
  135. login_zhangtao.setText(zhangtao);
  136. login_yonghu.setText(name);
  137. login_zhangtao.setSelection(login_zhangtao.length());
  138. login_yonghu.setSelection(login_yonghu.length());
  139. String changeFlag = preferencesjizhu.getString("changeuser", "");
  140. if (changeFlag.equals("1")) {
  141. Editor edit = preferencesjizhu.edit();
  142. edit.putString("changeuser", "0");
  143. edit.commit();
  144. login_yonghu.setFocusable(true);
  145. login_yonghu.requestFocus();
  146. } else {
  147. if (login_zhangtao.getText().toString().equals("") && login_yonghu.getText().toString().equals("")
  148. && login_mima.getText().toString().equals("")) {
  149. login_zhangtao.setFocusable(true);
  150. login_zhangtao.requestFocus();
  151. }
  152. if (!login_zhangtao.getText().toString().equals("") && !login_yonghu.getText().toString().equals("")) {
  153. login_mima.setFocusable(true);
  154. login_mima.requestFocus();
  155. String password = login_mima.getText().toString().trim();
  156. if (!TextUtils.isEmpty(password)) {
  157. login_mima.setSelection(password.length());
  158. }
  159. }
  160. }
  161. if (preferencesjizhu.getBoolean("iszidong", false)) {
  162. auto_login.setBackgroundResource(R.drawable.check_pressed);
  163. login_zhangtao.setText(preferencesjizhu.getString("AccountCode", ""));
  164. login_yonghu.setText(preferencesjizhu.getString("UserCode", "").toUpperCase());
  165. if (preferencesjizhu.getBoolean("isrememberpassword", false)) {
  166. IsRememberPassword = true;
  167. remember_password.setBackgroundResource(R.drawable.check_pressed);
  168. String encryptPassword = preferencesjizhu.getString("UserPassword", "");
  169. PrivateKey privateKey;
  170. try {
  171. InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
  172. privateKey = RSAUtils.loadPrivateKey(inPrivate);
  173. byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
  174. String decryptPassword = new String(decryptByte);
  175. if (TextUtils.isEmpty(decryptPassword)) {
  176. CustomToast.showToast(getApplicationContext(), "请输入密码", 2000);
  177. login_mima.setFocusable(true);
  178. login_mima.requestFocus();
  179. return;
  180. }
  181. login_mima.setText(decryptPassword);
  182. login_mima.setSelection(decryptPassword.length());
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. login(true);
  188. } else {
  189. if (preferencesjizhu.getBoolean("isrememberpassword", false)) {
  190. IsRememberPassword = true;
  191. remember_password.setBackgroundResource(R.drawable.check_pressed);
  192. String encryptPassword = preferencesjizhu.getString("UserPassword", "");
  193. PrivateKey privateKey;
  194. try {
  195. InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
  196. privateKey = RSAUtils.loadPrivateKey(inPrivate);
  197. byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
  198. String decryptPassword = new String(decryptByte);
  199. login_mima.setText(decryptPassword);
  200. login_mima.setSelection(decryptPassword.length());
  201. } catch (Exception e) {
  202. e.printStackTrace();
  203. }
  204. } else {
  205. IsRememberPassword = false;
  206. remember_password.setBackgroundResource(R.drawable.check);
  207. login_mima.setText("");
  208. }
  209. auto_login.setBackgroundResource(R.drawable.check);
  210. }
  211. setoncick();
  212. }
  213. @Override
  214. protected void onResume() {
  215. if (Build.VERSION.SDK_INT >= 23
  216. && getApplicationInfo().targetSdkVersion >= 23) {
  217. if (isNeedCheck) {
  218. checkPermissions(needPermissions);
  219. }
  220. }
  221. super.onResume();
  222. ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
  223. ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
  224. if (TextUtils.isEmpty(ServerAddress_duankou)) {
  225. ServerAddress_duankou = "80";
  226. }
  227. }
  228. /**
  229. *
  230. * @param permissions
  231. * @since 2.5.0 requestPermissions方法是请求某一权限,
  232. */
  233. private void checkPermissions(String... permissions) {
  234. try {
  235. if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) {
  236. List<String> needRequestPermissonList = findDeniedPermissions(permissions);
  237. if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) {
  238. String[] array = needRequestPermissonList.toArray(new String[needRequestPermissonList.size()]);
  239. Method method = getClass().getMethod("requestPermissions",
  240. new Class[] { String[].class, int.class });
  241. method.invoke(this, array, PERMISSON_REQUESTCODE);
  242. }
  243. }
  244. } catch (Throwable e) {
  245. }
  246. }
  247. /**
  248. * 获取权限集中需要申请权限的列表
  249. *
  250. * @param permissions
  251. * @return
  252. * @since 2.5.0 checkSelfPermission方法是在用来判断是否app已经获取到某一个权限
  253. * shouldShowRequestPermissionRationale方法用来判断是否
  254. * 显示申请权限对话框,如果同意了或者不在询问则返回false
  255. */
  256. private List<String> findDeniedPermissions(String[] permissions) {
  257. List<String> needRequestPermissonList = new ArrayList<String>();
  258. if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) {
  259. try {
  260. for (String perm : permissions) {
  261. Method checkSelfMethod = getClass().getMethod("checkSelfPermission", String.class);
  262. Method shouldShowRequestPermissionRationaleMethod = getClass()
  263. .getMethod("shouldShowRequestPermissionRationale", String.class);
  264. if ((Integer) checkSelfMethod.invoke(this, perm) != PackageManager.PERMISSION_GRANTED
  265. || (Boolean) shouldShowRequestPermissionRationaleMethod.invoke(this, perm)) {
  266. needRequestPermissonList.add(perm);
  267. }
  268. }
  269. } catch (Throwable e) {
  270. }
  271. }
  272. return needRequestPermissonList;
  273. }
  274. /**
  275. * 检测是否所有的权限都已经授权
  276. *
  277. * @param grantResults
  278. * @return
  279. * @since 2.5.0
  280. *
  281. */
  282. private boolean verifyPermissions(int[] grantResults) {
  283. for (int result : grantResults) {
  284. if (result != PackageManager.PERMISSION_GRANTED) {
  285. return false;
  286. }
  287. }
  288. return true;
  289. }
  290. /**
  291. * 申请权限结果的回调方法
  292. */
  293. // @TargetApi(23)
  294. // public void onRequestPermissionsResult(int requestCode,
  295. // String[] permissions, int[] paramArrayOfInt) {
  296. // if (requestCode == PERMISSON_REQUESTCODE) {
  297. // if (!verifyPermissions(paramArrayOfInt)) {
  298. // showMissingPermissionDialog();
  299. // isNeedCheck = false;
  300. // }
  301. // }
  302. // }
  303. /**
  304. * 显示提示信息
  305. *
  306. * @since 2.5.0
  307. *
  308. */
  309. private void showMissingPermissionDialog() {
  310. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  311. builder.setTitle("权限申请");
  312. builder.setMessage("缺少相关权限");
  313. // 拒绝, 退出应用
  314. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  315. @Override
  316. public void onClick(DialogInterface dialog, int which) {
  317. finish();
  318. }
  319. });
  320. builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
  321. @Override
  322. public void onClick(DialogInterface dialog, int which) {
  323. startAppSettings();
  324. }
  325. });
  326. builder.setCancelable(false);
  327. builder.show();
  328. }
  329. /**
  330. * 启动应用的设置
  331. *
  332. * @since 2.5.0
  333. *
  334. */
  335. private void startAppSettings() {
  336. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  337. intent.setData(Uri.parse("package:" + getPackageName()));
  338. startActivity(intent);
  339. }
  340. private void initview() {
  341. login_zhangtao = (EditText) findViewById(R.id.login_zhangtao);
  342. login_yonghu = (EditText) findViewById(R.id.login_yonghu);
  343. login_mima = (EditText) findViewById(R.id.login_mima);
  344. auto_login = (ImageView) findViewById(R.id.auto_login);
  345. remember_password = (ImageView) findViewById(R.id.remember_password);
  346. login_login = (Button) findViewById(R.id.login_login);
  347. cancel = (Button) findViewById(R.id.login_set);
  348. autologintext = (TextView) findViewById(R.id.logint1);
  349. rememberPasswordTxt = (TextView) findViewById(R.id.rememberPasswordTxt);
  350. login_mima.setTransformationMethod(PasswordTransformationMethod.getInstance());
  351. setting = (Button) findViewById(R.id.login_set);
  352. }
  353. private void setoncick() {
  354. auto_login.setOnClickListener(this);
  355. login_login.setOnClickListener(this);
  356. cancel.setOnClickListener(this);
  357. setting.setOnClickListener(this);
  358. autologintext.setOnClickListener(this);
  359. remember_password.setOnClickListener(this);
  360. rememberPasswordTxt.setOnClickListener(this);
  361. login_yonghu.addTextChangedListener(new TextWatcher() {
  362. public void onTextChanged(CharSequence text, int start, int before, int count) {
  363. userName = login_yonghu.getText().toString();
  364. // 判断输入的文字是否是小写
  365. for (int i = 0; i < userName.length(); i++) {
  366. if (userName.charAt(i) - 0 >= 97 && userName.charAt(i) - 0 <= 122) {
  367. handler.postDelayed(UpperCase, 300);
  368. }
  369. }
  370. }
  371. @Override
  372. public void beforeTextChanged(CharSequence text, int start, int count, int after) {
  373. }
  374. @Override
  375. public void afterTextChanged(Editable edit) {
  376. }
  377. });
  378. }
  379. Handler h = new Handler() {
  380. @Override
  381. public void handleMessage(Message msg) {
  382. switch (msg.what) {
  383. case 1:
  384. Bundle b = msg.getData();
  385. String content = b.getString("loginResult");
  386. login_login.setEnabled(true);
  387. autologinflag = b.getBoolean("autoLoginFlag");
  388. try {
  389. JSONObject jsonObject = new JSONObject(content);
  390. JSONObject jo = jsonObject.getJSONObject("d");
  391. int status = jo.getInt("Status");
  392. contentlength = jo.getInt("ApkSize");
  393. String message = jo.getString("Message");
  394. String validityEnd = jo.optString("ValidityEnd");
  395. String serverName = jo.optString("ServerName");
  396. if (mHandler != null) {
  397. mHandler.removeCallbacksAndMessages(null);
  398. mHandler.getLooper().quit();
  399. }
  400. if (status == 0) {
  401. Editor editor = preferences.edit();
  402. editor.putString("AccountCode", login_zhangtao.getText().toString());
  403. editor.putString("UserCode", login_yonghu.getText().toString());
  404. editor.putInt("ContentLength", contentlength);
  405. editor.putString("ValidityEnd", validityEnd);
  406. editor.putString("ServiceName", serverName);
  407. String passwd = login_mima.getText().toString().trim();
  408. String md5Password = Md5Utils.encode(passwd);
  409. editor.putString("UserPassword", md5Password);
  410. editor.putString("AccountID", jo.getInt("AccountID") + "");
  411. editor.putString("UserID", jo.getInt("UserID") + "");
  412. editor.putString("UserName", jo.getString("UserName"));
  413. editor.putString("SessionKey", jo.getString("SessionKey"));
  414. editor.putString("IsGroutingWorker", jo.optString("IsGroutingWorker"));
  415. editor.commit();
  416. if (!autologinflag) {
  417. Editor editor2 = preferencesjizhu.edit();
  418. editor2.putString("changeuser", "0");
  419. editor2.putBoolean("isrememberpassword", IsRememberPassword);
  420. editor2.putBoolean("iszidong", IsAutoLogin);
  421. editor2.putString("AccountCode", login_zhangtao.getText().toString());
  422. editor2.putString("UserCode", login_yonghu.getText().toString());
  423. String password = login_mima.getText().toString().trim();
  424. InputStream inPublic = getResources().getAssets().open("rsa_public_key.pem");
  425. PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
  426. // 加密
  427. byte[] encryptByte = RSAUtils.encryptData(password.getBytes(), publicKey);
  428. // 为了方便观察吧加密后的数据用base64加密转一下,要不然看起来是乱码,所以解密是也是要用Base64先转换
  429. String encryptPassword = Base64Utils.encode(encryptByte);
  430. editor2.putString("UserPassword", encryptPassword);
  431. editor2.putString("AccountID", jo.getInt("AccountID") + "");
  432. editor2.putString("UserID", jo.getInt("UserID") + "");
  433. editor2.putString("UserName", jo.getString("UserName"));
  434. editor2.putString("SessionKey", jo.getString("SessionKey"));
  435. editor2.commit();
  436. }
  437. serverApp = jo.getString("AppVersion");
  438. localVersion = CommonUtil.getVersionName(LoginActivity.this);
  439. Pattern pa = Pattern.compile(Constants.ServerVersionReg);
  440. if (!TextUtils.isEmpty(serverApp) && serverApp.length() > 0) {
  441. Matcher ma = pa.matcher(serverApp);
  442. boolean matchresult = ma.matches();
  443. if (matchresult) {
  444. boolean downloadFlag = CommonUtil.CompareServerVersion(localVersion, serverApp);
  445. if (downloadFlag) {
  446. if (contentlength == -1) {
  447. // if (contentlength != -1) {
  448. Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
  449. startActivity(intent);
  450. } else {
  451. dialog = new UpgradeDialog(LoginActivity.this, R.style.MyDialog);
  452. dialog.setDialog(R.layout.upgrade_layout, LoginActivity.this);
  453. dialog.tip.setText("发现新版本,请立刻升级?");
  454. dialog.versionTitle.setText("当前版本:");
  455. dialog.versionDesc.setText(localVersion);
  456. dialog.serverTitle.setText("最新版本:");
  457. dialog.serverDesc.setText(serverApp);
  458. dialog.versionUpgradeInfoTitle.setText("新版本介绍:");
  459. dialog.versionUpgradeInfoTxt.setText(R.string.versionDesc);
  460. dialog.ok.setOnClickListener(new View.OnClickListener() {
  461. @Override
  462. public void onClick(View v) {
  463. ShowDownloadDialog();
  464. }
  465. });
  466. dialog.cancel.setOnClickListener(new View.OnClickListener() {
  467. @Override
  468. public void onClick(View v) {
  469. dialog.dismiss();
  470. android.os.Process.killProcess(android.os.Process.myPid());
  471. }
  472. });
  473. dialog.showDialog();
  474. }
  475. } else {
  476. Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
  477. startActivity(intent);
  478. }
  479. }
  480. } else {
  481. Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
  482. startActivity(intent);
  483. }
  484. ProcessDialogUtils.closeProgressDilog();
  485. }
  486. else {
  487. ProcessDialogUtils.closeProgressDilog();
  488. CommonUtil.showAlertDialog(status, message, LoginActivity.this);
  489. if (status == 1) {
  490. login_zhangtao.setFocusable(true);
  491. login_zhangtao.requestFocus();
  492. return;
  493. } else if (status == 2) {
  494. login_yonghu.setFocusable(true);
  495. login_yonghu.requestFocus();
  496. return;
  497. } else if (status == 3) {
  498. login_mima.setFocusable(true);
  499. login_mima.requestFocus();
  500. return;
  501. }
  502. return;
  503. }
  504. } catch (Exception e) {
  505. e.printStackTrace();
  506. }
  507. break;
  508. case 2:
  509. ProcessDialogUtils.closeProgressDilog();
  510. Bundle b3 = msg.getData();
  511. String message3 = b3.getString("message");
  512. CustomToast.showToast(getApplicationContext(), message3, 2000);
  513. if (mHandler != null) {
  514. mHandler.removeCallbacksAndMessages(null);
  515. mHandler.getLooper().quit();
  516. }
  517. break;
  518. case DOWN_UPDATE:
  519. Bundle bu = msg.getData();
  520. int progress = bu.getInt("process");
  521. mDialog.mProgress.setProgress(progress);
  522. mDialog.setProgressNumber();
  523. mDialog.setProgressPercent();
  524. break;
  525. case DOWN_OVER:
  526. dialog.dismiss();
  527. if (mHandler != null) {
  528. mHandler.removeCallbacksAndMessages(null);
  529. mHandler.getLooper().quit();
  530. }
  531. CommonUtil.installApk(installFilePath, LoginActivity.this);
  532. break;
  533. }
  534. };
  535. };
  536. private void ShowDownloadDialog() {
  537. mDialog = new CommonProgressDialog(this);
  538. mDialog.setIndeterminate(false);
  539. mDialog.setCanceledOnTouchOutside(false);
  540. mDialog.setCancelable(false);
  541. mDialog.show();
  542. thread = new HandlerThread("updateThread", 5);
  543. thread.start();
  544. mHandler = new Handler(thread.getLooper());
  545. UpdateRunnable updateRunnable = new UpdateRunnable();
  546. mHandler.post(updateRunnable);
  547. }
  548. Runnable UpperCase = new Runnable() {
  549. @Override
  550. public void run() {
  551. // 小写转大写
  552. login_yonghu.setText(userName.toUpperCase());
  553. // 设置EditText光标位置
  554. login_yonghu.setSelection(userName.length());
  555. }
  556. };
  557. @Override
  558. public void onClick(View v) {
  559. switch (v.getId()) {
  560. case R.id.auto_login:
  561. if (IsAutoLogin) {
  562. IsAutoLogin = false;
  563. v.setBackgroundResource(R.drawable.check);
  564. } else {
  565. IsAutoLogin = true;
  566. IsRememberPassword = true;
  567. remember_password.setBackgroundResource(R.drawable.check_pressed);
  568. v.setBackgroundResource(R.drawable.check_pressed);
  569. }
  570. break;
  571. case R.id.remember_password:
  572. if (IsRememberPassword) {
  573. IsRememberPassword = false;
  574. v.setBackgroundResource(R.drawable.check);
  575. IsAutoLogin = false;
  576. auto_login.setBackgroundResource(R.drawable.check);
  577. } else {
  578. IsRememberPassword = true;
  579. v.setBackgroundResource(R.drawable.check_pressed);
  580. }
  581. break;
  582. case R.id.login_login:
  583. if (login_zhangtao.getText().toString().equals("")) {
  584. CustomToast.showToast(getApplicationContext(), "请输入帐套", 2000);
  585. login_zhangtao.setFocusable(true);
  586. login_zhangtao.requestFocus();
  587. return;
  588. } else {
  589. if (login_yonghu.getText().toString().equals("")) {
  590. // Toast.makeText(getApplicationContext(), "请输入用户名",
  591. // Toast.LENGTH_SHORT).show();
  592. CustomToast.showToast(getApplicationContext(), "请输入用户名", 2000);
  593. login_yonghu.setFocusable(true);
  594. login_yonghu.requestFocus();
  595. return;
  596. } else {
  597. if (login_mima.getText().toString().equals("")) {
  598. CustomToast.showToast(getApplicationContext(), "请输入密码", 2000);
  599. login_mima.setFocusable(true);
  600. login_mima.requestFocus();
  601. return;
  602. } else if (TextUtils.isEmpty(ServerAddress_ip)) {
  603. CustomToast.showToast(getApplicationContext(), "请输入IP", 2000);
  604. return;
  605. } else if (TextUtils.isEmpty(ServerAddress_duankou)) {
  606. // Toast.makeText(getApplicationContext(), "请输入端口号",
  607. // Toast.LENGTH_SHORT).show();
  608. CustomToast.showToast(getApplicationContext(), "请输入端口号", 2000);
  609. return;
  610. } else {
  611. login(false);
  612. }
  613. }
  614. }
  615. break;
  616. case R.id.login_set:
  617. Intent i = new Intent();
  618. i.setClass(LoginActivity.this, SetActivity.class);
  619. startActivity(i);
  620. break;
  621. case R.id.logint1:
  622. if (IsAutoLogin) {
  623. IsAutoLogin = false;
  624. auto_login.setBackgroundResource(R.drawable.check);
  625. } else {
  626. IsAutoLogin = true;
  627. IsRememberPassword = true;
  628. remember_password.setBackgroundResource(R.drawable.check_pressed);
  629. auto_login.setBackgroundResource(R.drawable.check_pressed);
  630. }
  631. break;
  632. case R.id.rememberPasswordTxt:
  633. if (IsRememberPassword) {
  634. IsRememberPassword = false;
  635. remember_password.setBackgroundResource(R.drawable.check);
  636. IsAutoLogin = false;
  637. auto_login.setBackgroundResource(R.drawable.check);
  638. } else {
  639. IsRememberPassword = true;
  640. remember_password.setBackgroundResource(R.drawable.check_pressed);
  641. }
  642. break;
  643. default:
  644. break;
  645. }
  646. }
  647. public class UpdateRunnable implements Runnable {
  648. @Override
  649. public void run() {
  650. String ServerAddress_ip = preferences.getString("ServerAddress_ip", "");
  651. String ServerAddress_duankou = preferences.getString("ServerAddress_duankou", "");
  652. if (TextUtils.isEmpty(ServerAddress_duankou)) {
  653. ServerAddress_duankou = "80";
  654. }
  655. String accountCode = preferences.getString("AccountCode", "");
  656. String login_userCode = preferences.getString("UserCode", "");
  657. String password = preferences.getString("UserPassword", "");
  658. String sessionkey = preferences.getString("SessionKey", "");
  659. String pPath = CommonUtil.getRootFilePath();
  660. String fileDir = pPath + Constants.DATA_CACHE_PATH;
  661. File file = new File(fileDir);
  662. if (!file.exists()) {
  663. file.mkdir();
  664. }
  665. String filep = "ibossprd.apk";
  666. File f = new File(file.getAbsolutePath(), filep);
  667. if (!f.exists()) {
  668. try {
  669. f.createNewFile();
  670. } catch (IOException e) {
  671. e.printStackTrace();
  672. }
  673. }
  674. try {
  675. installFilePath = f.getAbsolutePath();
  676. JSONObject params = new JSONObject();
  677. params.put("accountCode", accountCode);
  678. params.put("userCode", login_userCode);
  679. params.put("userPassword", password);
  680. params.put("sdk", Build.VERSION.SDK_INT);
  681. downloadFileStream("http://" + ServerAddress_ip + ":" + ServerAddress_duankou + Constants.serverAction
  682. + "/AutoUpgradeNew", installFilePath, params.toString());
  683. } catch (Exception e) {
  684. e.printStackTrace();
  685. Message m = new Message();
  686. m.what = 2;
  687. Bundle b = new Bundle();
  688. b.putString("message", "网络连接异常");
  689. m.setData(b);
  690. h.sendMessage(m);
  691. return;
  692. }
  693. }
  694. }
  695. public void downloadFileStream(String url, String filePath, String content) {
  696. try {
  697. int retry = 0;
  698. while (retry < MAX_RETRY)
  699. try {
  700. seek(0, url, content);
  701. break;
  702. } catch (Exception e) {
  703. retry++;
  704. }
  705. File wdFile = new File(filePath);
  706. output = new FileOutputStream(wdFile);
  707. input = connection.getInputStream();
  708. // 这将是有用的,这样你可以显示一个典型的0-100%的进度条
  709. // int fileLength = connection.getContentLength();
  710. // 下载文件
  711. mDialog.mProgress.setMax(contentlength);
  712. byte data[] = new byte[1024];
  713. int count = 0;
  714. do {
  715. int numread = input.read(data);
  716. count += numread;
  717. Message m = new Message();
  718. Bundle b = new Bundle();
  719. b.putInt("process", count);
  720. m.what = DOWN_UPDATE;
  721. m.setData(b);
  722. h.sendMessage(m);
  723. if (numread <= 0) {
  724. // 下载完成通知安装
  725. h.sendEmptyMessage(DOWN_OVER);
  726. break;
  727. }
  728. output.write(data, 0, numread);
  729. } while (true);
  730. output.flush();
  731. }
  732. catch (Exception e) {
  733. e.printStackTrace();
  734. }
  735. finally {
  736. try {
  737. output.close();
  738. input.close();
  739. } catch (IOException e) {
  740. e.printStackTrace();
  741. }
  742. }
  743. }
  744. public void seek(long start_pos, String conurl, String content) throws IOException {
  745. if (start_pos == cur_pos && input != null)
  746. return;
  747. if (connection != null) {
  748. connection.disconnect();
  749. connection = null;
  750. }
  751. if (input != null) {
  752. input.close();
  753. input = null;
  754. }
  755. url = new URL(conurl);
  756. connection = (HttpURLConnection) url.openConnection();
  757. connection.setRequestProperty("connection", "Keep-Alive");
  758. connection.setRequestMethod("POST");
  759. connection.setUseCaches(false);
  760. connection.setRequestProperty("Content-Type", "application/json");
  761. connection.setDoInput(true);
  762. connection.setConnectTimeout(180 * 1000);
  763. connection.setReadTimeout(180 * 1000);
  764. connection.setDoOutput(true);
  765. connection.setRequestProperty("Accept-Encoding", "identity");
  766. System.setProperty("http.keepAlive", "false");
  767. connection.connect();
  768. outputStream = connection.getOutputStream();
  769. outputStream.write(content.getBytes());
  770. outputStream.close();
  771. // httpConnection.setRequestProperty("Connection", "Keep-Alive");
  772. int responseCode = connection.getResponseCode();
  773. if (responseCode < 200 || responseCode >= 300) {
  774. try {
  775. Thread.sleep(500);
  776. } catch (InterruptedException e) {
  777. e.printStackTrace();
  778. }
  779. throw new IOException("HTTP responseCode=" + responseCode);
  780. }
  781. input = connection.getInputStream();
  782. cur_pos = start_pos;
  783. }
  784. private void login(final boolean iszidong11) {
  785. ProcessDialogUtils.showProcessDialog(LoginActivity.this);
  786. // String Ip = CommonUtil.getIpAddress(LoginActivity.this);
  787. // String Mac = CommonUtil.getMacFromWifi(LoginActivity.this);
  788. String Mac = "00:00:00:00:00:00";
  789. String Ip = "0.0.0.0";
  790. JSONObject params = new JSONObject();
  791. try {
  792. if (iszidong11) {
  793. params.put("accountCode", preferencesjizhu.getString("AccountCode", "").trim());
  794. params.put("userCode", preferencesjizhu.getString("UserCode", "").trim().toUpperCase());
  795. String encryptPassword = preferencesjizhu.getString("UserPassword", "").trim();
  796. // 从文件中得到私钥
  797. InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
  798. PrivateKey privateKey = RSAUtils.loadPrivateKey(inPrivate);
  799. // 因为RSA加密后的内容经Base64再加密转换了一下,所以先Base64解密回来再给RSA解密
  800. byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptPassword), privateKey);
  801. String decryptPassword = new String(decryptByte);
  802. params.put("userPassword", Md5Utils.encode(decryptPassword));
  803. } else {
  804. params.put("accountCode", login_zhangtao.getText().toString());
  805. params.put("userCode", login_yonghu.getText().toString());
  806. params.put("userPassword", Md5Utils.encode(login_mima.getText().toString().trim()));
  807. }
  808. params.put("macAddress", Mac);
  809. params.put("ipAddress", Ip);
  810. params.put("phoneCode", "android" );//Installation.id(LoginActivity.this) 2024年6月17日 安卓14
  811. params.put("phoneType", android.os.Build.MODEL);
  812. params.put("appVersion", verName);
  813. params.put("systemType", "Android");
  814. params.put("systemVersion", android.os.Build.VERSION.RELEASE);
  815. thread = new HandlerThread("loginThread", 5);
  816. thread.start();
  817. mHandler = new Handler(thread.getLooper());
  818. LoginRunnable loginRunnable = new LoginRunnable(iszidong11, obj, params);
  819. mHandler.post(loginRunnable);
  820. } catch (Exception e) {
  821. e.printStackTrace();
  822. }
  823. }
  824. public class LoginRunnable implements Runnable {
  825. private JSONObject param;
  826. private boolean autologinflag;
  827. private Object obj;
  828. public LoginRunnable(boolean loginflag, Object obj, JSONObject param) {
  829. autologinflag = loginflag;
  830. this.obj = obj;
  831. this.param = param;
  832. }
  833. @Override
  834. public void run() {
  835. synchronized (obj) {
  836. try {
  837. client = new WebClient();
  838. result = client.doPost("http://" + ServerAddress_ip + ":" + ServerAddress_duankou
  839. + Constants.serverAction + "/DoPDALogin", param.toString(), "application/json");
  840. Message m = new Message();
  841. m.what = 1;
  842. Bundle b = new Bundle();
  843. b.putBoolean("autoLoginFlag", autologinflag);
  844. b.putString("loginResult", result);
  845. m.setData(b);
  846. h.sendMessage(m);
  847. } catch (Exception e) {
  848. e.printStackTrace();
  849. Message m = new Message();
  850. m.what = 2;
  851. Bundle b = new Bundle();
  852. b.putString("message", "网络连接异常");
  853. m.setData(b);
  854. h.sendMessage(m);
  855. }
  856. }
  857. }
  858. }
  859. @Override
  860. public boolean dispatchTouchEvent(MotionEvent ev) {
  861. switch (ev.getAction()) {
  862. case MotionEvent.ACTION_DOWN:
  863. View view = getCurrentFocus();
  864. CommonUtil.hideKeyboard(ev, view, LoginActivity.this);// 调用方法判断是否需要隐藏键盘
  865. break;
  866. default:
  867. break;
  868. }
  869. return super.dispatchTouchEvent(ev);
  870. }
  871. @Override
  872. public Resources getResources() {
  873. Resources res = super.getResources();
  874. Configuration config = new Configuration();
  875. config.setToDefaults();
  876. res.updateConfiguration(config, res.getDisplayMetrics());
  877. return res;
  878. }
  879. }