Add Android KitKat support
This commit is contained in:
parent
c039585469
commit
cf76737e21
@ -9,7 +9,7 @@ by Sebastián Katzer ([github.com/katzer](https://github.com/katzer))
|
||||
- **iOS** *(Print from iOS devices to AirPrint compatible printers)*<br>
|
||||
See [Drawing and Printing Guide for iOS](http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html) for detailed informations and screenshots.
|
||||
|
||||
- **Android** *(<= 4.3, Print through 3rd party printing apps)*
|
||||
- **Android** *(Print through 3rd party printing apps on SDK <= 18)*
|
||||
|
||||
## Adding the Plugin to your project
|
||||
Through the [Command-line Interface](http://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html#The%20Command-line%20Interface):
|
||||
@ -28,6 +28,8 @@ cordova plugin rm de.appplant.cordova.plugin.printer
|
||||
#### Version 0.5.0 (not yet released)
|
||||
- Release under the Apache 2.0 license.
|
||||
- [***change:***] Removed the `callback` property from the `print` interface.
|
||||
- [enhancement:] Added Android KitKat support<br>
|
||||
*Based on the Print Android plugin made by* ***Eion Robb***
|
||||
|
||||
#### Version 0.4.0 (24.08.2013)
|
||||
- [feature]: Added Android support<br>
|
||||
@ -75,7 +77,7 @@ window.plugin.printer.print(page);
|
||||
|
||||
## Platform specifics
|
||||
|
||||
### Get all available printing apps on Android
|
||||
### Get all available printing apps on Android <= 4.3
|
||||
The callback function will be called with a second argument which is an array, indicating which printer apps are available for printing.
|
||||
```javascript
|
||||
window.plugin.printer.isServiceAvailable(
|
||||
@ -85,7 +87,7 @@ window.plugin.printer.isServiceAvailable(
|
||||
);
|
||||
```
|
||||
|
||||
### Specify printing app on Android
|
||||
### Specify printing app on Android <= 4.3
|
||||
An App-ID can be assigned as a platform configuration to indicate which 3rd party printing app shall be used. Otherwise the first found application will be used.
|
||||
```javascript
|
||||
window.plugin.printer.print(page, { appId: 'epson.print' });
|
||||
|
@ -17,6 +17,8 @@
|
||||
<engine name="cordova" version=">=3.0.0" />
|
||||
</engines>
|
||||
|
||||
<dependency id="org.apache.cordova.device" url="https://github.com/apache/cordova-plugin-device" />
|
||||
|
||||
<!-- interface -->
|
||||
<js-module src="www/printer.js" name="Printer">
|
||||
<clobbers target="plugin.printer" />
|
||||
|
@ -21,19 +21,37 @@
|
||||
|
||||
package de.appplant.cordova.plugin.printer;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.print.PrintAttributes;
|
||||
import android.print.PrintDocumentAdapter;
|
||||
import android.print.PrintManager;
|
||||
import android.view.View;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
@TargetApi(19)
|
||||
public class KitKatPrinter extends CordovaPlugin {
|
||||
|
||||
@Override
|
||||
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
// Es soll überprüft werden, ob ein Dienst zum Ausdrucken von Inhalten zur Verfügung steht
|
||||
if ("isServiceAvailable".equals(action)) {
|
||||
if (action.equalsIgnoreCase("isServiceAvailable")) {
|
||||
isServiceAvailable(callbackContext);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Etwas soll ausgedruckt werden
|
||||
if ("print".equals(action)) {
|
||||
if (action.equalsIgnoreCase("print")) {
|
||||
print(args, callbackContext);
|
||||
|
||||
return true;
|
||||
@ -47,7 +65,7 @@ public class KitKatPrinter extends CordovaPlugin {
|
||||
* Überprüft, ob ein Drucker zur Verfügung steht.
|
||||
*/
|
||||
private void isServiceAvailable (CallbackContext ctx) {
|
||||
Boolean supported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;;
|
||||
Boolean supported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK, supported);
|
||||
|
||||
ctx.sendPluginResult(result);
|
||||
@ -57,6 +75,68 @@ public class KitKatPrinter extends CordovaPlugin {
|
||||
* Druckt den HTML Content aus.
|
||||
*/
|
||||
private void print (final JSONArray args, CallbackContext ctx) {
|
||||
final KitKatPrinter self = this;
|
||||
|
||||
cordova.getActivity().runOnUiThread( new Runnable() {
|
||||
public void run() {
|
||||
String content = args.optString(0, "<html></html>");
|
||||
WebView controller = self.getPrintController();
|
||||
|
||||
self.loadContentIntoPrintController(content, controller);
|
||||
|
||||
self.startPrinterApp(controller);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt den Print-View.
|
||||
*/
|
||||
private WebView getPrintController () {
|
||||
WebView webview = new WebView(cordova.getActivity());
|
||||
|
||||
webview.setVisibility(View.INVISIBLE);
|
||||
webview.getSettings().setJavaScriptEnabled(false);
|
||||
|
||||
return webview;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
|
||||
*/
|
||||
private void loadContentIntoPrintController (String content, WebView webview) {
|
||||
//Set base URI to the assets/www folder
|
||||
String baseURL = webView.getUrl();
|
||||
baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1);
|
||||
|
||||
webview.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Öffnet die Printer App, damit der Content ausgedruckt werden kann.
|
||||
*/
|
||||
private void startPrinterApp (WebView webview) {
|
||||
webview.setWebViewClient (new WebViewClient() {
|
||||
public boolean shouldOverrideUrlLoading (WebView view, String url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void onPageFinished (WebView webview, String url) {
|
||||
// Get a PrintManager instance
|
||||
PrintManager printManager = (PrintManager) cordova.getActivity()
|
||||
.getSystemService(Context.PRINT_SERVICE);
|
||||
|
||||
// Get a print adapter instance
|
||||
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
|
||||
|
||||
// Get a print builder instance
|
||||
PrintAttributes.Builder builder = new PrintAttributes.Builder();
|
||||
|
||||
builder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);
|
||||
|
||||
// Create a print job with name and adapter instance
|
||||
printManager.print("Print Document", printAdapter, builder.build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,11 @@ Printer.prototype = {
|
||||
callback.apply(scope || window, args);
|
||||
};
|
||||
|
||||
cordova.exec(callbackFn, null, 'Printer', 'isServiceAvailable', []);
|
||||
if (device.platform == 'Android' && device.version >= '4.4') {
|
||||
cordova.exec(callbackFn, null, 'KitKatPrinter', 'isServiceAvailable', []);
|
||||
} else {
|
||||
cordova.exec(callbackFn, null, 'Printer', 'isServiceAvailable', []);
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
@ -57,7 +61,11 @@ Printer.prototype = {
|
||||
return;
|
||||
}
|
||||
|
||||
cordova.exec(null, null, 'Printer', 'print', [page, options]);
|
||||
if (device.platform == 'Android' && device.version >= '4.4') {
|
||||
cordova.exec(null, null, 'KitKatPrinter', 'print', [page, options]);
|
||||
} else {
|
||||
cordova.exec(null, null, 'Printer', 'print', [page, options]);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user