DetectionDealDiffMainActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.support.v7.widget.RecyclerView;
  11. import android.view.KeyEvent;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.LinearLayout;
  15. import android.widget.TextView;
  16. import com.elvishew.xlog.XLog;
  17. import com.fxy.R;
  18. import com.fxy.baselibrary.base.BaseActivity;
  19. import com.fxy.baselibrary.bean.BaseEventBusBean;
  20. import com.fxy.baselibrary.interfaces.OnRxScanerListener;
  21. import com.fxy.baselibrary.util.JsonUtil;
  22. import com.fxy.baselibrary.views.MarqueeTextView;
  23. import com.fxy.bean.ActionBean;
  24. import com.fxy.bean.ScanBean;
  25. import com.fxy.common.PictureSelectorUpload;
  26. import com.fxy.constant.BaseConfig;
  27. import com.fxy.constant.EventCode;
  28. import com.fxy.detection.bean.DealDiffBean;
  29. import com.fxy.detection.bean.NeedPhotoBean;
  30. import com.fxy.net.MyDialogCallback;
  31. import com.fxy.net.Urls;
  32. import com.google.gson.Gson;
  33. import com.google.zxing.Result;
  34. import com.luck.picture.lib.config.PictureConfig;
  35. import com.lzy.okgo.OkGo;
  36. import com.lzy.okgo.model.Response;
  37. import org.json.JSONArray;
  38. import org.json.JSONException;
  39. import org.json.JSONObject;
  40. import java.util.ArrayList;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import butterknife.BindView;
  44. import butterknife.ButterKnife;
  45. import butterknife.OnClick;
  46. import butterknife.Unbinder;
  47. import sto.android.app.StoJNI;
  48. import sto.android.app.StoPdaKeyEvent;
  49. import sto.android.app.StoTongJNI;
  50. public class DetectionDealDiffMainActivity extends BaseActivity implements StoJNI.ScanCallBack {
  51. /**
  52. * 列表适配器
  53. */
  54. Context mContext = this;
  55. @BindView(R.id.mt_box_number)
  56. MarqueeTextView mtBoxNumber;
  57. @BindView(R.id.mt_unit_code)
  58. MarqueeTextView mtUnitCode;
  59. @BindView(R.id.recyclerview)
  60. RecyclerView recyclerView;
  61. @BindView(R.id.ll_recycler_view)
  62. LinearLayout llRecyclerView;
  63. private DealDiffBean dealDiffBean = new DealDiffBean();
  64. private List<NeedPhotoBean> listNeedPhoto = new ArrayList<>();
  65. //节点
  66. private String nodeCode = "";
  67. //定义文件上传
  68. private HashMap<Integer,PictureSelectorUpload> mapPictureSelector = new HashMap<>();
  69. private int currentPictureInx = -1;
  70. //提交数据
  71. private JSONObject submitData = new JSONObject();
  72. private Unbinder unbinder;
  73. private ScanBean scanBean;
  74. @Override
  75. public int getContentViewResId() {
  76. return R.layout.activity_fxy_detection_deal_diff;
  77. }
  78. @Override
  79. public boolean showToolBar() {
  80. return true;
  81. }
  82. @Override
  83. public boolean openEventBus() {
  84. return true;
  85. }
  86. @Override
  87. protected void getBundleExtras(Bundle bundle) {
  88. dealDiffBean = JsonUtil.jsonString2Bean(bundle.getString("data"),DealDiffBean.class);
  89. }
  90. //finish的返回监听
  91. //这里的requestCode参数,就是上面设置的 1 ,当跳转的页面返回的时候,通过这个加以判断
  92. //resultCode ,这个参数是在跳转的页面里面规定的,它也是一个int类型的标志
  93. //第三个参数包含了返回的值
  94. //如果不需要所跳转的页面返回值,也就不需要这个方法了
  95. @Override
  96. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  97. super.onActivityResult(requestCode, resultCode, data);
  98. // 图片选择结果回调
  99. if (resultCode == RESULT_OK) {
  100. // 图片选择结果回调
  101. if (requestCode == PictureConfig.CHOOSE_REQUEST || requestCode == PictureConfig.REQUEST_CAMERA) {
  102. PictureSelectorUpload filedUpload = mapPictureSelector.get(currentPictureInx);
  103. if (filedUpload != null){
  104. filedUpload.getSelectImg(data);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * EventBus接收信息的方法,开启后才会调用(非粘性事件)
  111. *
  112. * @param event
  113. */
  114. @Override
  115. protected void EventBean(BaseEventBusBean event) {
  116. // XLog.e("----------接收返回--------------");
  117. // XLog.e("接收返回:"+event.getEventCode());
  118. if (event != null && event.getEventCode() == EventCode.displacement_refresh) {
  119. }
  120. }
  121. @Override
  122. public boolean isPlayMusic() {
  123. return true;
  124. }
  125. @Override
  126. protected void initView() {
  127. unbinder = ButterKnife.bind(this);
  128. setTitleName("主机差异");
  129. initData();
  130. }
  131. private void initData() {
  132. nodeCode = dealDiffBean.getNodeCode();
  133. mtBoxNumber.setText(dealDiffBean.getBoxNumber());
  134. mtUnitCode.setText(dealDiffBean.getUnitCode());
  135. listNeedPhoto = dealDiffBean.getNeedPhotos();
  136. if (listNeedPhoto!=null){
  137. for (int i=0;i<listNeedPhoto.size();i++){
  138. addPictureSelectorUpload(i,listNeedPhoto.get(i).getLimit());
  139. }
  140. }
  141. }
  142. /**
  143. *
  144. * @param position int 遍历添加
  145. */
  146. private void addPictureSelectorUpload(int position,int limit){
  147. String tagKey = "recyclerview"+position;
  148. View itemPictureSelector = View.inflate(mContext, R.layout.item_picture_selector, null);
  149. TextView tvPicturesTitle = itemPictureSelector.findViewById(R.id.tv_pictures_title);
  150. tvPicturesTitle.setText(listNeedPhoto.get(position).getFieldKey());
  151. RecyclerView rvLabelShowImg = (RecyclerView)itemPictureSelector.findViewById(R.id.recyclerview);
  152. itemPictureSelector.setTag(tagKey);
  153. llRecyclerView.addView(itemPictureSelector);
  154. PictureSelectorUpload tmpPictureSelector = setItemPicture(String.valueOf(position),rvLabelShowImg,limit,PictureConfig.TYPE_IMAGE);
  155. mapPictureSelector.put(position,tmpPictureSelector);
  156. }
  157. /**
  158. * 设置多个上传文件
  159. * @param actionType String
  160. * @param showImg RecyclerView
  161. */
  162. private PictureSelectorUpload setItemPicture(String actionType,RecyclerView showImg,int maxNum,int pictureType){
  163. PictureSelectorUpload pictureUpload = new PictureSelectorUpload(DetectionDealDiffMainActivity.this,showImg,"detection",maxNum,pictureType);
  164. pictureUpload.setActionType(actionType);
  165. pictureUpload.setCompress(true,100,500);
  166. pictureUpload.setOnlyCamera(true);
  167. pictureUpload.setProgressType(2);
  168. //showImg.onTouchEvent()
  169. pictureUpload.setOnChangeListener(new PictureSelectorUpload.UploadChangeListener() {
  170. @Override
  171. public void uploadResult(boolean State,String actionType) {
  172. if (State) {
  173. }
  174. }
  175. @Override
  176. public void uploadActionType(String actionType) {
  177. System.out.println("actionType:"+actionType);
  178. currentPictureInx = Integer.parseInt(actionType);
  179. }
  180. });
  181. return pictureUpload;
  182. }
  183. @Override
  184. public boolean dispatchKeyEvent(KeyEvent event) {
  185. //XLog.e("event:"+event.getKeyCode());
  186. // if ((event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER ) && event.getAction() == KeyEvent.ACTION_DOWN){
  187. // //按确定键
  188. // if(validate()){
  189. // doPutIn();
  190. // }
  191. // return true;
  192. // }
  193. return super.dispatchKeyEvent(event);
  194. }
  195. //监听按键
  196. public boolean onKeyDown(int keyCode, KeyEvent event) {
  197. //XLog.e("监听按键:"+keyCode);XLog.e(event);
  198. if (scanBean.getIsBroadcast() == 0 && StoTongJNI.getIns(mContext).getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_SCAN
  199. && event.getRepeatCount() == 0) {
  200. if (StoTongJNI.getIns(mContext).GetScannerIsScanning()) {
  201. //
  202. } else {
  203. StoTongJNI.getIns(mContext).SetScannerStart();
  204. }
  205. }
  206. return super.onKeyDown(keyCode, event);
  207. }
  208. @SuppressLint("HandlerLeak")
  209. Handler scanHandler = new Handler() {
  210. @Override
  211. public void handleMessage(Message msg) {
  212. // TODO Auto-generated method stub
  213. if (msg.obj != null) {
  214. String scanResult = msg.obj.toString().trim();
  215. if (!scanResult.isEmpty()) {
  216. setScanResult(scanResult);
  217. }
  218. }
  219. }
  220. };
  221. @Override
  222. public void onScanResults(String str) {
  223. Message msg = new Message();
  224. msg.obj = str;
  225. scanHandler.sendMessage(msg);
  226. }
  227. @Override
  228. public void onScanResults(String str, int type) {
  229. }
  230. /**
  231. * 定义广播接受
  232. */
  233. BroadcastReceiver mReceiver = new BroadcastReceiver() {
  234. @Override
  235. public void onReceive(Context context, Intent intent) {
  236. String scanResult = intent.getStringExtra(scanBean.getDataName());
  237. if (scanResult == null) {
  238. return;
  239. }
  240. String scanStatus = "";
  241. if (!scanBean.getStatusName().isEmpty()) {
  242. scanStatus = intent.getStringExtra(scanBean.getStatusName());
  243. } else {
  244. scanStatus = scanResult.isEmpty() ? "" : "ok"; //有值 默认是扫码成功;
  245. }
  246. //新大陆(MT65,MT90) 需要手动关闭
  247. if (!scanBean.getActionStop().isEmpty()) {
  248. Intent stopIntent = new Intent(scanBean.getActionStop());
  249. mContext.sendBroadcast(stopIntent);
  250. }
  251. if ("ok".equals(scanStatus)) {
  252. setScanResult(scanResult);
  253. } else {
  254. showErrorToast(getString(R.string.scan_failed));
  255. }
  256. }
  257. };
  258. /**
  259. * 注册广播
  260. */
  261. private void initReceiver() {
  262. try {
  263. String scanStr = getScanSetting();
  264. scanBean = JsonUtil.getObject(scanStr, ScanBean.class);
  265. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  266. if (is_broadcast == 1) {
  267. IntentFilter mFilter = new IntentFilter(scanBean.getAction());
  268. this.registerReceiver(mReceiver, mFilter);
  269. } else {
  270. //不广播进行处理 jni模式
  271. String pdaBrand = StoTongJNI.getmPdaBrand();
  272. if (!pdaBrand.isEmpty()) {
  273. StoTongJNI.getIns(this).setmScanCB(this);
  274. StoTongJNI.getIns(this).SetScannerOn();
  275. }
  276. }
  277. } catch (Exception e) {
  278. showErrorToast("获取扫码配置失败" + e.getMessage());
  279. XLog.e("获取扫码配置失败:" + e.getMessage());
  280. }
  281. }
  282. //统一按钮回调
  283. public void callbackEditor(View v) {
  284. switch (v.getId()) {
  285. default:
  286. }
  287. }
  288. private void initEdit() {
  289. }
  290. private OnRxScanerListener mScanerListener = new OnRxScanerListener() {
  291. @Override
  292. public void onSuccess(String type, Result result) {
  293. String scanResult = result.getText();
  294. switch (type) {
  295. case BaseConfig.COMMON_SCANNER_ONLY:
  296. setScanResult(scanResult);
  297. break;
  298. }
  299. }
  300. @Override
  301. public void onFail(String type, String message) {
  302. }
  303. };
  304. //把扫描结果赋值
  305. private void setScanResult(String scanResult) {
  306. //获取焦点的view对象
  307. View view = getWindow().getDecorView().findFocus();
  308. //如果是EditText
  309. if (view instanceof EditText) {
  310. ((EditText) view).setText(scanResult);
  311. //执行回调
  312. callbackEditor(view);
  313. }
  314. }
  315. /**
  316. * 完成
  317. */
  318. private void doConfirm() {
  319. if (!validate()) {
  320. return;
  321. }
  322. String postJson = submitData.toString();
  323. if (!checkRepeat(Urls.FXY_DETECTION_COMPLETED + (postJson))) {
  324. return;
  325. }
  326. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_COMPLETED).upJson(postJson)
  327. .execute(new MyDialogCallback(this, true, true) {
  328. @Override
  329. public void onSuccess(Response<String> response) {
  330. super.onSuccess(response);
  331. try {
  332. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  333. if (bean.code == 1) {
  334. showSuccessToast(bean.msg);
  335. finish();
  336. } else {
  337. showWarningToast(bean.msg);
  338. }
  339. } catch (Exception e) {
  340. e.printStackTrace();
  341. XLog.e("请求返回信息错误", e.getMessage());
  342. }
  343. }
  344. });
  345. }
  346. /*
  347. * 校验必录
  348. */
  349. private boolean validate() {
  350. try {
  351. submitData.put("box_number",dealDiffBean.getBoxNumber());
  352. submitData.put("goods_id","");
  353. submitData.put("unit_code",dealDiffBean.getUnitCode());
  354. JSONArray steps = new JSONArray();
  355. if (listNeedPhoto!=null && listNeedPhoto.size()>0){
  356. for (int i=0;i<listNeedPhoto.size();i++){
  357. JSONObject item = new JSONObject();
  358. ArrayList<String> urlList = mapPictureSelector.get(i).getPhotoList();
  359. String fieldKey = listNeedPhoto.get(i).getFieldKey();
  360. if (urlList.size()==0){
  361. showErrorToast(fieldKey+"需要拍照");
  362. return false;
  363. }
  364. item.put("node_code",nodeCode);
  365. item.put("attr_id","");
  366. item.put("field_key",fieldKey);
  367. item.put("url",(new Gson()).toJson(urlList));
  368. steps.put(i,item);
  369. }
  370. }
  371. } catch (JSONException e) {
  372. e.printStackTrace();
  373. showErrorToast(e.getMessage());
  374. return false;
  375. }
  376. return true;
  377. }
  378. @OnClick({R.id.btn_confirm})
  379. public void onViewClicked(View view) {
  380. Bundle bundle = new Bundle();
  381. switch (view.getId()) {
  382. case R.id.btn_confirm:
  383. doConfirm();
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. @Override
  390. protected void onDestroy() {
  391. unbinder.unbind();
  392. super.onDestroy();
  393. }
  394. @Override
  395. protected void onResume() {
  396. super.onResume();
  397. initReceiver();
  398. }
  399. @Override
  400. protected void onPause() {
  401. super.onPause();
  402. //销毁在onResume()方法中的广播
  403. try {
  404. //停止扫描
  405. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  406. if (is_broadcast == 0) {
  407. StoTongJNI.getIns(mContext).SetScannerStop();
  408. StoTongJNI.getIns(mContext).SetScannerOff();
  409. } else {
  410. unregisterReceiver(mReceiver);
  411. }
  412. } catch (Exception e) {
  413. XLog.e("销毁广播失败:", e.getMessage());
  414. }
  415. }
  416. }