HkOutboundMainActivity.java 21 KB

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