Skip to content

Commit 9a1702b

Browse files
author
Adam Howard
committed
getting there
1 parent a0a3ed5 commit 9a1702b

File tree

6 files changed

+200
-41
lines changed

6 files changed

+200
-41
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ dependencies {
2727
compile 'com.android.support:appcompat-v7:25.3.1'
2828
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
2929
compile 'cn.aigestudio.wheelpicker:WheelPicker:1.1.2'
30+
compile 'com.github.medavox:utils:v0.6'
3031
testCompile 'junit:junit:4.12'
3132
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.medavox.diabeticdiary">
4-
4+
<uses-permission android:name="android.permission.SEND_SMS"/>
55
<application
66
android:allowBackup="true"
77
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/medavox/diabeticdiary/MainActivity.java

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,132 @@
11
package com.medavox.diabeticdiary;
22

3+
import android.Manifest;
4+
import android.content.pm.PackageManager;
5+
import android.os.Build;
6+
import android.provider.Telephony;
37
import android.support.v7.app.AppCompatActivity;
48
import android.os.Bundle;
9+
import android.telephony.SmsManager;
10+
import android.text.InputFilter;
11+
import android.text.Spanned;
12+
import android.util.Log;
13+
import android.view.View;
14+
import android.widget.Button;
15+
import android.widget.CheckBox;
516
import android.widget.EditText;
617
import android.widget.TableLayout;
18+
19+
import com.medavox.util.validate.Validator;
20+
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
23+
24+
import static com.medavox.util.validate.Validator.check;
725
//todo: import numberpicker module from https://github.com/SimonVT/android-numberpicker,
826
//todo: then customise it to my needs
927

1028
public class MainActivity extends AppCompatActivity {
29+
private static final int smsSendRequestCode = 42;
30+
private static final String TAG = "DiabeticDiary";
1131

1232
@Override
1333
protected void onCreate(Bundle savedInstanceState) {
1434
super.onCreate(savedInstanceState);
1535
setContentView(R.layout.activity_main);
1636
//bg, cp, qa, bi, kt
37+
final String[] names = new String[] {"BG", "CP", "QA", "BI", "KT"};
1738
int[] inputIDs = new int[] {R.id.BGinput, R.id.CPinput, R.id.QAinput, R.id.BIinput,
1839
R.id.KTinput};
1940

20-
EditText[] inputs = new EditText[inputIDs.length];
41+
final EditText[] inputs = new EditText[inputIDs.length];
2142
for(int i = 0; i < inputs.length; i++) {
2243
inputs[i] = (EditText) findViewById(inputIDs[i]);
44+
}
45+
46+
int[] checkboxIDs = new int[] {R.id.BGcheckBox, R.id.CPcheckBox, R.id.QAcheckBox,
47+
R.id.BIcheckBox, R.id.KTcheckBox};
2348

49+
final CheckBox[] checkBoxes = new CheckBox[checkboxIDs.length];
50+
for(int i = 0; i < checkBoxes.length; i++) {
51+
checkBoxes[i] = (CheckBox)findViewById(checkboxIDs[i]);
2452
}
2553

54+
try {
55+
check(checkBoxes.length == inputs.length,
56+
"the number of names must equal the number of input fields!");
57+
}
58+
catch(Exception e) {
59+
Log.e("DiabeticDiary", "validation exception:"+e);
60+
}
61+
Button recordButton = (Button) findViewById(R.id.button);
62+
63+
//for BG input, only allow 2 digits before the decimal place, and 1 after
64+
Log.i(TAG, "existing filters: "+inputs[0].getFilters().length);
65+
inputs[0].setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2,1)});
66+
//the same with CP
67+
inputs[1].setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2,1)});
68+
69+
//for QA, allow no digits after the decimal point.
70+
// This might not work, as the field is already integer-constrained
71+
inputs[2].setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2,0)});
72+
73+
//with BI, allow 3 digit integers. I used to take ~80, so it's not impossible
74+
inputs[3].setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3,0)});
75+
76+
//for KT, i'd be worried if ketones were > 9, but again it's not impossible
77+
inputs[4].setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2,1)});
78+
79+
InputFilter filter = new InputFilter() {
80+
public CharSequence filter(CharSequence source, int start, int end,
81+
Spanned dest, int dstart, int dend) {
82+
for (int i = start; i < end; i++) {
83+
if (!Character.isLetterOrDigit(source.charAt(i))) {
84+
return "";
85+
}
86+
}
87+
return null;
88+
}
89+
};
90+
91+
recordButton.setOnClickListener(new View.OnClickListener(){
92+
@Override public void onClick(View view) {
93+
//get time button was pressed as time of reading
94+
long now = System.currentTimeMillis();
95+
//convert to number of 10-second periods (1/6 of a minute) since the epoch
96+
//this reduces unnecessary precision, and increases the time until we have a Y2K-type issue (in 2038)
97+
//plus, the time fits within an int, allowing us to use it as the index to an array
98+
//which can store the log entries
99+
long hectaMinutes = now/10000;
100+
//Log.i("DiabeticDiary", "hectaminutes fit within an int:"+ (hectaMinutes < Integer.MAX_VALUE));
101+
102+
//select which fields have been ticked
103+
String out = "Diabetic Diary ENTRY {";
104+
for(int i = 0; i < checkBoxes.length; i++) {
105+
if(checkBoxes[i].isChecked()){
106+
out += names[i]+":"+inputs[i].getText()+"; ";
107+
}
108+
}
109+
out += "}";
110+
Log.i(TAG, out);
111+
112+
//support runtime permission checks on android versions >= 6.0
113+
//if we're on android 6+ AND we haven't got location permissions yet, ask for them
114+
/*if (Build.VERSION.SDK_INT >= 23 && checkSelfPermission(Manifest.permission.SEND_SMS)
115+
!= PackageManager.PERMISSION_GRANTED) {
116+
117+
// todo: Show an explanation to the user *asynchronously*
118+
// After the user sees the explanation, try again to request the permission.
119+
120+
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, smsSendRequestCode);
121+
}
122+
else {
123+
124+
SmsManager.getDefault().sendTextMessage("07516041435", null, out, null, null);
125+
}*/
126+
127+
128+
}
129+
});
26130
//TableLayout
27131

