Merge pull request #63 from AdrianLxM/patch-16

LadyViktoria validation lib plus safety
This commit is contained in:
AdrianLxM 2017-02-11 21:12:22 +01:00 committed by GitHub
commit 104c146b2f
3 changed files with 212 additions and 51 deletions

View file

@ -1,10 +1,12 @@
package info.nightscout.androidaps; package info.nightscout.androidaps;
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -16,6 +18,10 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.fonts.FontAwesomeModule; import com.joanzapata.iconify.fonts.FontAwesomeModule;
@ -259,4 +265,21 @@ public class MainActivity extends AppCompatActivity {
} }
} }
} }
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
} }

View file

@ -4,22 +4,26 @@ package info.nightscout.androidaps.plugins.CircadianPercentageProfile;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.Editable; import android.text.Editable;
import android.text.Html; import android.text.Html;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.TextView; import android.widget.TextView;
import com.andreabaccega.widget.FormEditText;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -34,7 +38,6 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
import info.nightscout.utils.ToastUtils;
public class CircadianPercentageProfileFragment extends Fragment implements FragmentBase { public class CircadianPercentageProfileFragment extends Fragment implements FragmentBase {
private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfileFragment.class); private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfileFragment.class);
@ -45,13 +48,13 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
return circadianPercentageProfilePlugin; return circadianPercentageProfilePlugin;
} }
EditText diaView; FormEditText diaView;
RadioButton mgdlView; RadioButton mgdlView;
RadioButton mmolView; RadioButton mmolView;
EditText targetlowView; FormEditText targetlowView;
EditText targethighView; FormEditText targethighView;
EditText percentageView; FormEditText percentageView;
EditText timeshiftView; FormEditText timeshiftView;
TextView profileView; TextView profileView;
TextView baseprofileIC; TextView baseprofileIC;
TextView baseprofileBasal; TextView baseprofileBasal;
@ -64,19 +67,24 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
ImageView iceditIcon; ImageView iceditIcon;
ImageView isfeditIcon; ImageView isfeditIcon;
BasalEditDialog basalEditDialog; BasalEditDialog basalEditDialog;
LinearLayout ll;
static Boolean percentageViewHint = true;
static Boolean timeshiftViewHint = true;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.circadianpercentageprofile_fragment, container, false); View layout = inflater.inflate(R.layout.circadianpercentageprofile_fragment, container, false);
diaView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_dia); ll = (LinearLayout) layout.findViewById(R.id.circadianpercentageprofile_linearlayout);
ll.requestFocusFromTouch();
diaView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_dia);
mgdlView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mgdl); mgdlView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mgdl);
mmolView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mmol); mmolView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mmol);
targetlowView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); targetlowView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow);
targethighView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); targethighView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh);
percentageView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_percentage); percentageView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_percentage);
timeshiftView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift); timeshiftView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift);
profileView = (TextView) layout.findViewById(R.id.circadianpercentageprofile_profileview); profileView = (TextView) layout.findViewById(R.id.circadianpercentageprofile_profileview);
baseprofileBasal = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal); baseprofileBasal = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal);
baseprofileBasalLayout = (LinearLayout) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal_layout); baseprofileBasalLayout = (LinearLayout) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal_layout);
@ -137,23 +145,6 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
} }
}); });
timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b)
ToastUtils.showToastInUiThread(getContext(), getString(R.string.timeshift_hint));
}
});
percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b)
ToastUtils.showToastInUiThread(getContext(), getString(R.string.percentagefactor_hint));
}
});
timeIcon.setOnClickListener(new View.OnClickListener() { timeIcon.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -201,6 +192,70 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
} }
}); });
timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
timeshiftView.clearFocus();
ll.requestFocusFromTouch();
}
else {
if (timeshiftViewHint) {
customSnackbar(view, getString(R.string.timeshift_hint));
}
}
}
});
percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
percentageView.clearFocus();
ll.requestFocusFromTouch();
}
else {
if (percentageViewHint) {
customSnackbar(view, getString(R.string.percentagefactor_hint));
}
}
}
});
diaView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
diaView.clearFocus();
ll.requestFocusFromTouch();
}
}
});
targethighView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
targethighView.clearFocus();
ll.requestFocusFromTouch();
}
}
});
targetlowView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
targetlowView.clearFocus();
ll.requestFocusFromTouch();
}
}
});
TextWatcher textWatch = new TextWatcher() { TextWatcher textWatch = new TextWatcher() {
@ -217,15 +272,30 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
public void onTextChanged(CharSequence s, int start, public void onTextChanged(CharSequence s, int start,
int before, int count) { int before, int count) {
if (percentageView.testValidity()) {
if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) { if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) {
circadianPercentageProfilePlugin.percentage = 100; circadianPercentageProfilePlugin.percentage = 100;
} else { } else {
circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString());
} }
circadianPercentageProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); updateProfileInfo();
circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); }
circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); if (timeshiftView.testValidity()) {
circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString());
updateProfileInfo();
}
if (diaView.testValidity()) {
circadianPercentageProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString());
updateProfileInfo();
}
if (targethighView.testValidity()) {
circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString());
updateProfileInfo();
}
if (targetlowView.testValidity()) {
circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString());
updateProfileInfo();
}
circadianPercentageProfilePlugin.storeSettings(); circadianPercentageProfilePlugin.storeSettings();
updateProfileInfo(); updateProfileInfo();
} }
@ -242,6 +312,34 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
return layout; return layout;
} }
private void customSnackbar(View view, final String Msg) {
if (timeshiftViewHint || percentageViewHint) {
Snackbar mSnackBar = Snackbar.make(view,
Msg,
Snackbar.LENGTH_LONG)
.setDuration(7000)
.setActionTextColor(getResources().getColor(R.color.notificationInfo))
.setAction(getString(R.string.dismiss), new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Msg.equals(getString(R.string.percentagefactor_hint))) {
percentageViewHint = false;
} else if (Msg.equals(getString(R.string.timeshift_hint))) {
timeshiftViewHint = false;
}
}
});
view = mSnackBar.getView();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
view.setLayoutParams(params);
view.setBackgroundColor(getResources().getColor(R.color.cardview_dark_background));
TextView mainTextView = (TextView) (view).findViewById(android.support.design.R.id.snackbar_text);
mainTextView.setTextColor(getResources().getColor(R.color.mdtp_white));
mSnackBar.show();
}
}
private void updateProfileInfo() { private void updateProfileInfo() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("<h3>"); sb.append("<h3>");
@ -274,6 +372,7 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
basalEditDialog.dismiss(); basalEditDialog.dismiss();
} }
basalEditDialog = null; basalEditDialog = null;
ll.requestFocusFromTouch();
} }
public static class BasalEditDialog extends DialogFragment { public static class BasalEditDialog extends DialogFragment {
@ -354,6 +453,7 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
basalEditDialog = null; basalEditDialog = null;
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
ll.requestFocusFromTouch();
} }
@Override @Override
@ -361,6 +461,7 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag
super.onResume(); super.onResume();
MainApp.bus().register(this); MainApp.bus().register(this);
onStatusEvent(null); onStatusEvent(null);
ll.requestFocusFromTouch();
} }
@Subscribe @Subscribe

