DetectionDestroyOutboundMainActivity.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package com.fxy.detection;
  2. import android.annotation.SuppressLint;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.view.KeyEvent;
  13. import android.view.View;
  14. import android.view.inputmethod.EditorInfo;
  15. import android.widget.EditText;
  16. import android.widget.ImageButton;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import com.elvishew.xlog.XLog;
  20. import com.fxy.R;
  21. import com.fxy.baselibrary.base.BaseActivity;
  22. import com.fxy.baselibrary.bean.BaseEventBusBean;
  23. import com.fxy.baselibrary.interfaces.OnRxScanerListener;
  24. import com.fxy.baselibrary.qrcode.ActivityScanerCode;
  25. import com.fxy.baselibrary.util.JsonUtil;
  26. import com.fxy.bean.ActionBean;
  27. import com.fxy.bean.ScanBean;
  28. import com.fxy.constant.BaseConfig;
  29. import com.fxy.constant.EventCode;
  30. import com.fxy.net.MyDialogCallback;
  31. import com.fxy.net.Urls;
  32. import com.fxy.view.FloatingImageView;
  33. import com.google.zxing.Result;
  34. import com.lzy.okgo.OkGo;
  35. import com.lzy.okgo.model.Response;
  36. import org.json.JSONObject;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import butterknife.BindView;
  40. import butterknife.ButterKnife;
  41. import butterknife.OnClick;
  42. import butterknife.Unbinder;
  43. import sto.android.app.StoJNI;
  44. import sto.android.app.StoPdaKeyEvent;
  45. import sto.android.app.StoTongJNI;
  46. public class DetectionDestroyOutboundMainActivity extends BaseActivity implements StoJNI.ScanCallBack {
  47. /**
  48. * 列表适配器
  49. */
  50. Context mContext = this;
  51. //单号
  52. @BindView(R.id.et_internals_code)
  53. EditText etInternalsCode;
  54. //单号
  55. @BindView(R.id.et_destroy_batch_number)
  56. EditText etDestroyBatchNumber;
  57. //扫码扫码动作
  58. private ScanBean scanBean;
  59. //浮动按钮
  60. @BindView(R.id.iv_drag)
  61. FloatingImageView iv_drag;
  62. private Unbinder unbinder;
  63. @Override
  64. public int getContentViewResId() {
  65. return R.layout.activity_fxy_detection_destroy_outbound;
  66. }
  67. @Override
  68. public boolean showToolBar() {
  69. return true;
  70. }
  71. @Override
  72. public boolean openEventBus() {
  73. return true;
  74. }
  75. @Override
  76. protected void getBundleExtras(Bundle bundle) {
  77. }
  78. //finish的返回监听
  79. //这里的requestCode参数,就是上面设置的 1 ,当跳转的页面返回的时候,通过这个加以判断
  80. //resultCode ,这个参数是在跳转的页面里面规定的,它也是一个int类型的标志
  81. //第三个参数包含了返回的值
  82. //如果不需要所跳转的页面返回值,也就不需要这个方法了
  83. @Override
  84. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  85. super.onActivityResult(requestCode, resultCode, data);
  86. }
  87. /**
  88. * EventBus接收信息的方法,开启后才会调用(非粘性事件)
  89. *
  90. * @param event
  91. */
  92. @Override
  93. protected void EventBean(BaseEventBusBean event) {
  94. // XLog.e("----------接收返回--------------");
  95. // XLog.e("接收返回:"+event.getEventCode());
  96. if (event != null && event.getEventCode() == EventCode.displacement_refresh) {
  97. }
  98. }
  99. @Override
  100. public boolean isPlayMusic() {
  101. return true;
  102. }
  103. @Override
  104. protected void initView() {
  105. unbinder = ButterKnife.bind(this);
  106. setTitleName("销毁出库");
  107. initAdapter();
  108. initData();
  109. initEdit();
  110. //监听浮动按钮
  111. iv_drag.setOnClickListener(new View.OnClickListener() {
  112. @Override
  113. public void onClick(View v) {
  114. if (!iv_drag.isDrag()) {
  115. ActivityScanerCode.setScanerListener(mScanerListener);
  116. ActivityScanerCode.intent2Activity(mContext, BaseConfig.COMMON_SCANNER_ONLY);
  117. }
  118. }
  119. });
  120. }
  121. private void initData() {
  122. }
  123. private void initAdapter() {
  124. }
  125. @Override
  126. public boolean dispatchKeyEvent(KeyEvent event) {
  127. //XLog.e("event:"+event.getKeyCode());
  128. // if ((event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER ) && event.getAction() == KeyEvent.ACTION_DOWN){
  129. // //按确定键
  130. // if(validate()){
  131. // doPutIn();
  132. // }
  133. // return true;
  134. // }
  135. return super.dispatchKeyEvent(event);
  136. }
  137. //监听按键
  138. public boolean onKeyDown(int keyCode, KeyEvent event) {
  139. //XLog.e("监听按键:"+keyCode);XLog.e(event);
  140. if (scanBean.getIsBroadcast() == 0 && StoTongJNI.getIns(mContext).getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_SCAN
  141. && event.getRepeatCount() == 0) {
  142. if (StoTongJNI.getIns(mContext).GetScannerIsScanning()) {
  143. //
  144. } else {
  145. StoTongJNI.getIns(mContext).SetScannerStart();
  146. }
  147. }
  148. return super.onKeyDown(keyCode, event);
  149. }
  150. @SuppressLint("HandlerLeak")
  151. Handler scanHandler = new Handler() {
  152. @Override
  153. public void handleMessage(Message msg) {
  154. // TODO Auto-generated method stub
  155. if (msg.obj != null) {
  156. String scanResult = msg.obj.toString().trim();
  157. if (!scanResult.isEmpty()) {
  158. setScanResult(scanResult);
  159. }
  160. }
  161. }
  162. };
  163. @Override
  164. public void onScanResults(String str) {
  165. Message msg = new Message();
  166. msg.obj = str;
  167. scanHandler.sendMessage(msg);
  168. }
  169. @Override
  170. public void onScanResults(String str, int type) {
  171. }
  172. /**
  173. * 定义广播接受
  174. */
  175. BroadcastReceiver mReceiver = new BroadcastReceiver() {
  176. @Override
  177. public void onReceive(Context context, Intent intent) {
  178. String scanResult = intent.getStringExtra(scanBean.getDataName());
  179. if (scanResult == null) {
  180. return;
  181. }
  182. String scanStatus = "";
  183. if (!scanBean.getStatusName().isEmpty()) {
  184. scanStatus = intent.getStringExtra(scanBean.getStatusName());
  185. } else {
  186. scanStatus = scanResult.isEmpty() ? "" : "ok"; //有值 默认是扫码成功;
  187. }
  188. //新大陆(MT65,MT90) 需要手动关闭
  189. if (!scanBean.getActionStop().isEmpty()) {
  190. Intent stopIntent = new Intent(scanBean.getActionStop());
  191. mContext.sendBroadcast(stopIntent);
  192. }
  193. if ("ok".equals(scanStatus)) {
  194. setScanResult(scanResult);
  195. } else {
  196. showErrorToast(getString(R.string.scan_failed));
  197. }
  198. }
  199. };
  200. /**
  201. * 注册广播
  202. */
  203. private void initReceiver() {
  204. try {
  205. String scanStr = getScanSetting();
  206. scanBean = JsonUtil.getObject(scanStr, ScanBean.class);
  207. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  208. if (is_broadcast == 1) {
  209. IntentFilter mFilter = new IntentFilter(scanBean.getAction());
  210. this.registerReceiver(mReceiver, mFilter);
  211. } else {
  212. //不广播进行处理 jni模式
  213. String pdaBrand = StoTongJNI.getmPdaBrand();
  214. if (!pdaBrand.isEmpty()) {
  215. StoTongJNI.getIns(this).setmScanCB(this);
  216. StoTongJNI.getIns(this).SetScannerOn();
  217. }
  218. }
  219. } catch (Exception e) {
  220. showErrorToast("获取扫码配置失败" + e.getMessage());
  221. XLog.e("获取扫码配置失败:" + e.getMessage());
  222. }
  223. }
  224. //统一按钮回调
  225. public void callbackEditor(View v) {
  226. switch (v.getId()) {
  227. case R.id.et_destroy_batch_number:
  228. checkBatchNumber();
  229. break;
  230. case R.id.et_internals_code:
  231. deliveryCompleted();
  232. break;
  233. default:
  234. }
  235. }
  236. private void initEdit() {
  237. List<EditText> list = new ArrayList<>();
  238. list.add(etDestroyBatchNumber);
  239. list.add(etInternalsCode);
  240. unifyEdit(list);
  241. etDestroyBatchNumber.requestFocus();
  242. }
  243. private OnRxScanerListener mScanerListener = new OnRxScanerListener() {
  244. @Override
  245. public void onSuccess(String type, Result result) {
  246. String scanResult = result.getText();
  247. switch (type) {
  248. case BaseConfig.COMMON_SCANNER_ONLY:
  249. setScanResult(scanResult);
  250. break;
  251. }
  252. }
  253. @Override
  254. public void onFail(String type, String message) {
  255. }
  256. };
  257. //把扫描结果赋值
  258. private void setScanResult(String scanResult) {
  259. //获取焦点的view对象
  260. View view = getWindow().getDecorView().findFocus();
  261. //如果是EditText
  262. if (view instanceof EditText) {
  263. ((EditText) view).setText(scanResult);
  264. //执行回调
  265. callbackEditor(view);
  266. }
  267. }
  268. /**
  269. * 完成出库
  270. */
  271. private void deliveryCompleted() {
  272. if (!validate()) {
  273. return;
  274. }
  275. String postJson = formatParam(2);
  276. if (!checkRepeat(Urls.FXY_DETECTION_PUTAWAY + (postJson))) {
  277. return;
  278. }
  279. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_PUTAWAY).upJson(postJson)
  280. .execute(new MyDialogCallback(this, true, true) {
  281. @Override
  282. public void onSuccess(Response<String> response) {
  283. super.onSuccess(response);
  284. try {
  285. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  286. if (bean.code == 1) {
  287. showSuccessToast(bean.msg);
  288. clearData();
  289. } else {
  290. etInternalsCode.selectAll();
  291. showWarningToast(bean.msg);
  292. }
  293. } catch (Exception e) {
  294. e.printStackTrace();
  295. XLog.e("请求返回打托信息错误", e.getMessage());
  296. }
  297. }
  298. });
  299. }
  300. /**
  301. * 检查库位
  302. */
  303. private void checkBatchNumber() {
  304. if (!checkNull(etDestroyBatchNumber)) {
  305. setEditTextFocused(etDestroyBatchNumber, true);
  306. showWarningToast(etDestroyBatchNumber.getHint().toString());
  307. return ;
  308. }
  309. String postJson = formatParam(1);
  310. if (!checkRepeat(Urls.FXY_DETECTION_CHECK_LOCATION + (postJson))) {
  311. return;
  312. }
  313. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_CHECK_LOCATION).upJson(postJson)
  314. .execute(new MyDialogCallback(this, true, true) {
  315. @Override
  316. public void onSuccess(Response<String> response) {
  317. super.onSuccess(response);
  318. try {
  319. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  320. if (bean.code == 1) {
  321. showSuccessToast(bean.msg);
  322. etInternalsCode.requestFocus();
  323. } else {
  324. showWarningToast(bean.msg);
  325. }
  326. } catch (Exception e) {
  327. e.printStackTrace();
  328. XLog.e("请求返回箱号检查错误", e.getMessage());
  329. }
  330. }
  331. });
  332. }
  333. /**
  334. * 完成
  335. */
  336. private void finished() {
  337. clearData();
  338. }
  339. /**
  340. * @param type int 1、库位,2、箱号
  341. * @return
  342. */
  343. private String formatParam(int type) {
  344. JSONObject jsonObject = new JSONObject();
  345. try {
  346. jsonObject.put("location_number", etDestroyBatchNumber.getText().toString().trim());
  347. if (type > 1) {
  348. jsonObject.put("box_code", etInternalsCode.getText().toString().trim());
  349. }
  350. } catch (Exception e) {
  351. e.printStackTrace();
  352. XLog.e("组装数据失败");
  353. }
  354. return jsonObject.toString();
  355. }
  356. /*
  357. * 校验必录
  358. */
  359. private boolean validate() {
  360. if (!checkNull(etDestroyBatchNumber)) {
  361. setEditTextFocused(etDestroyBatchNumber, true);
  362. showWarningToast(etDestroyBatchNumber.getHint().toString());
  363. return false;
  364. }
  365. if (!checkNull(etInternalsCode)) {
  366. setEditTextFocused(etInternalsCode, true);
  367. showWarningToast(etInternalsCode.getHint().toString());
  368. return false;
  369. }
  370. return true;
  371. }
  372. //清除订单信息
  373. private void clearData() {
  374. etInternalsCode.setText("");
  375. etDestroyBatchNumber.setText("");
  376. setEditTextNormal(etDestroyBatchNumber, true);
  377. setEditTextNormal(etInternalsCode, true);
  378. etDestroyBatchNumber.requestFocus();
  379. }
  380. @OnClick({R.id.btn_confirm})
  381. public void onViewClicked(View view) {
  382. Bundle bundle = new Bundle();
  383. switch (view.getId()) {
  384. case R.id.btn_confirm:
  385. finished();
  386. break;
  387. default:
  388. break;
  389. }
  390. }
  391. /**
  392. * 统一设置Edit监听
  393. *
  394. * @param list
  395. */
  396. public void unifyEdit(List<EditText> list) {
  397. for (int i = 0; i < list.size(); i++) {
  398. EditText editText = list.get(i);
  399. LinearLayout linearLayout = (LinearLayout) editText.getParent();
  400. ImageButton imageButton = getChildImageButton(linearLayout);
  401. //监听按确定
  402. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  403. @Override
  404. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  405. XLog.e("actionId:" + actionId);
  406. if (event != null) {
  407. XLog.e("KeyCode:" + event.getKeyCode() + "--ActionCode:" + event.getAction());
  408. }
  409. //|| (actionId == KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_ENTER )
  410. if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE ||
  411. (actionId == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
  412. if (v.getText().toString().trim().isEmpty()) {
  413. setEditTextFocused(v, true);
  414. showWarningToast(v.getHint().toString());
  415. return true;
  416. } else {
  417. setEditTextNormal(v, true);
  418. }
  419. callbackEditor(v);
  420. return false;
  421. }
  422. return false;
  423. //返回true,保留软键盘。false,隐藏软键盘
  424. }
  425. });
  426. editText.addTextChangedListener(new TextWatcher() {
  427. public void onTextChanged(CharSequence s, int start, int before, int count) {
  428. EditText currentEditText = null;
  429. ImageButton currentImage = null;
  430. //获取焦点的view对象
  431. View view = getWindow().getDecorView().findFocus();
  432. //如果是EditText
  433. if (view instanceof EditText) {
  434. currentEditText = (EditText) view;
  435. currentImage = getChildImageButton((LinearLayout) currentEditText.getParent());
  436. }
  437. if (s.length() > 0) {
  438. if (currentImage != null) {
  439. currentImage.setVisibility(View.VISIBLE);
  440. }
  441. if (currentEditText != null) {
  442. setEditTextNormal(currentEditText, true);
  443. }
  444. } else {
  445. if (currentImage != null) {
  446. currentImage.setVisibility(View.INVISIBLE);
  447. }
  448. if (currentEditText != null) {
  449. setEditTextFocused(currentEditText, true);
  450. }
  451. }
  452. }
  453. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  454. }
  455. public void afterTextChanged(Editable s) {
  456. }
  457. });
  458. if (imageButton != null) {
  459. unifyClearEdit(imageButton);
  460. }
  461. }
  462. }
  463. @Override
  464. protected void onDestroy() {
  465. unbinder.unbind();
  466. super.onDestroy();
  467. }
  468. @Override
  469. protected void onResume() {
  470. super.onResume();
  471. initReceiver();
  472. }
  473. @Override
  474. protected void onPause() {
  475. super.onPause();
  476. //销毁在onResume()方法中的广播
  477. try {
  478. //停止扫描
  479. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  480. if (is_broadcast == 0) {
  481. StoTongJNI.getIns(mContext).SetScannerStop();
  482. StoTongJNI.getIns(mContext).SetScannerOff();
  483. } else {
  484. unregisterReceiver(mReceiver);
  485. }
  486. } catch (Exception e) {
  487. XLog.e("销毁广播失败:", e.getMessage());
  488. }
  489. }
  490. }