e54f2ac190f6a0f5a4122eb2db21310e418d9722.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package eVVM.apk.ui.home;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorSet;
  4. import android.animation.ObjectAnimator;
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.content.res.Resources;
  8. import android.location.Location;
  9. import android.nfc.NdefMessage;
  10. import android.nfc.NdefRecord;
  11. import android.nfc.NfcAdapter;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.os.Parcelable;
  15. import android.os.Bundle;
  16. import android.util.DisplayMetrics;
  17. import android.util.Log;
  18. import android.view.View;
  19. import com.hjq.bar.OnTitleBarListener;
  20. import com.hjq.bar.TitleBar;
  21. import com.hjq.base.BaseDialog;
  22. import com.hjq.base.BaseDialogFragment;
  23. import com.hjq.dialog.MessageDialog;
  24. import android.view.animation.AlphaAnimation;
  25. import android.view.animation.Animation;
  26. import android.view.animation.CycleInterpolator;
  27. import android.view.animation.DecelerateInterpolator;
  28. import android.view.animation.LinearInterpolator;
  29. import android.view.animation.RotateAnimation;
  30. import android.widget.ImageView;
  31. import android.widget.ProgressBar;
  32. import android.widget.RelativeLayout;
  33. import java.util.Arrays;
  34. import butterknife.BindView;
  35. import eVVM.apk.R;
  36. import eVVM.apk.helper.SPUtils;
  37. import eVVM.apk.helper.SoundPoolHelper;
  38. import eVVM.apk.ui.bean.VnDetailBean;
  39. import eVVM.apk.ui.home.uploadChip.UploadChipContract;
  40. import eVVM.apk.helper.ActivityStackManager;
  41. import eVVM.apk.helper.DoubleClickHelper;
  42. import eVVM.apk.ui.login.LoginActivity;
  43. import eVVM.apk.ui.vaccination.ReadingActivity;
  44. /**
  45. * 厂家操作员登录页面
  46. */
  47. public class FactoryOperatorActivity extends BaseNfcActivity implements UploadChipContract.View {
  48. private String textRecord;
  49. @BindView(R.id.reading_title)
  50. TitleBar readingTitle;
  51. @BindView(R.id.reading__line)
  52. ImageView imgreadline;
  53. @BindView(R.id.fly_phonerl)
  54. RelativeLayout flyphonerl;
  55. private Animation animation;
  56. private AlphaAnimation alp;
  57. private AnimatorSet animatorSetsuofang;
  58. private AnimatorSet animatorSetsuofang2;
  59. private BaseDialog loadDiago;
  60. private ProgressBar readpb;
  61. @Override
  62. protected int getLayoutId() {
  63. return R.layout.activity_factory_operator;
  64. }
  65. @Override
  66. protected int getTitleId() {
  67. return R.id.reading_title;
  68. }
  69. @Override
  70. protected void initView() {
  71. getAnimationSao(); //进行雷达扫描动画
  72. soundPoolHelper = new SoundPoolHelper(4, SoundPoolHelper.TYPE_MUSIC)
  73. .setRingtoneType(SoundPoolHelper.RING_TYPE_MUSIC)
  74. .loadDefault(FactoryOperatorActivity.this)
  75. .load(FactoryOperatorActivity.this, "factory_operator_success", R.raw.factory_operator_success)
  76. .load(FactoryOperatorActivity.this, "factory_operator_error", R.raw.factory_operator_error)
  77. .load(FactoryOperatorActivity.this, "nuliduquzhong", R.raw.nuliduquzhong);
  78. readingTitle.setOnTitleBarListener(new OnTitleBarListener() {
  79. @Override
  80. public void onLeftClick(View v) {
  81. }
  82. @Override
  83. public void onTitleClick(View v) {
  84. }
  85. @Override
  86. public void onRightClick(View v) {
  87. getExit();
  88. //toast("tuichu");
  89. }
  90. });
  91. int flyPhoneType = (int) SPUtils.get("FlyPhoneType", 0);
  92. if (flyPhoneType == 0) {
  93. getFlyPhone();
  94. SPUtils.put("FlyPhoneType", 1);
  95. } else {
  96. flyphonerl.setVisibility(View.GONE);
  97. }
  98. }
  99. private void getFlyPhone() {
  100. Resources resources = this.getResources();
  101. DisplayMetrics dm = resources.getDisplayMetrics();
  102. int height3 = dm.heightPixels;
  103. //组合动画
  104. animatorSetsuofang = new AnimatorSet();
  105. // ObjectAnimator scaleX = ObjectAnimator.ofFloat(scohhh, "translationX", 1.5f, 1f);
  106. ObjectAnimator scaleY = ObjectAnimator.ofFloat(flyphonerl, "translationY", height3, flyphonerl.getY());
  107. animatorSetsuofang.setDuration(1200);
  108. animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
  109. animatorSetsuofang.play(scaleY);
  110. animatorSetsuofang.start();
  111. animatorSetsuofang.addListener(new Animator.AnimatorListener() {
  112. @Override
  113. public void onAnimationStart(Animator animation) {
  114. }
  115. @Override
  116. public void onAnimationEnd(Animator animation) {
  117. postDelayed(new Runnable() {
  118. @Override
  119. public void run() {
  120. //flyphonerl.setVisibility(View.GONE);
  121. animatorSetsuofang.cancel();
  122. getFlyGoBack();
  123. }
  124. }, 5000);
  125. }
  126. @Override
  127. public void onAnimationCancel(Animator animation) {
  128. }
  129. @Override
  130. public void onAnimationRepeat(Animator animation) {
  131. }
  132. });
  133. }
  134. private void getFlyGoBack() {
  135. Resources resources = this.getResources();
  136. DisplayMetrics dm = resources.getDisplayMetrics();
  137. int height3 = dm.heightPixels;
  138. //组合动画
  139. animatorSetsuofang2 = new AnimatorSet();
  140. // ObjectAnimator scaleX = ObjectAnimator.ofFloat(scohhh, "translationX", 1.5f, 1f);
  141. ObjectAnimator scaleY = ObjectAnimator.ofFloat(flyphonerl, "translationY", 0, height3);
  142. animatorSetsuofang2.setDuration(1500);
  143. animatorSetsuofang2.setInterpolator(new DecelerateInterpolator());
  144. animatorSetsuofang2.play(scaleY);
  145. animatorSetsuofang2.start();
  146. animatorSetsuofang2.addListener(new Animator.AnimatorListener() {
  147. @Override
  148. public void onAnimationStart(Animator animation) {
  149. }
  150. @Override
  151. public void onAnimationEnd(Animator animation) {
  152. flyphonerl.setVisibility(View.GONE);
  153. animatorSetsuofang2.cancel();
  154. }
  155. @Override
  156. public void onAnimationCancel(Animator animation) {
  157. }
  158. @Override
  159. public void onAnimationRepeat(Animator animation) {
  160. }
  161. });
  162. }
  163. private void getExit() {
  164. new MessageDialog.Builder(this)
  165. .setTitle("") // 标题可以不用填写
  166. .setMessage("您确定要退出吗")
  167. .setConfirm("确定")
  168. .setCancel("取消") // 设置 null 表示不显示取消按钮
  169. //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
  170. .setListener(new MessageDialog.OnListener() {
  171. @Override
  172. public void onConfirm(Dialog dialog) {
  173. //退出的操作
  174. startActivity(LoginActivity.class);
  175. SPUtils.logout();
  176. // 进行内存优化,销毁掉所有的界面
  177. ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
  178. }
  179. @Override
  180. public void onCancel(Dialog dialog) {
  181. //toast("取消了");
  182. }
  183. })
  184. .show();
  185. }
  186. private void getAnimationSao() {
  187. animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  188. animation.setFillAfter(true);
  189. animation.setDuration(2000); //两秒转一圈
  190. animation.setInterpolator(new LinearInterpolator());
  191. animation.setRepeatCount(Animation.INFINITE); //循环执行
  192. imgreadline.startAnimation(animation);
  193. int[] antenna = new int[]{R.id.reading_antenna1, R.id.reading_antenna2, R.id.reading_antenna3, R.id.reading_antenna4, R.id.reading_antenna5, R.id.reading_antenna6,
  194. R.id.reading_antenna7, R.id.reading_antenna8, R.id.reading_antenna9};
  195. int nfcWireIndex = 2;
  196. try {
  197. nfcWireIndex = Integer.parseInt(String.valueOf(SPUtils.get("PHONE_MODEL", "2")));
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. }
  201. alp = new AlphaAnimation(1, 0.2f);
  202. alp.setStartOffset(500);
  203. alp.setDuration(1000);
  204. alp.setFillAfter(false);
  205. alp.setInterpolator(new CycleInterpolator(2));
  206. alp.setRepeatCount(Animation.INFINITE);
  207. findViewById(antenna[nfcWireIndex - 1]).setAnimation(alp);
  208. alp.start();
  209. }
  210. @Override
  211. protected void initData() {
  212. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  213. // gpsLocationManager = GPSLocationManager.getInstances(FactoryOperatorActivity.this);
  214. }
  215. //记录ProgressBar的完成进度
  216. private int status = 0; //进度条进度
  217. private int hasDate = 0;//每次快进多少
  218. //创建一个负责更新进度的Handler
  219. private Handler mHandler = new Handler() {
  220. @Override
  221. public void handleMessage(Message msg) {
  222. //表明消息是由该程序发送的
  223. if (msg.what == 0x111) {
  224. readpb.setProgress(status);
  225. if (status == 100) {
  226. loadDiago.dismiss();
  227. }
  228. }
  229. }
  230. };
  231. @Override
  232. public void onNewIntent(Intent intent) {
  233. readNfcTag(intent);
  234. Log.e("***", textRecord);
  235. if (textRecord != null) {
  236. hasDate = 0;
  237. status = 0;
  238. soundPoolHelper.play("nuliduquzhong", false);
  239. loadDiago = new BaseDialogFragment.Builder(FactoryOperatorActivity.this)
  240. .setContentView(R.layout.progressreader)
  241. .setAnimStyle(BaseDialog.AnimStyle.SCALE)
  242. //.setText(id, "我是预设置的文本")
  243. .addOnShowListener(new BaseDialog.OnShowListener() {
  244. @Override
  245. public void onShow(BaseDialog dialog) {
  246. //toast("Dialog 显示了");
  247. }
  248. })
  249. .addOnCancelListener(new BaseDialog.OnCancelListener() {
  250. @Override
  251. public void onCancel(BaseDialog dialog) {
  252. //toast("Dialog 取消了");
  253. }
  254. })
  255. .addOnDismissListener(new BaseDialog.OnDismissListener() {
  256. @Override
  257. public void onDismiss(BaseDialog dialog) {
  258. //进度条走完 进行加载
  259. if (textRecord.length() > 94) {
  260. if (SPUtils.contains("LOCATION")) {
  261. Log.e("LOCATION", SPUtils.get("LOCATION", "").toString());
  262. String location[] = SPUtils.get("LOCATION", "").toString().split(",");
  263. if (location.length > 1) {
  264. Double latitude = Double.parseDouble(location[0]);
  265. Double longitude = Double.parseDouble(location[1]);
  266. String chipNumber = textRecord.substring(6, chipNumberEndIndex);
  267. getPresenter().forFactoryOperator(chipNumber, textRecord, longitude, latitude);
  268. } else {
  269. toast("位置信息错误");
  270. }
  271. } else {
  272. toast("位置信息获取失败");
  273. }
  274. } else {
  275. toast("读取失败,请重试");
  276. }
  277. }
  278. })
  279. .show();
  280. readpb = loadDiago.findViewById(R.id.read_progress);
  281. //启动线程来执行任务
  282. new Thread() {
  283. @Override
  284. public void run() {
  285. super.run();
  286. while (status < 100) {
  287. //获取耗时操作的完成百分比
  288. status = doWork();
  289. //发送消息
  290. mHandler.sendEmptyMessage(0x111);
  291. }
  292. }
  293. }.start();
  294. }
  295. }
  296. //进行进度条加载
  297. private int doWork() {
  298. //为元素赋值
  299. hasDate += 10;
  300. try {
  301. Thread.sleep(200);
  302. } catch (InterruptedException e) {
  303. e.printStackTrace();
  304. }
  305. return hasDate;
  306. }
  307. private SoundPoolHelper soundPoolHelper;
  308. @Override
  309. public void uploadChipError(String msg) {
  310. if (msg.equals("芯片不存在")) {
  311. soundPoolHelper.play("factory_operator_error", false);
  312. }
  313. toast(msg);
  314. }
  315. @Override
  316. public void uploadChipSuccess(VnDetailBean data) {
  317. soundPoolHelper.play("factory_operator_success", false);
  318. toast("读取成功");
  319. Log.e("FactoryOperatorActivity", data.getCode() + "");
  320. }
  321. /**
  322. * 读取NFC标签文本数据
  323. */
  324. private void readNfcTag(Intent intent) {
  325. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
  326. Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
  327. NfcAdapter.EXTRA_NDEF_MESSAGES);
  328. NdefMessage msgs[] = null;
  329. int contentSize = 0;
  330. if (rawMsgs != null) {
  331. msgs = new NdefMessage[rawMsgs.length];
  332. for (int i = 0; i < rawMsgs.length; i++) {
  333. msgs[i] = (NdefMessage) rawMsgs[i];
  334. contentSize += msgs[i].toByteArray().length;
  335. }
  336. }
  337. try {
  338. if (msgs != null) {
  339. NdefRecord record = msgs[0].getRecords()[0];
  340. textRecord = parseTextRecord(record);
  341. // mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
  342. }
  343. } catch (Exception e) {
  344. }
  345. }
  346. }
  347. /**
  348. * 解析NDEF文本数据
  349. */
  350. public static String parseTextRecord(NdefRecord ndefRecord) {
  351. /**
  352. * 判断数据是否为NDEF格式
  353. */
  354. //判断TNF
  355. if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
  356. return null;
  357. }
  358. //判断可变的长度的类型
  359. if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
  360. return null;
  361. }
  362. try {
  363. byte[] bArray = ndefRecord.getPayload();
  364. StringBuffer sb = new StringBuffer(bArray.length);
  365. String sTemp;
  366. for (int i = 0; i < bArray.length; i++) {
  367. sTemp = Integer.toHexString(0xFF & bArray[i]);
  368. if (sTemp.length() < 2)
  369. sb.append(0);
  370. sb.append(sTemp.toUpperCase());
  371. }
  372. return sb.toString();
  373. } catch (Exception e) {
  374. throw new IllegalArgumentException();
  375. }
  376. }
  377. @Override
  378. protected void onStart() {
  379. super.onStart();
  380. }
  381. @Override
  382. protected void onStop() {
  383. super.onStop();
  384. //终止定位
  385. // gpsLocationManager.stop();
  386. }
  387. @Override
  388. protected void onDestroy() {
  389. animation.cancel();
  390. alp.cancel();
  391. mHandler.removeCallbacksAndMessages(true);
  392. super.onDestroy();
  393. }
  394. @Override
  395. public void onBackPressed() {
  396. if (DoubleClickHelper.isOnDoubleClick()) {
  397. //移动到上一个任务栈,避免侧滑引起的不良反应
  398. moveTaskToBack(false);
  399. postDelayed(new Runnable() {
  400. @Override
  401. public void run() {
  402. // 进行内存优化,销毁掉所有的界面
  403. ActivityStackManager.getInstance().finishAllActivities();
  404. // 销毁进程
  405. System.exit(0);
  406. }
  407. }, 300);
  408. } else {
  409. toast(getResources().getString(R.string.home_exit_hint));
  410. }
  411. }
  412. @Override
  413. public boolean isSupportSwipeBack() {
  414. // 不使用侧滑功能
  415. return false;
  416. }
  417. }