|
@@ -26,8 +26,6 @@ import com.itextpdf.text.Image;
|
26
|
26
|
import com.itextpdf.text.Paragraph;
|
27
|
27
|
import com.itextpdf.text.pdf.PdfWriter;
|
28
|
28
|
|
29
|
|
-import org.json.JSONException;
|
30
|
|
-import org.json.JSONObject;
|
31
|
29
|
import org.researchstack.backbone.StorageAccess;
|
32
|
30
|
import org.researchstack.backbone.model.ConsentDocument;
|
33
|
31
|
import org.researchstack.backbone.model.ConsentSection;
|
|
@@ -50,7 +48,8 @@ import java.io.IOException;
|
50
|
48
|
|
51
|
49
|
import uprrp.tania.GlobalValues;
|
52
|
50
|
import uprrp.tania.R;
|
53
|
|
-import uprrp.tania.SendConsentForm;
|
|
51
|
+import uprrp.tania.networking.SendConsentForm;
|
|
52
|
+import uprrp.tania.utils.URLEventListener;
|
54
|
53
|
|
55
|
54
|
public class GettingStartedActivity extends AppCompatActivity {
|
56
|
55
|
|
|
@@ -123,62 +122,64 @@ public class GettingStartedActivity extends AppCompatActivity {
|
123
|
122
|
}
|
124
|
123
|
|
125
|
124
|
|
126
|
|
- // TODO: decide which parts in this function to move to a background task
|
127
|
|
- // TODO: define a model for the JSON and use Gson to encode it
|
128
|
125
|
private void processConsentResult(TaskResult result) {
|
129
|
126
|
|
130
|
127
|
boolean consented = (boolean) result.getStepResult(CONSENT_DOC).getResult();
|
131
|
128
|
StorageAccess.getInstance().getAppDatabase().saveTaskResult(result);
|
132
|
129
|
|
133
|
130
|
if(consented) {
|
134
|
|
- try {
|
135
|
|
-
|
136
|
|
- // Get signature in base64
|
137
|
|
- TaskResult taskResult = StorageAccess.getInstance()
|
138
|
|
- .getAppDatabase()
|
139
|
|
- .loadLatestTaskResult(CONSENT);
|
140
|
|
- String signatureBase64 = (String) taskResult.getStepResult(SIGNATURE)
|
141
|
|
- .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE);
|
142
|
|
-
|
143
|
|
- // Get signature date
|
144
|
|
- String signatureDate = (String) result.getStepResult(SIGNATURE)
|
145
|
|
- .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE_DATE);
|
146
|
|
-
|
147
|
|
- // Use signature to get encoded consent form
|
148
|
|
- byte[] signatureBytes = Base64.decode(signatureBase64, Base64.DEFAULT);
|
149
|
|
- String consentFormBase64 = createPdf(signatureBytes, signatureDate);
|
150
|
|
-
|
151
|
|
- // Send JSON with consent form, signature date, and token
|
152
|
|
- JSONObject signatureJSON = new JSONObject();
|
153
|
|
- signatureJSON.put("signaturePDFBase64", consentFormBase64);
|
154
|
|
- signatureJSON.put("signatureDate", signatureDate);
|
155
|
|
- signatureJSON.put("token", GlobalValues.getInstance().getDeviceToken()); // FirebaseInstanceId.getInstance().getToken()
|
156
|
|
- Log.d("CONSENT JSON", signatureJSON.toString()); // log
|
157
|
|
- SendConsentForm sendConsentForm = new SendConsentForm();
|
158
|
|
- sendConsentForm.execute(signatureJSON.toString());
|
159
|
|
-
|
160
|
|
- // Save consent signature and date separately on preferences
|
161
|
|
- SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
162
|
|
- SharedPreferences.Editor editor = prefs.edit();
|
163
|
|
- editor.putString("consentSignature", signatureBase64);
|
164
|
|
- editor.putString("consentDate", signatureDate);
|
165
|
|
- editor.apply();
|
166
|
|
-
|
167
|
|
- // Prompt user to register as student
|
168
|
|
- Intent registration = new Intent(this, RegisterActivity.class);
|
169
|
|
- startActivity(registration);
|
170
|
|
-
|
171
|
|
- } catch (JSONException e) {
|
172
|
|
- Log.e(TAG, "Couldn't prepare consent form JSON!");
|
173
|
|
- e.printStackTrace();
|
174
|
|
- } catch (Exception e) {
|
175
|
|
- Log.e(TAG, "Error on processConsentResult(): " + e.getMessage());
|
176
|
|
- e.printStackTrace();
|
177
|
|
- }
|
|
131
|
+
|
|
132
|
+ // Get signature in base64
|
|
133
|
+ TaskResult taskResult = StorageAccess.getInstance()
|
|
134
|
+ .getAppDatabase()
|
|
135
|
+ .loadLatestTaskResult(CONSENT);
|
|
136
|
+ String signatureBase64 = (String) taskResult.getStepResult(SIGNATURE)
|
|
137
|
+ .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE);
|
|
138
|
+
|
|
139
|
+ // Get signature date
|
|
140
|
+ String signatureDate = (String) result.getStepResult(SIGNATURE)
|
|
141
|
+ .getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE_DATE);
|
|
142
|
+
|
|
143
|
+ // Use signature to get encoded consent form
|
|
144
|
+ byte[] signatureBytes = Base64.decode(signatureBase64, Base64.DEFAULT);
|
|
145
|
+ String consentFormBase64 = createPdf(signatureBytes, signatureDate);
|
|
146
|
+
|
|
147
|
+ // Send JSON with consent form, signature date, and token
|
|
148
|
+ String deviceToken = GlobalValues.getInstance().getDeviceToken();
|
|
149
|
+ this.sendConsentForm(deviceToken, consentFormBase64, signatureDate);
|
|
150
|
+
|
|
151
|
+ // Save consent signature and date separately on preferences
|
|
152
|
+ SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
|
153
|
+ SharedPreferences.Editor editor = prefs.edit();
|
|
154
|
+ editor.putString("consentSignature", signatureBase64);
|
|
155
|
+ editor.putString("consentDate", signatureDate);
|
|
156
|
+ editor.apply();
|
|
157
|
+
|
|
158
|
+ // Prompt user to register as student
|
|
159
|
+ Intent registration = new Intent(this, RegisterActivity.class);
|
|
160
|
+ startActivity(registration);
|
|
161
|
+
|
178
|
162
|
}
|
179
|
163
|
|
180
|
164
|
}
|
181
|
165
|
|
|
166
|
+ private void sendConsentForm(String deviceToken, String consentFormBase64, String signatureDate) {
|
|
167
|
+ SendConsentForm sendConsentFormTask = new SendConsentForm(new URLEventListener() {
|
|
168
|
+ @Override
|
|
169
|
+ public void onSuccess() {
|
|
170
|
+ Toast.makeText(getApplicationContext(), "Sent consent form to server!", Toast.LENGTH_LONG).show();
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ @Override
|
|
174
|
+ public void onFailure(Exception e) {
|
|
175
|
+ Toast.makeText(getApplicationContext(), "Couldn't send consent form to server!", Toast.LENGTH_LONG).show();
|
|
176
|
+ Log.e(TAG, "Couldn't send consent form to server!");
|
|
177
|
+ e.printStackTrace();
|
|
178
|
+ }
|
|
179
|
+ });
|
|
180
|
+
|
|
181
|
+ sendConsentFormTask.execute(deviceToken, consentFormBase64, signatureDate);
|
|
182
|
+ }
|
182
|
183
|
|
183
|
184
|
private void launchConsent() {
|
184
|
185
|
|