From a6e26e5ddcb2297e83a229d7da2a6575880742bd Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 11 Jul 2015 00:03:41 +0200 Subject: [PATCH] Cleanup, use fragment based charts (WIP #45) Known issue: scrolling a zoomed-in chart interferes with swiping to the next/previous chart (so far there's just one, but...) Workaround: Swipe down and then left or right in one go, this will let you scroll the zoomed chart --- app/src/main/AndroidManifest.xml | 7 - .../gadgetbridge/AbstractDeviceSupport.java | 4 +- .../gadgetbridge/ControlCenter.java | 6 +- .../activities/AbstractChartFragment.java | 166 ++++++++++++++ ...y.java => ActivitySleepChartFragment.java} | 212 +++--------------- .../activities/ChartsActivity.java | 30 +-- .../activities/SleepChartFragment.java | 172 ++------------ .../gadgetbridge/charts/SleepUtils.java | 12 + .../database/ActivityDatabaseHandler.java | 2 +- app/src/main/res/layout/fragment_charts.xml | 5 +- 10 files changed, 242 insertions(+), 374 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractChartFragment.java rename app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/{SleepChartActivity.java => ActivitySleepChartFragment.java} (57%) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/charts/SleepUtils.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 563016f8..b17bd9d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -59,13 +59,6 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".ControlCenter" /> - - - diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java index 229e19f8..b7dc0feb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java @@ -18,7 +18,7 @@ import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; -import nodomain.freeyourgadget.gadgetbridge.activities.SleepChartActivity; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractChartFragment; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; @@ -142,7 +142,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { public void handleGBDeviceEvent(GBDeviceEventSleepMonitorResult sleepMonitorResult) { Context context = getContext(); LOG.info("Got event for SLEEP_MONIOR_RES"); - Intent sleepMontiorIntent = new Intent(SleepChartActivity.ACTION_REFRESH); + Intent sleepMontiorIntent = new Intent(AbstractChartFragment.ACTION_REFRESH); sleepMontiorIntent.putExtra("smartalarm_from", sleepMonitorResult.smartalarm_from); sleepMontiorIntent.putExtra("smartalarm_to", sleepMonitorResult.smartalarm_to); sleepMontiorIntent.putExtra("recording_base_timestamp", sleepMonitorResult.recording_base_timestamp); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java index 8835dd20..13ba24ca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/ControlCenter.java @@ -29,8 +29,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms; -import nodomain.freeyourgadget.gadgetbridge.activities.SleepChartActivity; import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter; import nodomain.freeyourgadget.gadgetbridge.discovery.DiscoveryActivity; import nodomain.freeyourgadget.gadgetbridge.miband.MiBandConst; @@ -195,8 +195,8 @@ public class ControlCenter extends Activity { case R.id.controlcenter_start_sleepmonitor: if (selectedDevice != null) { Intent startIntent; - startIntent = new Intent(ControlCenter.this, SleepChartActivity.class); -// startIntent = new Intent(ControlCenter.this, ChartsActivity.class); +// startIntent = new Intent(ControlCenter.this, SleepChartActivity.class); + startIntent = new Intent(ControlCenter.this, ChartsActivity.class); startIntent.putExtra("device", selectedDevice); startActivity(startIntent); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractChartFragment.java new file mode 100644 index 00000000..71d32890 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractChartFragment.java @@ -0,0 +1,166 @@ +package nodomain.freeyourgadget.gadgetbridge.activities; + +import android.graphics.Color; +import android.support.v4.app.Fragment; + +import com.github.mikephil.charting.charts.BarLineChartBase; +import com.github.mikephil.charting.charts.Chart; +import com.github.mikephil.charting.data.BarDataSet; +import com.github.mikephil.charting.data.BarEntry; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBDevice; + +public class AbstractChartFragment extends Fragment { + public static final String ACTION_REFRESH + = "nodomain.freeyourgadget.gadgetbride.chart.action.refresh"; + + protected static final class ActivityKind { + public final byte type; + public final String label; + public final Integer color; + + public ActivityKind(byte type, String label, Integer color) { + this.type = type; + this.label = label; + this.color = color; + } + } + + protected ActivityKind akActivity = new ActivityKind(GBActivitySample.TYPE_UNKNOWN, "Activity", Color.rgb(89, 178, 44)); + protected ActivityKind akLightSleep = new ActivityKind(GBActivitySample.TYPE_LIGHT_SLEEP, "Light Sleep", Color.rgb(182, 191, 255)); + protected ActivityKind akDeepSleep = new ActivityKind(GBActivitySample.TYPE_DEEP_SLEEP, "Deep Sleep", Color.rgb(76, 90, 255)); + + protected static final int BACKGROUND_COLOR = Color.rgb(24, 22, 24); + protected static final int DESCRIPTION_COLOR = Color.WHITE; + protected static final int CHART_TEXT_COLOR = Color.WHITE; + protected static final int LEGEND_TEXT_COLOR = Color.WHITE; + + protected byte getProvider(GBDevice device) { + byte provider = -1; + switch (device.getType()) { + case MIBAND: + provider = GBActivitySample.PROVIDER_MIBAND; + break; + case PEBBLE: + provider = GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ; // FIXME + break; + } + return provider; + } + + protected ArrayList getSamples(GBDevice device, int tsFrom, int tsTo) { + if (tsFrom == -1) { + long ts = System.currentTimeMillis(); + tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours + } + + byte provider = getProvider(device); + return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); + } + + protected ArrayList getSleepSamples(GBDevice device, int tsFrom, int tsTo) { + if (tsFrom == -1) { + long ts = System.currentTimeMillis(); + tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours + } + + byte provider = getProvider(device); + return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); + } + + protected ArrayList getTestSamples(GBDevice device, int tsFrom, int tsTo) { + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(2015, Calendar.JUNE, 10, 6, 40); + // ignore provided date ranges + tsTo = (int) ((cal.getTimeInMillis() / 1000) & 0xffffffff); + tsFrom = tsTo - (24 * 60 * 60); + + byte provider = getProvider(device); + return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); + } + + protected void configureChartDefaults(Chart mChart) { + // if enabled, the chart will always start at zero on the y-axis + + // disable value highlighting + mChart.setHighlightEnabled(false); + + // enable touch gestures + mChart.setTouchEnabled(true); + } + + protected void configureBarLineChartDefaults(BarLineChartBase mChart) { + configureChartDefaults(mChart); + + // enable scaling and dragging + mChart.setDragEnabled(true); + mChart.setScaleEnabled(true); + + // if disabled, scaling can be done on x- and y-axis separately +// mChart.setPinchZoom(true); + + mChart.setDrawGridBackground(false); + } + + protected BarEntry createBarEntry(float value, int index) { + return new BarEntry(value, index); + } + + protected BarDataSet createActivitySet(List values, List colors, String label) { + BarDataSet set1 = new BarDataSet(values, label); + set1.setColors(colors); +// set1.setDrawCubic(true); +// set1.setCubicIntensity(0.2f); +// //set1.setDrawFilled(true); +// set1.setDrawCircles(false); +// set1.setLineWidth(2f); +// set1.setCircleSize(5f); +// set1.setFillColor(ColorTemplate.getHoloBlue()); + set1.setDrawValues(false); +// set1.setHighLightColor(Color.rgb(128, 0, 255)); +// set1.setColor(Color.rgb(89, 178, 44)); + set1.setValueTextColor(CHART_TEXT_COLOR); + return set1; + } + + protected BarDataSet createDeepSleepSet(List values, String label) { + BarDataSet set1 = new BarDataSet(values, label); +// set1.setDrawCubic(true); +// set1.setCubicIntensity(0.2f); +// //set1.setDrawFilled(true); +// set1.setDrawCircles(false); +// set1.setLineWidth(2f); +// set1.setCircleSize(5f); +// set1.setFillColor(ColorTemplate.getHoloBlue()); + set1.setDrawValues(false); +// set1.setHighLightColor(Color.rgb(244, 117, 117)); +// set1.setColor(Color.rgb(76, 90, 255)); + set1.setValueTextColor(CHART_TEXT_COLOR); + return set1; + } + + protected BarDataSet createLightSleepSet(List values, String label) { + BarDataSet set1 = new BarDataSet(values, label); + +// set1.setDrawCubic(true); +// set1.setCubicIntensity(0.2f); +// //set1.setDrawFilled(true); +// set1.setDrawCircles(false); +// set1.setLineWidth(2f); +// set1.setCircleSize(5f); +// set1.setFillColor(ColorTemplate.getHoloBlue()); + set1.setDrawValues(false); +// set1.setHighLightColor(Color.rgb(244, 117, 117)); +// set1.setColor(Color.rgb(182, 191, 255)); + set1.setValueTextColor(CHART_TEXT_COLOR); +// set1.setColor(Color.CYAN); + return set1; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySleepChartFragment.java similarity index 57% rename from app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartActivity.java rename to app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySleepChartFragment.java index a74b1b30..68b0c3b8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySleepChartFragment.java @@ -1,15 +1,16 @@ package nodomain.freeyourgadget.gadgetbridge.activities; -import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.os.Bundle; -import android.support.v4.app.NavUtils; +import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; -import android.view.MenuItem; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; import com.github.mikephil.charting.animation.Easing; import com.github.mikephil.charting.charts.BarLineChartBase; @@ -33,27 +34,11 @@ import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils; -public class SleepChartActivity extends Activity { - private static final float Y_VALUE_DEEP_SLEEP = 0.01f; - private static final float Y_VALUE_LIGHT_SLEEP = 0.016f; - - private static final class ActivityKind { - public final byte type; - public final String label; - public final Integer color; - - public ActivityKind(byte type, String label, Integer color) { - this.type = type; - this.label = label; - this.color = color; - } - } - - public static final String ACTION_REFRESH - = "nodomain.freeyourgadget.gadgetbride.chart.action.refresh"; - protected static final Logger LOG = LoggerFactory.getLogger(SleepChartActivity.class); +public class ActivitySleepChartFragment extends AbstractChartFragment { + protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class); private BarLineChartBase mChart; @@ -63,17 +48,11 @@ public class SleepChartActivity extends Activity { private int mSmartAlarmGoneOff = -1; private GBDevice mGBDevice = null; - private ActivityKind akActivity = new ActivityKind(GBActivitySample.TYPE_UNKNOWN, "Activity", Color.rgb(89, 178, 44)); - private ActivityKind akLightSleep = new ActivityKind(GBActivitySample.TYPE_LIGHT_SLEEP, "Light Sleep", Color.rgb(182, 191, 255)); - private ActivityKind akDeepSleep = new ActivityKind(GBActivitySample.TYPE_DEEP_SLEEP, "Deep Sleep", Color.rgb(76, 90, 255)); - private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(ControlCenter.ACTION_QUIT)) { - finish(); - } else if (action.equals(ACTION_REFRESH)) { + if (action.equals(ACTION_REFRESH)) { // TODO: use LimitLines to visualize smart alarms? mSmartAlarmFrom = intent.getIntExtra("smartalarm_from", -1); mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1); @@ -85,55 +64,39 @@ public class SleepChartActivity extends Activity { }; @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_charts, container, false); - Bundle extras = getIntent().getExtras(); + Bundle extras = getActivity().getIntent().getExtras(); if (extras != null) { - mGBDevice = extras.getParcelable("device"); + mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE); } - getActionBar().setDisplayHomeAsUpEnabled(true); - IntentFilter filter = new IntentFilter(); filter.addAction(ControlCenter.ACTION_QUIT); filter.addAction(ACTION_REFRESH); - LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); + LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter); -// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, -// WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_sleepmonitor2); + mChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart); - mChart = (BarLineChartBase) findViewById(R.id.sleepchart2); - mChart.setBackgroundColor(Color.rgb(24, 22, 24)); - mChart.setDescriptionColor(Color.WHITE); + setupChart(); - // if enabled, the chart will always start at zero on the y-axis + return rootView; + } - // disable value highlighting - mChart.setHighlightEnabled(false); + private void setupChart() { + mChart.setBackgroundColor(BACKGROUND_COLOR); + mChart.setDescriptionColor(DESCRIPTION_COLOR); + configureBarLineChartDefaults(mChart); - // enable touch gestures - mChart.setTouchEnabled(true); - - // enable scaling and dragging - mChart.setDragEnabled(true); - mChart.setScaleEnabled(true); - - // if disabled, scaling can be done on x- and y-axis separately -// mChart.setPinchZoom(true); - - mChart.setDrawGridBackground(false); - -// tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); XAxis x = mChart.getXAxis(); x.setDrawLabels(true); x.setDrawGridLines(false); -// x.setTypeface(tf); x.setEnabled(true); - x.setTextColor(Color.WHITE); + x.setTextColor(CHART_TEXT_COLOR); x.setDrawLimitLinesBehindData(true); YAxis y = mChart.getAxisLeft(); @@ -142,9 +105,8 @@ public class SleepChartActivity extends Activity { // TODO: make fixed max value optional y.setAxisMaxValue(1f); y.setDrawTopYLabelEntry(false); - y.setTextColor(Color.WHITE); + y.setTextColor(CHART_TEXT_COLOR); -// y.setTypeface(tf); // y.setLabelCount(5); y.setEnabled(true); @@ -153,60 +115,25 @@ public class SleepChartActivity extends Activity { yAxisRight.setEnabled(false); yAxisRight.setDrawLabels(false); yAxisRight.setDrawTopYLabelEntry(false); - yAxisRight.setTextColor(Color.WHITE); + yAxisRight.setTextColor(CHART_TEXT_COLOR); refresh(); - mChart.getLegend().setTextColor(Color.WHITE); + mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR); // mChart.getLegend().setEnabled(false); // // mChart.animateXY(2000, 2000); - // dont forget to refresh the drawing + // don't forget to refresh the drawing mChart.invalidate(); } @Override - protected void onDestroy() { - LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); + public void onDestroy() { + LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver); super.onDestroy(); } - private byte getProvider(GBDevice device) { - byte provider = -1; - switch (device.getType()) { - case MIBAND: - provider = GBActivitySample.PROVIDER_MIBAND; - break; - case PEBBLE: - provider = GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ; // FIXME - break; - } - return provider; - } - - private ArrayList getSamples(GBDevice device, int tsFrom, int tsTo) { - if (tsFrom == -1) { - long ts = System.currentTimeMillis(); - tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours - } - - byte provider = getProvider(device); - return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); - } - - private ArrayList getTestSamples(GBDevice device, int tsFrom, int tsTo) { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(2015, Calendar.JUNE, 10, 6, 40); - // ignore provided date ranges - tsTo = (int) ((cal.getTimeInMillis() / 1000) & 0xffffffff); - tsFrom = tsTo - (24 * 60 * 60); - - byte provider = getProvider(device); - return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); - } - private void refresh() { if (mGBDevice == null) { return; @@ -270,15 +197,15 @@ public class SleepChartActivity extends Activity { if (type == GBActivitySample.TYPE_DEEP_SLEEP) { // value = Y_VALUE_DEEP_SLEEP; value = ((float) movement) / movement_divisor; - value += Y_VALUE_DEEP_SLEEP; - activityEntries.add(createEntry(value, i)); + value += SleepUtils.Y_VALUE_DEEP_SLEEP; + activityEntries.add(createBarEntry(value, i)); colors.add(akDeepSleep.color); } else { if (type == GBActivitySample.TYPE_LIGHT_SLEEP) { value = ((float) movement) / movement_divisor; -// value += Y_VALUE_LIGHT_SLEEP; +// value += SleepUtils.Y_VALUE_LIGHT_SLEEP; // value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP); - activityEntries.add(createEntry(value, i)); + activityEntries.add(createBarEntry(value, i)); colors.add(akLightSleep.color); } else { byte steps = sample.getSteps(); @@ -287,7 +214,7 @@ public class SleepChartActivity extends Activity { movement = steps; } value = ((float) movement) / movement_divisor; - activityEntries.add(createEntry(value, i)); + activityEntries.add(createBarEntry(value, i)); colors.add(akActivity.color); } } @@ -343,16 +270,12 @@ public class SleepChartActivity extends Activity { mChart.setData(data); - mChart.animateX(1000, Easing.EasingOption.EaseInOutQuart); + mChart.animateX(500, Easing.EasingOption.EaseInOutQuart); // textView.setText(dateStringFrom + " to " + dateStringTo); } } - private boolean isSleep(byte type) { - return type == GBActivitySample.TYPE_DEEP_SLEEP || type == GBActivitySample.TYPE_LIGHT_SLEEP; - } - private void setupLegend(BarLineChartBase chart) { List legendColors = new ArrayList<>(3); List legendLabels = new ArrayList<>(3); @@ -365,69 +288,4 @@ public class SleepChartActivity extends Activity { chart.getLegend().setColors(legendColors); chart.getLegend().setLabels(legendLabels); } - - private BarEntry createEntry(float value, int index) { - return new BarEntry(value, index); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - NavUtils.navigateUpFromSameTask(this); - return true; - } - return super.onOptionsItemSelected(item); - } - - private BarDataSet createActivitySet(List values, List colors, String label) { - BarDataSet set1 = new BarDataSet(values, label); - set1.setColors(colors); -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(128, 0, 255)); -// set1.setColor(Color.rgb(89, 178, 44)); - set1.setValueTextColor(Color.WHITE); - return set1; - } - - private BarDataSet createDeepSleepSet(List values, String label) { - BarDataSet set1 = new BarDataSet(values, label); -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(244, 117, 117)); -// set1.setColor(Color.rgb(76, 90, 255)); - set1.setValueTextColor(Color.WHITE); - return set1; - } - - private BarDataSet createLightSleepSet(List values, String label) { - BarDataSet set1 = new BarDataSet(values, label); - -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(244, 117, 117)); -// set1.setColor(Color.rgb(182, 191, 255)); - set1.setValueTextColor(Color.WHITE); -// set1.setColor(Color.CYAN); - return set1; - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ChartsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ChartsActivity.java index 2371d352..23ca8b42 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ChartsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ChartsActivity.java @@ -51,29 +51,6 @@ public class ChartsActivity extends FragmentActivity { } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_charts, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); - } - /** * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. @@ -86,8 +63,8 @@ public class ChartsActivity extends FragmentActivity { @Override public Fragment getItem(int position) { - if (position == 1) { - return new SleepChartFragment(); + if (position == 0) { + return new ActivitySleepChartFragment(); } // getItem is called to instantiate the fragment for the given page. @@ -97,8 +74,9 @@ public class ChartsActivity extends FragmentActivity { @Override public int getCount() { + return 1; // Show 3 total pages. - return 3; +// return 3; } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartFragment.java index 54b46dc2..e100ac54 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SleepChartFragment.java @@ -34,27 +34,11 @@ import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils; -public class SleepChartFragment extends Fragment { - private static final float Y_VALUE_DEEP_SLEEP = 0.01f; - private static final float Y_VALUE_LIGHT_SLEEP = 0.016f; - - private static final class ActivityKind { - public final byte type; - public final String label; - public final Integer color; - - public ActivityKind(byte type, String label, Integer color) { - this.type = type; - this.label = label; - this.color = color; - } - } - - public static final String ACTION_REFRESH - = "nodomain.freeyourgadget.gadgetbride.chart.action.refresh"; - protected static final Logger LOG = LoggerFactory.getLogger(SleepChartFragment.class); +public class SleepChartFragment extends AbstractChartFragment { + protected static final Logger LOG = LoggerFactory.getLogger(ActivitySleepChartFragment.class); private BarLineChartBase mChart; @@ -64,10 +48,6 @@ public class SleepChartFragment extends Fragment { private int mSmartAlarmGoneOff = -1; private GBDevice mGBDevice = null; - private ActivityKind akActivity = new ActivityKind(GBActivitySample.TYPE_UNKNOWN, "Activity", Color.rgb(89, 178, 44)); - private ActivityKind akLightSleep = new ActivityKind(GBActivitySample.TYPE_LIGHT_SLEEP, "Light Sleep", Color.rgb(182, 191, 255)); - private ActivityKind akDeepSleep = new ActivityKind(GBActivitySample.TYPE_DEEP_SLEEP, "Deep Sleep", Color.rgb(76, 90, 255)); - private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -88,11 +68,9 @@ public class SleepChartFragment extends Fragment { Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_charts, container, false); -// setContentView(R.layout.activity_sleepmonitor2); - Bundle extras = getActivity().getIntent().getExtras(); if (extras != null) { - mGBDevice = extras.getParcelable("device"); + mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE); } IntentFilter filter = new IntentFilter(); @@ -109,34 +87,15 @@ public class SleepChartFragment extends Fragment { } private void setupChart() { - mChart.setBackgroundColor(Color.rgb(24, 22, 24)); - mChart.setDescriptionColor(Color.WHITE); + mChart.setBackgroundColor(BACKGROUND_COLOR); + mChart.setDescriptionColor(DESCRIPTION_COLOR); - // if enabled, the chart will always start at zero on the y-axis - - // disable value highlighting - mChart.setHighlightEnabled(false); - - // enable touch gestures - mChart.setTouchEnabled(true); - - // enable scaling and dragging - mChart.setDragEnabled(true); - mChart.setScaleEnabled(true); - - // if disabled, scaling can be done on x- and y-axis separately -// mChart.setPinchZoom(true); - - mChart.setDrawGridBackground(false); - -// tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); XAxis x = mChart.getXAxis(); x.setDrawLabels(true); x.setDrawGridLines(false); -// x.setTypeface(tf); x.setEnabled(true); - x.setTextColor(Color.WHITE); + x.setTextColor(CHART_TEXT_COLOR); x.setDrawLimitLinesBehindData(true); YAxis y = mChart.getAxisLeft(); @@ -145,9 +104,8 @@ public class SleepChartFragment extends Fragment { // TODO: make fixed max value optional y.setAxisMaxValue(1f); y.setDrawTopYLabelEntry(false); - y.setTextColor(Color.WHITE); + y.setTextColor(CHART_TEXT_COLOR); -// y.setTypeface(tf); // y.setLabelCount(5); y.setEnabled(true); @@ -156,16 +114,16 @@ public class SleepChartFragment extends Fragment { yAxisRight.setEnabled(false); yAxisRight.setDrawLabels(false); yAxisRight.setDrawTopYLabelEntry(false); - yAxisRight.setTextColor(Color.WHITE); + yAxisRight.setTextColor(CHART_TEXT_COLOR); refresh(); - mChart.getLegend().setTextColor(Color.WHITE); + mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR); // mChart.getLegend().setEnabled(false); // // mChart.animateXY(2000, 2000); - // dont forget to refresh the drawing + // don't forget to refresh the drawing mChart.invalidate(); } @@ -175,48 +133,13 @@ public class SleepChartFragment extends Fragment { super.onDestroy(); } - private byte getProvider(GBDevice device) { - byte provider = -1; - switch (device.getType()) { - case MIBAND: - provider = GBActivitySample.PROVIDER_MIBAND; - break; - case PEBBLE: - provider = GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ; // FIXME - break; - } - return provider; - } - - private ArrayList getSamples(GBDevice device, int tsFrom, int tsTo) { - if (tsFrom == -1) { - long ts = System.currentTimeMillis(); - tsFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours - } - - byte provider = getProvider(device); - return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); - } - - private ArrayList getTestSamples(GBDevice device, int tsFrom, int tsTo) { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(2015, Calendar.JUNE, 10, 6, 40); - // ignore provided date ranges - tsTo = (int) ((cal.getTimeInMillis() / 1000) & 0xffffffff); - tsFrom = tsTo - (24 * 60 * 60); - - byte provider = getProvider(device); - return GBApplication.getActivityDatabaseHandler().getGBActivitySamples(tsFrom, tsTo, provider); - } - private void refresh() { if (mGBDevice == null) { return; } // ArrayList samples = getTestSamples(mGBDevice, -1, -1); - ArrayList samples = getSamples(mGBDevice, -1, -1); + ArrayList samples = getSleepSamples(mGBDevice, -1, -1); Calendar cal = Calendar.getInstance(); cal.clear(); @@ -273,15 +196,15 @@ public class SleepChartFragment extends Fragment { if (type == GBActivitySample.TYPE_DEEP_SLEEP) { // value = Y_VALUE_DEEP_SLEEP; value = ((float) movement) / movement_divisor; - value += Y_VALUE_DEEP_SLEEP; - activityEntries.add(createEntry(value, i)); + value += SleepUtils.Y_VALUE_DEEP_SLEEP; + activityEntries.add(createBarEntry(value, i)); colors.add(akDeepSleep.color); } else { if (type == GBActivitySample.TYPE_LIGHT_SLEEP) { value = ((float) movement) / movement_divisor; -// value += Y_VALUE_LIGHT_SLEEP; +// value += SleepUtils.Y_VALUE_LIGHT_SLEEP; // value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP); - activityEntries.add(createEntry(value, i)); + activityEntries.add(createBarEntry(value, i)); colors.add(akLightSleep.color); } else { byte steps = sample.getSteps(); @@ -290,7 +213,7 @@ public class SleepChartFragment extends Fragment { movement = steps; } value = ((float) movement) / movement_divisor; - activityEntries.add(createEntry(value, i)); + activityEntries.add(createBarEntry(value, i)); colors.add(akActivity.color); } } @@ -346,16 +269,12 @@ public class SleepChartFragment extends Fragment { mChart.setData(data); - mChart.animateX(1000, Easing.EasingOption.EaseInOutQuart); + mChart.animateX(500, Easing.EasingOption.EaseInOutQuart); // textView.setText(dateStringFrom + " to " + dateStringTo); } } - private boolean isSleep(byte type) { - return type == GBActivitySample.TYPE_DEEP_SLEEP || type == GBActivitySample.TYPE_LIGHT_SLEEP; - } - private void setupLegend(BarLineChartBase chart) { List legendColors = new ArrayList<>(3); List legendLabels = new ArrayList<>(3); @@ -368,59 +287,4 @@ public class SleepChartFragment extends Fragment { chart.getLegend().setColors(legendColors); chart.getLegend().setLabels(legendLabels); } - - private BarEntry createEntry(float value, int index) { - return new BarEntry(value, index); - } - - private BarDataSet createActivitySet(List values, List colors, String label) { - BarDataSet set1 = new BarDataSet(values, label); - set1.setColors(colors); -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(128, 0, 255)); -// set1.setColor(Color.rgb(89, 178, 44)); - set1.setValueTextColor(Color.WHITE); - return set1; - } - - private BarDataSet createDeepSleepSet(List values, String label) { - BarDataSet set1 = new BarDataSet(values, label); -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(244, 117, 117)); -// set1.setColor(Color.rgb(76, 90, 255)); - set1.setValueTextColor(Color.WHITE); - return set1; - } - - private BarDataSet createLightSleepSet(List values, String label) { - BarDataSet set1 = new BarDataSet(values, label); - -// set1.setDrawCubic(true); -// set1.setCubicIntensity(0.2f); -// //set1.setDrawFilled(true); -// set1.setDrawCircles(false); -// set1.setLineWidth(2f); -// set1.setCircleSize(5f); -// set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setDrawValues(false); -// set1.setHighLightColor(Color.rgb(244, 117, 117)); -// set1.setColor(Color.rgb(182, 191, 255)); - set1.setValueTextColor(Color.WHITE); -// set1.setColor(Color.CYAN); - return set1; - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/charts/SleepUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/charts/SleepUtils.java new file mode 100644 index 00000000..7860dd27 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/charts/SleepUtils.java @@ -0,0 +1,12 @@ +package nodomain.freeyourgadget.gadgetbridge.charts; + +import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; + +public class SleepUtils { + public static final float Y_VALUE_DEEP_SLEEP = 0.01f; + public static final float Y_VALUE_LIGHT_SLEEP = 0.016f; + + public static final boolean isSleep(byte type) { + return type == GBActivitySample.TYPE_DEEP_SLEEP || type == GBActivitySample.TYPE_LIGHT_SLEEP; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java index c049ff79..94557438 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java @@ -120,7 +120,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper { String selectQuery = "SELECT * FROM " + TABLE_GBACTIVITYSAMPLES + " where (provider=" + provider + " and timestamp>=" + timestamp_from + " and timestamp<=" + timestamp_to + ") order by timestamp"; - try (SQLiteDatabase db = this.getWritableDatabase()) { + try (SQLiteDatabase db = this.getReadableDatabase()) { Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { diff --git a/app/src/main/res/layout/fragment_charts.xml b/app/src/main/res/layout/fragment_charts.xml index 549b3bc5..bdd6ffc2 100644 --- a/app/src/main/res/layout/fragment_charts.xml +++ b/app/src/main/res/layout/fragment_charts.xml @@ -1,9 +1,6 @@