David PHAM-VAN

Fix Android app freeze

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 3.1.0 3 ## 3.1.0
4 4
5 - Migrate to the new Android plugins APIs 5 - Migrate to the new Android plugins APIs
  6 +- Fix Android app freeze
6 7
7 ## 3.0.2 8 ## 3.0.2
8 9
@@ -128,25 +128,47 @@ public class PrintingJob extends PrintDocumentAdapter { @@ -128,25 +128,47 @@ public class PrintingJob extends PrintDocumentAdapter {
128 128
129 @Override 129 @Override
130 public void onFinish() { 130 public void onFinish() {
131 - try {  
132 - while (true) {  
133 - int state = printJob.getInfo().getState();  
134 -  
135 - if (state == PrintJobInfo.STATE_COMPLETED) {  
136 - printing.onCompleted(this, true, "");  
137 - break;  
138 - } else if (state == PrintJobInfo.STATE_CANCELED) {  
139 - printing.onCompleted(this, false, "User canceled");  
140 - break; 131 + Thread thread = new Thread(new Runnable() {
  132 + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  133 + @Override
  134 + public void run() {
  135 + try {
  136 + final boolean[] wait = {true};
  137 + while (wait[0]) {
  138 + new Handler(Looper.getMainLooper()).post(new Runnable() {
  139 + @Override
  140 + public void run() {
  141 + int state = printJob.getInfo().getState();
  142 +
  143 + if (state == PrintJobInfo.STATE_COMPLETED) {
  144 + printing.onCompleted(PrintingJob.this, true, "");
  145 + wait[0] = false;
  146 + } else if (state == PrintJobInfo.STATE_CANCELED) {
  147 + printing.onCompleted(PrintingJob.this, false, "User canceled");
  148 + wait[0] = false;
  149 + }
  150 + }
  151 + });
  152 +
  153 + if (wait[0]) {
  154 + Thread.sleep(200);
  155 + }
  156 + }
  157 + } catch (final Exception e) {
  158 + new Handler(Looper.getMainLooper()).post(new Runnable() {
  159 + @Override
  160 + public void run() {
  161 + printing.onCompleted(PrintingJob.this,
  162 + printJob != null && printJob.isCompleted(), e.getMessage());
  163 + }
  164 + });
141 } 165 }
142 166
143 - Thread.sleep(200); 167 + printJob = null;
144 } 168 }
145 - } catch (Exception e) {  
146 - printing.onCompleted(this, printJob != null && printJob.isCompleted(), e.getMessage());  
147 - } 169 + });
148 170
149 - printJob = null; 171 + thread.start();
150 } 172 }
151 173
152 void printPdf(@NonNull String name, Double width, Double height, Double marginLeft, 174 void printPdf(@NonNull String name, Double width, Double height, Double marginLeft,