package android_serialport_api.print_tool; import hdx.HdxUtil; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ConcurrentLinkedQueue; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.RemoteException; import android.util.Log; import android.view.WindowManager; import android.widget.Toast; import com.basewin.aidl.OnPrinterListener; import com.basewin.services.DeviceInfoBinder; import com.basewin.services.PrinterBinder; import com.ectrip.cyt.config.BindService; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.constant.DeviceType; import com.ectrip.cyt.constant.LogoType; import com.ectrip.cyt.constant.SelectAction; import com.ectrip.cyt.constant.constant; import com.ectrip.cyt.ui.PhomeScanerOrderActivity; import com.ectrip.cyt.utils.SharedPreferences2Obj; import com.ectrip.trips.check.R; /** * 盛本的红色pos机 * @author Administrator * */ public class BasewinRebPosPortTool { private WakeLock lock; private static final String TAG = "BasewinRebPosPortTool"; private Context context; private final int ENABLE_BUTTON = 2; private int printNum = 1;//打印次数 // 打印信息 private MyHandler handler; private DeviceInfoBinder deviceInfo = null; private PrinterBinder printer = null; // 初始化 @SuppressWarnings("deprecation") public void init(Context context, String contentStr) { this.context = context; if (handler == null) { handler = new MyHandler(); } if (deviceInfo == null) { deviceInfo = BindService.getInstance().getDeviceInfo(); } if (printer == null) { printer = BindService.getInstance().getPrinter(); } popUpDialog(); try { if (!deviceInfo.isSupportPrint()) { Toast.makeText(context, "不支持", Toast.LENGTH_LONG).show(); return; } } catch (RemoteException e) { e.printStackTrace(); Toast.makeText(context, "不支持", Toast.LENGTH_LONG).show(); return; } PowerManager pm = (PowerManager) context .getSystemService(Context.POWER_SERVICE); lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); new WriteThread(contentStr).start(); } @SuppressLint("SimpleDateFormat") private class WriteThread extends Thread { private String arr; public WriteThread(String str) { arr = str; } public void run() { super.run(); lock.acquire(); JSONObject titelJson = null; JSONObject SubtitleJson = null; if (isStatistic != null && isStatistic.equals("1")) { titelJson = getJsonObject(true, "统计信息\n"); } else { titelJson = getJsonObject(true, "订单信息\n"); if (Select == SelectAction.Reprint.getValue()) { SubtitleJson = getJsonObject(true, "(重打小票)\n"); try { SubtitleJson.put("bold", "0"); SubtitleJson.put("size", "2"); } catch (JSONException e) { e.printStackTrace(); } } } if (arr.contains("\n\n\n\n\n")) { arr = arr.replace("\n\n\n\n\n", "\n"); } JSONObject contentJson = getJsonObject(false, arr); JSONObject dateJson = null; JSONObject signJson = null; if (isStatistic != null && isStatistic.equals("1")) { } else { // 重打时间 if (Select == SelectAction.Query.getValue()) { // 打印时间 SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");// 设置日期格式 dateJson = getJsonObject(false, "检票时间:" + df.format(new Date())+"\n\n\n"); } else if (Select == SelectAction.Reprint.getValue()) {// 重打时间 SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");// 设置日期格式 dateJson = getJsonObject(false, " " + "\n\n"); } signJson = getJsonObject(false, "\n" + "\n取票人签名:\n\n"); } JSONObject logoJson = null; JSONObject urlJson = null; if (constant.logoMark == LogoType.CYT.getValue()) { logoJson = getJsonObject(false, "---畅游通—智慧旅游O2O平台---"); urlJson = getJsonObject(false, "--------www.jingqu.cn---------\n\n"); } else if (constant.logoMark == LogoType.QNE.getValue()) { logoJson = getJsonObject(false, "---欢迎使用去哪儿网O2O系统---"); urlJson = getJsonObject(false, "--------www.qunar.com--------\n\n"); } JSONObject blankJson = getJsonObject(false, " "+"\n\n\n\n\n"); JSONArray printTest = new JSONArray(); // 打印次数控制 for (int x = 0; x < printNum; x++) { if (x == 1 && isStatistic.equals("1")) { break; } if (titelJson != null) { printTest.put(titelJson); } if (SubtitleJson !=null) { printTest.put(SubtitleJson); } if (contentJson != null) { printTest.put(contentJson); } if (dateJson != null) { printTest.put(dateJson); } if (signJson != null) { printTest.put(signJson); } if (logoJson != null) { printTest.put(logoJson); } if (urlJson != null) { printTest.put(urlJson); } if (blankJson!=null) { printTest.put(blankJson); } } JSONObject priJsonObject = new JSONObject(); try { priJsonObject.put("spos", printTest); } catch (JSONException e) { e.printStackTrace(); } try { printer.printBottomFeedLine(2); printer.print(priJsonObject.toString(), null, printer_callback); } catch (Exception e) { e.printStackTrace(); } } } private final OnPrinterListener printer_callback = new OnPrinterListener() { @Override public void onStart() { // 打印开始 } @Override public void onFinish() { // 打印结束 close(); Boolean scanBack = SharedPreferences2Obj.getInstance(context).setName("config").getObject("scanBack", Boolean.class); if (MyApp.getInstance().getCheckType()==0&&scanBack != null && scanBack) { checkScan(); } } @Override public void onError(int arg0, String arg1) { // 打印出错 close(); Toast.makeText(context,"打印出错,请检查打印纸!",Toast.LENGTH_SHORT).show(); } }; private void checkScan() { if (MyApp.getInstance().getCheckType() == 0) { Integer Select = SharedPreferences2Obj .getInstance(context) .setName("SelectAction").getObject("Select", Integer.class); Intent intent = new Intent(context, PhomeScanerOrderActivity.class); if (Select != null) { if (Select == SelectAction.Check.getValue()) { intent.putExtra("titleName", context.getString(R.string.check_scan_code)); } else if (Select == SelectAction.Reprint.getValue()) { intent.putExtra("titleName", context.getString(R.string.repriint_scan_code)); } else if (Select == SelectAction.Query.getValue()) { intent.putExtra("titleName", context.getString(R.string.query_scan_code)); } } else { intent.putExtra("titleName", context.getString(R.string.scan_qr_code)); } context.startActivity(intent); Activity activity = (Activity) context; activity.finish(); } } private JSONObject getJsonObject(Boolean isTitel, String content) { JSONObject json1 = new JSONObject(); try { json1.put("content-type", "txt");// 打印类型 json1.put("content", content); json1.put("offset", "0"); json1.put("italic", "0");// “1”表示斜体, “0”表示正常 json1.put("height", "-1"); if (isTitel) { json1.put("bold", "1");// 是否加粗 json1.put("size", "3");// 字体大小 json1.put("position", "center");// 对齐方式 } else { json1.put("bold", "0"); json1.put("size", "2"); json1.put("position", "left"); } } catch (JSONException e) { e.printStackTrace(); } return json1; } @SuppressLint("HandlerLeak") private class MyHandler extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case ENABLE_BUTTON: try { if (dialog != null) { dialog.dismiss(); } } catch (Exception e) { e.printStackTrace(); } break; default: break; } } } /************************ 关闭处理 **********************************/ public void close() { if (deviceInfo != null) { deviceInfo = null; } if (printer != null) { printer = null; } if (dialog != null) { dialog.dismiss(); } } /*************************** 弹框处理 ********************************/ private ProgressDialog dialog; private String isStatistic; private Integer type; private int Select = 0; private void popUpDialog() { try { isStatistic = SharedPreferences2Obj.getInstance(context) .setName("SelectAction") .getObject("isStatistic", String.class); // 非统计判断 type = SharedPreferences2Obj.getInstance(context) .setName("MachineType").getObject("type", Integer.class); Select = SharedPreferences2Obj.getInstance(context) .setName("SelectAction").getObject("Select", Integer.class); printNum = MyApp.getInstance().getPrintNum(); } catch (Exception e1) { e1.printStackTrace(); } if (dialog == null) { // 显示ProgressDialog dialog = new ProgressDialog(context); dialog.setMessage("打印中..."); dialog.setCanceledOnTouchOutside(true); dialog.setCancelable(false); if (type == DeviceType.HANDSET.getValue()) { dialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); } try { dialog.show(); } catch (Exception e) { e.printStackTrace(); } } else { dialog.setMessage("打印中..."); dialog.show(); } } }