package com.dayu.baselibrary.tools.print; 
 | 
  
 | 
import android.content.ComponentName; 
 | 
import android.content.Context; 
 | 
import android.content.Intent; 
 | 
import android.content.ServiceConnection; 
 | 
import android.content.pm.PackageInfo; 
 | 
import android.content.pm.PackageManager; 
 | 
import android.graphics.Bitmap; 
 | 
import android.os.IBinder; 
 | 
import android.os.RemoteException; 
 | 
import android.util.Log; 
 | 
import android.widget.Toast; 
 | 
  
 | 
import com.dayu.baselibrary.R; 
 | 
import com.dayu.baselibrary.bean.TableItem; 
 | 
import com.dayu.baselibrary.utils.ESCUtil; 
 | 
import com.dayu.baselibrary.utils.PrinterCallback; 
 | 
  
 | 
  
 | 
import java.util.ArrayList; 
 | 
import java.util.LinkedList; 
 | 
import java.util.List; 
 | 
  
 | 
import woyou.aidlservice.jiuiv5.ICallback; 
 | 
import woyou.aidlservice.jiuiv5.IWoyouService; 
 | 
  
 | 
  
 | 
  
 | 
/** 
 | 
 * 商米打印模块相关代码 
 | 
 */ 
 | 
public class ShangMiAidlUtil { 
 | 
    private static final String SERVICE_PACKAGE = "woyou.aidlservice.jiuiv5"; 
 | 
    private static final String SERVICE_ACTION = "woyou.aidlservice.jiuiv5.IWoyouService"; 
 | 
  
 | 
    private IWoyouService woyouService; 
 | 
    private static ShangMiAidlUtil mAidlUtil = new ShangMiAidlUtil(); 
 | 
    private Context context; 
 | 
  
 | 
    private ShangMiAidlUtil() { 
 | 
    } 
 | 
  
