TrayMakeMainActivity.java 18 KB

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