Only convert indention to spaces

This commit is contained in:
Sebastián Katzer 2013-12-11 13:21:55 +01:00
parent 1a40324682
commit 9fc84624df

View File

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