From 8a38358edf789128aa2357a623f664908deca23c Mon Sep 17 00:00:00 2001 From: "januslo6211@icloud.com" Date: Wed, 12 Apr 2017 17:21:44 +0800 Subject: [PATCH] call back support call back support --- .../innerprinter/SunmiInnerPrinterModule.java | 268 ++++++++++++++++-- package.json | 2 +- 2 files changed, 244 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/sunmi/innerprinter/SunmiInnerPrinterModule.java b/android/src/main/java/com/sunmi/innerprinter/SunmiInnerPrinterModule.java index 760c7f2..3a75e01 100644 --- a/android/src/main/java/com/sunmi/innerprinter/SunmiInnerPrinterModule.java +++ b/android/src/main/java/com/sunmi/innerprinter/SunmiInnerPrinterModule.java @@ -6,10 +6,12 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.Promise; import android.widget.Toast; import java.util.Map; import java.io.IOException; -import woyou.aidlservice.jiuiv5.*; +import woyou.aidlservice.jiuiv5.IWoyouService; +import woyou.aidlservice.jiuiv5.ICallback; import android.os.RemoteException; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -106,17 +108,35 @@ public String getName() { * @return */ @ReactMethod - public void lineWrap(int n){ + public void lineWrap(int n,final Promise p){ final IWoyouService ss = woyouService; final int count = n; ThreadPoolManager.getInstance().executeTask(new Runnable(){ @Override public void run() { try { - ss.lineWrap(count,null); + ss.lineWrap(count,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -129,17 +149,35 @@ public void run() { * @param callback 结果回调 */ @ReactMethod - public void sendRAWData(String base64EncriptedData){ + public void sendRAWData(String base64EncriptedData,final Promise p){ final IWoyouService ss = woyouService; final byte[] d = Base64.decode(base64EncriptedData,Base64.DEFAULT) ; ThreadPoolManager.getInstance().executeTask(new Runnable(){ @Override public void run() { try { - ss.sendRAWData(d,null); + ss.sendRAWData(d,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -154,17 +192,35 @@ public void run() { * @param callback 结果回调 */ @ReactMethod - public void setAlignment(int alignment){ + public void setAlignment(int alignment,final Promise p){ final IWoyouService ss = woyouService; final int align = alignment; ThreadPoolManager.getInstance().executeTask(new Runnable(){ @Override public void run() { try { - ss.setAlignment(align,null); + ss.setAlignment(align,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -178,17 +234,35 @@ public void run() { * @param typeface: 字体名称 */ @ReactMethod - public void setFontName(String typeface){ + public void setFontName(String typeface,final Promise p){ final IWoyouService ss = woyouService; final String tf = typeface; ThreadPoolManager.getInstance().executeTask(new Runnable(){ @Override public void run() { try { - ss.setFontName(tf,null); + ss.setFontName(tf,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -203,17 +277,35 @@ public void run() { * @param fontsize: 字体大小 */ @ReactMethod - public void setFontSize(float fontsize){ + public void setFontSize(float fontsize,final Promise p){ final IWoyouService ss = woyouService; final float fs = fontsize; ThreadPoolManager.getInstance().executeTask(new Runnable(){ @Override public void run() { try { - ss.setFontSize(fs,null); + ss.setFontSize(fs,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -229,7 +321,7 @@ public void run() { * @param fontsize: 字体大小 */ @ReactMethod - public void printTextWithFont(String text, String typeface, float fontsize){ + public void printTextWithFont(String text, String typeface, float fontsize,final Promise p){ final IWoyouService ss = woyouService; final String txt = text; final String tf = typeface; @@ -238,10 +330,28 @@ public void printTextWithFont(String text, String typeface, float fontsize){ @Override public void run() { try { - ss.printTextWithFont(txt,tf,fs,null); + ss.printTextWithFont(txt,tf,fs,new ICallback.Stub(){ + @Override + public void onRunResult(boolean isSuccess){ + if(isSuccess){ + p.resolve(null); + }else{ + p.reject("0",isSuccess+""); + } + } + @Override + public void onReturnString(String result){ + p.resolve(result); + } + @Override + public void onRaiseException(int code, String msg){ + p.reject(""+code,msg); + } + }); } catch (Exception e) { e.printStackTrace(); Log.i(TAG,"ERROR: " + e.getMessage()); + p.reject(""+0,e.getMessage()); } } @@ -256,7 +366,7 @@ public void run() { * 备注: 三个参数的数组长度应该一致, 如果colsText[i]的宽度大于colsWidth[i], 则文本换行 */ @ReactMethod - public void printColumnsText(ReadableArray colsTextArr, ReadableArray colsWidthArr, ReadableArray colsAlign){ + public void printColumnsText(ReadableArray colsTextArr, ReadableArray colsWidthArr, ReadableArray colsAlign,final Promise p){ final IWoyouService ss = woyouService; final String[] clst = new String[colsTextArr.size()]; for(int i=0;i", "contributors": [],