Showing
3 changed files
with
23 additions
and
53 deletions
@@ -7,7 +7,6 @@ import android.print.PrintAttributes; | @@ -7,7 +7,6 @@ import android.print.PrintAttributes; | ||
7 | import androidx.annotation.NonNull; | 7 | import androidx.annotation.NonNull; |
8 | import androidx.annotation.RequiresApi; | 8 | import androidx.annotation.RequiresApi; |
9 | 9 | ||
10 | -import java.lang.ref.WeakReference; | ||
11 | import java.util.ArrayList; | 10 | import java.util.ArrayList; |
12 | import java.util.HashMap; | 11 | import java.util.HashMap; |
13 | 12 | ||
@@ -35,6 +34,8 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -35,6 +34,8 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
35 | final PrintingJob printJob = | 34 | final PrintingJob printJob = |
36 | new PrintingJob(context, this, (int) call.argument("job")); | 35 | new PrintingJob(context, this, (int) call.argument("job")); |
37 | assert name != null; | 36 | assert name != null; |
37 | + assert width != null; | ||
38 | + assert height != null; | ||
38 | printJob.printPdf(name, width, height); | 39 | printJob.printPdf(name, width, height); |
39 | 40 | ||
40 | result.success(1); | 41 | result.success(1); |
@@ -138,7 +139,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -138,7 +139,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
138 | } | 139 | } |
139 | 140 | ||
140 | @Override | 141 | @Override |
141 | - public void error(String errorCode, String errorMessage, Object errorDetails) { | 142 | + public void error(@NonNull String errorCode, String errorMessage, Object errorDetails) { |
142 | printJob.cancelJob(errorMessage); | 143 | printJob.cancelJob(errorMessage); |
143 | } | 144 | } |
144 | 145 |
@@ -137,17 +137,12 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -137,17 +137,12 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
137 | 137 | ||
138 | @Override | 138 | @Override |
139 | public void onFinish() { | 139 | public void onFinish() { |
140 | - Thread thread = new Thread(new Runnable() { | ||
141 | - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
142 | - @Override | ||
143 | - public void run() { | 140 | + Thread thread = new Thread(() -> { |
144 | try { | 141 | try { |
145 | final boolean[] wait = {true}; | 142 | final boolean[] wait = {true}; |
146 | int count = 5 * 60 * 10; // That's 10 minutes. | 143 | int count = 5 * 60 * 10; // That's 10 minutes. |
147 | while (wait[0]) { | 144 | while (wait[0]) { |
148 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
149 | - @Override | ||
150 | - public void run() { | 145 | + new Handler(Looper.getMainLooper()).post(() -> { |
151 | int state = printJob == null ? PrintJobInfo.STATE_FAILED | 146 | int state = printJob == null ? PrintJobInfo.STATE_FAILED |
152 | : printJob.getInfo().getState(); | 147 | : printJob.getInfo().getState(); |
153 | 148 | ||
@@ -158,11 +153,9 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -158,11 +153,9 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
158 | printing.onCompleted(PrintingJob.this, false, null); | 153 | printing.onCompleted(PrintingJob.this, false, null); |
159 | wait[0] = false; | 154 | wait[0] = false; |
160 | } else if (state == PrintJobInfo.STATE_FAILED) { | 155 | } else if (state == PrintJobInfo.STATE_FAILED) { |
161 | - printing.onCompleted( | ||
162 | - PrintingJob.this, false, "Unable to print"); | 156 | + printing.onCompleted(PrintingJob.this, false, "Unable to print"); |
163 | wait[0] = false; | 157 | wait[0] = false; |
164 | } | 158 | } |
165 | - } | ||
166 | }); | 159 | }); |
167 | 160 | ||
168 | if (--count <= 0) { | 161 | if (--count <= 0) { |
@@ -174,17 +167,14 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -174,17 +167,14 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
174 | } | 167 | } |
175 | } | 168 | } |
176 | } catch (final Exception e) { | 169 | } catch (final Exception e) { |
177 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
178 | - @Override | ||
179 | - public void run() { | ||
180 | - printing.onCompleted(PrintingJob.this, | ||
181 | - printJob != null && printJob.isCompleted(), e.getMessage()); | ||
182 | - } | ||
183 | - }); | 170 | + new Handler(Looper.getMainLooper()) |
171 | + .post(() | ||
172 | + -> printing.onCompleted(PrintingJob.this, | ||
173 | + printJob != null && printJob.isCompleted(), | ||
174 | + e.getMessage())); | ||
184 | } | 175 | } |
185 | 176 | ||
186 | printJob = null; | 177 | printJob = null; |
187 | - } | ||
188 | }); | 178 | }); |
189 | 179 | ||
190 | thread.start(); | 180 | thread.start(); |
@@ -439,10 +429,7 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -439,10 +429,7 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
439 | return; | 429 | return; |
440 | } | 430 | } |
441 | 431 | ||
442 | - Thread thread = new Thread(new Runnable() { | ||
443 | - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
444 | - @Override | ||
445 | - public void run() { | 432 | + Thread thread = new Thread(() -> { |
446 | String error = null; | 433 | String error = null; |
447 | try { | 434 | try { |
448 | File tempDir = context.getCacheDir(); | 435 | File tempDir = context.getCacheDir(); |
@@ -472,8 +459,7 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -472,8 +459,7 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
472 | 459 | ||
473 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | 460 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
474 | 461 | ||
475 | - page.render( | ||
476 | - bitmap, null, transform, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); | 462 | + page.render(bitmap, null, transform, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); |
477 | 463 | ||
478 | page.close(); | 464 | page.close(); |
479 | 465 | ||
@@ -481,13 +467,10 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -481,13 +467,10 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
481 | bitmap.copyPixelsToBuffer(buf); | 467 | bitmap.copyPixelsToBuffer(buf); |
482 | bitmap.recycle(); | 468 | bitmap.recycle(); |
483 | 469 | ||
484 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
485 | - @Override | ||
486 | - public void run() { | ||
487 | - printing.onPageRasterized( | ||
488 | - PrintingJob.this, buf.array(), width, height); | ||
489 | - } | ||
490 | - }); | 470 | + new Handler(Looper.getMainLooper()) |
471 | + .post(() | ||
472 | + -> printing.onPageRasterized( | ||
473 | + PrintingJob.this, buf.array(), width, height)); | ||
491 | } | 474 | } |
492 | 475 | ||
493 | renderer.close(); | 476 | renderer.close(); |
@@ -499,26 +482,14 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -499,26 +482,14 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
499 | } | 482 | } |
500 | 483 | ||
501 | final String finalError = error; | 484 | final String finalError = error; |
502 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
503 | - @Override | ||
504 | - public void run() { | ||
505 | - printing.onPageRasterEnd(PrintingJob.this, finalError); | ||
506 | - } | ||
507 | - }); | ||
508 | - } | 485 | + new Handler(Looper.getMainLooper()) |
486 | + .post(() -> printing.onPageRasterEnd(PrintingJob.this, finalError)); | ||
509 | }); | 487 | }); |
510 | 488 | ||
511 | - thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | ||
512 | - @Override | ||
513 | - public void uncaughtException(Thread t, Throwable e) { | 489 | + thread.setUncaughtExceptionHandler((t, e) -> { |
514 | final String finalError = e.getMessage(); | 490 | final String finalError = e.getMessage(); |
515 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
516 | - @Override | ||
517 | - public void run() { | ||
518 | - printing.onPageRasterEnd(PrintingJob.this, finalError); | ||
519 | - } | ||
520 | - }); | ||
521 | - } | 491 | + new Handler(Looper.getMainLooper()) |
492 | + .post(() -> printing.onPageRasterEnd(PrintingJob.this, finalError)); | ||
522 | }); | 493 | }); |
523 | 494 | ||
524 | thread.start(); | 495 | thread.start(); |
@@ -20,8 +20,6 @@ import android.content.Context; | @@ -20,8 +20,6 @@ import android.content.Context; | ||
20 | 20 | ||
21 | import androidx.annotation.NonNull; | 21 | import androidx.annotation.NonNull; |
22 | 22 | ||
23 | -import java.lang.ref.WeakReference; | ||
24 | - | ||
25 | import io.flutter.embedding.engine.plugins.FlutterPlugin; | 23 | import io.flutter.embedding.engine.plugins.FlutterPlugin; |
26 | import io.flutter.embedding.engine.plugins.activity.ActivityAware; | 24 | import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
27 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; | 25 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
@@ -59,7 +57,7 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | @@ -59,7 +57,7 @@ public class PrintingPlugin implements FlutterPlugin, ActivityAware { | ||
59 | } | 57 | } |
60 | 58 | ||
61 | @Override | 59 | @Override |
62 | - public void onAttachedToActivity(ActivityPluginBinding binding) { | 60 | + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { |
63 | if (context != null) { | 61 | if (context != null) { |
64 | context = null; | 62 | context = null; |
65 | } | 63 | } |
-
Please register or login to post a comment