123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- package com.bgy.autosale.payutil;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.Drawable;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.text.TextUtils;
- import android.util.Base64;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.bgy.autosale.R;
- import com.bumptech.glide.Glide;
- import com.bumptech.glide.load.engine.DiskCacheStrategy;
- import com.bumptech.glide.request.RequestOptions;
- import com.hboxs.base_library.base.BaseApplication;
- import com.hboxs.base_library.callback.DialogClickListener;
- import com.hboxs.base_library.constant.BaseConstant;
- import com.hboxs.base_library.constant.Global;
- import com.hboxs.base_library.constant.Name;
- import com.hboxs.base_library.event.ApiMessageEvent;
- import com.hboxs.base_library.util.DrawableSelectorUtils;
- import com.hboxs.base_library.util.GlideUtil;
- import com.hboxs.base_library.util.LogUtil;
- import com.hboxs.base_library.util.LogUtils;
- import com.hboxs.base_library.util.SharedPreferencesUtils;
- import com.hboxs.base_library.util.ToastUtil;
- import com.hboxs.base_library.util.UiUtil;
- import com.hboxs.base_library.widget.BaseDialog;
- import com.hboxs.base_library.widget.BaseDialogNoDatabinding;
- import com.orhanobut.hawk.Hawk;
- import com.orhanobut.logger.Logger;
- import org.greenrobot.eventbus.EventBus;
- import org.greenrobot.eventbus.Subscribe;
- import org.greenrobot.eventbus.ThreadMode;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.lang.ref.WeakReference;
- import java.util.Timer;
- import java.util.TimerTask;
- /**
- * @author whw
- * @time 2019/3/13
- * @Description 一个输入框弹框
- */
- public class ScanDialog extends BaseDialogNoDatabinding implements View.OnClickListener {
- private String TAG = this.getClass().getSimpleName();
- private ImageView ivScan;
- private ImageView ivQrCode;
- private TextView tvScanNow;
- private TextView tvScanTip;
- private RelativeLayout rlTime;
- private TextView tvTime;
- private TextView tvSecond;
- private TextView tvGuanbi;
- private Button btnCancel;
- private int count;
- private static int time = 90;
- private boolean isLoop = true;
- private Thread thread;
- private MyHandler mHandler;
- private String data;
- private int frpCode;
- private String msn;
- private LinearLayout llQuibinary;
- public void setListener(com.hboxs.base_library.callback.DialogClickListener listener) {
- this.listener = listener;
- }
- private DialogClickListener listener;
- public ScanDialog(Context context, Activity activity) {
- super(context, activity);
- }
- //設置訂單編號的數據
- public void setCodeData(String data, String sn) {
- this.data = data;
- this.msn = sn;
- LogUtil.d("sn", sn);
- if (isBase64Img(data)) {
- Bitmap bitmap = stringToBitmap(data);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- byte[] bytes = baos.toByteArray();
- //禁止缓存
- RequestOptions options = new RequestOptions()
- .diskCacheStrategy(DiskCacheStrategy.NONE)
- .skipMemoryCache(true);
- Glide.with(getContext())
- .load(bytes)
- .apply(options)
- .into(ivQrCode);
- } else {
- Bitmap bitmap = ZXingUtils.createQRImage(data, 600, 600);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- byte[] bytes = baos.toByteArray();
- //禁止缓存
- RequestOptions options = new RequestOptions()
- .diskCacheStrategy(DiskCacheStrategy.NONE)
- .skipMemoryCache(true);
- Glide.with(getContext())
- .load(bytes)
- .apply(options)
- .into(ivQrCode);
- }
- }
- @Override
- protected void initView() {
- mHandler = new MyHandler(this, mActivity);
- ivScan = findViewById(R.id.iv_scan);
- ivQrCode = findViewById(R.id.iv_qr_code);
- tvScanNow = findViewById(R.id.tv_scan_now);
- tvScanTip = findViewById(R.id.tv_scan_tip);
- rlTime = findViewById(R.id.rl_time);
- tvTime = findViewById(R.id.tv_time);
- tvSecond = findViewById(R.id.tv_second);
- tvGuanbi = findViewById(R.id.tv_guanbi);
- btnCancel = findViewById(R.id.btn_cancel);
- llQuibinary = findViewById(R.id.ll_quibinary);
- btnCancel.setOnClickListener(this);
- ivQrCode.setOnClickListener(this);
- isLoop = true;
- Logger.d("倒计时initview1:" + isLoop);
- }
- @Override
- public void show() {
- count = 0;
- LogUtils.logWrite("支付对话框显示了。");
- super.show();
- EventBus.getDefault().register(this);
- time = 90;
- LogUtil.d("关闭", "show: scandialog");
- tvTime.setText(String.valueOf(time));
- // isLoop = true;
- // thread = new Thread(new MyThread());
- // thread.start();
- startTimeCountDown();
- listener.onClickListener(130);
- Logger.d("倒计时show:");
- }
- @Override
- public void dismiss() {
- super.dismiss();
- LogUtils.logWrite("二维码弹窗关闭");
- stopTimeDown();
- EventBus.getDefault().unregister(this);
- // stopTimeDown();
- isLoop = false;
- thread = null;
- if (listener != null) {
- listener.onClickListener(126);
- listener.onClickListener(125);
- }
- Logger.d("倒计时dismiss:");
- // stopTimeDown();有可能不被执行
- if (listener!=null){
- listener.onClickListener(1000);
- }
- }
- @Override
- protected int getLayout() {
- return R.layout.dialog_scan;
- }
- @Override
- protected int getAnimType() {
- return BaseDialog.ANIM_CENTER_IN_OUT;
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_cancel:
- //二維碼消失的時候開啓定時任務
- // startGetTime(msn);
- OrderStatusInquiryCancel(String.valueOf(msn));
- dismiss();
- break;
- case R.id.iv_qr_code:
- listener.onClickListener(-1);
- break;
- }
- }
- // class MyThread implements Runnable {
- //
- // @Override
- // public void run() {
- // Logger.d("倒计时run1:" + isLoop);
- // while (isLoop) {
- // try {
- // Thread.sleep(1000);//每隔1s执行一次
- // Logger.d("倒计时run2:" + isLoop);
- // Message message = new Message();
- // message.what = 1;
- // mHandler.sendMessage(message);
- // time--;
- // LogUtil.d(TAG, "thread: 倒计时:" + time);
- // } catch (InterruptedException e) {
- // e.printStackTrace();
- // }
- //
- // }
- // }
- // }
- /**
- * 是否为base64数据
- *
- * @param imgurl 图片地址
- * @return
- */
- public boolean isBase64Img(String imgurl) {
- if (!TextUtils.isEmpty(imgurl) &&
- (imgurl.startsWith("data:image/png;base64,")
- || imgurl.startsWith("data:image/*;base64,")
- || imgurl.startsWith("data:image/jpg;base64,"))
- ) {
- return true;
- }
- return false;
- }
- /**
- * base64转换成图片
- *
- * @param string
- * @return
- */
- public Bitmap stringToBitmap(String string) {
- Bitmap bitmap = null;
- try {
- byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
- bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return bitmap;
- }
- private boolean readDataFromAssets(String gifName) {
- if (!isExternalStorageWritable()) {
- return false;
- }
- InputStream inputStream = null;
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- File dir = BaseApplication.getContext().getExternalFilesDir("gif");
- Log.e(TAG, "readDataFromAssets: dir = " + dir.getAbsolutePath());
- if (!dir.exists()) {
- dir.mkdirs();
- }
- try {
- inputStream = BaseApplication.getContext().getAssets().open(gifName);
- File file = new File(dir, gifName);
- if (file.exists()) {
- file.delete();
- }
- file.createNewFile();
- fos = new FileOutputStream(file);
- bos = new BufferedOutputStream(fos);
- byte[] bytes = new byte[1024];
- while (inputStream.read(bytes) > 0) {
- bos.write(bytes, 0, bytes.length);
- }
- //
- // inputStream.close();
- // bos.close();
- // fos.close();
- Logger.d("文件地址为:" + file.getAbsolutePath());
- GlideUtil.get().loadPic(file.getAbsolutePath(), ivScan);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- } finally {
- try {
- if (inputStream != null) {
- inputStream.close();
- }
- if (bos != null) {
- bos.close();
- }
- if (fos != null) {
- fos.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return true;
- }
- /* Checks if external storage is available for read and write */
- public boolean isExternalStorageWritable() {
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- return true;
- }
- Log.e(TAG, "isExternalStorageWritable: " + state);
- return false;
- }
- /**
- * 设置二维码类型
- *
- * @param frpCode
- */
- public void setGifType(int frpCode) {
- this.frpCode = frpCode;
- int param = (int) SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, 1);
- if (frpCode == 1) {
- //微信
- if (param == 1) {
- //中文
- GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
- // readDataFromAssets("wechat.gif");
- } else {
- GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
- // readDataFromAssets("wechat_E.gif");
- }
- }else if (frpCode == 10){
- GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
- llQuibinary.setVisibility(View.VISIBLE);
- } else {
- //支付宝
- if (param == 1) {
- GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
- // readDataFromAssets("alilpay.gif");
- } else {
- GlideUtil.get().loadPic(R.drawable.alilpay, ivScan);
- // readDataFromAssets("alipay_E.gif");
- }
- }
- }
- public void stopTimerDown() {
- LogUtil.d(TAG, "stopTimerDown: 停止倒计时1");
- // time=60;
- // isLoop=false;
- // thread=null;
- }
- private Timer countDownTimer;
- private TimerTask timerTask;
- public void startTimeCountDown() {
- time = 90;
- stopTimeDown();
- // isTimeFinishRun = true;
- LogUtil.d(TAG, " 开始倒计时 ");
- countDownTimer = new Timer();
- timerTask = new TimerTask() {
- @Override
- public void run() {
- Logger.d("倒计时run2:" + isLoop);
- Message message = new Message();
- message.what = 1;
- message.obj = msn;
- LogUtil.d("msn", msn);
- mHandler.sendMessage(message);
- time--;
- LogUtil.d(TAG, "thread: 倒计时:" + time);
- }
- };
- countDownTimer.schedule(timerTask, 0, 1000);
- }
- @Override
- public void stopTimeDown() {
- mHandler.removeCallbacksAndMessages(null);
- TimerUtil.stopTimerAndTimerTask(countDownTimer, timerTask);
- time = 90;
- }
- @Override
- protected void onStop() {
- stopTimeDown();
- super.onStop();
- }
- static class MyHandler extends Handler {
- private final Activity activity1;
- WeakReference<Dialog> mWeakReference;
- public MyHandler(Dialog activity, Activity activity1) {
- this.activity1 = activity1;
- mWeakReference = new WeakReference<Dialog>(activity);
- }
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- ScanDialog dialog = (ScanDialog) mWeakReference.get();
- switch (msg.what) {
- case 1:
- if (dialog != null) {
- if (time <= -1) {
- //二維碼消失的時候開啓定時任務
- dialog.isLoop = false;
- try {
- LogUtil.d("activity1", "handleMessage: " + activity1);
- if (activity1 != null) {
- if (!activity1.isFinishing()) {
- dialog.dismiss();
- Log.d("activity1", "handleMessage:dismiss ");
- // dialog.startOrderInquiry(String.valueOf(msg.obj));
- } else {
- dialog.stopTimerDown();
- }
- } else {
- dialog.stopTimerDown();
- }
- //二維碼消失的時候開啓定時任務
- //dialog.startGetTime(String.valueOf(msg.obj));
- } catch (IllegalArgumentException e) {
- // dialog = null;
- }
- return;
- }else if(time == 80) {
- Global.makeStatus = Name.IN_PRODUCTION;
- BaseConstant.CANCEL_POLL = true;
- }else if(time <=70 && time >=60){
- if (dialog.count<=0) {
- Log.d("time", "handleMessage: "+dialog.count);
- dialog.count++;
- dialog.startOrderInquiry(String.valueOf(msg.obj));
- }
- }
- dialog.tvTime.setText(String.valueOf(time));
- // time--;
- }
- break;
- }
- }
- }
- //定時任務,用於判斷是否需要上次數據
- // public void startGetTime(String sn) {
- // Timer timeGet = new Timer();
- // TimerTask timerTaskGet = new TimerTask() {
- //
- // @Override
- // public void run() {
- // if (sn.equals(Global.mapOrderReference.get(sn))) {
- // //發送驗證是否付錢了
- // EventBus.getDefault().post(new PaymentMessage("pay_ment", sn));
- // LogUtil.d("發送驗證是否付錢了", "退款");
- // } else {
- // //不退款
- // LogUtil.d("發送驗證是否付錢了", "不退款");
- // Global.mapOrderReference.remove(sn);
- // }
- // timeGet.cancel();
- //
- // }
- // };
- // timeGet.schedule(timerTaskGet, 300000);
- // }
- //在二維碼界面收到支付成功。
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void event(ApiMessageEvent messageEvent) {
- switch (messageEvent.getName()) {
- case "pay_success":
- //LogUtil.d("支付成功", "第三部:發送支付成功信息");
- //EventBus.getDefault().post(new ApiMessageEvent("scan_pay_success", null));
- break;
- }
- }
- /**
- * 查詢訂單是否支付
- * 1.二維碼自動消失,或者點擊了二維碼取消。那麽執行訂單查詢方法
- * 2.查詢到如果是pay_success,那麽發送scan_pay_success信息讓機器開始做糖,其他不管。
- * 3.如果是機器故障,那麽也會在幾分鐘后開始退款。
- */
- //定時任務,用於判斷是否需要上次數據
- @SuppressLint("LongLogTag")
- public void startOrderInquiry(String sn) {
- //發送驗證是否付錢了
- // PaySuccessConstant.sn = sn;
- LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
- LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
- EventBus.getDefault().post(new PaymentMessage("start_order_inquiry", sn));
- }
- @SuppressLint("LongLogTag")
- public void OrderStatusInquiryCancel(String sn) {
- //發送驗證是否付錢了
- // PaySuccessConstant.sn = sn;
- LogUtil.d("PaySuccessConstant.sn:"+PaySuccessConstant.sn, "sn:"+sn);
- LogUtils.logWrite("正扫PaySuccessConstant.sn:"+PaySuccessConstant.sn+ ";sn:"+sn);
- EventBus.getDefault().post(new PaymentMessage("start_order_inquiry_cancel", sn));
- }
- }
|