Showing
3 changed files
with
85 additions
and
115 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,54 +137,44 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -137,54 +137,44 @@ 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() { | ||
| 144 | - try { | ||
| 145 | - final boolean[] wait = {true}; | ||
| 146 | - int count = 5 * 60 * 10; // That's 10 minutes. | ||
| 147 | - while (wait[0]) { | ||
| 148 | - new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
| 149 | - @Override | ||
| 150 | - public void run() { | ||
| 151 | - int state = printJob == null ? PrintJobInfo.STATE_FAILED | ||
| 152 | - : printJob.getInfo().getState(); | ||
| 153 | - | ||
| 154 | - if (state == PrintJobInfo.STATE_COMPLETED) { | ||
| 155 | - printing.onCompleted(PrintingJob.this, true, null); | ||
| 156 | - wait[0] = false; | ||
| 157 | - } else if (state == PrintJobInfo.STATE_CANCELED) { | ||
| 158 | - printing.onCompleted(PrintingJob.this, false, null); | ||
| 159 | - wait[0] = false; | ||
| 160 | - } else if (state == PrintJobInfo.STATE_FAILED) { | ||
| 161 | - printing.onCompleted( | ||
| 162 | - PrintingJob.this, false, "Unable to print"); | ||
| 163 | - wait[0] = false; | ||
| 164 | - } | ||
| 165 | - } | ||
| 166 | - }); | ||
| 167 | - | ||
| 168 | - if (--count <= 0) { | ||
| 169 | - throw new Exception("Timeout waiting for the job to finish"); | 140 | + Thread thread = new Thread(() -> { |
| 141 | + try { | ||
| 142 | + final boolean[] wait = {true}; | ||
| 143 | + int count = 5 * 60 * 10; // That's 10 minutes. | ||
| 144 | + while (wait[0]) { | ||
| 145 | + new Handler(Looper.getMainLooper()).post(() -> { | ||
| 146 | + int state = printJob == null ? PrintJobInfo.STATE_FAILED | ||
| 147 | + : printJob.getInfo().getState(); | ||
| 148 | + | ||
| 149 | + if (state == PrintJobInfo.STATE_COMPLETED) { | ||
| 150 | + printing.onCompleted(PrintingJob.this, true, null); | ||
| 151 | + wait[0] = false; | ||
| 152 | + } else if (state == PrintJobInfo.STATE_CANCELED) { | ||
| 153 | + printing.onCompleted(PrintingJob.this, false, null); | ||
| 154 | + wait[0] = false; | ||
| 155 | + } else if (state == PrintJobInfo.STATE_FAILED) { | ||
| 156 | + printing.onCompleted(PrintingJob.this, false, "Unable to print"); | ||
| 157 | + wait[0] = false; | ||
| 170 | } | 158 | } |
| 159 | + }); | ||
| 171 | 160 | ||
| 172 | - if (wait[0]) { | ||
| 173 | - Thread.sleep(200); | ||
| 174 | - } | 161 | + if (--count <= 0) { |
| 162 | + throw new Exception("Timeout waiting for the job to finish"); | ||
| 175 | } | 163 | } |
| 176 | - } 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 | - }); | ||
| 184 | - } | ||
| 185 | 164 | ||
| 186 | - printJob = null; | 165 | + if (wait[0]) { |
| 166 | + Thread.sleep(200); | ||
| 167 | + } | ||
| 168 | + } | ||
| 169 | + } catch (final Exception e) { | ||
| 170 | + new Handler(Looper.getMainLooper()) | ||
| 171 | + .post(() | ||
| 172 | + -> printing.onCompleted(PrintingJob.this, | ||
| 173 | + printJob != null && printJob.isCompleted(), | ||
| 174 | + e.getMessage())); | ||
| 187 | } | 175 | } |
| 176 | + | ||
| 177 | + printJob = null; | ||
| 188 | }); | 178 | }); |
| 189 | 179 | ||
| 190 | thread.start(); | 180 | thread.start(); |
| @@ -439,86 +429,67 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -439,86 +429,67 @@ 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() { | ||
| 446 | - String error = null; | ||
| 447 | - try { | ||
| 448 | - File tempDir = context.getCacheDir(); | ||
| 449 | - File file = File.createTempFile("printing", null, tempDir); | ||
| 450 | - FileOutputStream oStream = new FileOutputStream(file); | ||
| 451 | - oStream.write(data); | ||
| 452 | - oStream.close(); | ||
| 453 | - | ||
| 454 | - FileInputStream iStream = new FileInputStream(file); | ||
| 455 | - ParcelFileDescriptor parcelFD = ParcelFileDescriptor.dup(iStream.getFD()); | ||
| 456 | - PdfRenderer renderer = new PdfRenderer(parcelFD); | ||
| 457 | - | ||
| 458 | - if (!file.delete()) { | ||
| 459 | - Log.e("PDF", "Unable to delete temporary file"); | ||
| 460 | - } | ||
| 461 | - | ||
| 462 | - final int pageCount = pages != null ? pages.size() : renderer.getPageCount(); | ||
| 463 | - for (int i = 0; i < pageCount; i++) { | ||
| 464 | - PdfRenderer.Page page = renderer.openPage(pages == null ? i : pages.get(i)); | ||
| 465 | - | ||
| 466 | - final int width = Double.valueOf(page.getWidth() * scale).intValue(); | ||
| 467 | - final int height = Double.valueOf(page.getHeight() * scale).intValue(); | ||
| 468 | - int stride = width * 4; | 432 | + Thread thread = new Thread(() -> { |
| 433 | + String error = null; | ||
| 434 | + try { | ||
| 435 | + File tempDir = context.getCacheDir(); | ||
| 436 | + File file = File.createTempFile("printing", null, tempDir); | ||
| 437 | + FileOutputStream oStream = new FileOutputStream(file); | ||
| 438 | + oStream.write(data); | ||
| 439 | + oStream.close(); | ||
| 440 | + | ||
| 441 | + FileInputStream iStream = new FileInputStream(file); | ||
| 442 | + ParcelFileDescriptor parcelFD = ParcelFileDescriptor.dup(iStream.getFD()); | ||
| 443 | + PdfRenderer renderer = new PdfRenderer(parcelFD); | ||
| 444 | + | ||
| 445 | + if (!file.delete()) { | ||
| 446 | + Log.e("PDF", "Unable to delete temporary file"); | ||
| 447 | + } | ||
| 469 | 448 | ||
| 470 | - Matrix transform = new Matrix(); | ||
| 471 | - transform.setScale(scale.floatValue(), scale.floatValue()); | 449 | + final int pageCount = pages != null ? pages.size() : renderer.getPageCount(); |
| 450 | + for (int i = 0; i < pageCount; i++) { | ||
| 451 | + PdfRenderer.Page page = renderer.openPage(pages == null ? i : pages.get(i)); | ||
| 472 | 452 | ||
| 473 | - Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | 453 | + final int width = Double.valueOf(page.getWidth() * scale).intValue(); |
| 454 | + final int height = Double.valueOf(page.getHeight() * scale).intValue(); | ||
| 455 | + int stride = width * 4; | ||
| 474 | 456 | ||
| 475 | - page.render( | ||
| 476 | - bitmap, null, transform, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); | 457 | + Matrix transform = new Matrix(); |
| 458 | + transform.setScale(scale.floatValue(), scale.floatValue()); | ||
| 477 | 459 | ||
| 478 | - page.close(); | 460 | + Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 479 | 461 | ||
| 480 | - final ByteBuffer buf = ByteBuffer.allocate(stride * height); | ||
| 481 | - bitmap.copyPixelsToBuffer(buf); | ||
| 482 | - bitmap.recycle(); | 462 | + page.render(bitmap, null, transform, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); |
| 483 | 463 | ||
| 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 | - }); | ||
| 491 | - } | 464 | + page.close(); |
| 492 | 465 | ||
| 493 | - renderer.close(); | ||
| 494 | - iStream.close(); | 466 | + final ByteBuffer buf = ByteBuffer.allocate(stride * height); |
| 467 | + bitmap.copyPixelsToBuffer(buf); | ||
| 468 | + bitmap.recycle(); | ||
| 495 | 469 | ||
| 496 | - } catch (IOException e) { | ||
| 497 | - e.printStackTrace(); | ||
| 498 | - error = e.getMessage(); | 470 | + new Handler(Looper.getMainLooper()) |
| 471 | + .post(() | ||
| 472 | + -> printing.onPageRasterized( | ||
| 473 | + PrintingJob.this, buf.array(), width, height)); | ||
| 499 | } | 474 | } |
| 500 | 475 | ||
| 501 | - 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 | - }); | 476 | + renderer.close(); |
| 477 | + iStream.close(); | ||
| 478 | + | ||
| 479 | + } catch (IOException e) { | ||
| 480 | + e.printStackTrace(); | ||
| 481 | + error = e.getMessage(); | ||
| 508 | } | 482 | } |
| 483 | + | ||
| 484 | + final String finalError = error; | ||
| 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) { | ||
| 514 | - 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 | - } | 489 | + thread.setUncaughtExceptionHandler((t, e) -> { |
| 490 | + final String finalError = e.getMessage(); | ||
| 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