Advertisement
lolohac

Funções POS Sunmin Imprimir

Feb 26th, 2021
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.65 KB | None | 0 0
  1.  
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.content.pm.PackageInfo;
  7. import android.content.pm.PackageManager;
  8. import android.graphics.Bitmap;
  9. import android.os.IBinder;
  10. import android.os.RemoteException;
  11. import android.widget.Toast;
  12.  
  13. import com.ahmedelsayed.sunmiprinterutill.R;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. import woyou.aidlservice.jiuiv5.ICallback;
  19. import woyou.aidlservice.jiuiv5.IWoyouService;
  20.  
  21.  
  22. public class AidlUtil {
  23.     private static final String SERVICE_PACKAGE = "woyou.aidlservice.jiuiv5";
  24.     private static final String SERVICE_ACTION = "woyou.aidlservice.jiuiv5.IWoyouService";
  25.     private static AidlUtil mAidlUtil = new AidlUtil();
  26.     private IWoyouService woyouService;
  27.     private Context context;
  28.  
  29.  
  30.  
  31.  
  32.     private ServiceConnection connService = new ServiceConnection() {
  33.  
  34.         @Override
  35.         public void onServiceDisconnected(ComponentName name) {
  36.             woyouService = null;
  37.         }
  38.  
  39.         @Override
  40.         public void onServiceConnected(ComponentName name, IBinder service) {
  41.             woyouService = IWoyouService.Stub.asInterface(service);
  42.         }
  43.     };
  44.  
  45.     private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0,
  46.             0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff};
  47.  
  48.     private AidlUtil() {
  49.     }
  50.  
  51.     public static AidlUtil getInstance() {
  52.         return mAidlUtil;
  53.     }
  54.  
  55.  
  56.     public void connectPrinterService(Context context) {
  57.         this.context = context.getApplicationContext();
  58.         Intent intent = new Intent();
  59.         intent.setPackage(SERVICE_PACKAGE);
  60.         intent.setAction(SERVICE_ACTION);
  61.         context.getApplicationContext().startService(intent);
  62.         context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE);
  63.     }
  64.  
  65.  
  66.     public void disconnectPrinterService(Context context) {
  67.         if (woyouService != null) {
  68.             context.getApplicationContext().unbindService(connService);
  69.             woyouService = null;
  70.         }
  71.     }
  72.  
  73.     public boolean isConnect() {
  74.         return woyouService != null;
  75.  
  76.     }
  77.  
  78.  
  79.     public ICallback generateCB(final PrinterCallback printerCallback) {
  80.         return new ICallback.Stub() {
  81.  
  82.  
  83.             @Override
  84.             public void onRunResult(boolean isSuccess) throws RemoteException {
  85.  
  86.             }
  87.  
  88.             @Override
  89.             public void onReturnString(String result) throws RemoteException {
  90.                 printerCallback.onReturnString(result);
  91.             }
  92.  
  93.             @Override
  94.             public void onRaiseException(int code, String msg) throws RemoteException {
  95.  
  96.             }
  97.         };
  98.     }
  99.  
  100.     public void setDarkness(int index) {
  101.         if (woyouService == null) {
  102.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  103.             return;
  104.         }
  105.  
  106.         int k = darkness[index];
  107.         try {
  108.             woyouService.sendRAWData(ESCUtil.setPrinterDarkness(k), null);
  109.             woyouService.printerSelfChecking(null);
  110.         } catch (RemoteException e) {
  111.             e.printStackTrace();
  112.         }
  113.     }
  114.  
  115.  
  116.     public List<String> getPrinterInfo(PrinterCallback printerCallback) {
  117.         if (woyouService == null) {
  118.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  119.             return null;
  120.         }
  121.  
  122.         List<String> info = new ArrayList<>();
  123.         try {
  124.             woyouService.getPrintedLength(generateCB(printerCallback));
  125.             info.add(woyouService.getPrinterSerialNo());
  126.             info.add(woyouService.getPrinterModal());
  127.             info.add(woyouService.getPrinterVersion());
  128.             info.add(printerCallback.getResult());
  129.             info.add("");
  130.             //info.add(woyouService.getServiceVersion());
  131.             PackageManager packageManager = context.getPackageManager();
  132.             try {
  133.                 PackageInfo packageInfo = packageManager.getPackageInfo(SERVICE_PACKAGE, 0);
  134.                 if (packageInfo != null) {
  135.                     info.add(packageInfo.versionName);
  136.                     info.add(packageInfo.versionCode + "");
  137.                 } else {
  138.                     info.add("");
  139.                     info.add("");
  140.                 }
  141.             } catch (PackageManager.NameNotFoundException e) {
  142.                 e.printStackTrace();
  143.             }
  144.  
  145.         } catch (RemoteException e) {
  146.             e.printStackTrace();
  147.         }
  148.         return info;
  149.     }
  150.  
  151.  
  152.     public void initPrinter() {
  153.         if (woyouService == null) {
  154.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  155.             return;
  156.         }
  157.  
  158.         try {
  159.             woyouService.printerInit(null);
  160.         } catch (RemoteException e) {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.  
  165.  
  166.     public void printQr(String data, int modulesize, int errorlevel) {
  167.         if (woyouService == null) {
  168.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  169.             return;
  170.         }
  171.  
  172.  
  173.         try {
  174.             woyouService.setAlignment(1, null);
  175.             woyouService.printQRCode(data, modulesize, errorlevel, null);
  176.             woyouService.lineWrap(3, null);
  177.         } catch (RemoteException e) {
  178.             e.printStackTrace();
  179.         }
  180.     }
  181.  
  182.  
  183.     public void printBarCode(String data, int symbology, int height, int width, int textposition) {
  184.         if (woyouService == null) {
  185.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  186.             return;
  187.         }
  188.  
  189.  
  190.         try {
  191.             woyouService.printBarCode(data, symbology, height, width, textposition, null);
  192.             woyouService.lineWrap(3, null);
  193.         } catch (RemoteException e) {
  194.             e.printStackTrace();
  195.         }
  196.     }
  197.  
  198.  
  199.     public void printText(String content, float size, boolean isBold, boolean isUnderLine, int lineBreak) {
  200.         if (woyouService == null) {
  201.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  202.             return;
  203.         }
  204.  
  205.         try {
  206.             if (isBold) {
  207.                 woyouService.sendRAWData(ESCUtil.boldOn(), null);
  208.             } else {
  209.                 woyouService.sendRAWData(ESCUtil.boldOff(), null);
  210.             }
  211.  
  212.             if (isUnderLine) {
  213.                 woyouService.sendRAWData(ESCUtil.underlineWithOneDotWidthOn(), null);
  214.             } else {
  215.                 woyouService.sendRAWData(ESCUtil.underlineOff(), null);
  216.             }
  217.  
  218.             woyouService.printTextWithFont(content, null, size, null);
  219.             woyouService.lineWrap(lineBreak, null);
  220.         } catch (RemoteException e) {
  221.             e.printStackTrace();
  222.         }
  223.  
  224.     }
  225.  
  226.  
  227.     public void printBitmap(Bitmap bitmap) {
  228.         if (woyouService == null) {
  229.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  230.             return;
  231.         }
  232.  
  233.         try {
  234.             woyouService.setAlignment(1, null);
  235.             woyouService.printBitmap(bitmap, null);
  236.             woyouService.lineWrap(3, null);
  237.         } catch (RemoteException e) {
  238.             e.printStackTrace();
  239.         }
  240.     }
  241.  
  242.  
  243. //    public void printTable(LinkedList<TableItem> list) {
  244. //        if (woyouService == null) {
  245. //            Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  246. //            return;
  247. //        }
  248. //
  249. //        try {
  250. //            for (TableItem tableItem : list) {
  251. //                Log.i("kaltin", "printTable: " + tableItem.getText()[0] + tableItem.getText()[1] + tableItem.getText()[2]);
  252. //                woyouService.printColumnsString(tableItem.getText(), tableItem.getWidth(), tableItem.getAlign(), null);
  253. //            }
  254. //            woyouService.lineWrap(3, null);
  255. //        } catch (RemoteException e) {
  256. //            e.printStackTrace();
  257. //        }
  258. //    }
  259. //    }
  260.  
  261.  
  262.     public void print3Line() {
  263.         if (woyouService == null) {
  264.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  265.             return;
  266.         }
  267.  
  268.         try {
  269.             woyouService.lineWrap(3, null);
  270.         } catch (RemoteException e) {
  271.             e.printStackTrace();
  272.         }
  273.     }
  274.  
  275.  
  276.     public void sendRawData(byte[] data) {
  277.         if (woyouService == null) {
  278.             Toast.makeText(context, R.string.toast_2, Toast.LENGTH_LONG).show();
  279.             return;
  280.         }
  281.  
  282.         try {
  283.             woyouService.sendRAWData(data, null);
  284.         } catch (RemoteException e) {
  285.             e.printStackTrace();
  286.         }
  287.     }
  288. }
  289.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement