Skip to content

Commit

Permalink
Merge pull request #171 from StAResComp/consent-data-submission
Browse files Browse the repository at this point in the history
Submit consent data to server
  • Loading branch information
pgmccann authored Jul 3, 2018
2 parents 7cc87bc + 7bf14f5 commit 90cfb55
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private boolean hasConsented() {
&& !prefs.getString(getString(R.string.pref_vessel_pln_key), "").isEmpty()
&& !prefs.getString(getString(R.string.pref_vessel_name_key), "").isEmpty()
&& !prefs.getString(getString(R.string.pref_owner_master_name_key), "").isEmpty()
&& prefs.getBoolean(getString(R.string.consent_confirmed_key), false)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

import uk.ac.masts.sifids.CatchApplication;
import uk.ac.masts.sifids.R;
import uk.ac.masts.sifids.singletons.RequestQueueSingleton;

/**
* A {@link PreferenceActivity} that presents a set of application settings. On
Expand Down Expand Up @@ -94,11 +109,9 @@ private static void bindPreferenceSummaryToValue(Preference preference) {
Object value;
if (preference instanceof MultiSelectListPreference) {
value = prefs.getStringSet(preference.getKey(), new HashSet<String>());
}
else if (preference instanceof SwitchPreference) {
} else if (preference instanceof SwitchPreference) {
value = prefs.getBoolean(preference.getKey(), false);
}
else {
} else {
value = prefs.getString(preference.getKey(), "");
}
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, value);
Expand All @@ -107,8 +120,8 @@ else if (preference instanceof SwitchPreference) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//((CatchApplication) this.getApplication()).redirectIfNecessary();
setupActionBar();

}

/**
Expand Down Expand Up @@ -337,6 +350,9 @@ public void onCreate(Bundle savedInstanceState) {
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class ConsentDetailsPreferenceFragment extends BasePreferenceFragment {

private Button submitButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -364,6 +380,7 @@ public void onCreate(Bundle savedInstanceState) {
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_vessel_pln_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_vessel_name_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_owner_master_name_key)));

}
}

Expand Down Expand Up @@ -406,4 +423,80 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

public void submitConsentDetails(View view) {
Log.e("CONSENT", "Submitting...");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
getBaseContext().getApplicationContext());
final Map<String, ?> prefsMap = prefs.getAll();
String[] consentPrefKeys = {
getString(R.string.consent_read_understand_key),
getString(R.string.consent_questions_opportunity_key),
getString(R.string.consent_questions_answered_key),
getString(R.string.consent_can_withdraw_key),
getString(R.string.consent_confidential_key),
getString(R.string.consent_data_archiving_key),
getString(R.string.consent_risks_key),
getString(R.string.consent_take_part_key),
getString(R.string.consent_photography_capture_key),
getString(R.string.consent_photography_publication_key),
getString(R.string.consent_photography_future_studies_key),
getString(R.string.consent_name_key),
getString(R.string.consent_email_key),
getString(R.string.consent_phone_key),
getString(R.string.consent_fish_1_key),
getString(R.string.consent_name_key),
getString(R.string.pref_vessel_pln_key),
getString(R.string.pref_vessel_name_key),
getString(R.string.pref_owner_master_name_key),
};
JSONObject consentJson = new JSONObject();
boolean goodToGo = true;
for (String prefKey : consentPrefKeys) {
Object pref = prefsMap.get(prefKey);
if (pref == null
|| (pref instanceof Boolean && !((boolean) pref))
|| (pref instanceof String && ((String) pref).isEmpty())) {
goodToGo = false;
break;
}
try {
consentJson.put(prefKey, pref.toString());
} catch (JSONException jse) {
goodToGo = false;
break;
}
}
if (goodToGo) {
final String url = getBaseContext().getString(R.string.post_request_url);
final SharedPreferences.Editor editor = prefs.edit();
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST, url, consentJson,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
editor.putBoolean(getString(R.string.consent_confirmed_key), true);
editor.apply();
Toast.makeText(getBaseContext(), getString(R.string.participant_consent_thank_you),
Toast.LENGTH_LONG).show();
Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
startActivity(intent);
SettingsActivity.this.finish();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
editor.putBoolean(getString(R.string.consent_confirmed_key), false);
editor.apply();
Toast.makeText(getBaseContext(), getString(R.string.participant_consent_error),
Toast.LENGTH_LONG).show();
}
}
);
RequestQueueSingleton.getInstance(getBaseContext()).addToRequestQueue(request);

}
}

}
9 changes: 9 additions & 0 deletions catch/src/main/res/layout/prefs_consent_fish_1_period.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@
android:layout_height="wrap_content"
android:text="@string/participant_consent_form_fish_1_period" />

<Button
android:id="@+id/btn_consent_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:onClick="submitConsentDetails"
android:text="@string/participant_consent_submit_button_text"/>

</LinearLayout>
4 changes: 4 additions & 0 deletions catch/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@
<xliff:g id="preference_key">consent_fish_1</xliff:g>
</string>

<string name="consent_confirmed_key">
<xliff:g id="preference_key">consent_confirmed</xliff:g>
</string>

</resources>
3 changes: 3 additions & 0 deletions catch/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<string name="participant_consent_form_fish_1_header">FISH1/ logbook Data Access Consent</string>
<string name="participant_consent_form_fish_1">I, the fishing license holder named above, agree for the researchers at the University of St Andrews to have access to the data submitted via the FISH1 form through the mobile phone application developed by the University of St Andrews for the purposes of research under the EMFF SIFIDS Project for the above stated vessel.</string>
<string name="participant_consent_form_fish_1_period">The information provided will span the period 1st December 2016 to 31st May 2019 (the duration of the SIFIDS Project).</string>
<string name="participant_consent_submit_button_text">Confirm consent</string>
<string name="participant_consent_thank_you">Thank-you for agreeing to take part in this study</string>
<string name="participant_consent_error">There was an error processing your agreement to take part. Please try again later.</string>

<!-- FISH1 Forms -->

Expand Down

0 comments on commit 90cfb55

Please sign in to comment.