|
@@ -0,0 +1,692 @@
|
|
1
|
+package uprrp.tania.activities;
|
|
2
|
+
|
|
3
|
+//import android.app.ProgressDialog;
|
|
4
|
+import android.content.Intent;
|
|
5
|
+import android.content.SharedPreferences;
|
|
6
|
+import android.content.pm.PackageManager;
|
|
7
|
+import android.net.Uri;
|
|
8
|
+import android.os.Bundle;
|
|
9
|
+ import android.util.Base64;
|
|
10
|
+import android.util.Log;
|
|
11
|
+ import android.widget.Button;
|
|
12
|
+import android.widget.TextView;
|
|
13
|
+import android.widget.Toast;
|
|
14
|
+
|
|
15
|
+import androidx.annotation.Nullable;
|
|
16
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
17
|
+
|
|
18
|
+ import com.itextpdf.text.BaseColor;
|
|
19
|
+import com.itextpdf.text.Chunk;
|
|
20
|
+import com.itextpdf.text.Document;
|
|
21
|
+ import com.itextpdf.text.Element;
|
|
22
|
+import com.itextpdf.text.Font;
|
|
23
|
+import com.itextpdf.text.Image;
|
|
24
|
+import com.itextpdf.text.Paragraph;
|
|
25
|
+import com.itextpdf.text.pdf.PdfWriter;
|
|
26
|
+
|
|
27
|
+import org.researchstack.backbone.StorageAccess;
|
|
28
|
+import org.researchstack.backbone.model.ConsentDocument;
|
|
29
|
+import org.researchstack.backbone.model.ConsentSection;
|
|
30
|
+import org.researchstack.backbone.model.ConsentSignature;
|
|
31
|
+import org.researchstack.backbone.result.TaskResult;
|
|
32
|
+import org.researchstack.backbone.step.ConsentDocumentStep;
|
|
33
|
+import org.researchstack.backbone.step.ConsentSignatureStep;
|
|
34
|
+import org.researchstack.backbone.step.ConsentVisualStep;
|
|
35
|
+import org.researchstack.backbone.task.OrderedTask;
|
|
36
|
+import org.researchstack.backbone.task.Task;
|
|
37
|
+import org.researchstack.backbone.ui.ViewTaskActivity;
|
|
38
|
+import org.researchstack.backbone.ui.step.layout.ConsentSignatureStepLayout;
|
|
39
|
+
|
|
40
|
+import java.io.BufferedInputStream;
|
|
41
|
+import java.io.File;
|
|
42
|
+import java.io.FileInputStream;
|
|
43
|
+ import java.io.FileOutputStream;
|
|
44
|
+import java.io.IOException;
|
|
45
|
+import java.util.Observable;
|
|
46
|
+import java.util.Observer;
|
|
47
|
+
|
|
48
|
+import uprrp.tania.GlobalValues;
|
|
49
|
+import uprrp.tania.R;
|
|
50
|
+import uprrp.tania.networking.SendConsentForm;
|
|
51
|
+import uprrp.tania.utils.URLEventListener;
|
|
52
|
+
|
|
53
|
+public class GettingStartedActivity extends AppCompatActivity implements Observer {
|
|
54
|
+
|
|
55
|
+ private static final String TAG = "GettingStartedActivity";
|
|
56
|
+ private static final int REQUEST_CONSENT = 0;
|
|
57
|
+ public static final String VISUAL_CONSENT_IDENTIFIER = "visual_consent_identifier";
|
|
58
|
+ public static final String CONSENT_DOC = "consent_doc";
|
|
59
|
+ // public static final String SIGNATURE_FORM_STEP = "form_step";
|
|
60
|
+// public static final String NAME = "name";
|
|
61
|
+ public static final String CONSENT = "consent";
|
|
62
|
+ public static final String SIGNATURE = "signature";
|
|
63
|
+// private static final int STORAGE_PERMISSION_CODE = 1;
|
|
64
|
+
|
|
65
|
+ @Override
|
|
66
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
67
|
+
|
|
68
|
+ // Constructor stuff
|
|
69
|
+ super.onCreate(savedInstanceState);
|
|
70
|
+ setContentView(R.layout.activity_getting_started);
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ // If user is already logged in, go to MainActivity
|
|
74
|
+ final SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
|
75
|
+ boolean needsToRegister = prefs.getBoolean("needsToRegister", true);
|
|
76
|
+ if(!needsToRegister) {
|
|
77
|
+ Intent intent = new Intent(GettingStartedActivity.this, MainActivity.class);
|
|
78
|
+ startActivity(intent);
|
|
79
|
+ return;
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+// // Get permission to write to external storage - files
|
|
84
|
+// String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE };
|
|
85
|
+// requestPermissions(permissions, 1);
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+ // Listen for device token changes
|
|
89
|
+ this.startObservingGlobals();
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+ // Disable interaction initially while token is being fetched
|
|
93
|
+ if(GlobalValues.getInstance().getDeviceToken() == null) {
|
|
94
|
+ disableInteraction();
|
|
95
|
+ } else {
|
|
96
|
+ enableInteraction();
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ TextView privacyPolicyLink = findViewById(R.id.privacy_policy_link);
|
|
100
|
+ privacyPolicyLink.setOnClickListener(v -> openPrivacyPolicy());
|
|
101
|
+
|
|
102
|
+ // Attach onClick listeners to buttons
|
|
103
|
+ final Button createAccountButton = findViewById(R.id.buttonCreateAccount);
|
|
104
|
+ final Button loginAccountButton = findViewById(R.id.buttonRecoverAccount);
|
|
105
|
+ createAccountButton.setTransformationMethod(null); // TODO: this is a workaround I found, any other acceptable solution is welcome
|
|
106
|
+ loginAccountButton.setTransformationMethod(null); // TODO: this is a workaround I found, any other acceptable solution is welcome
|
|
107
|
+ createAccountButton.setOnClickListener(v -> {
|
|
108
|
+ boolean alreadyConsented = prefs.getBoolean("alreadyConsented", false);
|
|
109
|
+
|
|
110
|
+ if(alreadyConsented) {
|
|
111
|
+ Intent intent = new Intent(GettingStartedActivity.this, UserRegistrationActivity.class);
|
|
112
|
+ startActivity(intent);
|
|
113
|
+ } else {
|
|
114
|
+ launchConsent();
|
|
115
|
+ }
|
|
116
|
+ });
|
|
117
|
+ loginAccountButton.setOnClickListener(v -> {
|
|
118
|
+ Intent intent = new Intent(GettingStartedActivity.this, AccountRecoveryActivity.class);
|
|
119
|
+ startActivity(intent);
|
|
120
|
+ });
|
|
121
|
+
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ // TAKEN FROM https://stackoverflow.com/questions/21886768/is-there-any-method-i-can-listen-for-changes-to-global-variable-to-trigger-an-ev
|
|
125
|
+ private void startObservingGlobals() {
|
|
126
|
+ GlobalValues.getInstance().addObserver(this);
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ // TAKEN FROM https://stackoverflow.com/questions/21886768/is-there-any-method-i-can-listen-for-changes-to-global-variable-to-trigger-an-ev
|
|
130
|
+ @Override
|
|
131
|
+ public void update(Observable observable, Object o) {
|
|
132
|
+ if(observable instanceof GlobalValues) {
|
|
133
|
+ if(o instanceof GlobalValues.ValueKey) {
|
|
134
|
+ if(((GlobalValues.ValueKey) o).getKey() == GlobalValues.ValueName.DEVICE_TOKEN) {
|
|
135
|
+ if(GlobalValues.getInstance().getDeviceToken() == null) {
|
|
136
|
+ disableInteraction();
|
|
137
|
+ } else {
|
|
138
|
+ enableInteraction();
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ private void enableInteraction() {
|
|
146
|
+ Button createAccountButton = findViewById(R.id.buttonCreateAccount);
|
|
147
|
+ Button loginAccountButton = findViewById(R.id.buttonRecoverAccount);
|
|
148
|
+ createAccountButton.setEnabled(true);
|
|
149
|
+ loginAccountButton.setEnabled(true);
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ private void disableInteraction() {
|
|
153
|
+ Button createAccountButton = findViewById(R.id.buttonCreateAccount);
|
|
154
|
+ Button loginAccountButton = findViewById(R.id.buttonRecoverAccount);
|
|
155
|
+ createAccountButton.setEnabled(false);
|
|
156
|
+ loginAccountButton.setEnabled(false);
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ @Override
|
|
160
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
161
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
162
|
+ if(requestCode == REQUEST_CONSENT && resultCode == RESULT_OK) {
|
|
163
|
+ TaskResult taskResult = (TaskResult) data.getSerializableExtra(ViewTaskActivity.EXTRA_TASK_RESULT);
|
|
164
|
+ assert taskResult != null;
|
|
165
|
+ this.processConsentResult(taskResult);
|
|
166
|
+ }
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+ @Override
|
|
171
|
+ public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
172
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
173
|
+ if (requestCode == 1) {
|
|
174
|
+ if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
|
|
175
|
+ Toast.makeText(getApplicationContext(), "Permission for writing to external storage: DENIED", Toast.LENGTH_LONG).show();
|
|
176
|
+ }
|
|
177
|
+ }
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+ private void processConsentResult(TaskResult result) {
|
|
182
|
+
|
|
183
|
+ boolean consented = (boolean) result.getStepResult(CONSENT_DOC).getResult();
|
|
184
|
+ StorageAccess.getInstance().getAppDatabase().saveTaskResult(result);
|
|
185
|
+
|
|
186
|
+ if(consented) {
|
|
187
|
+
|
|
188
|
+ // Get signature in base64
|
|
189
|
+ TaskResult taskResult = StorageAccess.getInstance()
|
|
190
|
+ .getAppDatabase()
|
|
191
|
+ .loadLatestTaskResult(CONSENT);
|
|
192
|
+ String signatureBase64 = (String) taskResult.getStepResult(SIGNATURE)
|
|
193
|
+ .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE);
|
|
194
|
+
|
|
195
|
+ // Get signature date
|
|
196
|
+ String signatureDate = (String) result.getStepResult(SIGNATURE)
|
|
197
|
+ .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE_DATE);
|
|
198
|
+
|
|
199
|
+ // Use signature to get encoded consent form
|
|
200
|
+ byte[] signatureBytes = Base64.decode(signatureBase64, Base64.DEFAULT);
|
|
201
|
+ String consentFormBase64 = this.createPdf(signatureBytes, signatureDate);
|
|
202
|
+ restoreConsentForm(consentFormBase64);
|
|
203
|
+ // Send consent form to backend
|
|
204
|
+ this.sendConsentForm(signatureBase64, consentFormBase64, signatureDate);
|
|
205
|
+
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ private void sendConsentForm(final String signatureBase64, String consentFormBase64, final String signatureDate) {
|
|
211
|
+ if (consentFormBase64 == null || consentFormBase64.isEmpty()) {
|
|
212
|
+ Log.e(TAG, "sendConsentForm: consentFormBase64 is null or empty");
|
|
213
|
+ // Handle this error appropriately, maybe show an error message to the user
|
|
214
|
+ return;
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ Toast.makeText(getApplicationContext(), "Sending consent form...", Toast.LENGTH_LONG).show();
|
|
218
|
+
|
|
219
|
+ // Initiate progress dialog
|
|
220
|
+ // TODO: find substitute for this deprecated dialog box
|
|
221
|
+// final ProgressDialog progressDialog = ProgressDialog.show(getApplicationContext(),
|
|
222
|
+// "Sending Consent Form",
|
|
223
|
+// getString(R.string.progressDialogDescriptionText));
|
|
224
|
+
|
|
225
|
+ SendConsentForm sendConsentFormTask = new SendConsentForm(new URLEventListener() {
|
|
226
|
+ @Override
|
|
227
|
+ public void onSuccess() {
|
|
228
|
+ runOnUiThread(() -> {
|
|
229
|
+ Toast.makeText(getApplicationContext(), "Consent form sent!", Toast.LENGTH_LONG).show();
|
|
230
|
+
|
|
231
|
+ // Save consent signature, consent date and consented boolean on preferences
|
|
232
|
+ SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
|
233
|
+ SharedPreferences.Editor editor = prefs.edit();
|
|
234
|
+ editor.putString("consentSignature", signatureBase64);
|
|
235
|
+ editor.putString("consentDate", signatureDate);
|
|
236
|
+ editor.putBoolean("alreadyConsented", true);
|
|
237
|
+ editor.apply();
|
|
238
|
+
|
|
239
|
+ // Redirect to UserRegistrationActivity
|
|
240
|
+ Intent intent = new Intent(getApplicationContext(), UserRegistrationActivity.class);
|
|
241
|
+ startActivity(intent);
|
|
242
|
+ });
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ @Override
|
|
246
|
+ public void onFailure(Exception e) {
|
|
247
|
+ runOnUiThread(() -> {
|
|
248
|
+ Toast.makeText(getApplicationContext(), "Couldn't send consent form to server!\nPlease retry registration...", Toast.LENGTH_LONG).show();
|
|
249
|
+ Log.e(TAG, "Couldn't send consent form to server!");
|
|
250
|
+ e.printStackTrace();
|
|
251
|
+
|
|
252
|
+ // TODO: prompt retry? although user can just register again...
|
|
253
|
+ // 1. when cancelling, return to getting started activity
|
|
254
|
+ // 2. when retrying, just do recursive call
|
|
255
|
+ });
|
|
256
|
+ }
|
|
257
|
+ });
|
|
258
|
+
|
|
259
|
+ String deviceToken = GlobalValues.getInstance().getDeviceToken();
|
|
260
|
+ sendConsentFormTask.execute(deviceToken, consentFormBase64, signatureDate);
|
|
261
|
+
|
|
262
|
+ }
|
|
263
|
+
|
|
264
|
+ private void launchConsent() {
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+ // Consent Section 1 - Overview (Welcome)
|
|
268
|
+ ConsentSection section1 = new ConsentSection(ConsentSection.Type.Overview);
|
|
269
|
+ section1.setTitle(getString(R.string.overviewTitleText));
|
|
270
|
+ section1.setSummary(getString(R.string.overviewConsentSectionSummaryText));
|
|
271
|
+ section1.setContent(getString(R.string.overviewConsentSectionContentText));
|
|
272
|
+
|
|
273
|
+ ConsentVisualStep visualStep1 = new ConsentVisualStep(VISUAL_CONSENT_IDENTIFIER);
|
|
274
|
+ visualStep1.setStepTitle(R.string.rsb_consent);
|
|
275
|
+ visualStep1.setSection(section1);
|
|
276
|
+ visualStep1.setNextButtonString("Next");
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+ // Consent Section 2 - Time Commitment
|
|
280
|
+ ConsentSection section2 = new ConsentSection(ConsentSection.Type.TimeCommitment);
|
|
281
|
+ section2.setTitle(getString(R.string.timeCommitmentTitleText));
|
|
282
|
+ section2.setSummary(getString(R.string.timeCommitmentConsentSectionSummaryText));
|
|
283
|
+ section2.setContent(getString(R.string.timeCommitmentConsentSectionContentText));
|
|
284
|
+
|
|
285
|
+ ConsentVisualStep visualStep2 = new ConsentVisualStep("visual_step_2");
|
|
286
|
+ visualStep2.setStepTitle(R.string.rsb_consent);
|
|
287
|
+ visualStep2.setSection(section2);
|
|
288
|
+ visualStep2.setNextButtonString("Next");
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+ // Consent Section 3 - Data Gathering
|
|
292
|
+ ConsentSection section3 = new ConsentSection(ConsentSection.Type.DataGathering);
|
|
293
|
+ section3.setTitle(getString(R.string.dataGatheringTitleText));
|
|
294
|
+ section3.setSummary(getString(R.string.dataGatheringConsentSectionSummaryText));
|
|
295
|
+ section3.setContent(getString(R.string.dataGatheringConsentSectionContentText));
|
|
296
|
+
|
|
297
|
+ ConsentVisualStep visualStep3 = new ConsentVisualStep("visual_step_3");
|
|
298
|
+ visualStep3.setStepTitle(R.string.rsb_consent);
|
|
299
|
+ visualStep3.setSection(section3);
|
|
300
|
+ visualStep3.setNextButtonString("Next");
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+ // Consent Section 4 - Privacy
|
|
304
|
+ ConsentSection section4 = new ConsentSection(ConsentSection.Type.Privacy);
|
|
305
|
+ section4.setTitle(getString(R.string.privacyTitleText));
|
|
306
|
+ section4.setSummary(getString(R.string.privacyConsentSectionSummaryText));
|
|
307
|
+ section4.setContent(getString(R.string.privacyConsentSectionContentText));
|
|
308
|
+
|
|
309
|
+ ConsentVisualStep visualStep4 = new ConsentVisualStep("visual_step_4");
|
|
310
|
+ visualStep4.setStepTitle(R.string.rsb_consent);
|
|
311
|
+ visualStep4.setSection(section4);
|
|
312
|
+ visualStep4.setNextButtonString("Next");
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+ // Consent Section 5 - Data Use
|
|
316
|
+ ConsentSection section5 = new ConsentSection(ConsentSection.Type.DataUse);
|
|
317
|
+ section5.setTitle(getString(R.string.dataUseTitleText));
|
|
318
|
+ section5.setSummary(getString(R.string.dataUseConsentSectionSummaryText));
|
|
319
|
+ section5.setContent(getString(R.string.dataUseConsentSectionContentText));
|
|
320
|
+
|
|
321
|
+ ConsentVisualStep visualStep5 = new ConsentVisualStep("visual_step_5");
|
|
322
|
+ visualStep5.setStepTitle(R.string.rsb_consent);
|
|
323
|
+ visualStep5.setSection(section5);
|
|
324
|
+ visualStep5.setNextButtonString("Next");
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+ // Create consent signature object and set what info is required
|
|
328
|
+ ConsentSignature signature = new ConsentSignature();
|
|
329
|
+ signature.setRequiresName(true);
|
|
330
|
+ signature.setRequiresSignatureImage(true);
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+ // Create our HTML to show the user and have them accept or decline.
|
|
334
|
+ String consentHtmlString =
|
|
335
|
+
|
|
336
|
+ "<br>" +
|
|
337
|
+
|
|
338
|
+ "<div style='padding: 10px;' class='header'>" +
|
|
339
|
+ "<h1 style='text-align: center; font-family:sans-serif-light;'>" + getString(R.string.rsb_consent_review_title) + "</h1>" +
|
|
340
|
+ "<p style='text-align: center'>" + getString(R.string.rsb_consent_review_instruction) + "</p>" +
|
|
341
|
+ "</div>" +
|
|
342
|
+
|
|
343
|
+ "<br>" +
|
|
344
|
+
|
|
345
|
+ "<div>" +
|
|
346
|
+ "<h2>" + getString(R.string.overviewTitleText) + "</h2>" +
|
|
347
|
+ "<p align='justify'>" + getString(R.string.consentOverview) + "</p>" +
|
|
348
|
+
|
|
349
|
+ "<h2>" + getString(R.string.timeCommitmentTitleText) + "</h2>" +
|
|
350
|
+ "<p align='justify'>" + getString(R.string.consentTimeCommitment) + "</p>" +
|
|
351
|
+
|
|
352
|
+ "<h2>" + getString(R.string.dataGatheringTitleText) + "</h2>" +
|
|
353
|
+ "<p align='justify'>" + getString(R.string.consentDataGathering) + "</p>" +
|
|
354
|
+
|
|
355
|
+ "<h2>" + getString(R.string.privacyTitleText) + "</h2>" +
|
|
356
|
+ "<p align='justify'>" + getString(R.string.consentPrivacy) + "</p>" +
|
|
357
|
+
|
|
358
|
+ "<h2>" + getString(R.string.dataUseTitleText) + "</h2>" +
|
|
359
|
+ "<p align='justify'>" + getString(R.string.consentDataUse) + "</p>" +
|
|
360
|
+
|
|
361
|
+ "<h2>" + getString(R.string.generalTitleText) + "</h2>" +
|
|
362
|
+ "<p align='justify'>" + getString(R.string.consentGeneral) + "</p>" +
|
|
363
|
+ "<p align='justify'>" + getString(R.string.consentConclusion) + "</p>" +
|
|
364
|
+ "</div>";
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+ // Create the consent document step and pass in our HTML
|
|
368
|
+ ConsentDocumentStep documentStep = new ConsentDocumentStep(CONSENT_DOC);
|
|
369
|
+ documentStep.setConsentHTML(consentHtmlString);
|
|
370
|
+ documentStep.setConfirmMessage(getString(R.string.rsb_consent_review_reason));
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+ // Create Consent form step, to get users first & last name
|
|
374
|
+// FormStep formStep = new FormStep(SIGNATURE_FORM_STEP,
|
|
375
|
+// "Form Title",
|
|
376
|
+// "Form step description");
|
|
377
|
+// formStep.setStepTitle(R.string.rsb_consent);
|
|
378
|
+//
|
|
379
|
+// TextAnswerFormat format = new TextAnswerFormat();
|
|
380
|
+// format.setIsMultipleLines(false);
|
|
381
|
+//
|
|
382
|
+// QuestionStep fullName = new QuestionStep(NAME, "Full name", format);
|
|
383
|
+// formStep.setFormSteps(Collections.singletonList(fullName));
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+ // Create the consent signature step (so that user can sign)
|
|
387
|
+ ConsentSignatureStep signatureStep = new ConsentSignatureStep(SIGNATURE);
|
|
388
|
+ signatureStep.setStepTitle(R.string.rsb_consent);
|
|
389
|
+ signatureStep.setTitle(getString(R.string.rsb_consent_signature_title));
|
|
390
|
+ signatureStep.setText(getString(R.string.rsb_consent_signature_instruction));
|
|
391
|
+ signatureStep.setSignatureDateFormat(signature.getSignatureDateFormatString());
|
|
392
|
+ signatureStep.setOptional(false);
|
|
393
|
+ signatureStep.setStepLayoutClass(ConsentSignatureStepLayout.class);
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+ // Create consent document
|
|
397
|
+ ConsentDocument document = new ConsentDocument();
|
|
398
|
+ document.setTitle(getString(R.string.appName) + " Consent");
|
|
399
|
+ document.setSignaturePageTitle(R.string.rsb_consent);
|
|
400
|
+ document.addSignature(signature);
|
|
401
|
+ document.getHtmlReviewContent();
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+ // Finally, create and present a task including these steps.
|
|
405
|
+ Task consentTask = new OrderedTask(CONSENT,
|
|
406
|
+ visualStep1,
|
|
407
|
+ visualStep2,
|
|
408
|
+ visualStep3,
|
|
409
|
+ visualStep4,
|
|
410
|
+ visualStep5,
|
|
411
|
+ documentStep,
|
|
412
|
+// formStep,
|
|
413
|
+ signatureStep);
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+ // Launch using the ViewTaskActivity and make sure to listen for the activity result
|
|
417
|
+ Intent intent = ViewTaskActivity.newIntent(this, consentTask);
|
|
418
|
+ startActivityForResult(intent, REQUEST_CONSENT);
|
|
419
|
+
|
|
420
|
+ }
|
|
421
|
+
|
|
422
|
+ // TODO: IO should be moved to a background task
|
|
423
|
+// private String createPdf(byte[] signatureBytes, String consentDate) {
|
|
424
|
+//
|
|
425
|
+// try {
|
|
426
|
+// Log.d(TAG,"Aqui");
|
|
427
|
+//
|
|
428
|
+// // Open document
|
|
429
|
+// Document document = new Document();
|
|
430
|
+// String filePath = Environment.getExternalStorageDirectory().getPath() + getString(R.string.consentFileName);
|
|
431
|
+// PdfWriter.getInstance(document, new FileOutputStream(filePath));
|
|
432
|
+// document.open();
|
|
433
|
+//
|
|
434
|
+// // Define fonts and offsets
|
|
435
|
+// Font titleFont = new Font(Font.FontFamily.HELVETICA, 20, Font.BOLD, BaseColor.BLACK);
|
|
436
|
+// Font subtitleFont = new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLACK);
|
|
437
|
+// Font signatureSectionFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLDITALIC, BaseColor.BLACK);
|
|
438
|
+// int signatureOffsetX = 145;
|
|
439
|
+// int signatureOffsetY = -100; // TODO: must set absolute offset to avoid bad signatures from cluttering up the PDF (originally -24)
|
|
440
|
+//
|
|
441
|
+//
|
|
442
|
+// // Create reusable newline component
|
|
443
|
+// Paragraph newLine = new Paragraph("\n");
|
|
444
|
+//
|
|
445
|
+//
|
|
446
|
+// // Consent Section 1 - Overview (Welcome)
|
|
447
|
+// Chunk titleChunk = new Chunk("Consent form:\n" + getString(R.string.appName), titleFont);
|
|
448
|
+// Paragraph title = new Paragraph(titleChunk);
|
|
449
|
+// title.setAlignment(Element.ALIGN_CENTER);
|
|
450
|
+// document.add(title);
|
|
451
|
+// document.add(newLine);
|
|
452
|
+//
|
|
453
|
+// String consentOverview = getString(R.string.consentOverview);
|
|
454
|
+// Paragraph paragraph1 = new Paragraph(consentOverview);
|
|
455
|
+// paragraph1.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
456
|
+// document.add(paragraph1);
|
|
457
|
+// document.add(newLine);
|
|
458
|
+//
|
|
459
|
+//
|
|
460
|
+// // Consent Section 2 - Time Commitment
|
|
461
|
+// Chunk subtitle1 = new Chunk(getString(R.string.timeCommitmentTitleText), subtitleFont);
|
|
462
|
+// Paragraph timeCommitmentTitle = new Paragraph(subtitle1);
|
|
463
|
+// document.add(timeCommitmentTitle);
|
|
464
|
+//
|
|
465
|
+// String consentTimeCommitment = getString(R.string.consentTimeCommitment);
|
|
466
|
+// Paragraph paragraph2 = new Paragraph(consentTimeCommitment);
|
|
467
|
+// paragraph2.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
468
|
+// document.add(paragraph2);
|
|
469
|
+// document.add(newLine);
|
|
470
|
+//
|
|
471
|
+//
|
|
472
|
+// // Consent Section 3 - Data Gathering
|
|
473
|
+// Chunk subtitle2 = new Chunk(getString(R.string.dataGatheringTitleText), subtitleFont);
|
|
474
|
+// Paragraph dataGatheringTitle = new Paragraph(subtitle2);
|
|
475
|
+// document.add(dataGatheringTitle);
|
|
476
|
+//
|
|
477
|
+// String consentDataGathering = getString(R.string.consentDataGathering);
|
|
478
|
+// Paragraph paragraph3 = new Paragraph(consentDataGathering);
|
|
479
|
+// paragraph3.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
480
|
+// document.add(paragraph3);
|
|
481
|
+// document.add(newLine);
|
|
482
|
+//
|
|
483
|
+//
|
|
484
|
+// // Consent Section 4 - Privacy
|
|
485
|
+// Chunk subtitle3 = new Chunk(getString(R.string.privacyTitleText), subtitleFont);
|
|
486
|
+// Paragraph privacyTitle = new Paragraph(subtitle3);
|
|
487
|
+// document.add(privacyTitle);
|
|
488
|
+//
|
|
489
|
+// String consentPrivacy = getString(R.string.consentPrivacy);
|
|
490
|
+// Paragraph paragraph4 = new Paragraph(consentPrivacy);
|
|
491
|
+// paragraph4.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
492
|
+// document.add(paragraph4);
|
|
493
|
+// document.add(newLine);
|
|
494
|
+//
|
|
495
|
+//
|
|
496
|
+// // Consent Section 5 - Data Use
|
|
497
|
+// Chunk subtitle4 = new Chunk(getString(R.string.dataUseTitleText), subtitleFont);
|
|
498
|
+// Paragraph useOfDataTitle = new Paragraph(subtitle4);
|
|
499
|
+// document.add(useOfDataTitle);
|
|
500
|
+//
|
|
501
|
+// String consentUseOfData = getString(R.string.consentDataUse);
|
|
502
|
+// Paragraph paragraph5 = new Paragraph(consentUseOfData);
|
|
503
|
+// paragraph5.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
504
|
+// document.add(paragraph5);
|
|
505
|
+// document.add(newLine);
|
|
506
|
+//
|
|
507
|
+//
|
|
508
|
+// // Consent Section 6 - General & Conclusion
|
|
509
|
+// Chunk subtitle5 = new Chunk(getString(R.string.generalTitleText), subtitleFont);
|
|
510
|
+// Paragraph generalTitle = new Paragraph(subtitle5);
|
|
511
|
+// document.add(generalTitle);
|
|
512
|
+//
|
|
513
|
+// String consentGeneral = getString(R.string.consentGeneral);
|
|
514
|
+// Paragraph paragraph6 = new Paragraph(consentGeneral);
|
|
515
|
+// paragraph6.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
516
|
+// document.add(paragraph6);
|
|
517
|
+// document.add(newLine);
|
|
518
|
+//
|
|
519
|
+// String consentConclusion = getString(R.string.consentConclusion);
|
|
520
|
+// Paragraph paragraph7 = new Paragraph(consentConclusion);
|
|
521
|
+// paragraph7.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
522
|
+// document.add(paragraph7);
|
|
523
|
+// document.add(newLine);
|
|
524
|
+//
|
|
525
|
+//
|
|
526
|
+// // Signature Section
|
|
527
|
+// Chunk participantSignature = new Chunk("Participant's signature:", signatureSectionFont);
|
|
528
|
+// Paragraph paragraph8 = new Paragraph(participantSignature);
|
|
529
|
+// document.add(paragraph8);
|
|
530
|
+//
|
|
531
|
+// Image consentSignatureImage = Image.getInstance(signatureBytes);
|
|
532
|
+// Chunk signatureImage = new Chunk(consentSignatureImage, signatureOffsetX, signatureOffsetY);
|
|
533
|
+// Paragraph paragraph11 = new Paragraph(signatureImage);
|
|
534
|
+// document.add(paragraph11);
|
|
535
|
+//
|
|
536
|
+// Chunk signatureDate = new Chunk("Signed on: " + consentDate, signatureSectionFont);
|
|
537
|
+// Paragraph paragraph9 = new Paragraph(signatureDate);
|
|
538
|
+// document.add(paragraph9);
|
|
539
|
+//
|
|
540
|
+//
|
|
541
|
+// // Finish and store document
|
|
542
|
+// document.close();
|
|
543
|
+//
|
|
544
|
+//
|
|
545
|
+// // Read document back into consentBytes array, and encode it to base64
|
|
546
|
+// File file = new File(filePath);
|
|
547
|
+// int size = (int) file.length();
|
|
548
|
+// byte[] consentBytes = new byte[size];
|
|
549
|
+//
|
|
550
|
+// BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
|
|
551
|
+// int bytesRead = buffer.read(consentBytes, 0, consentBytes.length);
|
|
552
|
+// Log.d(TAG, "Read a total of " + bytesRead + " bytes, which is supposed to be " + size + " bytes!");
|
|
553
|
+// buffer.close();
|
|
554
|
+//
|
|
555
|
+// return Base64.encodeToString(consentBytes, Base64.DEFAULT);
|
|
556
|
+//
|
|
557
|
+// } catch (FileNotFoundException e) {
|
|
558
|
+// Log.e(TAG, "Couldn't find generated consent form!");
|
|
559
|
+// e.printStackTrace();
|
|
560
|
+// return null;
|
|
561
|
+// } catch (IOException e) {
|
|
562
|
+// Log.e(TAG, "Couldn't read back generated consent form!");
|
|
563
|
+// e.printStackTrace();
|
|
564
|
+// return null;
|
|
565
|
+// } catch (BadElementException e) {
|
|
566
|
+// Log.e(TAG, "Couldn't create a consent form element!");
|
|
567
|
+// e.printStackTrace();
|
|
568
|
+// return null;
|
|
569
|
+// } catch (DocumentException e) {
|
|
570
|
+// Log.e(TAG, "Something went wrong while fiddling with the consent form creation!");
|
|
571
|
+// e.printStackTrace();
|
|
572
|
+// return null;
|
|
573
|
+// } catch (Exception e) {
|
|
574
|
+// Log.wtf(TAG, "Call to launchConsent() went wrong!");
|
|
575
|
+// e.printStackTrace();
|
|
576
|
+// return null;
|
|
577
|
+// }
|
|
578
|
+//
|
|
579
|
+// }
|
|
580
|
+ private String createPdf(byte[] signatureBytes, String consentDate) {
|
|
581
|
+ File file = null;
|
|
582
|
+ FileOutputStream outputStream = null;
|
|
583
|
+ BufferedInputStream buffer = null;
|
|
584
|
+
|
|
585
|
+ try {
|
|
586
|
+ Log.d(TAG, "Starting PDF creation");
|
|
587
|
+
|
|
588
|
+ // Create file in app-specific storage
|
|
589
|
+ file = new File(getFilesDir(), getString(R.string.consentFileName));
|
|
590
|
+ outputStream = new FileOutputStream(file);
|
|
591
|
+
|
|
592
|
+ // Open document
|
|
593
|
+ Document document = new Document();
|
|
594
|
+ PdfWriter.getInstance(document, outputStream);
|
|
595
|
+ document.open();
|
|
596
|
+
|
|
597
|
+ // Define fonts and offsets
|
|
598
|
+ Font titleFont = new Font(Font.FontFamily.HELVETICA, 20, Font.BOLD, BaseColor.BLACK);
|
|
599
|
+ Font subtitleFont = new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLACK);
|
|
600
|
+ Font signatureSectionFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLDITALIC, BaseColor.BLACK);
|
|
601
|
+ int signatureOffsetX = 145;
|
|
602
|
+ int signatureOffsetY = -100;
|
|
603
|
+
|
|
604
|
+ // Create reusable newline component
|
|
605
|
+ Paragraph newLine = new Paragraph("\n");
|
|
606
|
+
|
|
607
|
+ // Consent Section 1 - Overview (Welcome)
|
|
608
|
+ Chunk titleChunk = new Chunk("Consent form:\n" + getString(R.string.appName), titleFont);
|
|
609
|
+ Paragraph title = new Paragraph(titleChunk);
|
|
610
|
+ title.setAlignment(Element.ALIGN_CENTER);
|
|
611
|
+ document.add(title);
|
|
612
|
+ document.add(newLine);
|
|
613
|
+
|
|
614
|
+ String consentOverview = getString(R.string.consentOverview);
|
|
615
|
+ Paragraph paragraph1 = new Paragraph(consentOverview);
|
|
616
|
+ paragraph1.setAlignment(Element.ALIGN_JUSTIFIED);
|
|
617
|
+ document.add(paragraph1);
|
|
618
|
+ document.add(newLine);
|
|
619
|
+
|
|
620
|
+ // ... (rest of the content sections remain the same) ...
|
|
621
|
+
|
|
622
|
+ // Signature Section
|
|
623
|
+ Chunk participantSignature = new Chunk("Participant's signature:", signatureSectionFont);
|
|
624
|
+ Paragraph paragraph8 = new Paragraph(participantSignature);
|
|
625
|
+ document.add(paragraph8);
|
|
626
|
+
|
|
627
|
+ Image consentSignatureImage = Image.getInstance(signatureBytes);
|
|
628
|
+ Chunk signatureImage = new Chunk(consentSignatureImage, signatureOffsetX, signatureOffsetY);
|
|
629
|
+ Paragraph paragraph11 = new Paragraph(signatureImage);
|
|
630
|
+ document.add(paragraph11);
|
|
631
|
+
|
|
632
|
+ Chunk signatureDate = new Chunk("Signed on: " + consentDate, signatureSectionFont);
|
|
633
|
+ Paragraph paragraph9 = new Paragraph(signatureDate);
|
|
634
|
+ document.add(paragraph9);
|
|
635
|
+
|
|
636
|
+ // Finish and store document
|
|
637
|
+ document.close();
|
|
638
|
+
|
|
639
|
+ // Read document back into consentBytes array, and encode it to base64
|
|
640
|
+ int size = (int) file.length();
|
|
641
|
+ byte[] consentBytes = new byte[size];
|
|
642
|
+
|
|
643
|
+ buffer = new BufferedInputStream(new FileInputStream(file));
|
|
644
|
+ int bytesRead = buffer.read(consentBytes, 0, consentBytes.length);
|
|
645
|
+ Log.d(TAG, "Read a total of " + bytesRead + " bytes, which is supposed to be " + size + " bytes!");
|
|
646
|
+
|
|
647
|
+ return Base64.encodeToString(consentBytes, Base64.DEFAULT);
|
|
648
|
+
|
|
649
|
+ } catch (Exception e) {
|
|
650
|
+ Log.e(TAG, "Error creating or reading PDF", e);
|
|
651
|
+ return null;
|
|
652
|
+ } finally {
|
|
653
|
+ try {
|
|
654
|
+ if (buffer != null) buffer.close();
|
|
655
|
+ if (outputStream != null) outputStream.close();
|
|
656
|
+ } catch (IOException e) {
|
|
657
|
+ Log.e(TAG, "Error closing streams", e);
|
|
658
|
+ }
|
|
659
|
+ if (file != null && file.exists() && !file.delete()) {
|
|
660
|
+ Log.w(TAG, "Failed to delete temporary PDF file");
|
|
661
|
+ }
|
|
662
|
+ }
|
|
663
|
+ }
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+ private void restoreConsentForm(String response) {
|
|
667
|
+ FileOutputStream consentForm = null;
|
|
668
|
+ try {
|
|
669
|
+ byte[] consentBytes = Base64.decode(response, Base64.NO_WRAP);
|
|
670
|
+ File file = new File(getFilesDir(), getString(R.string.consentFileName));
|
|
671
|
+ consentForm = new FileOutputStream(file);
|
|
672
|
+ consentForm.write(consentBytes);
|
|
673
|
+ Log.d(TAG, "Consent form restored successfully");
|
|
674
|
+ } catch (IllegalArgumentException e) {
|
|
675
|
+ Log.e(TAG, "Invalid Base64 string", e);
|
|
676
|
+ } catch (IOException e) {
|
|
677
|
+ Log.e(TAG, "Error writing consent form", e);
|
|
678
|
+ } finally {
|
|
679
|
+ if (consentForm != null) {
|
|
680
|
+ try {
|
|
681
|
+ consentForm.close();
|
|
682
|
+ } catch (IOException e) {
|
|
683
|
+ Log.e(TAG, "Error closing FileOutputStream", e);
|
|
684
|
+ }
|
|
685
|
+ }
|
|
686
|
+ }
|
|
687
|
+ }
|
|
688
|
+ private void openPrivacyPolicy() {
|
|
689
|
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://tania.uprrp.edu/privacy_policy.html"));
|
|
690
|
+ startActivity(browserIntent);
|
|
691
|
+ }
|
|
692
|
+}
|