package android_serialport_api.print_tool; import hdx.pwm.PWMControl; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidParameterException; import java.text.SimpleDateFormat; import java.util.Date; 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.view.WindowManager; import android_serialport_api.SerialPort; import com.ectrip.cyt.callback.CloseCallback; 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.ScanerOrderActivity; import com.ectrip.cyt.utils.LogUtil; import com.ectrip.cyt.utils.SharedPreferences2Obj; import com.ectrip.trips.check.R; import com.hdx.lib.printer.SerialPrinter; import com.hdx.lib.serial.SerialParam; import com.hdx.lib.serial.SerialPortOperaion; import com.hdx.lib.serial.SerialPortOperaion.SerialReadData; public class SerialSmallPortTool implements CloseCallback{ private String TAG = "SerialSmallPortTool"; private Context mcontext; private boolean stop = false; private final int ENABLE_BUTTON = 2; private final int CLOSEDismiss = 3; protected SerialPort mSerialPort = null; protected OutputStream mOutputStream; public SerialPrinter mSerialPrinter = null; private InputStream mInputStream; private WakeLock lock; private ProgressDialog dialog; private Integer type; private int printNum=1; private int Select = 0; private String isStatistic="0"; private PowerManager pm; private String printinfo; private SerialDataHandler serialDataHandler; @SuppressLint("Wakelock") public void init(Context mContext,String printInfo) { this.printinfo=printInfo; this.mcontext = mContext; serialDataHandler=new SerialDataHandler(); smollPosHandler = new MySmollPosHandler(); popUpDialog(); new Thread(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { try { try { if (mSerialPort == null) { mSerialPort = getSerialPort(); } if (mSerialPrinter == null) { mSerialPrinter = SerialPrinter.GetSerialPrinter(); } try { mSerialPrinter.OpenPrinter(new SerialParam(115200, "/dev/ttyS2", 0),serialDataHandler,SerialSmallPortTool.this); } catch (Exception e) { try { mSerialPrinter.OpenPrinter(new SerialParam(115200, "/dev/ttyS2", 0),serialDataHandler,SerialSmallPortTool.this); } catch (Exception e1) { e1.printStackTrace(); } e.printStackTrace(); System.out.println(e+""); } if (pm == null) { pm = (PowerManager) mcontext .getSystemService(Context.POWER_SERVICE); } if (lock == null) { lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); } } catch (Exception e) { e.printStackTrace(); } if (mSerialPort != null) { mOutputStream = mSerialPort.getOutputStream(); mInputStream = mSerialPort.getInputStream(); } lock.acquire(); try { PWMControl.PrinterEnable(1); sleep(400); for (int x = 0; x < printNum; x++) { if(x==1&&isStatistic.equals("1")){ //close(); //return; break; } sendSmollCharacterDemo(printinfo,x); //sleep(1500); } mSerialPrinter.printString("printClose",1); }catch(Exception e){ close(); } } catch (SecurityException e) { close(); // DisplayError(R.string.error_security); } catch (InvalidParameterException e) { close(); // DisplayError(R.string.error_configuration); } catch (Exception e) { close(); // TODO: handle exception } } }).start(); } public void sleep(int ms) { try { java.lang.Thread.sleep(ms); } catch (Exception e) { e.printStackTrace(); } } private void popUpDialog(){ try { isStatistic=SharedPreferences2Obj.getInstance(mcontext). setName("SelectAction").getObject("isStatistic",String.class); //非统计判断 if(type==null){ type=SharedPreferences2Obj.getInstance(mcontext). setName("MachineType").getObject("type", Integer.class); } if(Select==0){ Select = SharedPreferences2Obj.getInstance(mcontext). setName("SelectAction").getObject("Select",Integer.class); } printNum= MyApp.getInstance().getPrintNum(); } catch (Exception e) { e.printStackTrace(); } try { if (dialog == null) { // 显示ProgressDialog dialog = new ProgressDialog(mcontext); dialog.setMessage("打印中..."); dialog.setCanceledOnTouchOutside(false); 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(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { if (mSerialPort == null) { // SharedPreferences sp = mContext.getSharedPreferences( // "android_serialport_api.sample_preferences", 0); String path = "/dev/ttyS2"; int baudrate = 115200; if ((path.length() == 0) || (baudrate == -1)) { path = "/dev/ttyS2"; baudrate = 115200; } mSerialPort = new SerialPort(new File(path), baudrate, 0); } return mSerialPort; } // 小pos机打印 private MySmollPosHandler smollPosHandler; @SuppressLint("HandlerLeak") private class SerialDataHandler extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case SerialPortOperaion.SERIAL_RECEIVED_DATA_MSG: SerialReadData data = (SerialReadData) msg.obj; StringBuilder sb = new StringBuilder(); for (int x = 0; x < data.size; x++) sb.append(String.format("%02x", data.data[x])); LogUtil.d(TAG, "data =" + sb); } } } @SuppressLint("HandlerLeak") private class MySmollPosHandler extends Handler { public MySmollPosHandler() { } public void handleMessage(Message msg) { if (stop == true) return; switch (msg.what) { case ENABLE_BUTTON: close(); case CLOSEDismiss: if(dialog!=null&&dialog.isShowing()){ try { dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } Boolean scanBack = SharedPreferences2Obj.getInstance(mcontext).setName("config").getObject("scanBack", Boolean.class); if (MyApp.getInstance().getCheckType()==0&&scanBack != null && scanBack) { checkScan(); } break; default: break; } } } private void checkScan() { if (MyApp.getInstance().getCheckType() == 0) { Integer Select = SharedPreferences2Obj .getInstance(mcontext) .setName("SelectAction").getObject("Select", Integer.class); Intent intent = new Intent(mcontext, ScanerOrderActivity.class); if (Select != null) { if (Select == SelectAction.Check.getValue()) { intent.putExtra("titleName", mcontext.getString(R.string.check_scan_code)); } else if (Select == SelectAction.Reprint.getValue()) { intent.putExtra("titleName", mcontext.getString(R.string.repriint_scan_code)); } else if (Select == SelectAction.Query.getValue()) { intent.putExtra("titleName", mcontext.getString(R.string.query_scan_code)); } } else { intent.putExtra("titleName", mcontext.getString(R.string.scan_qr_code)); } mcontext.startActivity(intent); Activity activity = (Activity) mcontext; activity.finish(); } } // 小pos机打印 @SuppressLint("SimpleDateFormat") private void sendSmollCharacterDemo(String arr,int x) { try { if(isStatistic!=null&&isStatistic.equals("1")){ mSerialPrinter.enlargeFontSize(2, 2);// mSerialPrinter.printString(" 统计信息",x); sleep(100); }else{ mSerialPrinter.enlargeFontSize(2, 2);// mSerialPrinter.printString(" 订单信息",x); sleep(100); // 当时重打小票的时候,多打一段字符 if (Select== SelectAction.Reprint.getValue()) { mSerialPrinter.enlargeFontSize(1, 1); mSerialPrinter.printString(" (重打小票)",x); sleep(100); } } } catch (Exception e) { e.printStackTrace(); } try { mSerialPrinter.setLineSpace(5); // 间隔 sleep(100); } catch (Exception e) { e.printStackTrace(); } if (arr == null) { return; } // 打印信息 try { mSerialPrinter.enlargeFontSize(1, 1); // for (int i = 0; i < arr.size(); i++) { mSerialPrinter.printString(arr,x); mSerialPrinter.sendLineFeed(); sleep(100); // } if(isStatistic!=null&&isStatistic.equals("1")){ }else{ if(Select== SelectAction.Query.getValue()){ //打印时间 SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");// 设置日期格式 mSerialPrinter.printString("检票时间:" + df.format(new Date()),x);// new // Date()为获取当前系统时间 mSerialPrinter.sendLineFeed(); sleep(100); }else if (Select== SelectAction.Reprint.getValue()) {// 重打时间 SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");// 设置日期格式 mSerialPrinter.printString("重打时间:" + df.format(new Date()),x);// new // Date()为获取当前系统时间 mSerialPrinter.sendLineFeed(); sleep(100); } mSerialPrinter.setLineSpace(10); mSerialPrinter.sendLineFeed(); mSerialPrinter.printString("取票人签名:",x); mSerialPrinter.setLineSpace(10); sleep(100); } mSerialPrinter.sendLineFeed(); if (constant.logoMark == LogoType.CYT.getValue()) { mSerialPrinter.printString("---畅游通—智慧旅游O2O平台---",x); mSerialPrinter.sendLineFeed(); sleep(100); mSerialPrinter .printString("--------www.jingqu.cn---------",x); sleep(100); } else if (constant.logoMark == LogoType.QNE.getValue()) { mSerialPrinter.printString("---欢迎使用去哪儿网O2O系统---",x); mSerialPrinter.sendLineFeed(); sleep(100); mSerialPrinter.printString("--------www.qunar.com--------",x); sleep(100); } mSerialPrinter.sendLineFeed(); } catch (Exception e) { e.printStackTrace(); } // 信息完打印的空格行数 try { mSerialPrinter.sendLineFeed(); mSerialPrinter.sendLineFeed(); mSerialPrinter.sendLineFeed(); mSerialPrinter.setLineSpace(30); // sleep(100); } catch (Exception e) { e.printStackTrace(); } try { mSerialPrinter.walkPaper(60);// sleep(100); } catch (Exception e) { e.printStackTrace(); } // smollPosHandler.sendMessage(smollPosHandler.obtainMessage( // ENABLE_BUTTON, 1, 0, null)); LogUtil.e(TAG, "print end"); } public void close() { new Thread(new Runnable() { @Override public void run() { if (mSerialPrinter != null) { try { mSerialPrinter.ClosePrinter(); } catch (Exception e) { e.printStackTrace(); } } if (mSerialPort != null) { try { mSerialPort.close(); mSerialPort = null; } catch (Exception e) { e.printStackTrace(); } } try { if (mOutputStream != null) { try { mOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } if (mInputStream != null) { mInputStream.close(); } } catch (IOException e) { }catch (Exception e) { } smollPosHandler.sendMessage(smollPosHandler.obtainMessage( CLOSEDismiss, 1, 0, null)); if (isStatistic.equals("1")){ sleep(3500); PWMControl.PrinterEnable(0); }else { if (printNum==2){ sleep(4000); PWMControl.PrinterEnable(0); }else if (printNum==3){ sleep(6000); PWMControl.PrinterEnable(0); }else if (printNum==1){ sleep(2000); PWMControl.PrinterEnable(0); } } } }).start(); } @Override public void closeback() { close(); } }