diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index b81138ed60..c804f8ae58 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -1,10 +1,12 @@ package info.nightscout.androidaps; import android.Manifest; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.graphics.Rect; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; @@ -16,6 +18,10 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; 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.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); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java index ca07d190a8..0b31f360bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java @@ -4,22 +4,26 @@ package info.nightscout.androidaps.plugins.CircadianPercentageProfile; import android.app.Activity; import android.content.Context; import android.os.Bundle; +import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.TextView; +import com.andreabaccega.widget.FormEditText; import com.squareup.otto.Subscribe; import org.slf4j.Logger; @@ -34,24 +38,24 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; -import info.nightscout.utils.ToastUtils; public class CircadianPercentageProfileFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfileFragment.class); private static CircadianPercentageProfilePlugin circadianPercentageProfilePlugin = new CircadianPercentageProfilePlugin(); + private Object snackbarCaller; public static CircadianPercentageProfilePlugin getPlugin() { return circadianPercentageProfilePlugin; } - EditText diaView; + FormEditText diaView; RadioButton mgdlView; RadioButton mmolView; - EditText targetlowView; - EditText targethighView; - EditText percentageView; - EditText timeshiftView; + FormEditText targetlowView; + FormEditText targethighView; + FormEditText percentageView; + FormEditText timeshiftView; TextView profileView; TextView baseprofileIC; TextView baseprofileBasal; @@ -64,19 +68,25 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag ImageView iceditIcon; ImageView isfeditIcon; BasalEditDialog basalEditDialog; + LinearLayout ll; + Snackbar mSnackBar; + static Boolean percentageViewHint = true; + static Boolean timeshiftViewHint = true; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); mmolView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mmol); - targetlowView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); - targethighView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); - percentageView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_percentage); - timeshiftView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift); + targetlowView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); + targethighView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); + percentageView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_percentage); + timeshiftView = (FormEditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift); profileView = (TextView) layout.findViewById(R.id.circadianpercentageprofile_profileview); baseprofileBasal = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal); baseprofileBasalLayout = (LinearLayout) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal_layout); @@ -137,23 +147,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() { @Override public void onClick(View view) { @@ -201,6 +194,76 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag } }); + timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + if(mSnackBar!=null && snackbarCaller == timeshiftView){ + mSnackBar.dismiss(); + } + timeshiftView.clearFocus(); + ll.requestFocusFromTouch(); + } + else { + if (timeshiftViewHint) { + customSnackbar(view, getString(R.string.timeshift_hint), timeshiftView); + } + } + } + }); + + percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + + @Override + public void onFocusChange(View view, boolean hasFocus) { + if (!hasFocus) { + if(mSnackBar!=null && snackbarCaller == percentageView){ + mSnackBar.dismiss(); + } + percentageView.clearFocus(); + ll.requestFocusFromTouch(); + } + else { + if (percentageViewHint) { + customSnackbar(view, getString(R.string.percentagefactor_hint), percentageView); + } + } + } + }); + + 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() { @@ -217,15 +280,30 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag public void onTextChanged(CharSequence s, int start, int before, int count) { - if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) { - circadianPercentageProfilePlugin.percentage = 100; - } else { - circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); + if (percentageView.testValidity()) { + if (SafeParse.stringToInt(percentageView.getText().toString()) == 0) { + circadianPercentageProfilePlugin.percentage = 100; + } else { + circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); + } + updateProfileInfo(); + } + if (timeshiftView.testValidity()) { + 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.dia = SafeParse.stringToDouble(diaView.getText().toString()); - circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); - circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); - circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); circadianPercentageProfilePlugin.storeSettings(); updateProfileInfo(); } @@ -242,6 +320,37 @@ public class CircadianPercentageProfileFragment extends Fragment implements Frag return layout; } + private void customSnackbar(View view, final String Msg, Object snackbarCaller) { + if(mSnackBar!= null) mSnackBar.dismiss(); + + this.snackbarCaller = snackbarCaller; + if (timeshiftViewHint || percentageViewHint) { + mSnackBar = Snackbar.make(view, + Msg, + Snackbar.LENGTH_LONG) + .setDuration(Snackbar.LENGTH_LONG) + .setActionTextColor(getResources().getColor(R.color.notificationInfo)) + .setAction(getString(R.string.dont_show_again), 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() { StringBuilder sb = new StringBuilder(); sb.append("