|
1 | 1 | package com.poupa.attestationdeplacement;
|
2 | 2 |
|
| 3 | +import android.app.ProgressDialog; |
3 | 4 | import android.app.TimePickerDialog;
|
4 | 5 | import android.content.Intent;
|
5 | 6 | import android.content.SharedPreferences;
|
6 | 7 | import android.content.res.AssetManager;
|
7 | 8 | import android.graphics.Bitmap;
|
| 9 | +import android.os.AsyncTask; |
8 | 10 | import android.os.Bundle;
|
9 | 11 | import android.view.View;
|
10 | 12 | import android.widget.CheckBox;
|
|
20 | 22 | import com.google.zxing.MultiFormatWriter;
|
21 | 23 | import com.google.zxing.WriterException;
|
22 | 24 | import com.google.zxing.common.BitMatrix;
|
23 |
| -import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
24 | 25 | import com.itextpdf.text.BaseColor;
|
25 | 26 | import com.itextpdf.text.DocumentException;
|
26 | 27 | import com.itextpdf.text.Element;
|
@@ -168,10 +169,67 @@ public void onTimeSet(TimePicker timePicker, int hour, int minute) {
|
168 | 169 | }
|
169 | 170 |
|
170 | 171 | /**
|
171 |
| - * Generates the PDF file and the QRCode |
| 172 | + * Generates the PDF by calling the async task |
172 | 173 | * @param v
|
173 | 174 | */
|
174 |
| - public void generate(View v) { |
| 175 | + public void startGenerate(View v) { |
| 176 | + new Thread(new Runnable() { |
| 177 | + @Override |
| 178 | + public void run() { |
| 179 | + final GeneratePdfTask task = new GeneratePdfTask(); |
| 180 | + task.execute(); |
| 181 | + } |
| 182 | + }).start(); |
| 183 | + } |
| 184 | + |
| 185 | + private class GeneratePdfTask extends AsyncTask<Void, Void, Void> { |
| 186 | + ProgressDialog nDialog; |
| 187 | + |
| 188 | + @Override |
| 189 | + protected void onPreExecute() { |
| 190 | + CreateAttestationActivity.this.runOnUiThread(new Runnable() |
| 191 | + { |
| 192 | + public void run() |
| 193 | + { |
| 194 | + nDialog = new ProgressDialog(CreateAttestationActivity.this); |
| 195 | + nDialog.setMessage(getString(R.string.loading)); |
| 196 | + nDialog.setTitle(getString(R.string.generating)); |
| 197 | + nDialog.setIndeterminate(true); |
| 198 | + nDialog.setCancelable(false); |
| 199 | + nDialog.show(); |
| 200 | + } |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + protected void onPostExecute(Void result) { |
| 206 | + CreateAttestationActivity.this.runOnUiThread(new Runnable() |
| 207 | + { |
| 208 | + public void run() |
| 209 | + { |
| 210 | + nDialog.dismiss(); |
| 211 | + |
| 212 | + Toast.makeText(CreateAttestationActivity.this, "Attestation générée !", Toast.LENGTH_SHORT).show(); |
| 213 | + |
| 214 | + Intent show = new Intent(CreateAttestationActivity.this, MainActivity.class); |
| 215 | + |
| 216 | + startActivity(show); |
| 217 | + } |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + protected Void doInBackground(Void... voids) { |
| 223 | + generate(); |
| 224 | + |
| 225 | + return null; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Generates the PDF file and the QRCode |
| 231 | + */ |
| 232 | + private void generate() { |
175 | 233 | try {
|
176 | 234 | saveFields();
|
177 | 235 |
|
@@ -199,11 +257,6 @@ public void generate(View v) {
|
199 | 257 | stamper.setFormFlattening(true);
|
200 | 258 |
|
201 | 259 | stamper.close();
|
202 |
| - |
203 |
| - Toast.makeText(this, "Attestation générée !", Toast.LENGTH_SHORT).show(); |
204 |
| - |
205 |
| - Intent show = new Intent(this, MainActivity.class); |
206 |
| - startActivity(show); |
207 | 260 | } catch (IOException | WriterException | DocumentException e) {
|
208 | 261 | e.printStackTrace();
|
209 | 262 | Toast.makeText(this, "Erreur", Toast.LENGTH_SHORT).show();
|
@@ -489,8 +542,7 @@ public Bitmap generateQrCode(String str, int width, int height) throws WriterExc
|
489 | 542 | BitMatrix result;
|
490 | 543 | try {
|
491 | 544 | Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
|
492 |
| - hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); |
493 |
| - hints.put(EncodeHintType.MARGIN, 1); |
| 545 | + hints.put(EncodeHintType.MARGIN, 0); |
494 | 546 | hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
495 | 547 | result = new MultiFormatWriter().encode(str,
|
496 | 548 | BarcodeFormat.QR_CODE, width, height, hints);
|
|
0 commit comments