fea9e69d4d71cbd18890cedb2b428941129307b5.svn-base 16 KB

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