Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

Commit 82b64d9

Browse files
committed
Generating the PDF in a thread
1 parent 4f8d270 commit 82b64d9

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
### Changé
1313
- Ajout de marges autour des motifs de sortie
1414
- Méthode d'ouverture du PDF, ajout d'un message informatif si pas de lecteur PDF installé
15+
- Création du PDF dans un thread, ajout d'une fenêtre de chargement
1516

1617
## [1.2.1] - 2020-04-11
1718
### Changé

app/src/main/java/com/poupa/attestationdeplacement/CreateAttestationActivity.java

+62-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.poupa.attestationdeplacement;
22

3+
import android.app.ProgressDialog;
34
import android.app.TimePickerDialog;
45
import android.content.Intent;
56
import android.content.SharedPreferences;
67
import android.content.res.AssetManager;
78
import android.graphics.Bitmap;
9+
import android.os.AsyncTask;
810
import android.os.Bundle;
911
import android.view.View;
1012
import android.widget.CheckBox;
@@ -20,7 +22,6 @@
2022
import com.google.zxing.MultiFormatWriter;
2123
import com.google.zxing.WriterException;
2224
import com.google.zxing.common.BitMatrix;
23-
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
2425
import com.itextpdf.text.BaseColor;
2526
import com.itextpdf.text.DocumentException;
2627
import com.itextpdf.text.Element;
@@ -168,10 +169,67 @@ public void onTimeSet(TimePicker timePicker, int hour, int minute) {
168169
}
169170

170171
/**
171-
* Generates the PDF file and the QRCode
172+
* Generates the PDF by calling the async task
172173
* @param v
173174
*/
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() {
175233
try {
176234
saveFields();
177235

@@ -199,11 +257,6 @@ public void generate(View v) {
199257
stamper.setFormFlattening(true);
200258

201259
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);
207260
} catch (IOException | WriterException | DocumentException e) {
208261
e.printStackTrace();
209262
Toast.makeText(this, "Erreur", Toast.LENGTH_SHORT).show();
@@ -489,8 +542,7 @@ public Bitmap generateQrCode(String str, int width, int height) throws WriterExc
489542
BitMatrix result;
490543
try {
491544
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);
494546
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
495547
result = new MultiFormatWriter().encode(str,
496548
BarcodeFormat.QR_CODE, width, height, hints);

app/src/main/res/layout/content_create_attestation.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
android:layout_height="wrap_content"
352352
android:layout_marginTop="8dp"
353353
android:layout_marginBottom="8dp"
354-
android:onClick="generate"
354+
android:onClick="startGenerate"
355355
android:text="@string/generate"
356356
android:textColor="@color/design_default_color_on_primary"
357357
app:layout_constraintBottom_toBottomOf="parent"

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@
3232

3333
<string name="delete_information">Voulez-vous vraiment supprimer cette attestation ?</string>
3434
<string name="no_pdf_reader">Vous n\'avez pas d\'application permettant de lire des fichiers PDF. Veuillez en installer une avant de réessayer.</string>
35+
<string name="generating">Génération de l\'attestation</string>
36+
<string name="loading">Chargement..</string>
3537
</resources>

0 commit comments

Comments
 (0)