David PHAM-VAN

Migrate to the new Android plugins APIs

1 # Changelog 1 # Changelog
2 2
  3 +## 3.1.0
  4 +
  5 +- Migrate to the new Android plugins APIs
  6 +
3 ## 3.0.2 7 ## 3.0.2
4 8
5 - Add Raster PDF to Image 9 - Add Raster PDF to Image
  1 +package net.nfet.flutter.printing;
  2 +
  3 +import android.app.Activity;
  4 +import android.print.PrintAttributes;
  5 +
  6 +import androidx.annotation.NonNull;
  7 +
  8 +import java.util.HashMap;
  9 +
  10 +import io.flutter.plugin.common.MethodCall;
  11 +import io.flutter.plugin.common.MethodChannel;
  12 +
  13 +public class PrintingHandler implements MethodChannel.MethodCallHandler {
  14 + private final Activity activity;
  15 + private final MethodChannel channel;
  16 +
  17 + public PrintingHandler(@NonNull Activity activity, @NonNull MethodChannel channel) {
  18 + this.activity = activity;
  19 + this.channel = channel;
  20 + }
  21 +
  22 + @Override
  23 + public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
  24 + switch (call.method) {
  25 + case "printPdf": {
  26 + final String name = call.argument("name");
  27 + final Double width = call.argument("width");
  28 + final Double height = call.argument("height");
  29 + final Double marginLeft = call.argument("marginLeft");
  30 + final Double marginTop = call.argument("marginTop");
  31 + final Double marginRight = call.argument("marginRight");
  32 + final Double marginBottom = call.argument("marginBottom");
  33 +
  34 + final PrintingJob printJob =
  35 + new PrintingJob(activity, this, (int) call.argument("job"));
  36 + assert name != null;
  37 + printJob.printPdf(
  38 + name, width, height, marginLeft, marginTop, marginRight, marginBottom);
  39 +
  40 + result.success(1);
  41 + break;
  42 + }
  43 + case "cancelJob": {
  44 + final PrintingJob printJob =
  45 + new PrintingJob(activity, this, (int) call.argument("job"));
  46 + printJob.cancelJob();
  47 + result.success(1);
  48 + break;
  49 + }
  50 + case "sharePdf": {
  51 + final byte[] document = call.argument("doc");
  52 + final String name = call.argument("name");
  53 + PrintingJob.sharePdf(activity, document, name);
  54 + result.success(1);
  55 + break;
  56 + }
  57 + case "convertHtml": {
  58 + Double width = call.argument("width");
  59 + Double height = call.argument("height");
  60 + Double marginLeft = call.argument("marginLeft");
  61 + Double marginTop = call.argument("marginTop");
  62 + Double marginRight = call.argument("marginRight");
  63 + Double marginBottom = call.argument("marginBottom");
  64 + final PrintingJob printJob =
  65 + new PrintingJob(activity, this, (int) call.argument("job"));
  66 +
  67 + assert width != null;
  68 + assert height != null;
  69 + assert marginLeft != null;
  70 + assert marginTop != null;
  71 + assert marginRight != null;
  72 + assert marginBottom != null;
  73 +
  74 + PrintAttributes.Margins margins =
  75 + new PrintAttributes.Margins(Double.valueOf(marginLeft * 1000.0).intValue(),
  76 + Double.valueOf(marginTop * 1000.0 / 72.0).intValue(),
  77 + Double.valueOf(marginRight * 1000.0 / 72.0).intValue(),
  78 + Double.valueOf(marginBottom * 1000.0 / 72.0).intValue());
  79 +
  80 + PrintAttributes.MediaSize size = new PrintAttributes.MediaSize("flutter_printing",
  81 + "Provided size", Double.valueOf(width * 1000.0 / 72.0).intValue(),
  82 + Double.valueOf(height * 1000.0 / 72.0).intValue());
  83 +
  84 + printJob.convertHtml((String) call.argument("html"), size, margins,
  85 + (String) call.argument("baseUrl"));
  86 + result.success(1);
  87 + break;
  88 + }
  89 + case "printingInfo": {
  90 + result.success(PrintingJob.printingInfo());
  91 + break;
  92 + }
  93 + case "rasterPdf": {
  94 + final byte[] document = call.argument("doc");
  95 + final int[] pages = call.argument("pages");
  96 + Double scale = call.argument("scale");
  97 + final PrintingJob printJob =
  98 + new PrintingJob(activity, this, (int) call.argument("job"));
  99 + printJob.rasterPdf(document, pages, scale);
  100 + result.success(1);
  101 + break;
  102 + }
  103 + default:
  104 + result.notImplemented();
  105 + break;
  106 + }
  107 + }
  108 +
  109 + /// Request the Pdf document from flutter
  110 + void onLayout(final PrintingJob printJob, Double width, double height, double marginLeft,
  111 + double marginTop, double marginRight, double marginBottom) {
  112 + HashMap<String, Object> args = new HashMap<>();
  113 + args.put("width", width);
  114 + args.put("height", height);
  115 +
  116 + args.put("marginLeft", marginLeft);
  117 + args.put("marginTop", marginTop);
  118 + args.put("marginRight", marginRight);
  119 + args.put("marginBottom", marginBottom);
  120 + args.put("job", printJob.index);
  121 +
  122 + channel.invokeMethod("onLayout", args, new MethodChannel.Result() {
  123 + @Override
  124 + public void success(Object result) {
  125 + if (result instanceof byte[]) {
  126 + printJob.setDocument((byte[]) result);
  127 + } else {
  128 + printJob.cancelJob();
  129 + }
  130 + }
  131 +
  132 + @Override
  133 + public void error(String errorCode, String errorMessage, Object errorDetails) {
  134 + printJob.cancelJob();
  135 + }
  136 +
  137 + @Override
  138 + public void notImplemented() {
  139 + printJob.cancelJob();
  140 + }
  141 + });
  142 + }
  143 +
  144 + /// send completion status to flutter
  145 + void onCompleted(PrintingJob printJob, boolean completed, String error) {
  146 + HashMap<String, Object> args = new HashMap<>();
  147 + args.put("completed", completed);
  148 +
  149 + args.put("error", error);
  150 + args.put("job", printJob.index);
  151 +
  152 + channel.invokeMethod("onCompleted", args);
  153 + }
  154 +
  155 + /// send html to pdf data result to flutter
  156 + void onHtmlRendered(PrintingJob printJob, byte[] pdfData) {
  157 + HashMap<String, Object> args = new HashMap<>();
  158 + args.put("doc", pdfData);
  159 + args.put("job", printJob.index);
  160 +
  161 + channel.invokeMethod("onHtmlRendered", args);
  162 + }
  163 +
  164 + /// send html to pdf conversion error to flutter
  165 + void onHtmlError(PrintingJob printJob, String error) {
  166 + HashMap<String, Object> args = new HashMap<>();
  167 + args.put("error", error);
  168 + args.put("job", printJob.index);
  169 +
  170 + channel.invokeMethod("onHtmlError", args);
  171 + }
  172 +
  173 + /// send pdf to raster data result to flutter
  174 + void onPageRasterized(PrintingJob printJob, byte[] imageData, int width, int height) {
  175 + HashMap<String, Object> args = new HashMap<>();
  176 + args.put("image", imageData);
  177 + args.put("width", width);
  178 + args.put("height", height);
  179 + args.put("job", printJob.index);
  180 +
  181 + channel.invokeMethod("onPageRasterized", args);
  182 + }
  183 +
  184 + void onPageRasterEnd(PrintingJob printJob) {
  185 + HashMap<String, Object> args = new HashMap<>();
  186 + args.put("job", printJob.index);
  187 +
  188 + channel.invokeMethod("onPageRasterEnd", args);
  189 + }
  190 +}
