WHBreakingBatchFragment.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. package com.uas.uaspda.fragment;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.graphics.drawable.BitmapDrawable;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.support.v4.content.FileProvider;
  14. import android.text.Editable;
  15. import android.text.TextUtils;
  16. import android.text.TextWatcher;
  17. import android.util.Log;
  18. import android.view.Gravity;
  19. import android.view.KeyEvent;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.view.inputmethod.EditorInfo;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.Button;
  26. import android.widget.ImageView;
  27. import android.widget.LinearLayout;
  28. import android.widget.ListView;
  29. import android.widget.PopupWindow;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32. import com.alibaba.fastjson.JSON;
  33. import com.alibaba.fastjson.JSONArray;
  34. import com.android.volley.AuthFailureError;
  35. import com.android.volley.Request;
  36. import com.android.volley.RequestQueue;
  37. import com.android.volley.Response;
  38. import com.android.volley.VolleyError;
  39. import com.android.volley.toolbox.JsonObjectRequest;
  40. import com.android.volley.toolbox.StringRequest;
  41. import com.android.volley.toolbox.Volley;
  42. import com.google.gson.Gson;
  43. import com.uas.uaspda.R;
  44. import com.uas.uaspda.activity.FunctionActivity;
  45. import com.uas.uaspda.bean.WHBreakingBatchBreakingBtnClickEvent;
  46. import com.uas.uaspda.bean.WHBreakingBatchGetBarcodeData;
  47. import com.uas.uaspda.global.GloableParams;
  48. import com.uas.uaspda.tools.SharedPreUtil;
  49. import com.uas.uaspda.tools.VolleyUtil;
  50. import com.uas.uaspda.util.CameraUtil;
  51. import com.uas.uaspda.util.CommonUtil;
  52. import com.uas.uaspda.util.Constants;
  53. import com.uas.uaspda.util.FileUtils;
  54. import com.uas.uaspda.util.FragmentUtils;
  55. import com.uas.uaspda.util.JsonTools;
  56. import com.uas.uaspda.util.JsonUtils;
  57. import com.uas.uaspda.util.LogUtil;
  58. import com.uas.uaspda.util.PrintUtils;
  59. import com.uas.uaspda.view.ClearableEditText;
  60. import com.uuzuche.lib_zxing.activity.CaptureActivity;
  61. import com.uuzuche.lib_zxing.activity.CodeUtils;
  62. import org.json.JSONException;
  63. import org.json.JSONObject;
  64. import java.io.File;
  65. import java.io.IOException;
  66. import java.io.UnsupportedEncodingException;
  67. import java.net.URLEncoder;
  68. import java.util.ArrayList;
  69. import java.util.HashMap;
  70. import java.util.Iterator;
  71. import java.util.List;
  72. import java.util.Map;
  73. import java.util.Set;
  74. import java.util.concurrent.TimeUnit;
  75. import okhttp3.Call;
  76. import okhttp3.Callback;
  77. import okhttp3.FormBody;
  78. import okhttp3.OkHttpClient;
  79. import okhttp3.RequestBody;
  80. /**
  81. * @note:分拆批次
  82. */
  83. public class WHBreakingBatchFragment extends BaseFragment implements View.OnClickListener, View.OnFocusChangeListener {
  84. public static final String TAG = "barcodeEnterEvent";
  85. private static final int SCAN_BARCODE_CODE = 102;
  86. private final int SPLIT_BARCODE_FAIL = 0x11;
  87. private final int SPLIT_BARCODE_SUCCESS = 0x12;
  88. private View popView;
  89. private PopupWindow pop;
  90. private Button btnBreaking, btnBack, btnPrintf, btnClose, btnPrintfSet;
  91. private ImageView mScanImageView;
  92. private ClearableEditText etOrBarcode, etBarRemain;
  93. private StringRequest stringRequest, stringRequest3;
  94. private RequestQueue mRequestQueue;
  95. private TextView tvOrRemain;
  96. private List<WHBreakingBatchBreakingBtnClickEvent.DataBean> messages = new ArrayList<>();
  97. private ListView listView;
  98. private MyAdapter adapter;
  99. private String strPrintDataList;
  100. private JsonObjectRequest jsonRequest;
  101. private String mPrintUrl = "";
  102. private okhttp3.Request mRequest;
  103. private OkHttpClient mOkHttpClient;
  104. private boolean isConnected = false;
  105. private String mOldAddress = "";
  106. private int mPrintDpi = 203;
  107. private Handler mHandler = new Handler() {
  108. @Override
  109. public void handleMessage(Message msg) {
  110. super.handleMessage(msg);
  111. switch (msg.what) {
  112. case Constants.CONSTANT.PRINT_SUCCESS:
  113. if (progressDialog.isShowing())
  114. progressDialog.dismiss();
  115. closePopupWindow();
  116. Toast.makeText(mActivity, R.string.obtain_print_file_success, Toast.LENGTH_LONG).show();
  117. try {
  118. Intent intent = new Intent();
  119. ComponentName comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintPDF");
  120. intent = new Intent();
  121. intent.setComponent(comp);
  122. intent.setAction("android.intent.action.VIEW");
  123. intent.setType("application/pdf");
  124. intent.setData(Uri.fromFile(new File(Constants.CONSTANT.PRINT_FILE_PATH, Constants.CONSTANT.PRINT_FILE_NAME)));
  125. startActivity(intent);
  126. } catch (Exception e) {
  127. Toast.makeText(mActivity, "打印失败", Toast.LENGTH_LONG).show();
  128. e.printStackTrace();
  129. }
  130. break;
  131. case Constants.CONSTANT.PRINT_FAILED:
  132. if (progressDialog.isShowing())
  133. progressDialog.dismiss();
  134. Toast.makeText(mActivity, R.string.obtain_print_file_failed, Toast.LENGTH_LONG).show();
  135. break;
  136. case Constants.CONSTANT.PRINT_OVERLOAD:
  137. if (progressDialog.isShowing())
  138. progressDialog.dismiss();
  139. Toast.makeText(mActivity, R.string.obtain_print_file_overload, Toast.LENGTH_LONG).show();
  140. break;
  141. case SPLIT_BARCODE_SUCCESS:
  142. try {
  143. String s = (String) msg.obj;
  144. WHBreakingBatchBreakingBtnClickEvent wh = JsonTools.parseJsonToBean(s, WHBreakingBatchBreakingBtnClickEvent.class);
  145. if (wh.getData() != null) {
  146. for (int i = 0; i < wh.getData().size(); i++) {
  147. messages.add(wh.getData().get(i));
  148. }
  149. }
  150. strPrintDataList = new Gson().toJson(messages);
  151. //打开popupWindow;
  152. initPopupWindow(s);
  153. reset();
  154. } catch (Exception e) {
  155. }
  156. break;
  157. case SPLIT_BARCODE_FAIL:
  158. String s = (String) msg.obj;
  159. String errorStr = "请求失败";
  160. if (JsonUtils.validate(s)) {
  161. try {
  162. JSONObject errorObject = new JSONObject(s);
  163. String exceptionInfo = JsonUtils.optStringNotNull(errorObject, "exceptionInfo");
  164. if (TextUtils.isEmpty(exceptionInfo)) {
  165. errorStr = "系统错误";
  166. } else {
  167. errorStr = exceptionInfo;
  168. }
  169. } catch (JSONException e) {
  170. e.printStackTrace();
  171. }
  172. }
  173. if (getActivity() != null) {
  174. CommonUtil.makeNotice();
  175. CommonUtil.toastNoRepeat(getActivity(), errorStr);
  176. etOrBarcode.setText("");
  177. etBarRemain.setText("");
  178. tvOrRemain.setText("");
  179. btnBreaking.setEnabled(false);
  180. CommonUtil.editTextGetFocus(etOrBarcode);
  181. progressDialog.dismiss();
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. };
  189. private int mFocusId;
  190. @Override
  191. protected int getLayout() {
  192. return R.layout.fragment_whbreaking_batch;
  193. }
  194. @Override
  195. protected void initViews() {
  196. ((TextView) (getActivity().findViewById(R.id.tv_actionbar_withback))).setText(getString(R.string.title_breakbatch));
  197. ((FunctionActivity) mActivity).setScanIvVisible(true);
  198. btnBreaking = (Button) root.findViewById(R.id.btn_breaking);
  199. etOrBarcode = (ClearableEditText) root.findViewById(R.id.et_or_barcode);
  200. etBarRemain = (ClearableEditText) root.findViewById(R.id.et_bar_remain);
  201. mScanImageView = (ImageView) mActivity.findViewById(R.id.btn_actionbar_scan_iv);
  202. tvOrRemain = (TextView) root.findViewById(R.id.tv_or_remain);
  203. mOkHttpClient = new OkHttpClient.Builder()
  204. .sslSocketFactory(FileUtils.createSSLSocketFactory(), new FileUtils.TrustAllCerts())
  205. .hostnameVerifier(new FileUtils.TrustAllHostnameVerifier())
  206. .connectTimeout(30, TimeUnit.SECONDS)
  207. .readTimeout(30, TimeUnit.SECONDS).build();
  208. isConnected = (SharedPreUtil.getInt(mActivity, "printConnect", 0) == 1);
  209. mOldAddress = SharedPreUtil.getString(mActivity, "printAddress", "");
  210. mPrintDpi = SharedPreUtil.getInt(mActivity, "printDpi", 203);
  211. }
  212. @Override
  213. protected void initEvents() {
  214. btnBreaking.setOnClickListener(this);
  215. CommonUtil.editTextGetFocus(etOrBarcode);
  216. etOrBarcode.setOnFocusChangeListener(this);
  217. etOrBarcode.addTextChangedListener(new TextWatcher() {
  218. @Override
  219. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  220. }
  221. @Override
  222. public void onTextChanged(CharSequence s, int start, int before, int count) {
  223. }
  224. @Override
  225. public void afterTextChanged(Editable s) {
  226. if (s.length() == 0) {
  227. etBarRemain.setEnabled(false);
  228. etBarRemain.setText(null);
  229. } else {
  230. etBarRemain.setEnabled(true);
  231. }
  232. }
  233. });
  234. etOrBarcode.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  235. @Override
  236. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  237. if (actionId == EditorInfo.IME_ACTION_SEND
  238. || actionId == EditorInfo.IME_ACTION_DONE
  239. || (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
  240. String str = etOrBarcode.getText().toString().trim();
  241. if (TextUtils.isEmpty(str)) {
  242. etOrBarcode.setWarnIconVisible();
  243. return true;
  244. }
  245. if (!str.matches(Constants.REGEX.NO_CN_SYMBOL)) {
  246. CommonUtil.toastNoRepeat(getActivity(), getResources().getString(R.string.barcode_cannot_contain_special));
  247. etOrBarcode.setText("");
  248. etOrBarcode.postDelayed(new Runnable() {
  249. @Override
  250. public void run() {
  251. etOrBarcode.requestFocus();
  252. }
  253. }, 100);
  254. return true;
  255. }
  256. CommonUtil.closeKeybord(etOrBarcode, mActivity);
  257. //请求详细数据
  258. getLoadingView();
  259. barcodeEnterEvent(str);
  260. return true;
  261. }
  262. return false;
  263. }
  264. });
  265. mScanImageView.setOnClickListener(new View.OnClickListener() {
  266. @Override
  267. public void onClick(View v) {
  268. if (CameraUtil.hasCamera()) {
  269. if (root.findFocus() != null) {
  270. mFocusId = root.findFocus().getId();
  271. Intent intent = new Intent();
  272. intent.setClass(mActivity, CaptureActivity.class);
  273. startActivityForResult(intent, SCAN_BARCODE_CODE);
  274. }
  275. } else {
  276. CommonUtil.toastNoRepeat(mActivity, getString(R.string.no_camera_detected));
  277. }
  278. }
  279. });
  280. }
  281. @Override
  282. protected void initDatas() {
  283. }
  284. @Override
  285. public void onHiddenChanged(boolean hidden) {
  286. super.onHiddenChanged(hidden);
  287. if (!hidden) {
  288. ((TextView) (getActivity().findViewById(R.id.tv_actionbar_withback))).setText(getString(R.string.title_breakbatch));
  289. ((FunctionActivity) mActivity).setScanIvVisible(true);
  290. isConnected = (SharedPreUtil.getInt(mActivity, "printConnect", 0) == 1);
  291. mOldAddress = SharedPreUtil.getString(mActivity, "printAddress", "");
  292. mPrintDpi = SharedPreUtil.getInt(mActivity, "printDpi", 203);
  293. } else {
  294. ((FunctionActivity) mActivity).setScanIvVisible(true);
  295. }
  296. }
  297. @Override
  298. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  299. super.onActivityResult(requestCode, resultCode, data);
  300. if (resultCode != Activity.RESULT_OK) {
  301. return;
  302. }
  303. if (requestCode == SCAN_BARCODE_CODE && data != null) {
  304. if (data.getExtras() != null) {
  305. String result = data.getExtras().getString(CodeUtils.RESULT_STRING);
  306. if (mFocusId == R.id.et_or_barcode) {
  307. etOrBarcode.setText(result);
  308. etOrBarcode.setSelection(result.length());
  309. } else if (mFocusId == R.id.et_bar_remain) {
  310. etBarRemain.setText(result);
  311. etBarRemain.setSelection(result.length());
  312. }
  313. }
  314. }
  315. }
  316. @Override
  317. public boolean onKeyDown(int keyCode, KeyEvent event) {
  318. return false;
  319. }
  320. @Override
  321. public boolean onFragmentBackPressed() {
  322. return false;
  323. }
  324. /**
  325. * 创建PopupWindow
  326. *
  327. * @param s
  328. */
  329. protected void initPopupWindow(String s) {
  330. com.alibaba.fastjson.JSONObject resultObject = JSON.parseObject(s);
  331. JSONArray listArray = resultObject.getJSONArray("data");
  332. popView = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow_whbreaking_batch, null);
  333. listView = (ListView) popView.findViewById(R.id.list_view);
  334. btnPrintf = (Button) popView.findViewById(R.id.btn_print);
  335. btnPrintfSet = (Button) popView.findViewById(R.id.btn_print_setting);
  336. btnClose = (Button) popView.findViewById(R.id.btn_close);
  337. btnBack = (Button) popView.findViewById(R.id.btn_back);
  338. btnBack.setOnClickListener(this);
  339. btnClose.setOnClickListener(this);
  340. btnPrintf.setOnClickListener(new View.OnClickListener() {
  341. @Override
  342. public void onClick(View v) {
  343. if (isConnected) {
  344. progressDialog.show();
  345. printEvent(listArray);
  346. } else {
  347. closePopupWindow();
  348. BlueToothPrintFragment blueToothPrintFragment = new BlueToothPrintFragment();
  349. Bundle bundle = new Bundle();
  350. bundle.putString(Constants.KEY.BARCODE_PRINT_ARRAY, listArray.toString());
  351. blueToothPrintFragment.setArguments(bundle);
  352. FragmentUtils.switchFragment(WHBreakingBatchFragment.this, blueToothPrintFragment);
  353. }
  354. }
  355. });
  356. btnPrintfSet.setOnClickListener(new View.OnClickListener() {
  357. @Override
  358. public void onClick(View v) {
  359. closePopupWindow();
  360. BlueToothPrintFragment blueToothPrintFragment = new BlueToothPrintFragment();
  361. Bundle bundle = new Bundle();
  362. bundle.putString(Constants.KEY.BARCODE_PRINT_ARRAY, listArray.toString());
  363. blueToothPrintFragment.setArguments(bundle);
  364. FragmentUtils.switchFragment(WHBreakingBatchFragment.this, blueToothPrintFragment);
  365. }
  366. });
  367. adapter = new MyAdapter(getActivity(), R.layout.item_list_whbreakingbatchfragment, messages);
  368. listView.setAdapter(adapter);
  369. //当只有一条数据的时候可以加上
  370. if (messages.size() == 1) {
  371. btnBack.setVisibility(View.VISIBLE);
  372. } else {
  373. btnBack.setVisibility(View.GONE);
  374. }
  375. // PopupWindow实例化,参数的意思是 view,长(不包括状态栏),高(不包括状态栏),是否聚焦
  376. pop = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT,
  377. LinearLayout.LayoutParams.WRAP_CONTENT, true);
  378. pop.setAnimationStyle(R.style.MenuAnimationFade);
  379. pop.setBackgroundDrawable(new BitmapDrawable());
  380. pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
  381. @Override
  382. public void onDismiss() {
  383. closePopupWindow();
  384. }
  385. });
  386. // 弹出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮
  387. pop.showAtLocation(btnBreaking, Gravity.CENTER, 0, 0);
  388. CommonUtil.setBackgroundAlpha(mActivity, 0.5f);
  389. }
  390. private void printEvent(JSONArray printArray) {
  391. if (printArray != null && printArray.size() > 0) {
  392. boolean isFail = false;
  393. for (int i = 0; i < printArray.size(); i++) {
  394. try {
  395. com.alibaba.fastjson.JSONObject printObject = printArray.getJSONObject(i);
  396. int printResult = PrintUtils.printBar(mActivity, printObject.toString(), mPrintDpi, null);
  397. if (printResult <= 0) {
  398. isFail = true;
  399. }
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. }
  403. }
  404. closePopupWindow();
  405. progressDialog.dismiss();
  406. if (isFail) {
  407. isConnected = false;
  408. SharedPreUtil.saveInt(mActivity, "printConnect", 0);
  409. CommonUtil.toastNoRepeat(mActivity, "打印失败,请重新连接打印机");
  410. BlueToothPrintFragment blueToothPrintFragment = new BlueToothPrintFragment();
  411. Bundle bundle = new Bundle();
  412. bundle.putString(Constants.KEY.BARCODE_PRINT_ARRAY, printArray.toString());
  413. blueToothPrintFragment.setArguments(bundle);
  414. FragmentUtils.switchFragment(WHBreakingBatchFragment.this, blueToothPrintFragment);
  415. } else {
  416. CommonUtil.toastNoRepeat(mActivity, "打印成功");
  417. }
  418. } else {
  419. CommonUtil.toastNoRepeat(mActivity, "打印失败,条码数据为空");
  420. }
  421. }
  422. @Override
  423. public void onClick(View v) {
  424. switch (v.getId()) {
  425. case R.id.btn_breaking:
  426. String str = etBarRemain.getText().toString().trim();
  427. if (TextUtils.isEmpty(str) || TextUtils.isEmpty(tvOrRemain.getText().toString().trim()) || TextUtils.isEmpty(etOrBarcode.getText().toString().trim())) {
  428. etBarRemain.setWarnIconVisible();
  429. //etBarRemain.setHint("还有必填项值为空");
  430. Toast.makeText(getActivity().getApplicationContext(), "还有必填项值为空", Toast.LENGTH_SHORT).show();
  431. return;
  432. }
  433. if (!etOrBarcode.getText().toString().trim().matches(Constants.REGEX.NO_CN_SYMBOL)) {
  434. CommonUtil.toastNoRepeat(getActivity(), getResources().getString(R.string.barcode_cannot_contain_special));
  435. return;
  436. }
  437. // if (!str.matches(Constants.REGEX.NO_CN_SYMBOL)) {
  438. // CommonUtil.toastNoRepeat(getActivity(), "请输入正确的拆分数量");
  439. // return;
  440. // }
  441. if (!str.matches(Constants.REGEX.IS_FLOAT)) {
  442. Toast.makeText(getActivity(), "请输入正确的拆分数量", Toast.LENGTH_SHORT).show();
  443. return;
  444. }
  445. double i = Double.parseDouble(str);
  446. double sum = Double.parseDouble(tvOrRemain.getText().toString().trim());
  447. if (i <= 0 || i >= sum) {
  448. etBarRemain.setWarnIconVisible();
  449. //etBarRemain.setHint("拆分数量必须在大于0,小于批数量");
  450. Toast.makeText(getActivity().getApplicationContext(), "拆分数量必须大于0,小于批数量", Toast.LENGTH_SHORT).show();
  451. return;
  452. }
  453. //隐藏软键盘
  454. CommonUtil.closeKeybord(etBarRemain, mActivity);
  455. //请求详细数据
  456. breakingBtnClickEvent();
  457. break;
  458. case R.id.btn_back:
  459. closePopupWindow();
  460. if (messages != null && messages.size() > 0) {
  461. tvOrRemain.setText(CommonUtil.doubleFormat(messages.get(0).getBAR_REMAIN()));
  462. CommonUtil.editTextGetFocus(etBarRemain);
  463. }
  464. break;
  465. case R.id.btn_print:
  466. if (!CommonUtil.appIsInstalled(getActivity(), Constants.CONSTANT.PRINT_SHARE_PACKAGE)) {
  467. new android.app.AlertDialog.Builder(getActivity()).setTitle("提示")
  468. .setMessage("您还未安装打印程序,点击确认开始安装")
  469. .setPositiveButton("确认", new DialogInterface.OnClickListener() {
  470. @Override
  471. public void onClick(DialogInterface dialog, int which) {
  472. Intent intent = new Intent(Intent.ACTION_VIEW);
  473. File assetsFileToCacheDir = FileUtils.getAssetsFileToCacheDir(getActivity(), "PrinterShare.apk");
  474. if (Build.VERSION.SDK_INT >= 24) {
  475. Uri apkUri = FileProvider.getUriForFile(mActivity, "com.uaspda.fileprovider", assetsFileToCacheDir);
  476. //Granting Temporary Permissions to a URI
  477. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  478. intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  479. } else {
  480. intent.setDataAndType(Uri.fromFile(assetsFileToCacheDir), "application/vnd.android.package-archive");
  481. }
  482. getActivity().startActivity(intent);
  483. }
  484. })
  485. .setNegativeButton("取消", null).create().show();
  486. } else {
  487. getPrintData();
  488. }
  489. break;
  490. case R.id.btn_close:
  491. closePopupWindow();
  492. reset();
  493. break;
  494. }
  495. }
  496. /**
  497. * 关闭窗口
  498. */
  499. private void closePopupWindow() {
  500. if (pop != null) {
  501. pop.dismiss();
  502. pop = null;
  503. CommonUtil.setBackgroundAlpha(mActivity, 1f);
  504. }
  505. }
  506. @Override
  507. public void onDestroy() {
  508. progressDialog.dismiss();
  509. CommonUtil.closeKeybord(etOrBarcode, mActivity);
  510. CommonUtil.closeKeybord(etBarRemain, mActivity);
  511. ((TextView) (getActivity().findViewById(R.id.tv_actionbar_withback))).setText(R.string.title_batch_content);
  512. ((FunctionActivity) mActivity).setScanIvVisible(false);
  513. super.onDestroy();
  514. }
  515. @Override
  516. public void onStop() {
  517. super.onStop();
  518. if (mRequestQueue != null) {
  519. mRequestQueue.cancelAll(TAG);
  520. }
  521. }
  522. /**
  523. * 文本焦点改变
  524. */
  525. @Override
  526. public void onFocusChange(View v, boolean hasFocus) {
  527. switch (v.getId()) {
  528. case R.id.et_or_barcode:
  529. if (!hasFocus && isVisible()) {
  530. String str = etOrBarcode.getText().toString().trim();
  531. if (TextUtils.isEmpty(str)) {
  532. etOrBarcode.setWarnIconVisible();
  533. return;
  534. }
  535. if (!str.matches(Constants.REGEX.NO_CN_SYMBOL)) {
  536. CommonUtil.toastNoRepeat(getActivity(), getResources().getString(R.string.barcode_cannot_contain_special));
  537. etOrBarcode.setText("");
  538. etOrBarcode.postDelayed(new Runnable() {
  539. @Override
  540. public void run() {
  541. etOrBarcode.requestFocus();
  542. }
  543. }, 100);
  544. return;
  545. }
  546. CommonUtil.closeKeybord(etOrBarcode, mActivity);
  547. //请求详细数据
  548. getLoadingView();
  549. barcodeEnterEvent(str);
  550. }
  551. break;
  552. }
  553. }
  554. /**
  555. * 显示loadingView
  556. */
  557. private void getLoadingView() {
  558. if (!progressDialog.isShowing()) {
  559. progressDialog.show();
  560. } else {
  561. progressDialog.dismiss();
  562. }
  563. }
  564. /**
  565. * 当编辑框里面的内容完成的时候,自动的,同时获取服务器的批量数
  566. */
  567. private void barcodeEnterEvent(String outboxcode) {
  568. mRequestQueue = Volley.newRequestQueue(getActivity());
  569. String url = null;
  570. try {
  571. url = GloableParams.ADDRESS_BARCODEENTEREVENT + "?code=" + URLEncoder.encode(outboxcode, "utf-8") + "&pr_ismsd=" + "false";
  572. } catch (UnsupportedEncodingException e) {
  573. e.printStackTrace();
  574. }
  575. stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
  576. @Override
  577. public void onResponse(String s) {
  578. LogUtil.e("msg", "ssss" + s);
  579. etBarRemain.setEnabled(true);
  580. etBarRemain.requestFocus();
  581. WHBreakingBatchGetBarcodeData whBreakingBatchGetBarcodeData = JsonTools.parseJsonToBean(s, WHBreakingBatchGetBarcodeData.class);
  582. tvOrRemain.setText(whBreakingBatchGetBarcodeData.getData().getData().getBAR_REMAIN() + "");
  583. getLoadingView();
  584. CommonUtil.setEnabled(btnBreaking);
  585. }
  586. }, new Response.ErrorListener() {
  587. @Override
  588. public void onErrorResponse(VolleyError volleyError) {
  589. etOrBarcode.setText("");
  590. etOrBarcode.requestFocus();
  591. CommonUtil.showErrorToast(volleyError);
  592. getLoadingView();
  593. }
  594. }) {
  595. @Override
  596. public Map<String, String> getHeaders() throws AuthFailureError {
  597. String cookie = SharedPreUtil.getString(getActivity().getApplicationContext(), "cookie", null);
  598. Map<String, String> headers = new HashMap<>();
  599. headers.put("Cookie", cookie);
  600. return headers;
  601. }
  602. };
  603. stringRequest.setTag(TAG);
  604. mRequestQueue.add(stringRequest);
  605. }
  606. /**
  607. * 分拆按钮点击事件,获取数据,同时获取popupWindow的数据
  608. */
  609. private void breakingBtnClickEvent() {
  610. messages.clear();
  611. progressDialog.show();
  612. String url = GloableParams.ADDRESS_BREAKINGBTNCLICKEVENT;
  613. Log.d("breakDeal", url);
  614. RequestBody requestBody = new FormBody.Builder()
  615. .add("or_barcode", etOrBarcode.getText().toString().trim())
  616. .add("or_remain", tvOrRemain.getText().toString().trim())
  617. .add("bar_remain", etBarRemain.getText().toString().trim())
  618. .build();
  619. okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(url).post(requestBody);
  620. Map<String, String> cookiesMap = VolleyUtil.getVolleyUtil().setCookies();
  621. if (cookiesMap != null) {
  622. Set<String> keys = cookiesMap.keySet();
  623. Iterator<String> iterator = keys.iterator();
  624. while (iterator.hasNext()) {
  625. String key = iterator.next();
  626. String value = cookiesMap.get(key);
  627. builder.addHeader(key, value);
  628. }
  629. }
  630. mRequest = builder.build();
  631. mOkHttpClient.newCall(mRequest).enqueue(new Callback() {
  632. @Override
  633. public void onFailure(Call call, IOException e) {
  634. progressDialog.dismiss();
  635. Message message = Message.obtain();
  636. message.what = SPLIT_BARCODE_FAIL;
  637. message.obj = "{\"exceptionInfo\":\"条码拆分异常\"}";
  638. mHandler.sendMessage(message);
  639. }
  640. @Override
  641. public void onResponse(Call call, okhttp3.Response response) throws IOException {
  642. progressDialog.dismiss();
  643. String s = response.body().string();
  644. if (response.code() == 200) {
  645. Message message = Message.obtain();
  646. message.what = SPLIT_BARCODE_SUCCESS;
  647. message.obj = s;
  648. mHandler.sendMessage(message);
  649. } else {
  650. Message message = Message.obtain();
  651. message.what = SPLIT_BARCODE_FAIL;
  652. message.obj = s;
  653. mHandler.sendMessage(message);
  654. }
  655. }
  656. });
  657. /*PdaApplication.mRequestQueue.cancelAll(TAG + "break");
  658. stringRequest = new StringRequest(Request.Method.POST, GloableParams.ADDRESS_BREAKINGBTNCLICKEVENT,
  659. new Response.Listener<String>() {
  660. @Override
  661. public void onResponse(String s) {
  662. LogUtil.e("breaking", s);
  663. WHBreakingBatchBreakingBtnClickEvent wh = JsonTools.parseJsonToBean(s, WHBreakingBatchBreakingBtnClickEvent.class);
  664. if (wh.getData() != null) {
  665. for (int i = 0; i < wh.getData().size(); i++) {
  666. messages.add(wh.getData().get(i));
  667. }
  668. }
  669. strPrintDataList = new Gson().toJson(messages);
  670. //打开popupWindow;
  671. initPopupWindow();
  672. reset();
  673. }
  674. },
  675. new Response.ErrorListener() {
  676. @Override
  677. public void onErrorResponse(VolleyError volleyError) {
  678. CommonUtil.showErrorToast(volleyError);
  679. etOrBarcode.setText("");
  680. etBarRemain.setText("");
  681. tvOrRemain.setText("");
  682. btnBreaking.setEnabled(false);
  683. CommonUtil.editTextGetFocus(etOrBarcode);
  684. progressDialog.dismiss();
  685. }
  686. }) {
  687. @Override
  688. protected Map<String, String> getParams() throws AuthFailureError {
  689. Map<String, String> params = new HashMap<>();
  690. params.put("or_barcode", etOrBarcode.getText().toString().trim());
  691. params.put("or_remain", tvOrRemain.getText().toString().trim());
  692. params.put("bar_remain", etBarRemain.getText().toString().trim());
  693. return params;
  694. }
  695. @Override
  696. public Map<String, String> getHeaders() throws AuthFailureError {
  697. return VolleyUtil.getVolleyUtil().setCookies();
  698. }
  699. };
  700. stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 0, 1f));
  701. stringRequest.setTag(TAG + "break");
  702. PdaApplication.mRequestQueue.add(stringRequest);*/
  703. }
  704. /**
  705. * popupWindow 设置适配器
  706. */
  707. private class MyAdapter extends ArrayAdapter<WHBreakingBatchBreakingBtnClickEvent.DataBean> {
  708. private int resourceId;
  709. public MyAdapter(Context context, int resource, List<WHBreakingBatchBreakingBtnClickEvent.DataBean> objects) {
  710. super(context, resource, objects);
  711. resourceId = resource;
  712. }
  713. @Override
  714. public View getView(int position, View convertView, ViewGroup parent) {
  715. //WHBreakingBatchBreakingBtnClickEvent.Message target = getItem(position);
  716. WHBreakingBatchBreakingBtnClickEvent.DataBean person = getItem(position);
  717. convertView = LayoutInflater.from(getContext()).inflate(resourceId,
  718. null);
  719. TextView tv_bar_code = (TextView) convertView.findViewById(R.id.tv_bar_code);
  720. TextView tv_bar_remain = (TextView) convertView.findViewById(R.id.tv_bar_remain);
  721. // tv_bar_code.setText(target.bar_code);
  722. // tv_bar_remain.setText(target.bar_remain);
  723. tv_bar_code.setText(person.getBAR_CODE());
  724. tv_bar_remain.setText(CommonUtil.doubleFormat(person.getBAR_REMAIN()));
  725. return convertView;
  726. }
  727. }
  728. /*打印*/
  729. public static final String GETPRINTDATA = "getPrintData";
  730. private void getPrintData() {
  731. progressDialog.show();
  732. String url = GloableParams.ADDRESS_LABELPRINT + "?data=" + strPrintDataList;
  733. stringRequest3 = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
  734. @Override
  735. public void onResponse(String s) {
  736. try {
  737. JSONObject resultObject = new JSONObject(s);
  738. if (resultObject.optBoolean("success")) {
  739. mPrintUrl = resultObject.optString("data");
  740. FileUtils.printBarcode(mPrintUrl, mHandler);
  741. }
  742. } catch (JSONException e) {
  743. if (progressDialog.isShowing())
  744. progressDialog.dismiss();
  745. e.printStackTrace();
  746. }
  747. }
  748. }, new Response.ErrorListener() {
  749. @Override
  750. public void onErrorResponse(VolleyError volleyError) {
  751. if (progressDialog.isShowing())
  752. progressDialog.dismiss();
  753. CommonUtil.showErrorToast(volleyError);
  754. }
  755. }) {
  756. @Override
  757. public Map<String, String> getHeaders() throws AuthFailureError {
  758. Map map = VolleyUtil.getVolleyUtil().setCookies();
  759. //不用改动
  760. map.put("Content-Type", "text/html; charset=UTF-8");
  761. return map;
  762. }
  763. };
  764. stringRequest3.setTag(GETPRINTDATA);
  765. mRequestQueue.add(stringRequest3);
  766. }
  767. public void reset() {
  768. etOrBarcode.getText().clear();
  769. etBarRemain.getText().clear();
  770. tvOrRemain.setText("");
  771. btnBreaking.setEnabled(false);
  772. CommonUtil.editTextGetFocus(etOrBarcode);
  773. progressDialog.dismiss();
  774. }
  775. }