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,26 +128,48 @@ public class PrintingJob extends PrintDocumentAdapter { @@ -128,26 +128,48 @@ public class PrintingJob extends PrintDocumentAdapter {
128 128
129 @Override 129 @Override
130 public void onFinish() { 130 public void onFinish() {
  131 + Thread thread = new Thread(new Runnable() {
  132 + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  133 + @Override
  134 + public void run() {
131 try { 135 try {
132 - while (true) { 136 + final boolean[] wait = {true};
  137 + while (wait[0]) {
  138 + new Handler(Looper.getMainLooper()).post(new Runnable() {
  139 + @Override
  140 + public void run() {
133 int state = printJob.getInfo().getState(); 141 int state = printJob.getInfo().getState();
134 142
135 if (state == PrintJobInfo.STATE_COMPLETED) { 143 if (state == PrintJobInfo.STATE_COMPLETED) {
136 - printing.onCompleted(this, true, "");  
137 - break; 144 + printing.onCompleted(PrintingJob.this, true, "");
  145 + wait[0] = false;
138 } else if (state == PrintJobInfo.STATE_CANCELED) { 146 } else if (state == PrintJobInfo.STATE_CANCELED) {
139 - printing.onCompleted(this, false, "User canceled");  
140 - break; 147 + printing.onCompleted(PrintingJob.this, false, "User canceled");
  148 + wait[0] = false;
  149 + }
141 } 150 }
  151 + });
142 152
  153 + if (wait[0]) {
143 Thread.sleep(200); 154 Thread.sleep(200);
144 } 155 }
145 - } catch (Exception e) {  
146 - printing.onCompleted(this, printJob != null && printJob.isCompleted(), e.getMessage()); 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 + });
147 } 165 }
148 166
149 printJob = null; 167 printJob = null;
150 } 168 }
  169 + });
  170 +
  171 + thread.start();
  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,
153 Double marginTop, Double marginRight, Double marginBottom) { 175 Double marginTop, Double marginRight, Double marginBottom) {