@@ -58,14 +58,14 @@ import java.util.HashMap; @@ -58,14 +58,14 @@ import java.util.HashMap;
58 public class PrintingJob extends PrintDocumentAdapter { 58 public class PrintingJob extends PrintDocumentAdapter {
59 private static PrintManager printManager; 59 private static PrintManager printManager;
60 private final Context context; 60 private final Context context;
61 - private final PrintingPlugin printing; 61 + private final PrintingHandler printing;
62 private PrintJob printJob; 62 private PrintJob printJob;
63 private byte[] documentData; 63 private byte[] documentData;
64 private String jobName; 64 private String jobName;
65 private LayoutResultCallback callback; 65 private LayoutResultCallback callback;
66 int index; 66 int index;
67 67
68 - PrintingJob(Context context, PrintingPlugin printing, int index) { 68 + PrintingJob(Context context, PrintingHandler printing, int index) {
69 this.context = context; 69 this.context = context;
70 this.printing = printing; 70 this.printing = printing;
71 this.index = index; 71 this.index = index;
@@ -17,12 +17,18 @@ @@ -17,12 +17,18 @@
17 package net.nfet.flutter.printing; 17 package net.nfet.flutter.printing;
18 18
19 import android.app.Activity; 19 import android.app.Activity;
  20 +import android.content.Context;
20 import android.print.PrintAttributes; 21 import android.print.PrintAttributes;
21 22
22 import androidx.annotation.NonNull; 23 import androidx.annotation.NonNull;
23 24
24 import java.util.HashMap; 25 import java.util.HashMap;
25 26
  27 +import io.flutter.embedding.engine.plugins.FlutterPlugin;
  28 +import io.flutter.embedding.engine.plugins.activity.ActivityAware;
  29 +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
  30 +import io.flutter.plugin.common.BinaryMessenger;
  31 +import io.flutter.plugin.common.EventChannel;
26 import io.flutter.plugin.common.MethodCall; 32 import io.flutter.plugin.common.MethodCall;
27 import io.flutter.plugin.common.MethodChannel; 33 import io.flutter.plugin.common.MethodChannel;
28 import io.flutter.plugin.common.MethodChannel.MethodCallHandler; 34 import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -32,17 +38,14 @@ import io.flutter.plugin.common.PluginRegistry.Registrar; @@ -32,17 +38,14 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
32 /** 38 /**
33 * PrintingPlugin 39 * PrintingPlugin
34 */ 40 */
35 -public class PrintingPlugin implements MethodCallHandler {  
36 - private final Activity activity;  
37 - private final MethodChannel channel;  
38 -  
39 - private PrintingPlugin(@NonNull Activity activity, @NonNull MethodChannel channel) {  
40 - this.activity = activity;  
41 - this.channel = channel;  
42 - } 41 +public class PrintingPlugin implements FlutterPlugin, ActivityAware {
  42 + private Activity activity;
  43 + private MethodChannel channel;
  44 + private PrintingHandler handler;
43 45
44 /** 46 /**
45 * Plugin registration. 47 * Plugin registration.
  48 + * for legacy Embedding API
46 */ 49 */
47 public static void registerWith(Registrar registrar) { 50 public static void registerWith(Registrar registrar) {
48 Activity activity = registrar.activity(); 51 Activity activity = registrar.activity();
@@ -50,176 +53,60 @@ public class PrintingPlugin implements MethodCallHandler { @@ -50,176 +53,60 @@ public class PrintingPlugin implements MethodCallHandler {
50 return; // We can't print without an activity 53 return; // We can't print without an activity
51 } 54 }
52 55
53 - final MethodChannel channel = new MethodChannel(registrar.messenger(), "net.nfet.printing");  
54 - channel.setMethodCallHandler(new PrintingPlugin(activity, channel)); 56 + final PrintingPlugin instance = new PrintingPlugin();
  57 + instance.onAttachedToEngine(registrar.messenger());
  58 + instance.onAttachedToActivity(activity);
55 } 59 }
56 60
57 @Override 61 @Override
58 - public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {  
59 - switch (call.method) {  
60 - case "printPdf": {  
61 - final String name = call.argument("name");  
62 - final Double width = call.argument("width");  
63 - final Double height = call.argument("height");  
64 - final Double marginLeft = call.argument("marginLeft");  
65 - final Double marginTop = call.argument("marginTop");  
66 - final Double marginRight = call.argument("marginRight");  
67 - final Double marginBottom = call.argument("marginBottom");  
68 -  
69 - final PrintingJob printJob =  
70 - new PrintingJob(activity, this, (int) call.argument("job"));  
71 - assert name != null;  
72 - printJob.printPdf(  
73 - name, width, height, marginLeft, marginTop, marginRight, marginBottom);  
74 -  
75 - result.success(1);  
76 - break;  
77 - }  
78 - case "cancelJob": {  
79 - final PrintingJob printJob =  
80 - new PrintingJob(activity, this, (int) call.argument("job"));  
81 - printJob.cancelJob();  
82 - result.success(1);  
83 - break;  
84 - }  
85 - case "sharePdf": {  
86 - final byte[] document = call.argument("doc");  
87 - final String name = call.argument("name");  
88 - PrintingJob.sharePdf(activity, document, name);  
89 - result.success(1);  
90 - break;  
91 - }  
92 - case "convertHtml": {  
93 - Double width = call.argument("width");  
94 - Double height = call.argument("height");  
95 - Double marginLeft = call.argument("marginLeft");  
96 - Double marginTop = call.argument("marginTop");  
97 - Double marginRight = call.argument("marginRight");  
98 - Double marginBottom = call.argument("marginBottom");  
99 - final PrintingJob printJob =  
100 - new PrintingJob(activity, this, (int) call.argument("job"));  
101 -  
102 - assert width != null;  
103 - assert height != null;  
104 - assert marginLeft != null;  
105 - assert marginTop != null;  
106 - assert marginRight != null;  
107 - assert marginBottom != null;  
108 -  
109 - PrintAttributes.Margins margins =  
110 - new PrintAttributes.Margins(Double.valueOf(marginLeft * 1000.0).intValue(),  
111 - Double.valueOf(marginTop * 1000.0 / 72.0).intValue(),  
112 - Double.valueOf(marginRight * 1000.0 / 72.0).intValue(),  
113 - Double.valueOf(marginBottom * 1000.0 / 72.0).intValue());  
114 -  
115 - PrintAttributes.MediaSize size = new PrintAttributes.MediaSize("flutter_printing",  
116 - "Provided size", Double.valueOf(width * 1000.0 / 72.0).intValue(),  
117 - Double.valueOf(height * 1000.0 / 72.0).intValue());  
118 -  
119 - printJob.convertHtml((String) call.argument("html"), size, margins,  
120 - (String) call.argument("baseUrl"));  
121 - result.success(1);  
122 - break;  
123 - }  
124 - case "printingInfo": {  
125 - result.success(PrintingJob.printingInfo());  
126 - break;  
127 - }  
128 - case "rasterPdf": {  
129 - final byte[] document = call.argument("doc");  
130 - final int[] pages = call.argument("pages");  
131 - Double scale = call.argument("scale");  
132 - final PrintingJob printJob =  
133 - new PrintingJob(activity, this, (int) call.argument("job"));  
134 - printJob.rasterPdf(document, pages, scale);  
135 - result.success(1);  
136 - break;  
137 - }  
138 - default:  
139 - result.notImplemented();  
140 - break;  
141 - }  
142 - }  
143 -  
144 - /// Request the Pdf document from flutter  
145 - void onLayout(final PrintingJob printJob, Double width, double height, double marginLeft,  
146 - double marginTop, double marginRight, double marginBottom) {  
147 - HashMap<String, Object> args = new HashMap<>();  
148 - args.put("width", width);  
149 - args.put("height", height);  
150 -  
151 - args.put("marginLeft", marginLeft);  
152 - args.put("marginTop", marginTop);  
153 - args.put("marginRight", marginRight);  
154 - args.put("marginBottom", marginBottom);  
155 - args.put("job", printJob.index);  
156 -  
157 - channel.invokeMethod("onLayout", args, new Result() {  
158 - @Override  
159 - public void success(Object result) {  
160 - if (result instanceof byte[]) {  
161 - printJob.setDocument((byte[]) result);  
162 - } else {  
163 - printJob.cancelJob();  
164 - }  
165 - }  
166 -  
167 - @Override  
168 - public void error(String errorCode, String errorMessage, Object errorDetails) {  
169 - printJob.cancelJob();  
170 - }  
171 -  
172 - @Override  
173 - public void notImplemented() {  
174 - printJob.cancelJob();  
175 - }  
176 - }); 62 + public void onAttachedToEngine(FlutterPluginBinding binding) {
  63 + onAttachedToEngine(binding.getBinaryMessenger());
177 } 64 }
178 65
179 - /// send completion status to flutter  
180 - void onCompleted(PrintingJob printJob, boolean completed, String error) {  
181 - HashMap<String, Object> args = new HashMap<>();  
182 - args.put("completed", completed);  
183 -  
184 - args.put("error", error);  
185 - args.put("job", printJob.index); 66 + private void onAttachedToEngine(BinaryMessenger messenger) {
  67 + channel = new MethodChannel(messenger, "net.nfet.printing");
186 68
187 - channel.invokeMethod("onCompleted", args); 69 + if (activity != null) {
  70 + handler = new PrintingHandler(activity, channel);
  71 + channel.setMethodCallHandler(handler);
  72 + }
188 } 73 }
189 74
190 - /// send html to pdf data result to flutter  
191 - void onHtmlRendered(PrintingJob printJob, byte[] pdfData) {  
192 - HashMap<String, Object> args = new HashMap<>();  
193 - args.put("doc", pdfData);  
194 - args.put("job", printJob.index); 75 + @Override
  76 + public void onDetachedFromEngine(FlutterPluginBinding binding) {
  77 + channel.setMethodCallHandler(null);
  78 + channel = null;
  79 + handler = null;
  80 + }
195 81
196 - channel.invokeMethod("onHtmlRendered", args); 82 + @Override
  83 + public void onAttachedToActivity(ActivityPluginBinding binding) {
  84 + onAttachedToActivity(binding.getActivity());
197 } 85 }
198 86
199 - /// send html to pdf conversion error to flutter  
200 - void onHtmlError(PrintingJob printJob, String error) {  
201 - HashMap<String, Object> args = new HashMap<>();  
202 - args.put("error", error);  
203 - args.put("job", printJob.index); 87 + private void onAttachedToActivity(Activity _activity) {
  88 + activity = _activity;
204 89
205 - channel.invokeMethod("onHtmlError", args); 90 + if (activity != null && channel != null) {
  91 + handler = new PrintingHandler(activity, channel);
  92 + channel.setMethodCallHandler(handler);
  93 + }
206 } 94 }
207 95
208 - /// send pdf to raster data result to flutter  
209 - void onPageRasterized(PrintingJob printJob, byte[] imageData, int width, int height) {  
210 - HashMap<String, Object> args = new HashMap<>();  
211 - args.put("image", imageData);  
212 - args.put("width", width);  
213 - args.put("height", height);  
214 - args.put("job", printJob.index);  
215 -  
216 - channel.invokeMethod("onPageRasterized", args); 96 + @Override
  97 + public void onDetachedFromActivityForConfigChanges() {
  98 + onDetachedFromActivity();
217 } 99 }
218 100
219 - void onPageRasterEnd(PrintingJob printJob) {  
220 - HashMap<String, Object> args = new HashMap<>();  
221 - args.put("job", printJob.index); 101 + @Override
  102 + public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
  103 + onAttachedToActivity(binding.getActivity());
  104 + }
222 105
223 - channel.invokeMethod("onPageRasterEnd", args); 106 + @Override
  107 + public void onDetachedFromActivity() {
  108 + channel.setMethodCallHandler(null);
  109 + activity = null;
  110 + handler = null;
224 } 111 }
225 } 112 }
@@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 3.0.2 7 +version: 3.1.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"