HkTrayLocationMainActivity.java 18 KB

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