TallyPictureMainActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package com.fxy.fms.putIn;
  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 com.elvishew.xlog.XLog;
  16. import com.google.gson.Gson;
  17. import com.google.zxing.Result;
  18. import com.luck.picture.lib.config.PictureConfig;
  19. import com.lzy.okgo.OkGo;
  20. import com.lzy.okgo.model.Response;
  21. import com.fxy.baselibrary.base.BaseActivity;
  22. import com.fxy.baselibrary.bean.BaseEventBusBean;
  23. import com.fxy.baselibrary.interfaces.OnRxScanerListener;
  24. import com.fxy.baselibrary.qrcode.ActivityScanerCode;
  25. import com.fxy.baselibrary.util.JsonUtil;
  26. import com.fxy.baselibrary.views.dialog.RxDialogChooseImage;
  27. import com.fxy.R;
  28. import com.fxy.common.PictureSelectorUpload;
  29. import com.fxy.base.fragment.ShowImgFragment;
  30. import com.fxy.bean.ActionBean;
  31. import com.fxy.bean.ScanBean;
  32. import com.fxy.constant.BaseConfig;
  33. import com.fxy.constant.EventCode;
  34. import com.fxy.fms.logic.ViewLogic;
  35. import com.fxy.net.MyDialogCallback;
  36. import com.fxy.net.Urls;
  37. import com.fxy.view.FloatingImageView;
  38. import org.json.JSONException;
  39. import org.json.JSONObject;
  40. import java.util.ArrayList;
  41. import butterknife.BindView;
  42. import butterknife.ButterKnife;
  43. import butterknife.OnClick;
  44. import butterknife.Unbinder;
  45. import sto.android.app.StoJNI;
  46. import sto.android.app.StoPdaKeyEvent;
  47. import sto.android.app.StoTongJNI;
  48. public class TallyPictureMainActivity extends BaseActivity implements StoJNI.ScanCallBack {
  49. /**
  50. * 列表适配器
  51. */
  52. Context mContext = this;
  53. //材积明细
  54. @BindView(R.id.ll_add_img)
  55. LinearLayout llAddImg;
  56. //浮动按钮
  57. @BindView(R.id.iv_drag)
  58. FloatingImageView iv_drag;
  59. @BindView(R.id.ll_tally_info)
  60. LinearLayout llTallyInfo;
  61. @BindView(R.id.rv_show_img)
  62. RecyclerView rvShowImg;
  63. //扫码扫码动作
  64. private ScanBean scanBean;
  65. //订单信息对象
  66. private JSONObject inboundObject;
  67. private JSONObject paramObject;
  68. private Unbinder unbinder;
  69. protected String action;
  70. private ShowImgFragment showImgFragment;
  71. // ArrayList<String> imgList = new ArrayList<String>();
  72. private RxDialogChooseImage rxDialogChooseImage;
  73. private ArrayList photoList =new ArrayList<>();
  74. private PictureSelectorUpload pictureSelectorUpload;
  75. private JSONObject postParam = new JSONObject();
  76. @Override
  77. public int getContentViewResId() {
  78. return R.layout.activity_fms_tally_picture;
  79. }
  80. @Override
  81. public boolean showToolBar() {
  82. return true;
  83. }
  84. @Override
  85. public boolean openEventBus() {
  86. return true;
  87. }
  88. @Override
  89. protected void getBundleExtras(Bundle bundle) {
  90. try {
  91. inboundObject = new JSONObject(bundle.getString("inbound_object"));
  92. paramObject = new JSONObject(bundle.getString("param"));
  93. postParam = new JSONObject(bundle.getString("post_param"));
  94. } catch (JSONException e) {
  95. XLog.e("接收数据解析:"+e.getMessage());
  96. e.printStackTrace();
  97. }
  98. }
  99. //finish的返回监听
  100. //这里的requestCode参数,就是上面设置的 1 ,当跳转的页面返回的时候,通过这个加以判断
  101. //resultCode ,这个参数是在跳转的页面里面规定的,它也是一个int类型的标志
  102. //第三个参数包含了返回的值
  103. //如果不需要所跳转的页面返回值,也就不需要这个方法了
  104. @Override
  105. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  106. super.onActivityResult(requestCode, resultCode, data);
  107. if (resultCode == RESULT_OK) {
  108. if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调
  109. pictureSelectorUpload.getSelectImg(data);
  110. }
  111. }
  112. }
  113. /**
  114. * EventBus接收信息的方法,开启后才会调用(非粘性事件)
  115. *
  116. * @param event
  117. */
  118. @Override
  119. protected void EventBean(BaseEventBusBean event) {
  120. // XLog.e("----------接收返回--------------");
  121. // XLog.e("接收返回:"+event.getEventCode());
  122. if (event != null && event.getEventCode() == EventCode.displacement_refresh){
  123. }
  124. }
  125. @Override
  126. public boolean isPlayMusic() {
  127. return true;
  128. }
  129. @Override
  130. protected void initView() {
  131. unbinder = ButterKnife.bind(this);
  132. setTitleName("录入材积重");
  133. initEdit();
  134. //loadServiceData();
  135. initData();
  136. //监听浮动按钮
  137. iv_drag.setOnClickListener(new View.OnClickListener() {
  138. @Override
  139. public void onClick(View v) {
  140. if(!iv_drag.isDrag()){
  141. ActivityScanerCode.setScanerListener(mScanerListener);
  142. ActivityScanerCode.intent2Activity(mContext, BaseConfig.COMMON_SCANNER_ONLY);
  143. }
  144. }
  145. });
  146. if (inboundObject!=null){
  147. //设置订单信息
  148. ViewLogic logic = new ViewLogic(llTallyInfo);
  149. logic.setTallyData(inboundObject);
  150. logic.setTallyMore(paramObject);
  151. }
  152. pictureSelectorUpload = new PictureSelectorUpload(TallyPictureMainActivity.this,rvShowImg,"tally");
  153. }
  154. private void initData(){
  155. }
  156. @Override
  157. public boolean dispatchKeyEvent(KeyEvent event) {
  158. //XLog.e("event:"+event.getKeyCode());
  159. if ((event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode()==KeyEvent.KEYCODE_ENTER ) && event.getAction() == KeyEvent.ACTION_DOWN){
  160. //按确定键
  161. if(this.validate()){
  162. addReceive();
  163. }
  164. }
  165. return super.dispatchKeyEvent(event);
  166. }
  167. //监听按键
  168. public boolean onKeyDown(int keyCode, KeyEvent event) {
  169. XLog.e("监听按键:"+keyCode);XLog.e(event);
  170. if (scanBean.getIsBroadcast()==0 && StoTongJNI.getIns(mContext).getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_SCAN
  171. && event.getRepeatCount() == 0) {
  172. if (StoTongJNI.getIns(mContext).GetScannerIsScanning()) {
  173. //
  174. } else {
  175. StoTongJNI.getIns(mContext).SetScannerStart();
  176. }
  177. }
  178. return super.onKeyDown(keyCode, event);
  179. }
  180. @SuppressLint("HandlerLeak")
  181. Handler scanHandler = new Handler() {
  182. @Override
  183. public void handleMessage(Message msg) {
  184. // TODO Auto-generated method stub
  185. if (msg.obj != null) {
  186. String scanResult = msg.obj.toString().trim();
  187. if (!scanResult.isEmpty()){
  188. setScanResult(scanResult);
  189. }
  190. }
  191. }
  192. };
  193. @Override
  194. public void onScanResults(String str) {
  195. Message msg = new Message();
  196. msg.obj = str;
  197. scanHandler.sendMessage(msg);
  198. }
  199. @Override
  200. public void onScanResults(String str, int type) {
  201. }
  202. /**
  203. * 定义广播接受
  204. */
  205. BroadcastReceiver mReceiver = new BroadcastReceiver() {
  206. @Override
  207. public void onReceive(Context context, Intent intent) {
  208. String scanResult = intent.getStringExtra(scanBean.getDataName());
  209. if (scanResult==null){return; }
  210. String scanStatus = "";
  211. if (!scanBean.getStatusName().isEmpty()){
  212. scanStatus = intent.getStringExtra(scanBean.getStatusName());
  213. }else{
  214. scanStatus = scanResult.isEmpty()? "" : "ok"; //有值 默认是扫码成功;
  215. }
  216. //新大陆(MT65,MT90) 需要手动关闭
  217. if (!scanBean.getActionStop().isEmpty()){
  218. Intent stopIntent = new Intent(scanBean.getActionStop());
  219. mContext.sendBroadcast(stopIntent);
  220. }
  221. if ("ok".equals(scanStatus)){
  222. setScanResult(scanResult);
  223. }else{
  224. showErrorToast(getString(R.string.scan_failed));
  225. }
  226. }
  227. };
  228. /**
  229. * 注册广播
  230. */
  231. private void initReceiver(){
  232. try {
  233. String scanStr = getScanSetting();
  234. scanBean = JsonUtil.getObject(scanStr, ScanBean.class);
  235. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  236. if (is_broadcast ==1){
  237. IntentFilter mFilter = new IntentFilter(scanBean.getAction());
  238. this.registerReceiver(mReceiver, mFilter);
  239. }else{
  240. //不广播进行处理 jni模式
  241. String pdaBrand = StoTongJNI.getmPdaBrand();
  242. if (!pdaBrand.isEmpty()){
  243. StoTongJNI.getIns(this).setmScanCB(this);
  244. StoTongJNI.getIns(this).SetScannerOn();
  245. }
  246. }
  247. }catch (Exception e) {
  248. showErrorToast("获取扫码配置失败"+e.getMessage());
  249. XLog.e("获取扫码配置失败:" + e.getMessage());
  250. }
  251. }
  252. private void initEdit() {
  253. }
  254. private OnRxScanerListener mScanerListener = new OnRxScanerListener() {
  255. @Override
  256. public void onSuccess(String type, Result result) {
  257. String scanResult = result.getText();
  258. switch (type){
  259. case BaseConfig.COMMON_SCANNER_ONLY:
  260. setScanResult(scanResult);
  261. break;
  262. }
  263. }
  264. @Override
  265. public void onFail(String type, String message) {
  266. }
  267. };
  268. //把扫描结果赋值
  269. private void setScanResult(String scanResult){
  270. //获取焦点的view对象
  271. View view=getWindow().getDecorView().findFocus();
  272. //如果是EditText
  273. if(view instanceof EditText)
  274. {
  275. ((EditText) view).setText(scanResult);
  276. switch (view.getId()) {
  277. case R.id.et_order_no:
  278. break;
  279. default:
  280. }
  281. }
  282. }
  283. /*
  284. * 校验必录
  285. */
  286. private boolean validate(){
  287. photoList = pictureSelectorUpload.getPhotoList();
  288. if (photoList.size()==0){
  289. showWarningToast("请拍照拍照");
  290. return false;
  291. }
  292. return true;
  293. }
  294. @OnClick({R.id.btn_confirm,R.id.tv_clear_pictures})
  295. public void onViewClicked(final View view) {
  296. Bundle bundle = new Bundle();
  297. switch (view.getId()) {
  298. case R.id.btn_confirm:
  299. addReceive();
  300. break;
  301. case R.id.tv_clear_pictures:
  302. pictureSelectorUpload.emptyRemake();
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. private void addReceive(){
  309. //校验数据
  310. if (!validate()){
  311. return;
  312. }
  313. try {
  314. Gson gson = new Gson();
  315. postParam.put("is_confirm","Y");
  316. postParam.put("photo",gson.toJson(photoList));
  317. if (!checkRepeat(Urls.FMS_ADD_RECEIVE+(postParam.toString()))){return;}
  318. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FMS_ADD_RECEIVE).upJson(postParam)
  319. .execute(new MyDialogCallback(this, true, true) {
  320. @Override
  321. public void onSuccess(Response<String> response) {
  322. super.onSuccess(response);
  323. try {
  324. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  325. if(bean.code == 1){
  326. showSuccessToast(getString(R.string.str_tally_success));
  327. Intent intent = new Intent();
  328. intent.putExtra("opt_status", "success");
  329. setResult(200, intent);//返回值,200 是改返回的标志,也会返回
  330. finish();
  331. }else{
  332. showWarningToast(bean.msg);
  333. }
  334. } catch (Exception e) {
  335. e.printStackTrace();
  336. XLog.e("添加理货数据结果返回错误",e.getMessage());
  337. }
  338. }
  339. });
  340. } catch (JSONException e) {
  341. XLog.e("sku数据解析出错",e);
  342. e.printStackTrace();
  343. }
  344. }
  345. //获取输入框值
  346. private String getValStr(EditText v){
  347. return v.getText().toString().trim();
  348. }
  349. @Override
  350. protected void onDestroy() {
  351. unbinder.unbind();
  352. pictureSelectorUpload.removeHandler();
  353. super.onDestroy();
  354. }
  355. @Override
  356. protected void onResume() {
  357. super.onResume();
  358. initReceiver();
  359. }
  360. @Override
  361. protected void onPause() {
  362. super.onPause();
  363. //销毁在onResume()方法中的广播
  364. try {
  365. //停止扫描
  366. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  367. if (is_broadcast==0){
  368. StoTongJNI.getIns(mContext).SetScannerStop();
  369. StoTongJNI.getIns(mContext).SetScannerOff();
  370. }else{
  371. unregisterReceiver(mReceiver);
  372. }
  373. }catch (Exception e){
  374. XLog.e("销毁广播失败:",e.getMessage());
  375. }
  376. }
  377. }