diff --git a/src/android/Printer.java b/src/android/Printer.java index 59014f7..33aa468 100644 --- a/src/android/Printer.java +++ b/src/android/Printer.java @@ -49,295 +49,295 @@ import org.apache.cordova.PluginResult; public class Printer extends CordovaPlugin { - private CallbackContext ctx; + private CallbackContext ctx; - /** - * Auflistung von App-IDs, welche den Content ausdrucken können - */ - private String printAppIds[] = { - "kr.co.iconlab.BasicPrintingProfile", // Bluetooth Smart Printing - "com.blueslib.android.app", // Bluetooth SPP Printer API - "com.brother.mfc.brprint", // Brother iPrint&Scan - "com.brother.ptouch.sdk", // Brother Print Library - "jp.co.canon.bsd.android.aepp.activity", // Canon Easy-PhotoPrint - "com.pauloslf.cloudprint", // Cloud Print - "com.dlnapr1.printer", // CMC DLNA Print Client - "com.dell.mobileprint", // Dell Mobile Print - "com.printjinni.app.print", // PrintJinni - "epson.print", // Epson iPrint - "jp.co.fujixerox.prt.PrintUtil.PCL", // Fuji Xerox Print Utility - "jp.co.fujixerox.prt.PrintUtil.Karin", // Fuji Xeros Print&Scan (S) - "com.hp.android.print", // HP ePrint" "com.hp.android.print - "com.blackspruce.lpd", // Let's Print Droid - "com.threebirds.notesprint", // NotesPrint print your notes - "com.xerox.mobileprint", // Print Portal (Xerox) - "com.zebra.kdu", // Print Station (Zebra) - "net.jsecurity.printbot", // PrintBot - "com.dynamixsoftware.printhand", // PrintHand Mobile Print - "com.dynamixsoftware.printhand.premium", // PrintHand Mobile Print Premium - "com.sec.print.mobileprint", // Samsung Mobile Print - "com.rcreations.send2printer", // Send 2 Printer - "com.ivc.starprint", // StarPrint - "com.threebirds.easyviewer", // WiFi Print - "com.woosim.android.print", // Woosim BT printer - "com.woosim.bt.app", // WoosimPrinter - "com.zebra.android.zebrautilities", // Zebra Utilities - }; + /** + * Auflistung von App-IDs, welche den Content ausdrucken können + */ + private String printAppIds[] = { + "kr.co.iconlab.BasicPrintingProfile", // Bluetooth Smart Printing + "com.blueslib.android.app", // Bluetooth SPP Printer API + "com.brother.mfc.brprint", // Brother iPrint&Scan + "com.brother.ptouch.sdk", // Brother Print Library + "jp.co.canon.bsd.android.aepp.activity", // Canon Easy-PhotoPrint + "com.pauloslf.cloudprint", // Cloud Print + "com.dlnapr1.printer", // CMC DLNA Print Client + "com.dell.mobileprint", // Dell Mobile Print + "com.printjinni.app.print", // PrintJinni + "epson.print", // Epson iPrint + "jp.co.fujixerox.prt.PrintUtil.PCL", // Fuji Xerox Print Utility + "jp.co.fujixerox.prt.PrintUtil.Karin", // Fuji Xeros Print&Scan (S) + "com.hp.android.print", // HP ePrint" "com.hp.android.print + "com.blackspruce.lpd", // Let's Print Droid + "com.threebirds.notesprint", // NotesPrint print your notes + "com.xerox.mobileprint", // Print Portal (Xerox) + "com.zebra.kdu", // Print Station (Zebra) + "net.jsecurity.printbot", // PrintBot + "com.dynamixsoftware.printhand", // PrintHand Mobile Print + "com.dynamixsoftware.printhand.premium", // PrintHand Mobile Print Premium + "com.sec.print.mobileprint", // Samsung Mobile Print + "com.rcreations.send2printer", // Send 2 Printer + "com.ivc.starprint", // StarPrint + "com.threebirds.easyviewer", // WiFi Print + "com.woosim.android.print", // Woosim BT printer + "com.woosim.bt.app", // WoosimPrinter + "com.zebra.android.zebrautilities", // Zebra Utilities + }; - @Override - public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - // Etwas soll ausgedruckt werden - if ("print".equals(action)) { - print(args, callbackContext); + @Override + public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + // Etwas soll ausgedruckt werden + if ("print".equals(action)) { + print(args, callbackContext); - return true; - } + return true; + } - // Es soll überprüft werden, ob ein Dienst zum Ausdrucken von Inhalten zur Verfügung steht - if ("isServiceAvailable".equals(action)) { - isServiceAvailable(callbackContext); + // Es soll überprüft werden, ob ein Dienst zum Ausdrucken von Inhalten zur Verfügung steht + if ("isServiceAvailable".equals(action)) { + isServiceAvailable(callbackContext); - return true; - } + return true; + } - // Returning false results in a "MethodNotFound" error. - return false; - } + // Returning false results in a "MethodNotFound" error. + return false; + } - /** - * Überprüft, ob ein Drucker zur Verfügung steht. - */ - private void isServiceAvailable (CallbackContext ctx) { - JSONArray appIds = this.getInstalledAppIds(); - Boolean available = appIds.length() > 0; - PluginResult result = new PluginResult(PluginResult.Status.OK, available); + /** + * Überprüft, ob ein Drucker zur Verfügung steht. + */ + private void isServiceAvailable (CallbackContext ctx) { + JSONArray appIds = this.getInstalledAppIds(); + Boolean available = appIds.length() > 0; + PluginResult result = new PluginResult(PluginResult.Status.OK, available); - ctx.sendPluginResult(result); - } + ctx.sendPluginResult(result); + } - /** - * Druckt den HTML Content aus. - */ - private void print (final JSONArray args, CallbackContext ctx) { - final Printer self = this; + /** + * Druckt den HTML Content aus. + */ + private void print (final JSONArray args, CallbackContext ctx) { + final Printer self = this; - this.ctx = ctx; + this.ctx = ctx; cordova.getActivity().runOnUiThread( new Runnable() { public void run() { - JSONObject platformConfig = args.optJSONObject(1); - String appId = self.getPrintAppId(platformConfig); + JSONObject platformConfig = args.optJSONObject(1); + String appId = self.getPrintAppId(platformConfig); - if (appId == null) { - self.ctx.success(4); - return; - }; + if (appId == null) { + self.ctx.success(4); + return; + }; - String content = args.optString(0, ""); - Intent controller = self.getPrintController(appId); + String content = args.optString(0, ""); + Intent controller = self.getPrintController(appId); - self.adjustSettingsForPrintController(controller); - self.loadContentIntoPrintController(content, controller); + self.adjustSettingsForPrintController(controller); + self.loadContentIntoPrintController(content, controller); - self.startPrinterApp(controller); + self.startPrinterApp(controller); } }); - } + } - /** - * Gibt die zu verwendende App-ID an. - */ - private String getPrintAppId (JSONObject platformConfig) { - String appId = platformConfig.optString("appId", null); + /** + * Gibt die zu verwendende App-ID an. + */ + private String getPrintAppId (JSONObject platformConfig) { + String appId = platformConfig.optString("appId", null); - if (appId != null) { - return (this.isAppInstalled(appId)) ? appId : null; - } else { - return this.getFirstInstalledAppId(); - } - } + if (appId != null) { + return (this.isAppInstalled(appId)) ? appId : null; + } else { + return this.getFirstInstalledAppId(); + } + } - /** - * Erstellt den Print-View. - */ - private Intent getPrintController (String appId) { - String intentId = "android.intent.action.SEND"; + /** + * Erstellt den Print-View. + */ + private Intent getPrintController (String appId) { + String intentId = "android.intent.action.SEND"; - if (appId.equals("com.rcreations.send2printer")) { - intentId = "com.rcreations.send2printer.print"; - } else if (appId.equals("com.dynamixsoftware.printershare")) { - intentId = "android.intent.action.VIEW"; - } else if (appId.equals("com.hp.android.print")) { - intentId = "org.androidprinting.intent.action.PRINT"; - } + if (appId.equals("com.rcreations.send2printer")) { + intentId = "com.rcreations.send2printer.print"; + } else if (appId.equals("com.dynamixsoftware.printershare")) { + intentId = "android.intent.action.VIEW"; + } else if (appId.equals("com.hp.android.print")) { + intentId = "org.androidprinting.intent.action.PRINT"; + } - Intent intent = new Intent(intentId); + Intent intent = new Intent(intentId); - if (appId != null) - intent.setPackage(appId); + if (appId != null) + intent.setPackage(appId); - return intent; - } + return intent; + } - /** - * Stellt die Eigenschaften des Druckers ein. - */ - private void adjustSettingsForPrintController (Intent intent) { - String mimeType = "image/png"; - String appId = intent.getPackage(); + /** + * Stellt die Eigenschaften des Druckers ein. + */ + private void adjustSettingsForPrintController (Intent intent) { + String mimeType = "image/png"; + String appId = intent.getPackage(); - // Check for special cases that can receive HTML - if (appId.equals("com.rcreations.send2printer") || appId.equals("com.dynamixsoftware.printershare")) { - mimeType = "text/html"; - } + // Check for special cases that can receive HTML + if (appId.equals("com.rcreations.send2printer") || appId.equals("com.dynamixsoftware.printershare")) { + mimeType = "text/html"; + } - intent.setType(mimeType); - } + intent.setType(mimeType); + } - /** - * Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll. - */ - private void loadContentIntoPrintController (String content, Intent intent) { - String mimeType = intent.getType(); + /** + * Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll. + */ + private void loadContentIntoPrintController (String content, Intent intent) { + String mimeType = intent.getType(); - if (mimeType.equals("text/html")) { - loadContentAsHtmlIntoPrintController(content, intent); - } else { - loadContentAsBitmapIntoPrintController(content, intent); - } - } + if (mimeType.equals("text/html")) { + loadContentAsHtmlIntoPrintController(content, intent); + } else { + loadContentAsBitmapIntoPrintController(content, intent); + } + } - /** - * Lädt den zu druckenden Content als HTML in ein WebView, welcher vom Drucker ausgedruckt werden soll. - */ - private void loadContentAsHtmlIntoPrintController (String content, Intent intent) { - intent.putExtra(Intent.EXTRA_TEXT, content); - } + /** + * Lädt den zu druckenden Content als HTML in ein WebView, welcher vom Drucker ausgedruckt werden soll. + */ + private void loadContentAsHtmlIntoPrintController (String content, Intent intent) { + intent.putExtra(Intent.EXTRA_TEXT, content); + } - /** - * Lädt den zu druckenden Content als BMP in ein WebView, welcher vom Drucker ausgedruckt werden soll. - */ - private void loadContentAsBitmapIntoPrintController (String content, final Intent intent) { - Activity ctx = cordova.getActivity(); - final WebView page = new WebView(ctx); - final Printer self = this; + /** + * Lädt den zu druckenden Content als BMP in ein WebView, welcher vom Drucker ausgedruckt werden soll. + */ + private void loadContentAsBitmapIntoPrintController (String content, final Intent intent) { + Activity ctx = cordova.getActivity(); + final WebView page = new WebView(ctx); + final Printer self = this; - page.setVisibility(View.INVISIBLE); - page.getSettings().setJavaScriptEnabled(false); + page.setVisibility(View.INVISIBLE); + page.getSettings().setJavaScriptEnabled(false); - page.setWebViewClient( new WebViewClient() { - @Override - public void onPageFinished(final WebView page, String url) { - new Handler().postDelayed( new Runnable() { - @Override - public void run() { - Bitmap screenshot = self.takeScreenshot(page); - File tmpFile = self.saveScreenshotToTmpFile(screenshot); - ViewGroup vg = (ViewGroup)(page.getParent()); + page.setWebViewClient( new WebViewClient() { + @Override + public void onPageFinished(final WebView page, String url) { + new Handler().postDelayed( new Runnable() { + @Override + public void run() { + Bitmap screenshot = self.takeScreenshot(page); + File tmpFile = self.saveScreenshotToTmpFile(screenshot); + ViewGroup vg = (ViewGroup)(page.getParent()); - intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile)); + intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile)); - vg.removeView(page); - } - }, 1000); - } - }); + vg.removeView(page); + } + }, 1000); + } + }); - //Set base URI to the assets/www folder - String baseURL = webView.getUrl(); - baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1); + //Set base URI to the assets/www folder + String baseURL = webView.getUrl(); + baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1); - ctx.addContentView(page, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - page.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null); - } + ctx.addContentView(page, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + page.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null); + } - /** - * Nimmt einen Screenshot der Seite auf. - */ - private Bitmap takeScreenshot (WebView page) { - Picture picture = page.capturePicture(); - Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); + /** + * Nimmt einen Screenshot der Seite auf. + */ + private Bitmap takeScreenshot (WebView page) { + Picture picture = page.capturePicture(); + Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); - picture.draw(canvas); + picture.draw(canvas); - return bitmap; - } + return bitmap; + } - /** - * Speichert den Screenshot der Seite in einer tmp. Datei ab. - */ - private File saveScreenshotToTmpFile (Bitmap screenshot) { - try { - File tmpFile = File.createTempFile("screenshot", ".tmp"); - FileOutputStream stream = new FileOutputStream(tmpFile); + /** + * Speichert den Screenshot der Seite in einer tmp. Datei ab. + */ + private File saveScreenshotToTmpFile (Bitmap screenshot) { + try { + File tmpFile = File.createTempFile("screenshot", ".tmp"); + FileOutputStream stream = new FileOutputStream(tmpFile); - screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream); - stream.close(); + screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream); + stream.close(); - return tmpFile; - } catch (IOException e) { - e.printStackTrace(); - } + return tmpFile; + } catch (IOException e) { + e.printStackTrace(); + } - return null; - } + return null; + } - /** - * Öffnet die Printer App, damit der Content ausgedruckt werden kann. - */ - private void startPrinterApp (Intent intent) { - cordova.startActivityForResult(this, intent, 0); - } + /** + * Öffnet die Printer App, damit der Content ausgedruckt werden kann. + */ + private void startPrinterApp (Intent intent) { + cordova.startActivityForResult(this, intent, 0); + } - /** - * Findet heraus, ob die Anwendung installiert ist. - */ - private boolean isAppInstalled (String appId) { - PackageManager pm = cordova.getActivity().getPackageManager(); + /** + * Findet heraus, ob die Anwendung installiert ist. + */ + private boolean isAppInstalled (String appId) { + PackageManager pm = cordova.getActivity().getPackageManager(); - try { - PackageInfo pi = pm.getPackageInfo(appId, 0); + try { + PackageInfo pi = pm.getPackageInfo(appId, 0); - if (pi != null){ - return true; - } - } catch (PackageManager.NameNotFoundException e) {} + if (pi != null){ + return true; + } + } catch (PackageManager.NameNotFoundException e) {} - return false; - } + return false; + } - /** - * Die IDs aller verfügbaren Drucker-Apps. - */ - private JSONArray getInstalledAppIds () { - JSONArray appIds = new JSONArray(); + /** + * Die IDs aller verfügbaren Drucker-Apps. + */ + private JSONArray getInstalledAppIds () { + JSONArray appIds = new JSONArray(); - for (int i = 0; i < printAppIds.length; i++) { - String appId = printAppIds[i]; - Boolean isInstalled = this.isAppInstalled(appId); + for (int i = 0; i < printAppIds.length; i++) { + String appId = printAppIds[i]; + Boolean isInstalled = this.isAppInstalled(appId); - if (isInstalled){ - appIds.put(appId); - } - } + if (isInstalled){ + appIds.put(appId); + } + } - return appIds; - } + return appIds; + } - /** - * Die erste ID in der Liste, deren App installiert ist. - */ - private String getFirstInstalledAppId () { - for (int i = 0; i < printAppIds.length; i++) { - String appId = printAppIds[i]; - Boolean isInstalled = this.isAppInstalled(appId); + /** + * Die erste ID in der Liste, deren App installiert ist. + */ + private String getFirstInstalledAppId () { + for (int i = 0; i < printAppIds.length; i++) { + String appId = printAppIds[i]; + Boolean isInstalled = this.isAppInstalled(appId); - if (isInstalled){ - return appId; - } - } + if (isInstalled){ + return appId; + } + } - return null; - } + return null; + } }