ScanDialog.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package com.bgy.autosale.payutil;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.drawable.Drawable;
  9. import android.os.Environment;
  10. import android.os.Handler;
  11. import android.os.IBinder;
  12. import android.os.Message;
  13. import android.text.TextUtils;
  14. import android.util.Base64;
  15. import android.util.Log;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.inputmethod.InputMethodManager;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ImageView;
  22. import android.widget.LinearLayout;
  23. import android.widget.RadioButton;
  24. import android.widget.RadioGroup;
  25. import android.widget.RelativeLayout;
  26. import android.widget.TextView;
  27. import com.bgy.autosale.R;
  28. import com.bumptech.glide.Glide;
  29. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  30. import com.bumptech.glide.request.RequestOptions;
  31. import com.hboxs.base_library.base.BaseApplication;
  32. import com.hboxs.base_library.callback.DialogClickListener;
  33. import com.hboxs.base_library.constant.BaseConstant;
  34. import com.hboxs.base_library.constant.Global;
  35. import com.hboxs.base_library.constant.Name;
  36. import com.hboxs.base_library.event.ApiMessageEvent;
  37. import com.hboxs.base_library.util.DrawableSelectorUtils;
  38. import com.hboxs.base_library.util.GlideUtil;
  39. import com.hboxs.base_library.util.LogUtil;
  40. import com.hboxs.base_library.util.LogUtils;
  41. import com.hboxs.base_library.util.SharedPreferencesUtils;
  42. import com.hboxs.base_library.util.ToastUtil;
  43. import com.hboxs.base_library.util.UiUtil;
  44. import com.hboxs.base_library.widget.BaseDialog;
  45. import com.hboxs.base_library.widget.BaseDialogNoDatabinding;
  46. import com.orhanobut.hawk.Hawk;
  47. import com.orhanobut.logger.Logger;
  48. import org.greenrobot.eventbus.EventBus;
  49. import org.greenrobot.eventbus.Subscribe;
  50. import org.greenrobot.eventbus.ThreadMode;
  51. import java.io.BufferedOutputStream;
  52. import java.io.ByteArrayOutputStream;
  53. import java.io.File;
  54. import java.io.FileOutputStream;
  55. import java.io.IOException;
  56. import java.io.InputStream;
  57. import java.lang.ref.WeakReference;
  58. import java.util.Timer;
  59. import java.util.TimerTask;
  60. /**
  61. * @author whw
  62. * @time 2019/3/13
  63. * @Description 一个输入框弹框
  64. */
  65. public class ScanDialog extends BaseDialogNoDatabinding implements View.OnClickListener {
  66. private String TAG = this.getClass().getSimpleName();
  67. private ImageView ivScan;
  68. private ImageView ivQrCode;
  69. private TextView tvScanNow;
  70. private TextView tvScanTip;
  71. private RelativeLayout rlTime;
  72. private TextView tvTime;
  73. private TextView tvSecond;
  74. private TextView tvGuanbi;
  75. private Button btnCancel;
  76. private int count;
  77. private static int time = 90;
  78. private boolean isLoop = true;
  79. private Thread thread;
  80. private MyHandler mHandler;
  81. private String data;
  82. private int frpCode;
  83. private String msn;
  84. private LinearLayout llQuibinary;
  85. public void setListener(com.hboxs.base_library.callback.DialogClickListener listener) {
  86. this.listener = listener;
  87. }
  88. private DialogClickListener listener;
  89. public ScanDialog(Context context, Activity activity) {
  90. super(context, activity);
  91. }
  92. //設置訂單編號的數據
  93. public void setCodeData(String data, String sn) {
  94. this.data = data;
  95. this.msn = sn;
  96. LogUtil.d("sn", sn);
  97. if (isBase64Img(data)) {
  98. Bitmap bitmap = stringToBitmap(data);
  99. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  100. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  101. byte[] bytes = baos.toByteArray();
  102. //禁止缓存
  103. RequestOptions options = new RequestOptions()
  104. .diskCacheStrategy(DiskCacheStrategy.NONE)
  105. .skipMemoryCache(true);
  106. Glide.with(getContext())
  107. .load(bytes)
  108. .apply(options)
  109. .into(ivQrCode);
  110. } else {
  111. Bitmap bitmap = ZXingUtils.createQRImage(data, 600, 600);
  112. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  113. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  114. byte[] bytes = baos.toByteArray();
  115. //禁止缓存
  116. RequestOptions options = new RequestOptions()
  117. .diskCacheStrategy(DiskCacheStrategy.NONE)
  118. .skipMemoryCache(true);
  119. Glide.with(getContext())
  120. .load(bytes)
  121. .apply(options)
  122. .into(ivQrCode);
  123. }
  124. }
  125. @Override
  126. protected void initView() {
  127. mHandler = new MyHandler(this, mActivity);
  128. ivScan = findViewById(R.id.iv_scan);
  129. ivQrCode = findViewById(R.id.iv_qr_code);
  130. tvScanNow = findViewById(R.id.tv_scan_now);
  131. tvScanTip = findViewById(R.id.tv_scan_tip);
  132. rlTime = findViewById(R.id.rl_time);
  133. tvTime = findViewById(R.id.tv_time);
  134. tvSecond = findViewById(R.id.tv_second);
  135. tvGuanbi = findViewById(R.id.tv_guanbi);
  136. btnCancel = findViewById(R.id.btn_cancel);
  137. llQuibinary = findViewById(R.id.ll_quibinary);
  138. btnCancel.setOnClickListener(this);
  139. ivQrCode.setOnClickListener(this);
  140. isLoop = true;
  141. Logger.d("倒计时initview1:" + isLoop);
  142. }
  143. @Override
  144. public void show() {
  145. count = 0;
  146. LogUtils.logWrite("支付对话框显示了。");
  147. super.show();
  148. EventBus.getDefault().register(this);
  149. time = 90;
  150. LogUtil.d("关闭", "show: scandialog");
  151. tvTime.setText(String.valueOf(time));
  152. // isLoop = true;
  153. // thread = new Thread(new MyThread());
  154. // thread.start();
  155. startTimeCountDown();
  156. listener.onClickListener(130);
  157. Logger.d("倒计时show:");
  158. }
  159. @Override
  160. public void dismiss() {
  161. super.dismiss();
  162. LogUtils.logWrite("支付取消了");
  163. stopTimeDown();
  164. EventBus.getDefault().unregister(this);
  165. // stopTimeDown();
  166. isLoop = false;
  167. thread = null;
  168. if (listener != null) {
  169. listener.onClickListener(126);
  170. listener.onClickListener(125);
  171. }
  172. Logger.d("倒计时dismiss:");
  173. // stopTimeDown();有可能不被执行
  174. }
  175. @Override
  176. protected int getLayout() {
  177. return R.layout.dialog_scan;
  178. }
  179. @Override
  180. protected int getAnimType() {
  181. return BaseDialog.ANIM_CENTER_IN_OUT;
  182. }
  183. @Override
  184. public void onClick(View v) {
  185. switch (v.getId()) {
  186. case R.id.btn_cancel:
  187. //二維碼消失的時候開啓定時任務
  188. // startGetTime(msn);
  189. OrderStatusInquiryCancel(String.valueOf(msn));
  190. dismiss();
  191. break;
  192. case R.id.iv_qr_code:
  193. listener.onClickListener(-1);
  194. break;
  195. }
  196. }
  197. // class MyThread implements Runnable {
  198. //
  199. // @Override
  200. // public void run() {
  201. // Logger.d("倒计时run1:" + isLoop);
  202. // while (isLoop) {
  203. // try {
  204. // Thread.sleep(1000);//每隔1s执行一次
  205. // Logger.d("倒计时run2:" + isLoop);
  206. // Message message = new Message();
  207. // message.what = 1;
  208. // mHandler.sendMessage(message);
  209. // time--;
  210. // LogUtil.d(TAG, "thread: 倒计时:" + time);
  211. // } catch (InterruptedException e) {
  212. // e.printStackTrace();
  213. // }
  214. //
  215. // }
  216. // }
  217. // }
  218. /**
  219. * 是否为base64数据
  220. *
  221. * @param imgurl 图片地址
  222. * @return
  223. */
  224. public boolean isBase64Img(String imgurl) {
  225. if (!TextUtils.isEmpty(imgurl) &&
  226. (imgurl.startsWith("data:image/png;base64,")
  227. || imgurl.startsWith("data:image/*;base64,")
  228. || imgurl.startsWith("data:image/jpg;base64,"))
  229. ) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * base64转换成图片
  236. *
  237. * @param string
  238. * @return
  239. */
  240. public Bitmap stringToBitmap(String string) {
  241. Bitmap bitmap = null;
  242. try {
  243. byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
  244. bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
  245. } catch (Exception e) {
  246. e.printStackTrace();
  247. }
  248. return bitmap;
  249. }
  250. private boolean readDataFromAssets(String gifName) {
  251. if (!isExternalStorageWritable()) {
  252. return false;
  253. }
  254. InputStream inputStream = null;
  255. FileOutputStream fos = null;
  256. BufferedOutputStream bos = null;
  257. File dir = BaseApplication.getContext().getExternalFilesDir("gif");
  258. Log.e(TAG, "readDataFromAssets: dir = " + dir.getAbsolutePath());
  259. if (!dir.exists()) {
  260. dir.mkdirs();
  261. }
  262. try {
  263. inputStream = BaseApplication.getContext().getAssets().open(gifName);
  264. File file = new File(dir, gifName);
  265. if (file.exists()) {
  266. file.delete();
  267. }
  268. file.createNewFile();
  269. fos = new FileOutputStream(file);
  270. bos = new BufferedOutputStream(fos);
  271. byte[] bytes = new byte[1024];
  272. while (inputStream.read(bytes) > 0) {
  273. bos.write(bytes, 0, bytes.length);
  274. }
  275. //
  276. // inputStream.close();
  277. // bos.close();
  278. // fos.close();
  279. Logger.d("文件地址为:" + file.getAbsolutePath());
  280. GlideUtil.get().loadPic(file.getAbsolutePath(), ivScan);
  281. } catch (IOException e) {
  282. e.printStackTrace();
  283. return false;
  284. } finally {
  285. try {
  286. if (inputStream != null) {
  287. inputStream.close();
  288. }
  289. if (bos != null) {
  290. bos.close();
  291. }
  292. if (fos != null) {
  293. fos.close();
  294. }
  295. } catch (IOException e) {
  296. e.printStackTrace();
  297. }
  298. }
  299. return true;
  300. }
  301. /* Checks if external storage is available for read and write */
  302. public boolean isExternalStorageWritable() {
  303. String state = Environment.getExternalStorageState();
  304. if (Environment.MEDIA_MOUNTED.equals(state)) {
  305. return true;
  306. }
  307. Log.e(TAG, "isExternalStorageWritable: " + state);
  308. return false;
  309. }
  310. /**
  311. * 设置二维码类型
  312. *
  313. * @param frpCode
  314. */
  315. public void setGifType(int frpCode) {
  316. this.frpCode = frpCode;
  317. int param = (int) SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, 1);
  318. if (frpCode == 1) {
  319. //微信
  320. if (param == 1) {
  321. //中文
  322. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  323. // readDataFromAssets("wechat.gif");
  324. } else {
  325. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  326. // readDataFromAssets("wechat_E.gif");
  327. }
  328. }else if (frpCode == 10){
  329. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  330. llQuibinary.setVisibility(View.VISIBLE);
  331. } else {
  332. //支付宝
  333. if (param == 1) {
  334. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  335. // readDataFromAssets("alilpay.gif");
  336. } else {
  337. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  338. // readDataFromAssets("alipay_E.gif");
  339. }
  340. }
  341. }
  342. public void stopTimerDown() {
  343. LogUtil.d(TAG, "stopTimerDown: 停止倒计时1");
  344. // time=60;
  345. // isLoop=false;
  346. // thread=null;
  347. }
  348. private Timer countDownTimer;
  349. private TimerTask timerTask;
  350. public void startTimeCountDown() {
  351. time = 90;
  352. stopTimeDown();
  353. // isTimeFinishRun = true;
  354. LogUtil.d(TAG, " 开始倒计时 ");
  355. countDownTimer = new Timer();
  356. timerTask = new TimerTask() {
  357. @Override
  358. public void run() {
  359. Logger.d("倒计时run2:" + isLoop);
  360. Message message = new Message();
  361. message.what = 1;
  362. message.obj = msn;
  363. LogUtil.d("msn", msn);
  364. mHandler.sendMessage(message);
  365. time--;
  366. LogUtil.d(TAG, "thread: 倒计时:" + time);
  367. }
  368. };
  369. countDownTimer.schedule(timerTask, 0, 1000);
  370. }
  371. @Override
  372. public void stopTimeDown() {
  373. mHandler.removeCallbacksAndMessages(null);
  374. TimerUtil.stopTimerAndTimerTask(countDownTimer, timerTask);
  375. time = 60;
  376. }
  377. @Override
  378. protected void onStop() {
  379. stopTimeDown();
  380. super.onStop();
  381. }
  382. static class MyHandler extends Handler {
  383. private final Activity activity1;
  384. WeakReference<Dialog> mWeakReference;
  385. public MyHandler(Dialog activity, Activity activity1) {
  386. this.activity1 = activity1;
  387. mWeakReference = new WeakReference<Dialog>(activity);
  388. }
  389. @Override
  390. public void handleMessage(Message msg) {
  391. super.handleMessage(msg);
  392. ScanDialog dialog = (ScanDialog) mWeakReference.get();
  393. switch (msg.what) {
  394. case 1:
  395. if (dialog != null) {
  396. if (time <= -1) {
  397. //二維碼消失的時候開啓定時任務
  398. dialog.isLoop = false;
  399. try {
  400. LogUtil.d("activity1", "handleMessage: " + activity1);
  401. if (activity1 != null) {
  402. if (!activity1.isFinishing()) {
  403. dialog.dismiss();
  404. Log.d("activity1", "handleMessage:dismiss ");
  405. // dialog.startOrderInquiry(String.valueOf(msg.obj));
  406. } else {
  407. dialog.stopTimerDown();
  408. }
  409. } else {
  410. dialog.stopTimerDown();
  411. }
  412. //二維碼消失的時候開啓定時任務
  413. //dialog.startGetTime(String.valueOf(msg.obj));
  414. } catch (IllegalArgumentException e) {
  415. // dialog = null;
  416. }
  417. return;
  418. }else if(time == 80) {
  419. Global.makeStatus = Name.IN_PRODUCTION;
  420. BaseConstant.CANCEL_POLL = true;
  421. }else if(time <=70 && time >=60){
  422. if (dialog.count<=0) {
  423. Log.d("time", "handleMessage: "+dialog.count);
  424. dialog.count++;
  425. dialog.startOrderInquiry(String.valueOf(msg.obj));
  426. }
  427. }
  428. dialog.tvTime.setText(String.valueOf(time));
  429. // time--;
  430. }
  431. break;
  432. }
  433. }
  434. }
  435. //定時任務,用於判斷是否需要上次數據
  436. // public void startGetTime(String sn) {
  437. // Timer timeGet = new Timer();
  438. // TimerTask timerTaskGet = new TimerTask() {
  439. //
  440. // @Override
  441. // public void run() {
  442. // if (sn.equals(Global.mapOrderReference.get(sn))) {
  443. // //發送驗證是否付錢了
  444. // EventBus.getDefault().post(new PaymentMessage("pay_ment", sn));
  445. // LogUtil.d("發送驗證是否付錢了", "退款");
  446. // } else {
  447. // //不退款
  448. // LogUtil.d("發送驗證是否付錢了", "不退款");
  449. // Global.mapOrderReference.remove(sn);
  450. // }
  451. // timeGet.cancel();
  452. //
  453. // }
  454. // };
  455. // timeGet.schedule(timerTaskGet, 300000);
  456. // }
  457. //在二維碼界面收到支付成功。
  458. @Subscribe(threadMode = ThreadMode.MAIN)
  459. public void event(ApiMessageEvent messageEvent) {
  460. switch (messageEvent.getName()) {
  461. case "pay_success":
  462. //LogUtil.d("支付成功", "第三部:發送支付成功信息");
  463. //EventBus.getDefault().post(new ApiMessageEvent("scan_pay_success", null));
  464. break;
  465. }
  466. }
  467. /**
  468. * 查詢訂單是否支付
  469. * 1.二維碼自動消失,或者點擊了二維碼取消。那麽執行訂單查詢方法
  470. * 2.查詢到如果是pay_success,那麽發送scan_pay_success信息讓機器開始做糖,其他不管。
  471. * 3.如果是機器故障,那麽也會在幾分鐘后開始退款。
  472. */
  473. //定時任務,用於判斷是否需要上次數據
  474. @SuppressLint("LongLogTag")
  475. public void startOrderInquiry(String sn) {
  476. //發送驗證是否付錢了
  477. // PaySuccessConstant.sn = sn;
  478. LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
  479. LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
  480. EventBus.getDefault().post(new PaymentMessage("start_order_inquiry", sn));
  481. }
  482. @SuppressLint("LongLogTag")
  483. public void OrderStatusInquiryCancel(String sn) {
  484. //發送驗證是否付錢了
  485. // PaySuccessConstant.sn = sn;
  486. LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
  487. LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
  488. EventBus.getDefault().post(new PaymentMessage("start_order_inquiry_cancel", sn));
  489. }
  490. }