 | 
    public static ShangMiAidlUtil getInstance() { 
 | 
        return mAidlUtil; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 连接服务 
 | 
     * 
 | 
     * @param context context 
 | 
     */ 
 | 
    public void connectPrinterService(Context context) { 
 | 
        this.context = context.getApplicationContext(); 
 | 
        Intent intent = new Intent(); 
 | 
        intent.setPackage(SERVICE_PACKAGE); 
 | 
        intent.setAction(SERVICE_ACTION); 
 | 
        context.getApplicationContext().startService(intent); 
 | 
        context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 断开服务 
 | 
     * 
 | 
     * @param context context 
 | 
     */ 
 | 
    public void disconnectPrinterService(Context context) { 
 | 
        if (woyouService != null) { 
 | 
            context.getApplicationContext().unbindService(connService); 
 | 
            woyouService = null; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public boolean isConnect() { 
 | 
        return woyouService != null; 
 | 
    } 
 | 
  
 | 
    private ServiceConnection connService = new ServiceConnection() { 
 | 
  
 | 
        @Override 
 | 
        public void onServiceDisconnected(ComponentName name) { 
 | 
            woyouService = null; 
 | 
        } 
 | 
  
 | 
        @Override 
 | 
        public void onServiceConnected(ComponentName name, IBinder service) { 
 | 
            woyouService = IWoyouService.Stub.asInterface(service); 
 | 
        } 
 | 
    }; 
 | 
  
 | 
    public ICallback generateCB(final PrinterCallback printerCallback) { 
 | 
        return new ICallback.Stub() { 
 | 
  
 | 
  
 | 
            @Override 
 | 
            public void onRunResult(boolean isSuccess) throws RemoteException { 
 | 
  
 | 
            } 
 | 
  
 | 
            @Override 
 | 
            public void onReturnString(String result) throws RemoteException { 
 | 
                printerCallback.onReturnString(result); 
 | 
            } 
 | 
  
 | 
            @Override 
 | 
            public void onRaiseException(int code, String msg) throws RemoteException { 
 | 
  
 | 
            } 
 | 
        }; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置打印浓度 
 | 
     */ 
 | 
    private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0, 
 | 
            0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff}; 
 | 
  
 | 
    public void setDarkness(int index) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        int k = darkness[index]; 
 | 
        try { 
 | 
            woyouService.sendRAWData(ESCUtil.setPrinterDarkness(k), null); 
 | 
            woyouService.printerSelfChecking(null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 取得打印机系统信息,放在list中 
 | 
     * 
 | 
     * @return list 
 | 
     */ 
 | 
    public List<String> getPrinterInfo(PrinterCallback printerCallback) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return null; 
 | 
        } 
 | 
  
 | 
        List<String> info = new ArrayList<>(); 
 | 
        try { 
 | 
            woyouService.getPrintedLength(generateCB(printerCallback)); 
 | 
            info.add(woyouService.getPrinterSerialNo()); 
 | 
            info.add(woyouService.getPrinterModal()); 
 | 
            info.add(woyouService.getPrinterVersion()); 
 | 
            info.add(printerCallback.getResult()); 
 | 
            info.add(""); 
 | 
            //info.add(woyouService.getServiceVersion()); 
 | 
            PackageManager packageManager = context.getPackageManager(); 
 | 
            try { 
 | 
                PackageInfo packageInfo = packageManager.getPackageInfo(SERVICE_PACKAGE, 0); 
 | 
                if (packageInfo != null) { 
 | 
                    info.add(packageInfo.versionName); 
 | 
                    info.add(packageInfo.versionCode + ""); 
 | 
                } else { 
 | 
                    info.add(""); 
 | 
                    info.add(""); 
 | 
                } 
 | 
            } catch (PackageManager.NameNotFoundException e) { 
 | 
                e.printStackTrace(); 
 | 
            } 
 | 
  
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
        return info; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 初始化打印机 
 | 
     */ 
 | 
    public void initPrinter() { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            woyouService.printerInit(null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 打印二维码 
 | 
     */ 
 | 
    public void printQr(String data, int modulesize, int errorlevel) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
  
 | 
        try { 
 | 
            woyouService.setAlignment(1, null); 
 | 
            woyouService.printQRCode(data, modulesize, errorlevel, null); 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 打印条形码 
 | 
     */ 
 | 
    public void printBarCode(String data, int symbology, int height, int width, int textposition) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
  
 | 
        try { 
 | 
            woyouService.printBarCode(data, symbology, height, width, textposition, null); 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 打印文字 
 | 
     */ 
 | 
    public void printText(List<String> content, float size, boolean isBold, boolean isUnderLine) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            if (isBold) { 
 | 
                woyouService.sendRAWData(ESCUtil.boldOn(), null); 
 | 
            } else { 
 | 
                woyouService.sendRAWData(ESCUtil.boldOff(), null); 
 | 
            } 
 | 
  
 | 
            if (isUnderLine) { 
 | 
                woyouService.sendRAWData(ESCUtil.underlineWithOneDotWidthOn(), null); 
 | 
            } else { 
 | 
                woyouService.sendRAWData(ESCUtil.underlineOff(), null); 
 | 
            } 
 | 
            woyouService.setFontSize(size, null); 
 | 
            for (int i = 0; i < content.size(); i++) { 
 | 
                woyouService.printText(content.get(i) + "\n", null); 
 | 
            } 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
    /* 
 | 
     *打印图片 
 | 
     */ 
 | 
    public void printBitmap(Bitmap bitmap) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            woyouService.setAlignment(1, null); 
 | 
            woyouService.printBitmap(bitmap, null); 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 打印图片和文字按照指定排列顺序 
 | 
     */ 
 | 
    public void printBitmap(Bitmap bitmap, int orientation) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, "打印服务已断开!", Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            if (orientation == 0) { 
 | 
                woyouService.printBitmap(bitmap, null); 
 | 
                woyouService.printText("横向排列\n", null); 
 | 
                woyouService.printBitmap(bitmap, null); 
 | 
                woyouService.printText("横向排列\n", null); 
 | 
            } else { 
 | 
                woyouService.printBitmap(bitmap, null); 
 | 
                woyouService.printText("\n纵向排列\n", null); 
 | 
                woyouService.printBitmap(bitmap, null); 
 | 
                woyouService.printText("\n纵向排列\n", null); 
 | 
            } 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 打印表格 
 | 
     */ 
 | 
    public void printTable(LinkedList<TableItem> list) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            for (TableItem tableItem : list) { 
 | 
                Log.i("kaltin", "printTable: " + tableItem.getText()[0] + tableItem.getText()[1] + tableItem.getText()[2]); 
 | 
                woyouService.printColumnsString(tableItem.getText(), tableItem.getWidth(), tableItem.getAlign(), null); 
 | 
            } 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /* 
 | 
     * 空打三行! 
 | 
     */ 
 | 
    public void print3Line() { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            woyouService.lineWrap(3, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    public void sendRawData(byte[] data) { 
 | 
        if (woyouService == null) { 
 | 
            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show(); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        try { 
 | 
            woyouService.sendRAWData(data, null); 
 | 
        } catch (RemoteException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
} 
 |