TrayPutInMainActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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.Bundle;
  10. import android.os.Handler;
  11. import android.os.Looper;
  12. import android.os.Message;
  13. import android.support.v7.widget.RecyclerView;
  14. import android.text.Editable;
  15. import android.text.TextWatcher;
  16. import android.view.KeyEvent;
  17. import android.view.View;
  18. import android.view.inputmethod.EditorInfo;
  19. import android.widget.EditText;
  20. import android.widget.ImageButton;
  21. import android.widget.LinearLayout;
  22. import android.widget.TextView;
  23. import com.elvishew.xlog.XLog;
  24. import com.fxy.R;
  25. import com.fxy.baselibrary.base.BaseActivity;
  26. import com.fxy.baselibrary.bean.BaseEventBusBean;
  27. import com.fxy.baselibrary.interfaces.OnRxScanerListener;
  28. import com.fxy.baselibrary.util.JsonUtil;
  29. import com.fxy.bean.ActionBean;
  30. import com.fxy.bean.ScanBean;
  31. import com.fxy.common.AsyncPictureSelector;
  32. import com.fxy.constant.BaseConfig;
  33. import com.fxy.constant.EventCode;
  34. import com.fxy.constant.SPCache;
  35. import com.fxy.detection.bean.TakePictureBean;
  36. import com.fxy.detection.bean.TrackingBoxBean;
  37. import com.fxy.net.MyDialogCallback;
  38. import com.fxy.net.Urls;
  39. import com.fxy.utils.UploadUtils;
  40. import com.google.gson.Gson;
  41. import com.google.zxing.Result;
  42. import com.luck.picture.lib.config.PictureConfig;
  43. import com.lzy.okgo.OkGo;
  44. import com.lzy.okgo.model.Response;
  45. import org.json.JSONObject;
  46. import java.util.ArrayList;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import butterknife.BindView;
  51. import butterknife.ButterKnife;
  52. import butterknife.OnClick;
  53. import butterknife.Unbinder;
  54. import sto.android.app.StoJNI;
  55. import sto.android.app.StoPdaKeyEvent;
  56. import sto.android.app.StoTongJNI;
  57. public class TrayPutInMainActivity extends BaseActivity implements StoJNI.ScanCallBack{
  58. /**
  59. * 列表适配器
  60. */
  61. Context mContext = this;
  62. @BindView(R.id.et_tracking_number)
  63. EditText etTrackingNumber;
  64. @BindView(R.id.ll_label_picture)
  65. LinearLayout llLabelPicture;
  66. @BindView(R.id.ll_await_num)
  67. LinearLayout llAwaitNum;
  68. @BindView(R.id.tv_await_num)
  69. TextView tvAwaitNum;
  70. private HashMap<String, Object> postData = new HashMap<>();
  71. private HashMap<String, AsyncPictureSelector> mapPictureSelector = new HashMap<>();
  72. private TrackingBoxBean trackingBoxBean;
  73. private String currentPictureType= "label_img";
  74. private boolean currentUploadStatus = false;
  75. //
  76. //扫码扫码动作
  77. private ScanBean scanBean;
  78. private boolean showWeightImg = false;
  79. private boolean isRequiredShow = false;
  80. private ArrayList<String> baseImgList = new ArrayList<>();
  81. protected String action;
  82. private Unbinder unbinder;
  83. private JSONObject postParam = new JSONObject();
  84. private int leisure = 10;
  85. @Override
  86. public int getContentViewResId() {
  87. return R.layout.activity_fxy_detection_tray_putin;
  88. }
  89. @Override
  90. public boolean showToolBar() {
  91. return true;
  92. }
  93. @Override
  94. public boolean openEventBus() {
  95. return true;
  96. }
  97. @Override
  98. protected void getBundleExtras(Bundle bundle) {
  99. //XLog.e("接收数据解析:",bundle);
  100. }
  101. //finish的返回监听
  102. //这里的requestCode参数,就是上面设置的 1 ,当跳转的页面返回的时候,通过这个加以判断
  103. //resultCode ,这个参数是在跳转的页面里面规定的,它也是一个int类型的标志
  104. //第三个参数包含了返回的值
  105. //如果不需要所跳转的页面返回值,也就不需要这个方法了
  106. @Override
  107. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  108. super.onActivityResult(requestCode, resultCode, data);
  109. if (resultCode == RESULT_OK) {
  110. // 图片选择结果回调
  111. if (requestCode == PictureConfig.CHOOSE_REQUEST) {
  112. if (mapPictureSelector.containsKey(currentPictureType)){
  113. mapPictureSelector.get(currentPictureType).setNotifyParam(getNotifyParam());
  114. mapPictureSelector.get(currentPictureType).getSelectImg(data);
  115. }
  116. totalAwaitNum();
  117. }
  118. }
  119. if (requestCode == 200 && resultCode == 200)//之前提到的两个标志,在这里显示出了作用
  120. {
  121. initFinish();
  122. }
  123. }
  124. /**
  125. * EventBus接收信息的方法,开启后才会调用(非粘性事件)
  126. *
  127. * @param event
  128. */
  129. @Override
  130. protected void EventBean(BaseEventBusBean event) {
  131. // XLog.e("----------接收返回--------------");
  132. // XLog.e("接收返回:"+event.getEventCode());
  133. assert event != null;
  134. switch (event.getEventCode()){
  135. case EventCode.picture_selector_lister:
  136. String pictureType = event.getData().toString();
  137. if (mapPictureSelector.containsKey(pictureType)){
  138. currentPictureType = pictureType;
  139. }
  140. break;
  141. case EventCode.file_upload_lister:
  142. totalAwaitNum();
  143. break;
  144. }
  145. }
  146. @Override
  147. public boolean isPlayMusic() {
  148. return true;
  149. }
  150. @Override
  151. protected void initView() {
  152. unbinder = ButterKnife.bind(this);
  153. setTitleName("托盘入库");
  154. initEdit();
  155. iniData();
  156. RecyclerView rvLabelShowImg = (RecyclerView)llLabelPicture.findViewById(R.id.rv_show_img);
  157. mapPictureSelector.put("label_img",setItemPicture("label_img",rvLabelShowImg,20));
  158. totalAwaitNum();
  159. }
  160. /**
  161. * 设置多个上传文件
  162. * @param uploadType String
  163. * @param showImg RecyclerView
  164. */
  165. private AsyncPictureSelector setItemPicture(String uploadType,RecyclerView showImg,int num){
  166. String notifyUrl = Urls.getServiceAddress(this) + Urls.FXY_DETECTION_PUTIN;
  167. AsyncPictureSelector pictureUpload = new AsyncPictureSelector(TrayPutInMainActivity.this,showImg,"detection",uploadType,notifyUrl,false);
  168. pictureUpload.setUploadType(uploadType);
  169. pictureUpload.setCompress(true,100,100);
  170. pictureUpload.setOnlyCamera(true);
  171. pictureUpload.setMaxSelectNum(num);
  172. return pictureUpload;
  173. }
  174. @Override
  175. public boolean dispatchKeyEvent(KeyEvent event) {
  176. //XLog.e("event:"+event.getKeyCode());
  177. if ((event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode()==KeyEvent.KEYCODE_ENTER ) && event.getAction() == KeyEvent.ACTION_DOWN){
  178. //按确定键
  179. }
  180. return super.dispatchKeyEvent(event);
  181. }
  182. public HashMap<String, Object> getNotifyParam(){
  183. postData.put("tracking_number",etTrackingNumber.getText().toString().trim());
  184. return postData;
  185. }
  186. //监听按键
  187. public boolean onKeyDown(int keyCode, KeyEvent event) {
  188. XLog.e("监听按键:"+keyCode);XLog.e(event);
  189. if (scanBean.getIsBroadcast()==0 && StoTongJNI.getIns(mContext).getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_SCAN
  190. && event.getRepeatCount() == 0) {
  191. if (StoTongJNI.getIns(mContext).GetScannerIsScanning()) {
  192. //
  193. } else {
  194. StoTongJNI.getIns(mContext).SetScannerStart();
  195. }
  196. }
  197. return super.onKeyDown(keyCode, event);
  198. }
  199. @SuppressLint("HandlerLeak")
  200. Handler scanHandler = new Handler() {
  201. @Override
  202. public void handleMessage(Message msg) {
  203. // TODO Auto-generated method stub
  204. if (msg.obj != null) {
  205. String scanResult = msg.obj.toString().trim();
  206. if (!scanResult.isEmpty()){
  207. setScanResult(scanResult);
  208. }
  209. }
  210. }
  211. };
  212. @Override
  213. public void onScanResults(String str) {
  214. Message msg = new Message();
  215. msg.obj = str;
  216. scanHandler.sendMessage(msg);
  217. }
  218. @Override
  219. public void onScanResults(String str, int type) {
  220. }
  221. /**
  222. * 定义广播接受
  223. */
  224. BroadcastReceiver mReceiver = new BroadcastReceiver() {
  225. @Override
  226. public void onReceive(Context context, Intent intent) {
  227. String scanResult = intent.getStringExtra(scanBean.getDataName());
  228. if (scanResult==null){return; }
  229. String scanStatus = "";
  230. if (!scanBean.getStatusName().isEmpty()){
  231. scanStatus = intent.getStringExtra(scanBean.getStatusName());
  232. }else{
  233. scanStatus = scanResult.isEmpty()? "" : "ok"; //有值 默认是扫码成功;
  234. }
  235. //新大陆(MT65,MT90) 需要手动关闭
  236. if (!scanBean.getActionStop().isEmpty()){
  237. Intent stopIntent = new Intent(scanBean.getActionStop());
  238. mContext.sendBroadcast(stopIntent);
  239. }
  240. if ("ok".equals(scanStatus)){
  241. setScanResult(scanResult);
  242. }else{
  243. showErrorToast(getString(R.string.scan_failed));
  244. }
  245. }
  246. };
  247. /**
  248. * 注册广播
  249. */
  250. private void initReceiver(){
  251. try {
  252. String scanStr = getScanSetting();
  253. scanBean = JsonUtil.getObject(scanStr, ScanBean.class);
  254. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  255. if (is_broadcast ==1){
  256. IntentFilter mFilter = new IntentFilter(scanBean.getAction());
  257. this.registerReceiver(mReceiver, mFilter);
  258. }else{
  259. //不广播进行处理 jni模式
  260. String pdaBrand = StoTongJNI.getmPdaBrand();
  261. if (!pdaBrand.isEmpty()){
  262. StoTongJNI.getIns(this).setmScanCB(this);
  263. StoTongJNI.getIns(this).SetScannerOn();
  264. }
  265. }
  266. }catch (Exception e) {
  267. showErrorToast("获取扫码配置失败"+e.getMessage());
  268. XLog.e("获取扫码配置失败:" + e.getMessage());
  269. }
  270. }
  271. private void initEdit() {
  272. List<EditText> list = new ArrayList<>();
  273. list.add(etTrackingNumber);
  274. unifyEdit(list);
  275. }
  276. private void iniData(){
  277. baseImgList.add("label_img");
  278. }
  279. //getAwaitNum
  280. private void totalAwaitNum(){
  281. int awaitNum = UploadUtils.getAwaitNum(mRealm,getClass().getSimpleName());
  282. tvAwaitNum.setText(String.valueOf(awaitNum));
  283. }
  284. //统一按钮回调
  285. @SuppressLint("NonConstantResourceId")
  286. public void callbackEditor(View v){
  287. switch (v.getId()) {
  288. case R.id.et_tracking_number:
  289. getTrackingNumber();
  290. break;
  291. default:
  292. }
  293. }
  294. /**
  295. * 统一设置Edit监听
  296. * @param list
  297. */
  298. public void unifyEdit(List<EditText> list){
  299. for (int i=0;i<list.size();i++){
  300. EditText editText = list.get(i);
  301. LinearLayout linearLayout = (LinearLayout)editText.getParent();
  302. ImageButton imageButton = getChildImageButton(linearLayout);
  303. //监听按确定
  304. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  305. @Override
  306. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  307. //XLog.e("actionId:"+actionId);
  308. if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE ) {
  309. if (v.getText().toString().trim().isEmpty()) {
  310. setEditTextFocused(v,true);
  311. showWarningToast(v.getHint().toString());
  312. return false;
  313. }else{
  314. setEditTextNormal(v,true);
  315. }
  316. callbackEditor(v);
  317. return false;
  318. }
  319. return false;
  320. }
  321. });
  322. editText.addTextChangedListener(new TextWatcher() {
  323. public void onTextChanged(CharSequence s, int start, int before, int count) {
  324. EditText currentEditText = null;
  325. ImageButton currentImage = null;
  326. //获取焦点的view对象
  327. View view=getWindow().getDecorView().findFocus();
  328. //如果是EditText
  329. if(view instanceof EditText){
  330. currentEditText = (EditText) view;
  331. currentImage = getChildImageButton((LinearLayout)currentEditText.getParent());
  332. }
  333. if (s.length() > 0 ) {
  334. if (currentImage !=null ){
  335. currentImage.setVisibility(View.VISIBLE);
  336. }
  337. if (currentEditText!=null){
  338. setEditTextNormal(currentEditText,true);
  339. }
  340. } else {
  341. if (currentImage !=null ){
  342. currentImage.setVisibility(View.INVISIBLE);
  343. }
  344. if (currentEditText!=null && isRequiredShow){
  345. setEditTextFocused(currentEditText,true);
  346. }
  347. }
  348. }
  349. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  350. }
  351. public void afterTextChanged(Editable s) {
  352. }
  353. });
  354. if (imageButton !=null ){
  355. unifyClearEdit(imageButton);
  356. }
  357. }
  358. }
  359. private OnRxScanerListener mScanerListener = new OnRxScanerListener() {
  360. @Override
  361. public void onSuccess(String type, Result result) {
  362. String scanResult = result.getText();
  363. switch (type){
  364. case BaseConfig.COMMON_SCANNER_ONLY:
  365. setScanResult(scanResult);
  366. break;
  367. }
  368. }
  369. @Override
  370. public void onFail(String type, String message) {
  371. }
  372. };
  373. //把扫描结果赋值
  374. private void setScanResult(String scanResult){
  375. //获取焦点的view对象
  376. View view=getWindow().getDecorView().findFocus();
  377. //如果是EditText
  378. if(view instanceof EditText)
  379. {
  380. ((EditText) view).setText(scanResult);
  381. showSuccessToast("扫描成功");
  382. callbackEditor(view);
  383. }
  384. }
  385. //检查是否存在上传
  386. private void checkIsUpload(){
  387. }
  388. //获取跟踪单号
  389. private void getTrackingNumber(){
  390. checkIsUpload();
  391. if (etTrackingNumber.getText().toString().isEmpty()){
  392. showErrorToast(etTrackingNumber.getHint().toString());
  393. return;
  394. }
  395. Map<String,Object> math = new HashMap<>();
  396. math.put("tracking_number",etTrackingNumber.getText().toString().trim());
  397. String mathJson = (new Gson()).toJson(math);
  398. trackingBoxBean = null;
  399. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_PALLET).upJson(mathJson)
  400. .execute(new MyDialogCallback(this, true, true) {
  401. @Override
  402. public void onSuccess(Response<String> response) {
  403. super.onSuccess(response);
  404. try {
  405. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  406. if (bean.data!=null && !bean.data.isEmpty() && !bean.data.equals("[]")){
  407. trackingBoxBean = JsonUtil.jsonString2Bean(bean.data,TrackingBoxBean.class);
  408. }
  409. if (!trackingBoxBean.getTrackingNumber().isEmpty()){
  410. etTrackingNumber.setText(trackingBoxBean.getTrackingNumber());
  411. }
  412. if (trackingBoxBean.getNeedConfirm().equals("Y")){
  413. new AlertDialog.Builder(mContext).setTitle(R.string.dl_hint)
  414. .setMessage(trackingBoxBean.getConfirmMsg())
  415. .setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
  416. @Override
  417. public void onClick(DialogInterface dialogInterface, int i) {
  418. dialogInterface.dismiss();
  419. etTrackingNumber.selectAll();
  420. }
  421. }).setPositiveButton(R.string.str_submit, new DialogInterface.OnClickListener() {
  422. @Override
  423. public void onClick(DialogInterface dialogInterface, int i) {
  424. mapPictureSelector.get(currentPictureType).authCamera();
  425. }
  426. }).show();
  427. }else{
  428. String voiceName = trackingBoxBean.getVoiceName();
  429. if(bean.code == 1){
  430. showSuccessToast(bean.msg,voiceName);
  431. mapPictureSelector.get(currentPictureType).authCamera();
  432. }else{
  433. showWarningToast(bean.msg,voiceName);
  434. }
  435. }
  436. }catch (Exception e) {
  437. e.printStackTrace();
  438. XLog.e("确认错误",e.getMessage());
  439. }
  440. }
  441. });
  442. }
  443. //提交面单信息
  444. private void submitLabelImg(){
  445. checkIsUpload();
  446. if (etTrackingNumber.getText().toString().isEmpty()){
  447. showErrorToast(etTrackingNumber.getHint().toString());
  448. return;
  449. }
  450. Gson gson = new Gson();
  451. Map<String,Object> math = new HashMap<>();
  452. math.put("tracking_number",etTrackingNumber.getText().toString().trim());
  453. //面单
  454. List<String> stringList = mapPictureSelector.get("label_img").getOssPhotoList();
  455. if (stringList.size()==0){
  456. showWarningToast("面单照片不能为空");
  457. return;
  458. }
  459. math.put("label_img_tag",mapPictureSelector.get("label_img").getUniqueTag());
  460. math.put("label_img",gson.toJson(stringList));
  461. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_PALLET_RECEIVE).upJson(gson.toJson(math))
  462. .execute(new MyDialogCallback(this, true, false) {
  463. @Override
  464. public void onSuccess(Response<String> response) {
  465. super.onSuccess(response);
  466. try {
  467. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  468. if (bean.data!=null && !bean.data.isEmpty() && !bean.data.equals("[]")){
  469. trackingBoxBean = JsonUtil.jsonString2Bean(bean.data,TrackingBoxBean.class);
  470. }
  471. String voiceName = trackingBoxBean.getVoiceName();
  472. if(bean.code == 1){
  473. showSuccessToast(bean.msg,voiceName);
  474. initFinish();
  475. }else{
  476. showWarningToast(bean.msg,voiceName);
  477. }
  478. }catch (Exception e) {
  479. e.printStackTrace();
  480. XLog.e("第二步确认错误",e.getMessage());
  481. }
  482. }
  483. });
  484. }
  485. private void initFinish(){
  486. for (int i=0;i<baseImgList.size();i++){
  487. if (mapPictureSelector.containsKey(baseImgList.get(i))){
  488. mapPictureSelector.get(baseImgList.get(i)).emptyRemake();
  489. }
  490. }
  491. llLabelPicture.setVisibility(View.VISIBLE);
  492. etTrackingNumber.setText("");
  493. etTrackingNumber.requestFocus();
  494. }
  495. //sIsGoodinfoMatch,sIsBattery,sIsMagnetic,sIsWood,sIsExtra,sIsOther
  496. @OnClick({R.id.btn_confirm})
  497. public void onViewClicked(View view) {
  498. Bundle bundle = new Bundle();
  499. switch (view.getId()) {
  500. case R.id.btn_confirm:
  501. submitLabelImg();
  502. default:
  503. break;
  504. }
  505. }
  506. @Override
  507. protected void onDestroy() {
  508. unbinder.unbind();
  509. super.onDestroy();
  510. }
  511. @Override
  512. protected void onResume() {
  513. super.onResume();
  514. initReceiver();
  515. }
  516. @Override
  517. protected void onPause() {
  518. super.onPause();
  519. //销毁在onResume()方法中的广播
  520. try {
  521. //停止扫描
  522. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  523. if (is_broadcast==0){
  524. StoTongJNI.getIns(mContext).SetScannerStop();
  525. StoTongJNI.getIns(mContext).SetScannerOff();
  526. }else{
  527. unregisterReceiver(mReceiver);
  528. }
  529. }catch (Exception e){
  530. XLog.e("销毁广播失败:",e.getMessage());
  531. }
  532. }
  533. }