61f649e214998931cb4bea3c6c76dd169cddb214.svn-base 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package eVVM.apk.ui.vaccination;
  2. import android.content.Intent;
  3. import android.location.Location;
  4. import android.nfc.NdefMessage;
  5. import android.nfc.NdefRecord;
  6. import android.nfc.NfcAdapter;
  7. import android.os.Bundle;
  8. import android.os.Parcelable;
  9. import android.view.View;
  10. import android.view.animation.Animation;
  11. import android.view.animation.LinearInterpolator;
  12. import android.view.animation.RotateAnimation;
  13. import android.widget.ImageView;
  14. import java.math.BigInteger;
  15. import java.util.Arrays;
  16. import java.util.LinkedList;
  17. import butterknife.BindView;
  18. import butterknife.OnClick;
  19. import eVVM.apk.R;
  20. import eVVM.apk.helper.GPS.GPSLocationListener;
  21. import eVVM.apk.helper.GPS.GPSLocationManager;
  22. import eVVM.apk.helper.SPUtils;
  23. import eVVM.apk.helper.SoundPoolHelper;
  24. import eVVM.apk.ui.bean.VnDetailBean;
  25. import eVVM.apk.ui.home.BaseNfcActivity;
  26. import eVVM.apk.ui.home.uploadChip.UploadChipContract;
  27. public class ReadingActivity extends BaseNfcActivity implements UploadChipContract.View {
  28. private GPSLocationManager gpsLocationManager;
  29. private Location location;
  30. private boolean isWarning = false;
  31. @BindView(R.id.reading__line)
  32. ImageView imgreadline;
  33. @Override
  34. protected int getLayoutId() {
  35. return R.layout.activity_reading;
  36. }
  37. @Override
  38. protected int getTitleId() {
  39. return R.id.reading_title;
  40. }
  41. @Override
  42. protected void initView() {
  43. getAnimationSao(); //进行雷达扫描动画
  44. soundPoolHelper = new SoundPoolHelper(4,SoundPoolHelper.TYPE_MUSIC)
  45. .setRingtoneType(SoundPoolHelper.RING_TYPE_MUSIC)
  46. .loadDefault(ReadingActivity.this)
  47. .load(ReadingActivity.this,"factory_operator_success",R.raw.factory_operator_success)
  48. .load(ReadingActivity.this,"factory_operator_error",R.raw.factory_operator_error);
  49. }
  50. private void getAnimationSao() {
  51. Animation animation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  52. animation.setFillAfter(true);
  53. animation.setDuration(2000); //两秒转一圈
  54. animation.setInterpolator(new LinearInterpolator());
  55. animation.setRepeatCount(Animation.INFINITE); //循环执行
  56. imgreadline.startAnimation(animation);
  57. }
  58. @OnClick({R.id.again_bt})
  59. public void onViewClicked(View view) {
  60. switch (view.getId()) {
  61. case R.id.again_bt:
  62. startActivityFinish(QrCodeActivity.class);
  63. break;
  64. }
  65. }
  66. @Override
  67. protected void initData() {
  68. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  69. gpsLocationManager = GPSLocationManager.getInstances(ReadingActivity.this);
  70. }
  71. private SoundPoolHelper soundPoolHelper;
  72. /**
  73. * 读取NFC标签文本数据
  74. */
  75. private String readNfcTag(Intent intent) {
  76. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
  77. Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES);
  78. NdefMessage msgs[] = null;
  79. if (rawMsgs != null) {
  80. msgs = new NdefMessage[rawMsgs.length];
  81. for (int i = 0; i < rawMsgs.length; i++) {
  82. msgs[i] = (NdefMessage) rawMsgs[i];
  83. }
  84. }
  85. try {
  86. if (msgs != null) {
  87. NdefRecord record = msgs[0].getRecords()[0];
  88. return parseTextRecord(record);
  89. }
  90. } catch (Exception e) {
  91. }
  92. }
  93. return null;
  94. }
  95. public String parseTextRecord(NdefRecord ndefRecord) {
  96. if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
  97. return null;
  98. }
  99. if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
  100. return null;
  101. }
  102. try {
  103. byte[] bArray = ndefRecord.getPayload();
  104. StringBuffer sb = new StringBuffer(bArray.length);
  105. String sTemp;
  106. for (int i = 0; i < bArray.length; i++) {
  107. sTemp = Integer.toHexString(0xFF & bArray[i]);
  108. if (sTemp.length() < 2)
  109. sb.append(0);
  110. sb.append(sTemp.toUpperCase());
  111. }
  112. alarmDetection(bArray,sb.substring(6,chipNumberEndIndex));
  113. return sb.toString();
  114. } catch (Exception e) {
  115. throw new IllegalArgumentException();
  116. }
  117. }
  118. @Override
  119. public void onNewIntent(Intent intent) {
  120. isWarning = false;
  121. String textRecord = readNfcTag(intent);
  122. if(textRecord != null && textRecord.length() > 56 * 2){
  123. if(location != null){
  124. String inoculatorIds = (String) SPUtils.get("INOCULATOR_LIST","");
  125. if(inoculatorIds.equals("")){
  126. // toast("无受种者信息");
  127. }
  128. String chipNumber = textRecord.substring(6,chipNumberEndIndex);
  129. getPresenter().forDoctor(chipNumber,textRecord,inoculatorIds,location.getLongitude(),location.getLatitude());
  130. }else{
  131. toast("位置信息获取失败");
  132. }
  133. }else{
  134. toast("芯片识别错误");
  135. }
  136. }
  137. @Override
  138. public void uploadChipError(String msg) {
  139. // if(msg.equals("芯片不存在")){
  140. // soundPoolHelper.play("factory_operator_error",false);
  141. // }
  142. // toast(msg);
  143. //保存未提交信息
  144. SPUtils.remove("INOCULATOR_LIST");
  145. }
  146. @Override
  147. public void uploadChipSuccess(VnDetailBean data) {
  148. VnDetailBean.DataBean dataBean = data.getData();
  149. soundPoolHelper.play("factory_operator_success",false);
  150. SPUtils.remove("INOCULATOR_LIST");
  151. Intent in = new Intent(ReadingActivity.this, VnReportDetailForDoctorActivity.class);
  152. in.putExtra("dataBean",dataBean);
  153. in.putExtra("isWarning",isWarning);
  154. startActivityFinish(in);
  155. }
  156. //判断是否报警
  157. public void alarmDetection(byte[] bytes,String chipNumber){
  158. try{
  159. //将byte[]转为各种进制的字符串
  160. String binaryStr = new BigInteger(1, bytes).toString(2);
  161. int startIndex = (3 + 16 + 16 + 1 + 4 + 7 ) * 4 + 1 * 4 ; //减去前边非温度部分和启用后的一小时
  162. int marginRight = 2 * 4; //减去疫苗使用前的两小时
  163. String binaryArr[] = new String[(binaryStr.length() - marginRight) / 2 - startIndex];
  164. for (int i = startIndex; i < (binaryStr.length() - marginRight) / 2; i++) {
  165. binaryArr[i - startIndex] = String.valueOf(binaryStr.charAt(i * 2)) + binaryStr.charAt(i * 2 + 1);
  166. }
  167. LinkedList<Integer> exceedingIndexs = new LinkedList<Integer>();
  168. exceedingIndexs.add(-1);
  169. for (int i = 0; i < binaryArr.length; i++) { //不计算开始的一小时和最后两小时
  170. if(!binaryArr[i].equals("01")){
  171. if(i != 0 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i)){
  172. exceedingIndexs.add(i);
  173. }
  174. if(i != binaryArr.length - 1 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i + 1)){
  175. exceedingIndexs.add(i + 1);
  176. }
  177. }
  178. }
  179. // 48小时
  180. int type = Integer.parseInt(chipNumber.substring(1,2), 16);
  181. int category = Integer.parseInt(SPUtils.get("categorytimes_" + type,48).toString());
  182. if((exceedingIndexs.size() - 1) / 2.f > category){
  183. // toast("报警");
  184. isWarning = true;
  185. }else{
  186. // toast("正常");
  187. }
  188. }catch (Exception e){
  189. e.printStackTrace();
  190. }
  191. }
  192. @Override
  193. protected void onStart() {
  194. super.onStart();
  195. //开启定位
  196. gpsLocationManager.start(new ReadingActivity.MyListener());
  197. }
  198. @Override
  199. protected void onStop() {
  200. super.onStop();
  201. //终止定位
  202. gpsLocationManager.stop();
  203. }
  204. @Override
  205. protected void onDestroy() {
  206. super.onDestroy();
  207. }
  208. class MyListener implements GPSLocationListener {
  209. @Override
  210. public void UpdateLocation(Location _location) {
  211. if (_location != null) {
  212. location = _location;
  213. }
  214. }
  215. @Override
  216. public void UpdateStatus(String provider, int status, Bundle extras) {
  217. if ("gps" == provider) {
  218. //Toast.makeText(FactoryOperatorActivity.this, "定位类型:" + provider, Toast.LENGTH_SHORT).show();
  219. }
  220. }
  221. @Override
  222. public void UpdateGPSProviderStatus(int gpsStatus) {
  223. }
  224. }
  225. //
  226. // @Override
  227. // public void onBackPressed() {
  228. // if (DoubleClickHelper.isOnDoubleClick()) {
  229. // //移动到上一个任务栈,避免侧滑引起的不良反应
  230. // moveTaskToBack(false);
  231. // postDelayed(new Runnable() {
  232. //
  233. // @Override
  234. // public void run() {
  235. // // 进行内存优化,销毁掉所有的界面
  236. // ActivityStackManager.getInstance().finishAllActivities();
  237. // // 销毁进程
  238. // System.exit(0);
  239. // }
  240. // }, 300);
  241. // } else {
  242. // toast(getResources().getString(R.string.home_exit_hint));
  243. // }
  244. // }
  245. @Override
  246. public boolean isSupportSwipeBack() {
  247. // 不使用侧滑功能
  248. return false;
  249. }
  250. }