View file

@ -1,4 +1,5 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:validate="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -12,7 +13,10 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical"
android:id="@+id/circadianpercentageprofile_linearlayout"
android:focusable="true"
android:focusableInTouchMode="true" >
<TextView <TextView
android:id="@+id/circadianpercentageprofile_profileview" android:id="@+id/circadianpercentageprofile_profileview"
@ -33,9 +37,11 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:background="@drawable/pillborder"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="5dp"> android:padding="5dp"
android:backgroundTint="@color/mdtp_transparent_black"
android:background="@drawable/pillborder"
android:backgroundTintMode="src_over">
<ImageView <ImageView
android:id="@+id/circadianpercentageprofile_percentageicon" android:id="@+id/circadianpercentageprofile_percentageicon"
@ -44,23 +50,34 @@
android:src="@mipmap/ic_percentage" android:src="@mipmap/ic_percentage"
android:textAppearance="?android:attr/textAppearanceMedium"/> android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText <com.andreabaccega.widget.FormEditText
validate:testType="numericRange"
validate:minNumber="50"
validate:maxNumber="200"
android:digits="0123456789.,"
android:id="@+id/circadianpercentageprofile_percentage" android:id="@+id/circadianpercentageprofile_percentage"
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/percentagefactor_hint" android:inputType="number"
android:inputType="number"/> android:selectAllOnFocus="true"
android:textAlignment="center" />
</LinearLayout> </LinearLayout>
<Space
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:background="@drawable/pillborder"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="5dp"> android:padding="5dp"
android:backgroundTint="@color/mdtp_transparent_black"
android:background="@drawable/pillborder"
android:backgroundTintMode="src_over">
<ImageView <ImageView
android:id="@+id/circadianpercentageprofile_timeicon" android:id="@+id/circadianpercentageprofile_timeicon"
@ -69,12 +86,17 @@
android:src="@mipmap/ic_time" android:src="@mipmap/ic_time"
android:textAppearance="?android:attr/textAppearanceMedium"/> android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText <com.andreabaccega.widget.FormEditText
validate:testType="numericRange"
validate:minNumber="0"
validate:maxNumber="24"
android:digits="0123456789.,"
android:id="@+id/circadianpercentageprofile_timeshift" android:id="@+id/circadianpercentageprofile_timeshift"
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/timeshift_hint" android:inputType="number"
android:inputType="number"/> android:selectAllOnFocus="true"
android:textAlignment="center" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
@ -231,7 +253,12 @@
android:text="@string/dia" android:text="@string/dia"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText <com.andreabaccega.widget.FormEditText
validate:testType="floatNumericRange"
validate:floatminNumber="1.0"
validate:floatmaxNumber="10.0"
android:digits="0123456789.,"
android:selectAllOnFocus="true"
android:id="@+id/circadianpercentageprofile_dia" android:id="@+id/circadianpercentageprofile_dia"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -249,14 +276,24 @@
android:text="@string/target_range" android:text="@string/target_range"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText <com.andreabaccega.widget.FormEditText
validate:testType="floatNumericRange"
validate:floatminNumber="5.0"
validate:floatmaxNumber="180.0"
android:digits="0123456789.,"
android:selectAllOnFocus="true"
android:id="@+id/circadianpercentageprofile_targetlow" android:id="@+id/circadianpercentageprofile_targetlow"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="2" android:layout_column="2"
android:inputType="numberDecimal" /> android:inputType="numberDecimal" />
<EditText <com.andreabaccega.widget.FormEditText
validate:testType="floatNumericRange"
validate:floatminNumber="10.0"
validate:floatmaxNumber="270.0"
android:digits="0123456789.,"
android:selectAllOnFocus="true"
android:id="@+id/circadianpercentageprofile_targethigh" android:id="@+id/circadianpercentageprofile_targethigh"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"