Last active
May 3, 2016 08:57
-
-
Save raphaelbussa/6e97f5c175204da3ac17eb3a9dbe7c07 to your computer and use it in GitHub Desktop.
Simple helper for print an url or an html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2016 Raphaël Bussa | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package rebus.utils.helper; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.Build; | |
import android.print.PrintAttributes; | |
import android.print.PrintDocumentAdapter; | |
import android.print.PrintJob; | |
import android.print.PrintManager; | |
import android.text.Html; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
/** | |
* Created by raphaelbussa on 02/05/16. | |
*/ | |
public class PrintHelper { | |
private Context context; | |
private WebView webView; | |
private String url; | |
private String html; | |
private String name; | |
private CallBack callBack; | |
/** | |
* @param context current context | |
*/ | |
private PrintHelper(Context context) { | |
this.context = context; | |
this.webView = new WebView(context); | |
} | |
/** | |
* @param context current context | |
* @return new instance of PrintHelper | |
*/ | |
public static PrintHelper with(Context context) { | |
return new PrintHelper(context); | |
} | |
/** | |
* @param name document name | |
* @return current instance | |
*/ | |
public PrintHelper name(String name) { | |
this.name = Html.fromHtml(name).toString(); | |
return this; | |
} | |
/** | |
* @param url url to print | |
* @return current instance | |
*/ | |
public PrintHelper url(String url) { | |
this.url = url; | |
this.html = null; | |
return this; | |
} | |
/** | |
* @param html html to print | |
* @return current instance | |
*/ | |
public PrintHelper html(String html) { | |
this.html = html; | |
this.url = null; | |
return this; | |
} | |
/** | |
* @param callBack interface with result of print | |
*/ | |
public void print(CallBack callBack) { | |
this.callBack = callBack; | |
if (url == null && html == null || name == null) { | |
this.callBack.onFinish(RESULT.NULL_VALUES); | |
} else { | |
webView.setWebViewClient(new Client()); | |
if (url != null) { | |
webView.loadUrl(url); | |
} | |
if (html != null) { | |
webView.loadDataWithBaseURL(null, html, "text/HTML", "UTF-8", null); | |
} | |
} | |
} | |
/** | |
* print method, checks that the Android version is superior to kitkat | |
* create a PrintJob and add it to PrintManager | |
* @throws Exception generic Exception, you never know | |
*/ | |
@TargetApi(Build.VERSION_CODES.KITKAT) | |
private void printDocument() throws Exception { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE); | |
PrintDocumentAdapter printAdapter; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
printAdapter = webView.createPrintDocumentAdapter(name); | |
} else { | |
printAdapter = webView.createPrintDocumentAdapter(); | |
} | |
PrintJob printJob = printManager.print(name, printAdapter, new PrintAttributes.Builder().build()); | |
printManager.getPrintJobs().add(printJob); | |
this.callBack.onFinish(RESULT.SUCCESS); | |
} else { | |
this.callBack.onFinish(RESULT.MIN_SDK); | |
} | |
} | |
/** | |
* Custom WebViewClient, in onPageFinished it call method printDocument() | |
*/ | |
private class Client extends WebViewClient { | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
super.onPageFinished(view, url); | |
try { | |
printDocument(); | |
} catch (Exception e) { | |
callBack.onFinish(RESULT.ERROR); | |
} | |
} | |
} | |
/** | |
* public enum of result type of print process | |
*/ | |
public enum RESULT { | |
SUCCESS, MIN_SDK, ERROR, NULL_VALUES | |
} | |
/** | |
* public interface with print process result | |
*/ | |
public interface CallBack { | |
void onFinish(RESULT result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment