ScanDialog.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. if (listener!=null){
  175. listener.onClickListener(1000);
  176. }
  177. }
  178. @Override
  179. protected int getLayout() {
  180. return R.layout.dialog_scan;
  181. }
  182. @Override
  183. protected int getAnimType() {
  184. return BaseDialog.ANIM_CENTER_IN_OUT;
  185. }
  186. @Override
  187. public void onClick(View v) {
  188. switch (v.getId()) {
  189. case R.id.btn_cancel:
  190. //二維碼消失的時候開啓定時任務
  191. // startGetTime(msn);
  192. OrderStatusInquiryCancel(String.valueOf(msn));
  193. dismiss();
  194. break;
  195. case R.id.iv_qr_code:
  196. listener.onClickListener(-1);
  197. break;
  198. }
  199. }
  200. // class MyThread implements Runnable {
  201. //
  202. // @Override
  203. // public void run() {
  204. // Logger.d("倒计时run1:" + isLoop);
  205. // while (isLoop) {
  206. // try {
  207. // Thread.sleep(1000);//每隔1s执行一次
  208. // Logger.d("倒计时run2:" + isLoop);
  209. // Message message = new Message();
  210. // message.what = 1;
  211. // mHandler.sendMessage(message);
  212. // time--;
  213. // LogUtil.d(TAG, "thread: 倒计时:" + time);
  214. // } catch (InterruptedException e) {
  215. // e.printStackTrace();
  216. // }
  217. //
  218. // }
  219. // }
  220. // }
  221. /**
  222. * 是否为base64数据
  223. *
  224. * @param imgurl 图片地址
  225. * @return
  226. */
  227. public boolean isBase64Img(String imgurl) {
  228. if (!TextUtils.isEmpty(imgurl) &&
  229. (imgurl.startsWith("data:image/png;base64,")
  230. || imgurl.startsWith("data:image/*;base64,")
  231. || imgurl.startsWith("data:image/jpg;base64,"))
  232. ) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * base64转换成图片
  239. *
  240. * @param string
  241. * @return
  242. */
  243. public Bitmap stringToBitmap(String string) {
  244. Bitmap bitmap = null;
  245. try {
  246. byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
  247. bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
  248. } catch (Exception e) {
  249. e.printStackTrace();
  250. }
  251. return bitmap;
  252. }
  253. private boolean readDataFromAssets(String gifName) {
  254. if (!isExternalStorageWritable()) {
  255. return false;
  256. }
  257. InputStream inputStream = null;
  258. FileOutputStream fos = null;
  259. BufferedOutputStream bos = null;
  260. File dir = BaseApplication.getContext().getExternalFilesDir("gif");
  261. Log.e(TAG, "readDataFromAssets: dir = " + dir.getAbsolutePath());
  262. if (!dir.exists()) {
  263. dir.mkdirs();
  264. }
  265. try {
  266. inputStream = BaseApplication.getContext().getAssets().open(gifName);
  267. File file = new File(dir, gifName);
  268. if (file.exists()) {
  269. file.delete();
  270. }
  271. file.createNewFile();
  272. fos = new FileOutputStream(file);
  273. bos = new BufferedOutputStream(fos);
  274. byte[] bytes = new byte[1024];
  275. while (inputStream.read(bytes) > 0) {
  276. bos.write(bytes, 0, bytes.length);
  277. }
  278. //
  279. // inputStream.close();
  280. // bos.close();
  281. // fos.close();
  282. Logger.d("文件地址为:" + file.getAbsolutePath());
  283. GlideUtil.get().loadPic(file.getAbsolutePath(), ivScan);
  284. } catch (IOException e) {
  285. e.printStackTrace();
  286. return false;
  287. } finally {
  288. try {
  289. if (inputStream != null) {
  290. inputStream.close();
  291. }
  292. if (bos != null) {
  293. bos.close();
  294. }
  295. if (fos != null) {
  296. fos.close();
  297. }
  298. } catch (IOException e) {
  299. e.printStackTrace();
  300. }
  301. }
  302. return true;
  303. }
  304. /* Checks if external storage is available for read and write */
  305. public boolean isExternalStorageWritable() {
  306. String state = Environment.getExternalStorageState();
  307. if (Environment.MEDIA_MOUNTED.equals(state)) {
  308. return true;
  309. }
  310. Log.e(TAG, "isExternalStorageWritable: " + state);
  311. return false;
  312. }
  313. /**
  314. * 设置二维码类型
  315. *
  316. * @param frpCode
  317. */
  318. public void setGifType(int frpCode) {
  319. this.frpCode = frpCode;
  320. int param = (int) SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, 1);
  321. if (frpCode == 1) {
  322. //微信
  323. if (param == 1) {
  324. //中文
  325. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  326. // readDataFromAssets("wechat.gif");
  327. } else {
  328. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  329. // readDataFromAssets("wechat_E.gif");
  330. }
  331. }else if (frpCode == 10){
  332. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  333. llQuibinary.setVisibility(View.VISIBLE);
  334. } else {
  335. //支付宝
  336. if (param == 1) {
  337. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  338. // readDataFromAssets("alilpay.gif");
  339. } else {
  340. GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
  341. // readDataFromAssets("alipay_E.gif");
  342. }
  343. }
  344. }
  345. public void stopTimerDown() {
  346. LogUtil.d(TAG, "stopTimerDown: 停止倒计时1");
  347. // time=60;
  348. // isLoop=false;
  349. // thread=null;
  350. }
  351. private Timer countDownTimer;
  352. private TimerTask timerTask;
  353. public void startTimeCountDown() {
  354. time = 90;
  355. stopTimeDown();
  356. // isTimeFinishRun = true;
  357. LogUtil.d(TAG, " 开始倒计时 ");
  358. countDownTimer = new Timer();
  359. timerTask = new TimerTask() {
  360. @Override
  361. public void run() {
  362. Logger.d("倒计时run2:" + isLoop);
  363. Message message = new Message();
  364. message.what = 1;
  365. message.obj = msn;
  366. LogUtil.d("msn", msn);
  367. mHandler.sendMessage(message);
  368. time--;
  369. LogUtil.d(TAG, "thread: 倒计时:" + time);
  370. }
  371. };
  372. countDownTimer.schedule(timerTask, 0, 1000);
  373. }
  374. @Override
  375. public void stopTimeDown() {
  376. mHandler.removeCallbacksAndMessages(null);
  377. TimerUtil.stopTimerAndTimerTask(countDownTimer, timerTask);
  378. time = 90;
  379. }
  380. @Override
  381. protected void onStop() {
  382. stopTimeDown();
  383. super.onStop();
  384. }
  385. static class MyHandler extends Handler {
  386. private final Activity activity1;
  387. WeakReference<Dialog> mWeakReference;
  388. public MyHandler(Dialog activity, Activity activity1) {
  389. this.activity1 = activity1;
  390. mWeakReference = new WeakReference<Dialog>(activity);
  391. }
  392. @Override
  393. public void handleMessage(Message msg) {
  394. super.handleMessage(msg);
  395. ScanDialog dialog = (ScanDialog) mWeakReference.get();
  396. switch (msg.what) {
  397. case 1:
  398. if (dialog != null) {
  399. if (time <= -1) {
  400. //二維碼消失的時候開啓定時任務
  401. dialog.isLoop = false;
  402. try {
  403. LogUtil.d("activity1", "handleMessage: " + activity1);
  404. if (activity1 != null) {
  405. if (!activity1.isFinishing()) {
  406. dialog.dismiss();
  407. Log.d("activity1", "handleMessage:dismiss ");
  408. // dialog.startOrderInquiry(String.valueOf(msg.obj));
  409. } else {
  410. dialog.stopTimerDown();
  411. }
  412. } else {
  413. dialog.stopTimerDown();
  414. }
  415. //二維碼消失的時候開啓定時任務
  416. //dialog.startGetTime(String.valueOf(msg.obj));
  417. } catch (IllegalArgumentException e) {
  418. // dialog = null;
  419. }
  420. return;
  421. }else if(time == 80) {
  422. Global.makeStatus = Name.IN_PRODUCTION;
  423. BaseConstant.CANCEL_POLL = true;
  424. }else if(time <=70 && time >=60){
  425. if (dialog.count<=0) {
  426. Log.d("time", "handleMessage: "+dialog.count);
  427. dialog.count++;
  428. dialog.startOrderInquiry(String.valueOf(msg.obj));
  429. }
  430. }
  431. dialog.tvTime.setText(String.valueOf(time));
  432. // time--;
  433. }
  434. break;
  435. }
  436. }
  437. }
  438. //定時任務,用於判斷是否需要上次數據
  439. // public void startGetTime(String sn) {
  440. // Timer timeGet = new Timer();
  441. // TimerTask timerTaskGet = new TimerTask() {
  442. //
  443. // @Override
  444. // public void run() {
  445. // if (sn.equals(Global.mapOrderReference.get(sn))) {
  446. // //發送驗證是否付錢了
  447. // EventBus.getDefault().post(new PaymentMessage("pay_ment", sn));
  448. // LogUtil.d("發送驗證是否付錢了", "退款");
  449. // } else {
  450. // //不退款
  451. // LogUtil.d("發送驗證是否付錢了", "不退款");
  452. // Global.mapOrderReference.remove(sn);
  453. // }
  454. // timeGet.cancel();
  455. //
  456. // }
  457. // };
  458. // timeGet.schedule(timerTaskGet, 300000);
  459. // }
  460. //在二維碼界面收到支付成功。
  461. @Subscribe(threadMode = ThreadMode.MAIN)
  462. public void event(ApiMessageEvent messageEvent) {
  463. switch (messageEvent.getName()) {
  464. case "pay_success":
  465. //LogUtil.d("支付成功", "第三部:發送支付成功信息");
  466. //EventBus.getDefault().post(new ApiMessageEvent("scan_pay_success", null));
  467. break;
  468. }
  469. }
  470. /**
  471. * 查詢訂單是否支付
  472. * 1.二維碼自動消失,或者點擊了二維碼取消。那麽執行訂單查詢方法
  473. * 2.查詢到如果是pay_success,那麽發送scan_pay_success信息讓機器開始做糖,其他不管。
  474. * 3.如果是機器故障,那麽也會在幾分鐘后開始退款。
  475. */
  476. //定時任務,用於判斷是否需要上次數據
  477. @SuppressLint("LongLogTag")
  478. public void startOrderInquiry(String sn) {
  479. //發送驗證是否付錢了
  480. // PaySuccessConstant.sn = sn;
  481. LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
  482. LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
  483. EventBus.getDefault().post(new PaymentMessage("start_order_inquiry", sn));
  484. }
  485. @SuppressLint("LongLogTag")
  486. public void OrderStatusInquiryCancel(String sn) {
  487. //發送驗證是否付錢了
  488. // PaySuccessConstant.sn = sn;
  489. LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
  490. LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
  491. EventBus.getDefault().post(new PaymentMessage("start_order_inquiry_cancel", sn));
  492. }
  493. }