ScanPhotosMainActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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.Message;
  12. import android.support.v7.widget.RecyclerView;
  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 android.widget.Toast;
  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.qrcode.ActivityScanerCode;
  29. import com.fxy.baselibrary.util.JsonUtil;
  30. import com.fxy.baselibrary.util.StringUtils;
  31. import com.fxy.bean.ActionBean;
  32. import com.fxy.bean.ScanBean;
  33. import com.fxy.common.AsyncPictureSelector;
  34. import com.fxy.common.PictureSelectorUpload;
  35. import com.fxy.constant.BaseConfig;
  36. import com.fxy.constant.EventCode;
  37. import com.fxy.net.MyDialogCallback;
  38. import com.fxy.net.Urls;
  39. import com.fxy.utils.UploadUtils;
  40. import com.fxy.view.FloatingImageView;
  41. import com.google.gson.Gson;
  42. import com.google.zxing.Result;
  43. import com.luck.picture.lib.config.PictureConfig;
  44. import com.lzy.okgo.OkGo;
  45. import com.lzy.okgo.model.Response;
  46. import org.json.JSONException;
  47. import org.json.JSONObject;
  48. import java.util.ArrayList;
  49. import java.util.List;
  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 ScanPhotosMainActivity extends BaseActivity implements StoJNI.ScanCallBack{
  58. /**
  59. * 列表适配器
  60. */
  61. Context mContext = this;
  62. @BindView(R.id.ll_take_picture)
  63. LinearLayout llTakePicture;
  64. @BindView(R.id.ll_scan_confirm)
  65. LinearLayout llScanConfirm;
  66. @BindView(R.id.ll_confirm)
  67. LinearLayout llConfirm;
  68. @BindView(R.id.tv_await_num)
  69. TextView tvAwaitNum;
  70. private List<String> photoList =new ArrayList<>();
  71. private AsyncPictureSelector pictureSelectorUpload;
  72. //
  73. //扫码扫码动作
  74. private ScanBean scanBean;
  75. //浮动按钮
  76. @BindView(R.id.iv_drag)
  77. FloatingImageView iv_drag;
  78. private String urlCode = "";
  79. protected String action;
  80. private Unbinder unbinder;
  81. private JSONObject postParam = new JSONObject();
  82. @Override
  83. public int getContentViewResId() {
  84. return R.layout.activity_fxy_scan_photos;
  85. }
  86. @Override
  87. public boolean showToolBar() {
  88. return true;
  89. }
  90. @Override
  91. public boolean openEventBus() {
  92. return true;
  93. }
  94. @Override
  95. protected void getBundleExtras(Bundle bundle) {
  96. //XLog.e("接收数据解析:",bundle);
  97. action = bundle.getString("action","add");
  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. totalAwaitNum();
  112. }
  113. }
  114. //getAwaitNum
  115. private void totalAwaitNum(){
  116. int awaitNum = UploadUtils.getAwaitNum(mRealm,getClass().getSimpleName());
  117. tvAwaitNum.setText(String.valueOf(awaitNum));
  118. }
  119. /**
  120. * EventBus接收信息的方法,开启后才会调用(非粘性事件)
  121. *
  122. * @param event
  123. */
  124. @Override
  125. protected void EventBean(BaseEventBusBean event) {
  126. assert event != null;
  127. if (event != null && event.getEventCode() == EventCode.file_upload_lister) {
  128. totalAwaitNum();
  129. }
  130. }
  131. @Override
  132. public boolean isPlayMusic() {
  133. return true;
  134. }
  135. @Override
  136. protected void initView() {
  137. unbinder = ButterKnife.bind(this);
  138. setTitleName("扫码拍照");
  139. initEdit();
  140. iniData();
  141. RecyclerView rvShowImg = (RecyclerView)llTakePicture.findViewById(R.id.rv_show_img);
  142. pictureSelectorUpload = setItemPicture("detection",rvShowImg,99);
  143. //监听浮动按钮
  144. iv_drag.setOnClickListener(new View.OnClickListener() {
  145. @Override
  146. public void onClick(View v) {
  147. if(!iv_drag.isDrag()){
  148. ActivityScanerCode.setScanerListener(mScanerListener);
  149. ActivityScanerCode.intent2Activity(mContext, BaseConfig.COMMON_SCANNER_ONLY);
  150. }
  151. }
  152. });
  153. }
  154. /**
  155. * 设置多个上传文件
  156. * @param uploadType String
  157. * @param showImg RecyclerView
  158. */
  159. private AsyncPictureSelector setItemPicture(String uploadType,RecyclerView showImg,int num){
  160. String notifyUrl = Urls.getServiceAddress(this) + Urls.FXY_DETECTION_PUTIN;
  161. AsyncPictureSelector pictureUpload = new AsyncPictureSelector(ScanPhotosMainActivity.this,showImg,"detection",uploadType,notifyUrl,false);
  162. pictureUpload.setUploadType(uploadType);
  163. pictureUpload.setCompress(true,100,100);
  164. pictureUpload.setOnlyCamera(true);
  165. pictureUpload.setMaxSelectNum(num);
  166. return pictureUpload;
  167. }
  168. @Override
  169. public boolean dispatchKeyEvent(KeyEvent event) {
  170. //XLog.e("event:"+event.getKeyCode());
  171. if ((event.getKeyCode()==KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode()==KeyEvent.KEYCODE_ENTER ) && event.getAction() == KeyEvent.ACTION_DOWN){
  172. //按确定键
  173. }
  174. return super.dispatchKeyEvent(event);
  175. }
  176. //监听按键
  177. public boolean onKeyDown(int keyCode, KeyEvent event) {
  178. XLog.e("监听按键:"+keyCode);XLog.e(event);
  179. if (scanBean.getIsBroadcast()==0 && StoTongJNI.getIns(mContext).getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_SCAN
  180. && event.getRepeatCount() == 0) {
  181. if (StoTongJNI.getIns(mContext).GetScannerIsScanning()) {
  182. //
  183. } else {
  184. StoTongJNI.getIns(mContext).SetScannerStart();
  185. }
  186. }
  187. return super.onKeyDown(keyCode, event);
  188. }
  189. @SuppressLint("HandlerLeak")
  190. Handler scanHandler = new Handler() {
  191. @Override
  192. public void handleMessage(Message msg) {
  193. // TODO Auto-generated method stub
  194. if (msg.obj != null) {
  195. String scanResult = msg.obj.toString().trim();
  196. if (!scanResult.isEmpty()){
  197. setScanResult(scanResult);
  198. }
  199. }
  200. }
  201. };
  202. @Override
  203. public void onScanResults(String str) {
  204. Message msg = new Message();
  205. msg.obj = str;
  206. scanHandler.sendMessage(msg);
  207. }
  208. @Override
  209. public void onScanResults(String str, int type) {
  210. }
  211. /**
  212. * 定义广播接受
  213. */
  214. BroadcastReceiver mReceiver = new BroadcastReceiver() {
  215. @Override
  216. public void onReceive(Context context, Intent intent) {
  217. String scanResult = intent.getStringExtra(scanBean.getDataName());
  218. if (scanResult==null){return; }
  219. String scanStatus = "";
  220. if (!scanBean.getStatusName().isEmpty()){
  221. scanStatus = intent.getStringExtra(scanBean.getStatusName());
  222. }else{
  223. scanStatus = scanResult.isEmpty()? "" : "ok"; //有值 默认是扫码成功;
  224. }
  225. //新大陆(MT65,MT90) 需要手动关闭
  226. if (!scanBean.getActionStop().isEmpty()){
  227. Intent stopIntent = new Intent(scanBean.getActionStop());
  228. mContext.sendBroadcast(stopIntent);
  229. }
  230. if ("ok".equals(scanStatus)){
  231. setScanResult(scanResult);
  232. }else{
  233. showErrorToast(getString(R.string.scan_failed));
  234. }
  235. }
  236. };
  237. /**
  238. * 注册广播
  239. */
  240. private void initReceiver(){
  241. try {
  242. String scanStr = getScanSetting();
  243. scanBean = JsonUtil.getObject(scanStr, ScanBean.class);
  244. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  245. if (is_broadcast ==1){
  246. IntentFilter mFilter = new IntentFilter(scanBean.getAction());
  247. this.registerReceiver(mReceiver, mFilter);
  248. }else{
  249. //不广播进行处理 jni模式
  250. String pdaBrand = StoTongJNI.getmPdaBrand();
  251. if (!pdaBrand.isEmpty()){
  252. StoTongJNI.getIns(this).setmScanCB(this);
  253. StoTongJNI.getIns(this).SetScannerOn();
  254. }
  255. }
  256. }catch (Exception e) {
  257. showErrorToast("获取扫码配置失败"+e.getMessage());
  258. XLog.e("获取扫码配置失败:" + e.getMessage());
  259. }
  260. }
  261. private void initEdit() {
  262. }
  263. private void iniData(){
  264. }
  265. private OnRxScanerListener mScanerListener = new OnRxScanerListener() {
  266. @Override
  267. public void onSuccess(String type, Result result) {
  268. String scanResult = result.getText();
  269. switch (type){
  270. case BaseConfig.COMMON_SCANNER_ONLY:
  271. setScanResult(scanResult);
  272. break;
  273. }
  274. }
  275. @Override
  276. public void onFail(String type, String message) {
  277. }
  278. };
  279. //把扫描结果赋值
  280. private void setScanResult(String scanResult){
  281. showSuccessToast("扫描成功");
  282. urlCode = scanResult;
  283. pictureSelectorUpload.authCamera();
  284. setActionTake();
  285. }
  286. private void setActionScan(){
  287. llTakePicture.setVisibility(View.GONE);
  288. llConfirm.setVisibility(View.GONE);
  289. llScanConfirm.setVisibility(View.VISIBLE);
  290. }
  291. private void setActionTake (){
  292. llTakePicture.setVisibility(View.VISIBLE);
  293. llConfirm.setVisibility(View.VISIBLE);
  294. llScanConfirm.setVisibility(View.GONE);
  295. }
  296. private void pictureInbound(){
  297. if (!validate()){
  298. return;
  299. }
  300. if (!checkRepeat(Urls.FXY_DETECTION_CODE_PHOTO+(postParam.toString()))){
  301. Toast.makeText(this, getString(R.string.error_data_processed), Toast.LENGTH_LONG).show();
  302. return;
  303. }
  304. OkGo.<String>post(Urls.getServiceAddress(this) + Urls.FXY_DETECTION_CODE_PHOTO).upJson(postParam)
  305. .execute(new MyDialogCallback(this, true, true) {
  306. @Override
  307. public void onSuccess(Response<String> response) {
  308. super.onSuccess(response);
  309. try {
  310. ActionBean bean = JsonUtil.getObject(response.body(), ActionBean.class);
  311. if(bean.code == 1){
  312. showSuccessToast(bean.msg);
  313. pictureSelectorUpload.emptyRemake();//清空图片
  314. photoList.clear();
  315. urlCode = "";
  316. setActionScan();
  317. }else{
  318. showWarningToast(bean.msg);
  319. }
  320. } catch (Exception e) {
  321. e.printStackTrace();
  322. XLog.e("添加理货数据结果返回错误",e.getMessage());
  323. }
  324. }
  325. });
  326. }
  327. /*
  328. * 校验必录
  329. */
  330. private boolean validate(){
  331. photoList = pictureSelectorUpload.getOssPhotoList();
  332. if (urlCode.isEmpty()){
  333. showWarningToast("请选扫码");
  334. return false;
  335. }
  336. try {
  337. postParam = new JSONObject();
  338. //检查是否拍照 工单处理时拍照拍照非必传
  339. if (photoList.size()==0){
  340. showWarningToast("请拍照上传");
  341. return false;
  342. }
  343. Gson gson = new Gson();
  344. postParam.put("label_img",gson.toJson(photoList));
  345. postParam.put("url_code",urlCode);
  346. } catch (JSONException e) {
  347. showWarningToast("系统异常"+e.getMessage());
  348. e.printStackTrace();
  349. return false;
  350. }
  351. return true;
  352. }
  353. //sIsGoodinfoMatch,sIsBattery,sIsMagnetic,sIsWood,sIsExtra,sIsOther
  354. @OnClick({R.id.btn_scan_confirm,R.id.btn_confirm})
  355. public void onViewClicked(View view) {
  356. Bundle bundle = new Bundle();
  357. switch (view.getId()) {
  358. case R.id.btn_scan_confirm:
  359. if (urlCode.isEmpty() || (pictureSelectorUpload!=null && pictureSelectorUpload.getOssPhotoList().size()==0) ){
  360. ActivityScanerCode.setScanerListener(mScanerListener);
  361. ActivityScanerCode.intent2Activity(mContext, BaseConfig.COMMON_SCANNER_ONLY);
  362. }else{
  363. new AlertDialog.Builder(mContext)
  364. .setTitle(R.string.dl_hint)
  365. .setMessage("还拍照数据未上传,需要重新上传吗?")
  366. .setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
  367. @Override
  368. public void onClick(DialogInterface dialogInterface, int i) {
  369. dialogInterface.dismiss();
  370. }
  371. }).setPositiveButton(R.string.str_submit, new DialogInterface.OnClickListener() {
  372. @Override
  373. public void onClick(DialogInterface dialogInterface, int i) {
  374. //取消上传任务
  375. }
  376. }).show();
  377. }
  378. break;
  379. case R.id.btn_confirm:
  380. pictureInbound();
  381. break;
  382. default:
  383. break;
  384. }
  385. }
  386. @Override
  387. protected void onDestroy() {
  388. unbinder.unbind();
  389. super.onDestroy();
  390. }
  391. @Override
  392. protected void onResume() {
  393. super.onResume();
  394. initReceiver();
  395. }
  396. @Override
  397. protected void onPause() {
  398. super.onPause();
  399. //销毁在onResume()方法中的广播
  400. try {
  401. //停止扫描
  402. Integer is_broadcast = scanBean.getIsBroadcast();//是否广播
  403. if (is_broadcast==0){
  404. StoTongJNI.getIns(mContext).SetScannerStop();
  405. StoTongJNI.getIns(mContext).SetScannerOff();
  406. }else{
  407. unregisterReceiver(mReceiver);
  408. }
  409. }catch (Exception e){
  410. XLog.e("销毁广播失败:",e.getMessage());
  411. }
  412. }
  413. }