Skip to content
Snippets Groups Projects
Commit a59552cb authored by Lennart Goedhart's avatar Lennart Goedhart
Browse files

Fix #42

parent 10a2d076
Branches
Tags
No related merge requests found
package info.nightscout.android.utils;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import info.nightscout.android.R;
/**
* Created by lgoedhart on 29/05/2016.
*
* Code found at http://stackoverflow.com/a/31345545
*/
public class ValidatingEditTextPreference extends EditTextPreference {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ValidatingEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public ValidatingEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ValidatingEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ValidatingEditTextPreference(Context context) {
super(context);
}
@Override
protected void showDialog(Bundle state) {
super.showDialog(state);
AlertDialog dlg = (AlertDialog) getDialog();
View positiveButton = dlg.getButton(DialogInterface.BUTTON_POSITIVE);
getEditText().setError(null);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPositiveButtonClicked(v);
}
});
}
private void onPositiveButtonClicked(View v) {
String errorMessage = onValidate(getEditText().getText().toString());
if (errorMessage == null) {
getEditText().setError(null);
onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
getDialog().dismiss();
} else {
getEditText().setError(errorMessage);
}
}
/***
* Called to validate contents of the edit text.
* <p/>
* Return null to indicate success, or return a validation error message to display on the edit text.
*
* @param text The text to validate.
* @return An error message, or null if the value passes validation.
*/
public String onValidate(String text) {
if (text.length() > 12) {
return null;
} else {
return getContext().getString(R.string.error_msg_api_secret_length);
}
}
}
\ No newline at end of file
...@@ -38,4 +38,7 @@ ...@@ -38,4 +38,7 @@
<string name="button_text_clear_log">Clear Log</string> <string name="button_text_clear_log">Clear Log</string>
<string name="button_text_get_now">Get Now</string> <string name="button_text_get_now">Get Now</string>
<string name="button_text_start_uploading_data">Start Uploading CGM Data</string> <string name="button_text_start_uploading_data">Start Uploading CGM Data</string>
<string name="preference_eula_accepted">IUNDERSTAND</string>
<string name="preference_enable_rest_upload">EnableRESTUpload</string>
<string name="error_msg_api_secret_length">API Secret must be 12 characters or longer.</string>
</resources> </resources>
...@@ -22,28 +22,28 @@ ...@@ -22,28 +22,28 @@
<PreferenceCategory android:title="Sharing"> <PreferenceCategory android:title="Sharing">
<info.nightscout.android.utils.CustomSwitchPreference <info.nightscout.android.utils.CustomSwitchPreference
android:disableDependentsState="false" android:disableDependentsState="false"
android:key="EnableRESTUpload" android:key="@string/preference_enable_rest_upload"
android:summary="Enable upload of data to Nightscout" android:summary="Enable upload of data to Nightscout"
android:title="REST API Upload"></info.nightscout.android.utils.CustomSwitchPreference> android:title="REST API Upload"></info.nightscout.android.utils.CustomSwitchPreference>
<EditTextPreference <EditTextPreference
android:defaultValue="https://YOUR.NIGHTSCOUT.SITE" android:defaultValue="https://YOUR.NIGHTSCOUT.SITE"
android:dependency="EnableRESTUpload" android:dependency="@string/preference_enable_rest_upload"
android:dialogMessage="The hostname of your Nightscout site" android:dialogMessage="The hostname of your Nightscout site"
android:dialogTitle="Enter Nightscout URL" android:dialogTitle="Enter Nightscout URL"
android:key="@string/preference_nightscout_url" android:key="@string/preference_nightscout_url"
android:title="Nightscout URL"></EditTextPreference> android:title="Nightscout URL"></EditTextPreference>
<EditTextPreference <info.nightscout.android.utils.ValidatingEditTextPreference
android:defaultValue="YOURAPISECRET" android:defaultValue="YOURAPISECRET"
android:dependency="EnableRESTUpload" android:dependency="@string/preference_enable_rest_upload"
android:dialogMessage="Your Nightscout API secret" android:dialogMessage="Your Nightscout API secret"
android:dialogTitle="Enter your Nightscout API secret" android:dialogTitle="Enter your Nightscout API secret"
android:key="@string/preference_api_secret" android:key="@string/preference_api_secret"
android:title="API Secret"></EditTextPreference> android:title="API Secret"></info.nightscout.android.utils.ValidatingEditTextPreference>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="Disclaimer"> <PreferenceCategory android:title="Disclaimer">
<info.nightscout.android.utils.CustomSwitchPreference <info.nightscout.android.utils.CustomSwitchPreference
android:disableDependentsState="false" android:disableDependentsState="false"
android:key="IUNDERSTAND" android:key="@string/preference_eula_accepted"
android:summaryOff="Deactivated. Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers" android:summaryOff="Deactivated. Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers"
android:summaryOn="Activated. Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers" android:summaryOn="Activated. Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers"
android:switchTextOff="NO" android:switchTextOff="NO"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment