From a2723f60e064218f8cff273ad7e3add1a91cec89 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Thu, 9 Feb 2017 14:25:25 +0100 Subject: [PATCH] wear wizard on watch part first working --- wear/src/main/AndroidManifest.xml | 10 +- .../actions/ActionsDefinitions.java | 7 +- .../actions/bolus/GridActivity.java | 6 +- .../actions/utils/PlusMinusEditText.java | 196 ++++++++++++++++++ .../androidaps/actions/utils/SafeParse.java | 36 ++++ .../actions/wizard/WizardActivity.java | 107 ++++++++++ ...item.xml => action_editplusminus_item.xml} | 6 +- wear/src/main/res/layout/action_send_item.xml | 28 +++ wear/wear.iml | 30 +-- 9 files changed, 399 insertions(+), 27 deletions(-) create mode 100644 wear/src/main/java/info/nightscout/androidaps/actions/utils/PlusMinusEditText.java create mode 100644 wear/src/main/java/info/nightscout/androidaps/actions/utils/SafeParse.java create mode 100644 wear/src/main/java/info/nightscout/androidaps/actions/wizard/WizardActivity.java rename wear/src/main/res/layout/{grid_view_pager_item.xml => action_editplusminus_item.xml} (91%) create mode 100644 wear/src/main/res/layout/action_send_item.xml diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml index d2a06c5c12..2c1a9f01eb 100644 --- a/wear/src/main/AndroidManifest.xml +++ b/wear/src/main/AndroidManifest.xml @@ -129,6 +129,14 @@ - + + + + + + + \ No newline at end of file diff --git a/wear/src/main/java/info/nightscout/androidaps/actions/ActionsDefinitions.java b/wear/src/main/java/info/nightscout/androidaps/actions/ActionsDefinitions.java index be134955b0..150edb7d66 100644 --- a/wear/src/main/java/info/nightscout/androidaps/actions/ActionsDefinitions.java +++ b/wear/src/main/java/info/nightscout/androidaps/actions/ActionsDefinitions.java @@ -7,6 +7,7 @@ import info.nightscout.androidaps.ListenerService; import info.nightscout.androidaps.NWPreferences; import info.nightscout.androidaps.actions.bolus.BolusActivity; import info.nightscout.androidaps.actions.bolus.GridActivity; +import info.nightscout.androidaps.actions.wizard.WizardActivity; /** * Created by adrian on 08/02/17. @@ -16,13 +17,13 @@ final class ActionsDefinitions { private static final String[] ACTION_NAMES = { "Temp Target", - "Bolus", + "Wizard", "Settings", "Resend Data", "Fillpreset 1", "Fillpreset 2", "Fillpreset 3", - "008"}; + "009"}; public static void doAction(int position, Context ctx) { @@ -32,7 +33,7 @@ final class ActionsDefinitions { case 0: break; case 1: - intent = new Intent(ctx, BolusActivity.class); + intent = new Intent(ctx, WizardActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(intent); break; diff --git a/wear/src/main/java/info/nightscout/androidaps/actions/bolus/GridActivity.java b/wear/src/main/java/info/nightscout/androidaps/actions/bolus/GridActivity.java index 230c8bc2e4..e18d2e839e 100644 --- a/wear/src/main/java/info/nightscout/androidaps/actions/bolus/GridActivity.java +++ b/wear/src/main/java/info/nightscout/androidaps/actions/bolus/GridActivity.java @@ -2,18 +2,14 @@ package info.nightscout.androidaps.actions.bolus; import android.app.Activity; -import android.content.Context; import android.content.res.Resources; -import android.graphics.Color; import android.os.Bundle; -import android.support.wearable.activity.WearableActivity; import android.support.wearable.view.DotsPageIndicator; import android.support.wearable.view.GridPagerAdapter; import android.support.wearable.view.GridViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; import android.widget.TextView; import info.nightscout.androidaps.R; @@ -62,7 +58,7 @@ public class GridActivity extends Activity { @Override public Object instantiateItem(ViewGroup container, int row, int col) { - final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.grid_view_pager_item, container, false); + final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item, container, false); final TextView textView = (TextView) view.findViewById(R.id.label); textView.setText("label: " + col); container.addView(view); diff --git a/wear/src/main/java/info/nightscout/androidaps/actions/utils/PlusMinusEditText.java b/wear/src/main/java/info/nightscout/androidaps/actions/utils/PlusMinusEditText.java new file mode 100644 index 0000000000..b92603c567 --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/actions/utils/PlusMinusEditText.java @@ -0,0 +1,196 @@ +package info.nightscout.androidaps.actions.utils; + +import android.os.Handler; +import android.os.Message; +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import java.text.NumberFormat; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + + +/** + * Created by mike on 28.06.2016. + */ +public class PlusMinusEditText implements View.OnKeyListener, + View.OnTouchListener, View.OnClickListener { + + Integer editTextID; + public TextView editText; + ImageView minusImage; + ImageView plusImage; + + Double value; + Double minValue = 0d; + Double maxValue = 1d; + Double step = 1d; + NumberFormat formater; + boolean allowZero = false; + + private Handler mHandler; + private ScheduledExecutorService mUpdater; + + private class UpdateCounterTask implements Runnable { + private boolean mInc; + private int repeated = 0; + private int multiplier = 1; + + private final int doubleLimit = 5; + + public UpdateCounterTask(boolean inc) { + mInc = inc; + } + + public void run() { + Message msg = new Message(); + if (repeated % doubleLimit == 0) multiplier *= 2; + repeated++; + msg.arg1 = multiplier; + msg.arg2 = repeated; + if (mInc) { + msg.what = MSG_INC; + } else { + msg.what = MSG_DEC; + } + mHandler.sendMessage(msg); + } + } + + private static final int MSG_INC = 0; + private static final int MSG_DEC = 1; + + public PlusMinusEditText(View view, int editTextID, int plusID, int minusID, Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero) { + editText = (TextView) view.findViewById(editTextID); + minusImage = (ImageView) view.findViewById(minusID); + plusImage = (ImageView) view.findViewById(plusID); + + this.value = initValue; + this.minValue = minValue; + this.maxValue = maxValue; + this.step = step; + this.formater = formater; + this.allowZero = allowZero; + + mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_INC: + inc(msg.arg1); + return; + case MSG_DEC: + dec(msg.arg1); + return; + } + super.handleMessage(msg); + } + }; + + minusImage.setOnTouchListener(this); + minusImage.setOnKeyListener(this); + minusImage.setOnClickListener(this); + plusImage.setOnTouchListener(this); + plusImage.setOnKeyListener(this); + plusImage.setOnClickListener(this); + updateEditText(); + } + + public void setValue(Double value) { + this.value = value; + updateEditText(); + } + + public Double getValue() { + return value; + } + + public void setStep(Double step) { + this.step = step; + } + + private void inc(int multiplier) { + value += step * multiplier; + if (value > maxValue) { + value = maxValue; + stopUpdating(); + } + updateEditText(); + } + + private void dec( int multiplier) { + value -= step * multiplier; + if (value < minValue) { + value = minValue; + stopUpdating(); + } + updateEditText(); + } + + private void updateEditText() { + if (value == 0d && !allowZero) + editText.setText(""); + else + editText.setText(formater.format(value)); + } + + private void startUpdating(boolean inc) { + if (mUpdater != null) { + return; + } + mUpdater = Executors.newSingleThreadScheduledExecutor(); + mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200, + TimeUnit.MILLISECONDS); + } + + private void stopUpdating() { + if (mUpdater != null) { + mUpdater.shutdownNow(); + mUpdater = null; + } + } + + @Override + public void onClick(View v) { + if (mUpdater == null) { + if (v == plusImage) { + inc(1); + } else { + dec(1); + } + } + } + + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER; + boolean isReleased = event.getAction() == KeyEvent.ACTION_UP; + boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN + && event.getAction() != KeyEvent.ACTION_MULTIPLE; + + if (isKeyOfInterest && isReleased) { + stopUpdating(); + } else if (isKeyOfInterest && isPressed) { + startUpdating(v == plusImage); + } + return false; + } + + @Override + public boolean onTouch(View v, MotionEvent event) { + boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL; + boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN; + + if (isReleased) { + stopUpdating(); + } else if (isPressed) { + startUpdating(v == plusImage); + } + return false; + } + +} diff --git a/wear/src/main/java/info/nightscout/androidaps/actions/utils/SafeParse.java b/wear/src/main/java/info/nightscout/androidaps/actions/utils/SafeParse.java new file mode 100644 index 0000000000..2f896155fb --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/actions/utils/SafeParse.java @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.actions.utils; + +/** + * Created by mike on 23.06.2016. + */ +public class SafeParse { + public static Double stringToDouble(String input) { + Double result = 0d; + input = input.replace(",", "."); + try { + result = Double.parseDouble(input); + } catch (Exception e) { + } + return result; + } + + public static Integer stringToInt(String input) { + Integer result = 0; + input = input.replace(",", "."); + try { + result = Integer.parseInt(input); + } catch (Exception e) { + } + return result; + } + + public static Long stringToLong(String input) { + Long result = 0L; + input = input.replace(",", "."); + try { + result = Long.parseLong(input); + } catch (Exception e) { + } + return result; + } +} diff --git a/wear/src/main/java/info/nightscout/androidaps/actions/wizard/WizardActivity.java b/wear/src/main/java/info/nightscout/androidaps/actions/wizard/WizardActivity.java new file mode 100644 index 0000000000..a00b78a3df --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/actions/wizard/WizardActivity.java @@ -0,0 +1,107 @@ +package info.nightscout.androidaps.actions.wizard; + + +import android.app.Activity; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.wearable.view.DotsPageIndicator; +import android.support.wearable.view.GridPagerAdapter; +import android.support.wearable.view.GridViewPager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import java.text.DecimalFormat; + +import info.nightscout.androidaps.ListenerService; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.actions.utils.PlusMinusEditText; +import info.nightscout.androidaps.actions.utils.SafeParse; + +/** + * Created by adrian on 09/02/17. + */ + + +public class WizardActivity extends Activity { + + PlusMinusEditText editCarbs; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.grid_layout); + final Resources res = getResources(); + final GridViewPager pager = (GridViewPager) findViewById(R.id.pager); + + pager.setAdapter(new MyGridViewPagerAdapter()); + DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator); + dotsPageIndicator.setPager(pager); + } + + + @Override + protected void onPause() { + super.onPause(); + finish(); + } + + + private class MyGridViewPagerAdapter extends GridPagerAdapter { + @Override + public int getColumnCount(int arg0) { + return 2; + } + + @Override + public int getRowCount() { + return 1; + } + + @Override + public Object instantiateItem(ViewGroup container, int row, int col) { + + if(col == 0){ + final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item, container, false); + final TextView textView = (TextView) view.findViewById(R.id.label); + textView.setText("carbs"); + editCarbs = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, 0d, 0d, 100d, 1d, new DecimalFormat("0"), false); + container.addView(view); + return view; + } else { + + final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_send_item, container, false); + final ImageView confirmbutton = (ImageView) view.findViewById(R.id.confirmbutton); + confirmbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + //TODO: check if it can happen that the fagment is never created that hold data + // (you have to swipe past them anyways - but still) + + String actionstring = "wizard " + SafeParse.stringToInt(editCarbs.editText.getText().toString());; + ListenerService.initiateAction(WizardActivity.this, actionstring); + finish(); + } + }); + container.addView(view); + return view; + } + } + + @Override + public void destroyItem(ViewGroup container, int row, int col, Object view) { + //TODO: Handle this to get the data before the view is destroyed? + container.removeView((View)view); + } + + @Override + public boolean isViewFromObject(View view, Object object) { + return view==object; + } + + + } +} \ No newline at end of file diff --git a/wear/src/main/res/layout/grid_view_pager_item.xml b/wear/src/main/res/layout/action_editplusminus_item.xml similarity index 91% rename from wear/src/main/res/layout/grid_view_pager_item.xml rename to wear/src/main/res/layout/action_editplusminus_item.xml index 2979dc00cb..6e12ad857b 100644 --- a/wear/src/main/res/layout/grid_view_pager_item.xml +++ b/wear/src/main/res/layout/action_editplusminus_item.xml @@ -12,7 +12,7 @@ android:gravity="center"> + + + + + + + + + + diff --git a/wear/wear.iml b/wear/wear.iml index 9ccd598295..33cd39a6c1 100644 --- a/wear/wear.iml +++ b/wear/wear.iml @@ -43,6 +43,13 @@ + + + + + + + @@ -51,13 +58,6 @@ - - - - - - - @@ -66,14 +66,6 @@ - - - - - - - - @@ -82,6 +74,14 @@ + + + + + + + +