28132

@@ -74,5 +178,50 @@ protected void onCreate(Bundle savedInstanceState) {
74178
minorKT.setMinValue(0);
75179
minorKT.setMaxValue(9);
76180
*/
181+
182+
183+
}
184+
//taken from stackoverflow.com/questions/5357455
185+
public class DecimalDigitsInputFilter implements InputFilter {
186+
187+
Pattern mPattern;
188+
public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
189+
String pat="[0-9]{0,"+digitsBeforeZero+"}((\\.[0-9]{0,"+digitsAfterZero+"})|(\\.)?)";
190+
mPattern=Pattern.compile(pat);
191+
Log.i(TAG, "pattern:"+pat);
192+
}
193+
194+
@Override
195+
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
196+
Matcher matcher = mPattern.matcher(dest+source.toString());
197+
//Log.i(TAG, "dest:"+dest+"; dstart:"+dstart+"; dend:"+dend);
198+
//Log.i(TAG, "source:"+source+"; start:"+start+"; end:"+end);
199+
if(!matcher.matches()) {
200+
return "";
201+
}
202+
return null;
203+
}
204+
}
205+
206+
@Override
207+
public void onRequestPermissionsResult(int requestCode,
208+
String[] permissions, int[] grantResults) {
209+
//todo: handle multiple permissions
210+
//if there are no permissions etc to process, return early.
211+
//See https://developer.android.com/reference/android/support/v4/app/ActivityCompat.OnRequestPermissionsResultCallback.html#onRequestPermissionsResult%28int,%20java.lang.String[],%20int[]%29
212+
if(permissions.length != 1 || grantResults.length != 1) {
213+
return;
214+
}
215+
Log.i(TAG, "permissions results length:" + permissions.length);
216+
217+
218+
Log.i(TAG, "permission \"" + permissions[0] + "\" result: " + grantResults[0]);
219+
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
220+
221+
}
222+
else if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
223+
//the user has granted permission, so start the location service
224+
//this happens by starting the service in onResume()
225+
}
77226
}
78227
}

