-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMainActivity.java
More file actions
386 lines (324 loc) · 14.5 KB
/
MainActivity.java
File metadata and controls
386 lines (324 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package org.researchstack.backboneapp;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.researchstack.backbone.StorageAccess;
import org.researchstack.backbone.answerformat.AnswerFormat;
import org.researchstack.backbone.answerformat.BooleanAnswerFormat;
import org.researchstack.backbone.answerformat.ChoiceAnswerFormat;
import org.researchstack.backbone.answerformat.DateAnswerFormat;
import org.researchstack.backbone.answerformat.TextAnswerFormat;
import org.researchstack.backbone.model.Choice;
import org.researchstack.backbone.model.ConsentDocument;
import org.researchstack.backbone.model.ConsentSection;
import org.researchstack.backbone.model.ConsentSignature;
import org.researchstack.backbone.result.StepResult;
import org.researchstack.backbone.result.TaskResult;
import org.researchstack.backbone.step.ConsentDocumentStep;
import org.researchstack.backbone.step.ConsentSignatureStep;
import org.researchstack.backbone.step.ConsentVisualStep;
import org.researchstack.backbone.step.FormStep;
import org.researchstack.backbone.step.InstructionStep;
import org.researchstack.backbone.step.QuestionStep;
import org.researchstack.backbone.task.OrderedTask;
import org.researchstack.backbone.task.Task;
import org.researchstack.backbone.ui.PinCodeActivity;
import org.researchstack.backbone.ui.ViewTaskActivity;
import org.researchstack.backbone.ui.step.layout.ConsentSignatureStepLayout;
import java.util.Collections;
public class MainActivity extends PinCodeActivity
{
// Activity Request Codes
private static final int REQUEST_CONSENT = 0;
private static final int REQUEST_SURVEY = 1;
// Task/Step Identifiers
public static final String INSTRUCTION = "identifier";
public static final String NUTRITION = "nutrition";
public static final String SIGNATURE = "signature";
public static final String VISUAL_CONSENT_IDENTIFIER = "visual_consent_identifier";
public static final String CONSENT_DOC = "consent_doc";
public static final String SIGNATURE_FORM_STEP = "form_step";
public static final String NAME = "name";
public static final String CONSENT = "consent";
public static final String MULTI_STEP = "multi_step";
public static final String DATE = "date";
public static final String SAMPLE_SURVEY = "sample_survey";
public static final String SKIPPED_RESULT_VALUE = "[SKIPPED_RESULT]";
// Views
private AppCompatButton consentButton;
private AppCompatButton surveyButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
consentButton = (AppCompatButton) findViewById(R.id.consent_button);
consentButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
launchConsent();
}
});
surveyButton = (AppCompatButton) findViewById(R.id.survey_button);
surveyButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
launchSurvey();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.menu_clear)
{
clearData();
Toast.makeText(this, R.string.menu_data_cleared, Toast.LENGTH_SHORT).show();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
private void clearData()
{
AppPrefs appPrefs = AppPrefs.getInstance(this);
appPrefs.setHasSurveyed(false);
appPrefs.setHasConsented(false);
initViews();
}
@Override
public void onDataReady()
{
super.onDataReady();
initViews();
}
private void initViews()
{
AppPrefs prefs = AppPrefs.getInstance(this);
View lblConsentedDate = findViewById(R.id.consented_date_lbl);
TextView consentedDate = (TextView)findViewById(R.id.consented_date);
ImageView consentedSig = (ImageView) findViewById(R.id.consented_signature);
if(prefs.hasConsented())
{
consentButton.setVisibility(View.GONE);
surveyButton.setEnabled(true);
consentedSig.setVisibility(View.VISIBLE);
consentedDate.setVisibility(View.VISIBLE);
lblConsentedDate.setVisibility(View.VISIBLE);
printConsentInfo(consentedDate, consentedSig);
}
else
{
consentButton.setVisibility(View.VISIBLE);
surveyButton.setEnabled(false);
consentedSig.setVisibility(View.INVISIBLE);
consentedSig.setImageBitmap(null);
consentedDate.setVisibility(View.INVISIBLE);
lblConsentedDate.setVisibility(View.INVISIBLE);
}
TextView surveyAnswer = (TextView) findViewById(R.id.survey_results);
if(prefs.hasSurveyed())
{
surveyAnswer.setVisibility(View.VISIBLE);
printSurveyInfo(surveyAnswer);
}
else
{
surveyAnswer.setVisibility(View.GONE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CONSENT && resultCode == RESULT_OK)
{
processConsentResult((TaskResult) data.getSerializableExtra(ViewTaskActivity.EXTRA_TASK_RESULT));
}
else if(requestCode == REQUEST_SURVEY && resultCode == RESULT_OK)
{
processSurveyResult((TaskResult) data.getSerializableExtra(ViewTaskActivity.EXTRA_TASK_RESULT));
}
}
// Consent stuff
private void launchConsent()
{
ConsentDocument document = new ConsentDocument();
document.setTitle("Demo Consent");
document.setSignaturePageTitle(R.string.rsb_consent);
// Create consent visual sections
ConsentSection section1 = new ConsentSection(ConsentSection.Type.DataGathering);
section1.setTitle("The title of the section goes here ...");
section1.setSummary("The summary about the section goes here ...");
section1.setContent("The content to show in learn more ...");
// ...add more sections as needed, then create a visual consent step
ConsentVisualStep visualStep = new ConsentVisualStep(VISUAL_CONSENT_IDENTIFIER);
visualStep.setStepTitle(R.string.rsb_consent);
visualStep.setSection(section1);
visualStep.setNextButtonString(getString(R.string.rsb_next));
// Create consent signature object and set what info is required
ConsentSignature signature = new ConsentSignature();
signature.setRequiresName(true);
signature.setRequiresSignatureImage(true);
// Create our HTML to show the user and have them accept or decline.
StringBuilder docBuilder = new StringBuilder(
"</br><div style=\"padding: 10px 10px 10px 10px;\" class='header'>");
String title = getString(R.string.rsb_consent_review_title);
docBuilder.append(String.format(
"<h1 style=\"text-align: center; font-family:sans-serif-light;\">%1$s</h1>",
title));
String detail = getString(R.string.rsb_consent_review_instruction);
docBuilder.append(String.format("<p style=\"text-align: center\">%1$s</p>", detail));
docBuilder.append("</div></br>");
docBuilder.append("<div><h2> HTML Consent Doc goes here </h2></div>");
// Create the Consent doc step, pass in our HTML doc
ConsentDocumentStep documentStep = new ConsentDocumentStep(CONSENT_DOC);
documentStep.setConsentHTML(docBuilder.toString());
documentStep.setConfirmMessage(getString(R.string.rsb_consent_review_reason));
// Create Consent form step, to get users first & last name
FormStep formStep = new FormStep(SIGNATURE_FORM_STEP,
"Form Title",
"Form step description");
formStep.setStepTitle(R.string.rsb_consent);
TextAnswerFormat format = new TextAnswerFormat();
format.setIsMultipleLines(false);
QuestionStep fullName = new QuestionStep(NAME, "Full name", format);
formStep.setFormSteps(Collections.singletonList(fullName));
// Create Consent signature step, user can sign their name
ConsentSignatureStep signatureStep = new ConsentSignatureStep(SIGNATURE);
signatureStep.setStepTitle(R.string.rsb_consent);
signatureStep.setTitle(getString(R.string.rsb_consent_signature_title));
signatureStep.setText(getString(R.string.rsb_consent_signature_instruction));
signatureStep.setSignatureDateFormat(signature.getSignatureDateFormatString());
signatureStep.setOptional(false);
signatureStep.setStepLayoutClass(ConsentSignatureStepLayout.class);
// Finally, create and present a task including these steps.
Task consentTask = new OrderedTask(CONSENT,
visualStep,
documentStep,
formStep,
signatureStep);
// Launch using hte ViewTaskActivity and make sure to listen for the activity result
Intent intent = ViewTaskActivity.newIntent(this, consentTask);
startActivityForResult(intent, REQUEST_CONSENT);
}
private void processConsentResult(TaskResult result)
{
boolean consented = (boolean) result.getStepResult(CONSENT_DOC).getResult();
if(consented)
{
StorageAccess.getInstance().getAppDatabase().saveTaskResult(result);
AppPrefs prefs = AppPrefs.getInstance(this);
prefs.setHasConsented(true);
initViews();
}
}
private void printConsentInfo(TextView consentedDate, ImageView consentedSig)
{
TaskResult result = StorageAccess.getInstance()
.getAppDatabase()
.loadLatestTaskResult(CONSENT);
String signatureBase64 = (String) result.getStepResult(SIGNATURE)
.getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE);
String signatureDate = (String) result.getStepResult(SIGNATURE)
.getResultForIdentifier(ConsentSignatureStepLayout.KEY_SIGNATURE_DATE);
consentedDate.setText(signatureDate);
byte[] signatureBytes = Base64.decode(signatureBase64, Base64.DEFAULT);
consentedSig.setImageBitmap(BitmapFactory.decodeByteArray(
signatureBytes,
0,
signatureBytes.length));
}
// Survey Stuff
private void launchSurvey()
{
InstructionStep instructionStep = new InstructionStep(INSTRUCTION,
"Selection Survey",
"This survey can help us understand your eligibility for the fitness study");
instructionStep.setStepTitle(R.string.survey);
TextAnswerFormat format = new TextAnswerFormat();
QuestionStep ageStep = new QuestionStep(NAME, "What is your name?", format);
ageStep.setStepTitle(R.string.survey);
DateAnswerFormat dateFormat = new DateAnswerFormat(AnswerFormat.DateAnswerStyle.Date);
QuestionStep dateStep = new QuestionStep(DATE, "Enter a date", dateFormat);
dateStep.setStepTitle(R.string.survey);
// Create a Boolean step to include in the task.
QuestionStep booleanStep = new QuestionStep(NUTRITION);
booleanStep.setStepTitle(R.string.survey);
booleanStep.setTitle("Do you take nutritional supplements?");
booleanStep.setAnswerFormat(new BooleanAnswerFormat(getString(R.string.rsb_yes),
getString(R.string.rsb_no)));
booleanStep.setOptional(false);
QuestionStep multiStep = new QuestionStep(MULTI_STEP);
multiStep.setStepTitle(R.string.survey);
AnswerFormat multiFormat = new ChoiceAnswerFormat(AnswerFormat.ChoiceAnswerStyle.MultipleChoice,
new Choice<>("Zero", 0),
new Choice<>("One", 1),
new Choice<>("Two", 2));
multiStep.setTitle("Select multiple");
multiStep.setAnswerFormat(multiFormat);
multiStep.setOptional(false);
// Create a task wrapping the steps.
OrderedTask task = new OrderedTask(SAMPLE_SURVEY, instructionStep, ageStep, dateStep,
// formStep,
booleanStep, multiStep);
// Create an activity using the task and set a delegate.
Intent intent = ViewTaskActivity.newIntent(this, task);
startActivityForResult(intent, REQUEST_SURVEY);
}
private void processSurveyResult(TaskResult result)
{
StorageAccess.getInstance().getAppDatabase().saveTaskResult(result);
AppPrefs prefs = AppPrefs.getInstance(this);
prefs.setHasSurveyed(true);
initViews();
}
private void printSurveyInfo(TextView surveyAnswer)
{
TaskResult taskResult = StorageAccess.getInstance()
.getAppDatabase()
.loadLatestTaskResult(SAMPLE_SURVEY);
String results = "";
String currentResult;
for(String id : taskResult.getResults().keySet())
{
StepResult stepResult = taskResult.getStepResult(id);
if (stepResult.getResult() == null)
{
currentResult = SKIPPED_RESULT_VALUE;
}
else
{
currentResult = stepResult.getResult().toString();
}
results += id + ": " + currentResult + "\n";
}
surveyAnswer.setText(results);
}
}