app/src/main/res/layout/activity_main.xml

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,51 @@
88
tools:context="com.medavox.diabeticdiary.MainActivity">
99

1010

11-
<ScrollView
12-
android:layout_width="368dp"
13-
android:layout_height="495dp"
14-
tools:layout_editor_absoluteX="10dp"
15-
tools:layout_editor_absoluteY="8dp">
11+
<ScrollView
12+
android:layout_width="368dp"
13+
android:layout_height="495dp"
14+
tools:layout_editor_absoluteX="10dp"
15+
tools:layout_editor_absoluteY="8dp">
1616

17-
<LinearLayout
18-
android:layout_width="match_parent"
19-
android:layout_height="match_parent"
20-
android:orientation="vertical">
17+
<!-- all-things wrapper for scrollview -->
18+
<LinearLayout
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:orientation="vertical">
2122

2223

23-
<TableLayout
24-
android:layout_width="match_parent"
25-
android:layout_height="match_parent">
24+
<TableLayout
25+
android:layout_width="match_parent"
26+
android:layout_height="match_parent">
2627

27-
<TableRow
28-
android:layout_width="match_parent"
29-
android:layout_height="match_parent"/>
28+
<TableRow
29+
android:layout_width="match_parent"
30+
android:layout_height="match_parent"/>
3031

31-
<TableRow
32-
android:layout_width="match_parent"
33-
android:layout_height="match_parent"/>
32+
<TableRow
33+
android:layout_width="match_parent"
34+
android:layout_height="match_parent"/>
3435

35-
<TableRow
36-
android:layout_width="match_parent"
37-
android:layout_height="match_parent"/>
36+
<TableRow
37+
android:layout_width="match_parent"
38+
android:layout_height="match_parent"/>
3839

39-
<TableRow
40-
android:layout_width="match_parent"
41-
android:layout_height="match_parent"/>
42-
</TableLayout>
40+
<TableRow
41+
android:layout_width="match_parent"
42+
android:layout_height="match_parent"/>
43+
</TableLayout>
4344

44-
<LinearLayout
45+
<!-- 2 column container -->
46+
<LinearLayout
4547
android:layout_width="match_parent"
4648
android:layout_height="wrap_content"
4749
android:orientation="horizontal"
4850
tools:layout_editor_absoluteX="8dp"
4951
tools:layout_editor_absoluteY="8dp">
5052

51-
52-
<LinearLayout
53+
54+
<!-- checkbox column -->
55+
<LinearLayout
5356
android:id="@+id/checkbox_column"
5457
android:layout_width="wrap_content"
5558
android:layout_height="match_parent"
@@ -79,7 +82,6 @@
7982
android:checked="false"
8083
android:text="Quick-Acting Insulin"/>
8184

82-
8385
<CheckBox
8486
android:id="@+id/BIcheckBox"
8587
android:layout_width="wrap_content"
@@ -88,7 +90,6 @@
8890
android:checked="false"
8991
android:text="Background Insulin"/>
9092

91-
9293
<CheckBox
9394
android:id="@+id/KTcheckBox"
9495
android:layout_width="wrap_content"
@@ -146,15 +147,16 @@
146147
android:ems="10"
147148
android:inputType="numberDecimal"/>
148149

149-
150150
</LinearLayout>
151-
<Button
152-
android:id="@+id/button"
153-
android:layout_width="match_parent"
154-
android:layout_height="wrap_content"
155-
android:text="Record"/>
156151

157-
</LinearLayout>
158-
</LinearLayout>
159-
</ScrollView>
152+
</LinearLayout> <!-- end 2 column container -->
153+
154+
<Button
155+
android:id="@+id/button"
156+
android:layout_width="match_parent"
157+
android:layout_height="wrap_content"
158+
android:text="Record"/>
159+
160+
</LinearLayout> <!-- end all-things wrapper for scrollview -->
161+
</ScrollView>
160162
</android.support.constraint.ConstraintLayout>

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ buildscript {
1515
allprojects {
1616
repositories {
1717
jcenter()
18+
maven { url "https://jitpack.io" }
1819
}
1920
}
2021

0 commit comments

Comments
 (0)