Initial work on supporting multiple charts.
- one fragment per chart screen - common chart code should move to fragment baseclass and the host Activity (ChartsActivity) Currently it's not used, change ControlCenter to invoke ChartsActivity instead of SleepChartActivity to test it. WIP for #79
This commit is contained in:
parent
5ae812c854
commit
4b241ca9eb
|
@ -16,8 +16,16 @@
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
<uses-permission android:name="com.fsck.k9.permission.READ_MESSAGES" />
|
<uses-permission android:name="com.fsck.k9.permission.READ_MESSAGES" />
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
|
<uses-feature
|
||||||
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
|
android:name="android.hardware.bluetooth"
|
||||||
|
android:required="true" />
|
||||||
|
<uses-feature
|
||||||
|
android:name="android.hardware.bluetooth_le"
|
||||||
|
android:required="false" />
|
||||||
|
|
||||||
|
<android:uses-permission
|
||||||
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="18" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".GBApplication"
|
android:name=".GBApplication"
|
||||||
|
@ -108,7 +116,8 @@
|
||||||
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
<receiver android:name=".externalevents.TimeChangeReceiver"
|
<receiver
|
||||||
|
android:name=".externalevents.TimeChangeReceiver"
|
||||||
android:enabled="false" >
|
android:enabled="false" >
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
|
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
|
||||||
|
@ -183,6 +192,14 @@
|
||||||
android:name=".miband.MiBandPairingActivity"
|
android:name=".miband.MiBandPairingActivity"
|
||||||
android:label="@string/title_activity_mi_band_pairing" >
|
android:label="@string/title_activity_mi_band_pairing" >
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name=".activities.ChartsActivity"
|
||||||
|
android:label="@string/title_activity_charts"
|
||||||
|
android:parentActivityName=".ControlCenter" >
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.PARENT_ACTIVITY"
|
||||||
|
android:value="nodomain.freeyourgadget.gadgetbridge.ControlCenter" />
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
|
@ -29,6 +29,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.activities.SleepChartActivity;
|
import nodomain.freeyourgadget.gadgetbridge.activities.SleepChartActivity;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.discovery.DiscoveryActivity;
|
import nodomain.freeyourgadget.gadgetbridge.discovery.DiscoveryActivity;
|
||||||
|
@ -190,6 +191,7 @@ public class ControlCenter extends Activity {
|
||||||
if (selectedDevice != null) {
|
if (selectedDevice != null) {
|
||||||
Intent startIntent;
|
Intent startIntent;
|
||||||
startIntent = new Intent(ControlCenter.this, SleepChartActivity.class);
|
startIntent = new Intent(ControlCenter.this, SleepChartActivity.class);
|
||||||
|
// startIntent = new Intent(ControlCenter.this, ChartsActivity.class);
|
||||||
startIntent.putExtra("device", selectedDevice);
|
startIntent.putExtra("device", selectedDevice);
|
||||||
startActivity(startIntent);
|
startActivity(startIntent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,160 @@
|
||||||
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import android.support.v4.app.FragmentActivity;
|
||||||
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||||
|
import android.support.v4.app.NavUtils;
|
||||||
|
import android.support.v7.app.ActionBarActivity;
|
||||||
|
import android.support.v7.app.ActionBar;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
import android.support.v4.app.FragmentManager;
|
||||||
|
import android.support.v4.app.FragmentTransaction;
|
||||||
|
import android.support.v4.app.FragmentPagerAdapter;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.view.ViewPager;
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||||
|
|
||||||
|
public class ChartsActivity extends FragmentActivity {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||||
|
* fragments for each of the sections. We use a
|
||||||
|
* {@link FragmentPagerAdapter} derivative, which will keep every
|
||||||
|
* loaded fragment in memory. If this becomes too memory intensive, it
|
||||||
|
* may be best to switch to a
|
||||||
|
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
|
||||||
|
*/
|
||||||
|
SectionsPagerAdapter mSectionsPagerAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link ViewPager} that will host the section contents.
|
||||||
|
*/
|
||||||
|
ViewPager mViewPager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_charts);
|
||||||
|
|
||||||
|
|
||||||
|
// Create the adapter that will return a fragment for each of the three
|
||||||
|
// primary sections of the activity.
|
||||||
|
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
||||||
|
|
||||||
|
// Set up the ViewPager with the sections adapter.
|
||||||
|
mViewPager = (ViewPager) findViewById(R.id.pager);
|
||||||
|
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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.
|
||||||
|
*/
|
||||||
|
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
|
||||||
|
|
||||||
|
public SectionsPagerAdapter(FragmentManager fm) {
|
||||||
|
super(fm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Fragment getItem(int position) {
|
||||||
|
if (position == 1) {
|
||||||
|
return new SleepChartFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
// getItem is called to instantiate the fragment for the given page.
|
||||||
|
// Return a PlaceholderFragment (defined as a static inner class below).
|
||||||
|
return PlaceholderFragment.newInstance(position + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
// Show 3 total pages.
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence getPageTitle(int position) {
|
||||||
|
Locale l = Locale.getDefault();
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
return "Today".toUpperCase(l);
|
||||||
|
case 1:
|
||||||
|
return "Week".toUpperCase(l);
|
||||||
|
case 2:
|
||||||
|
return "Month".toUpperCase(l);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A placeholder fragment containing a simple view.
|
||||||
|
*/
|
||||||
|
public static class PlaceholderFragment extends Fragment {
|
||||||
|
/**
|
||||||
|
* The fragment argument representing the section number for this
|
||||||
|
* fragment.
|
||||||
|
*/
|
||||||
|
private static final String ARG_SECTION_NUMBER = "section_number";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new instance of this fragment for the given section
|
||||||
|
* number.
|
||||||
|
*/
|
||||||
|
public static PlaceholderFragment newInstance(int sectionNumber) {
|
||||||
|
PlaceholderFragment fragment = new PlaceholderFragment();
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlaceholderFragment() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
View rootView = inflater.inflate(R.layout.fragment_dummy, container, false);
|
||||||
|
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
|
||||||
|
int number = getArguments().getInt(ARG_SECTION_NUMBER);
|
||||||
|
textView.setText("Fragment " + number);
|
||||||
|
return rootView;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,426 @@
|
||||||
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||||
|
|
||||||
|
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.Fragment;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
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;
|
||||||
|
import com.github.mikephil.charting.components.XAxis;
|
||||||
|
import com.github.mikephil.charting.components.YAxis;
|
||||||
|
import com.github.mikephil.charting.data.BarData;
|
||||||
|
import com.github.mikephil.charting.data.BarDataSet;
|
||||||
|
import com.github.mikephil.charting.data.BarEntry;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
private BarLineChartBase mChart;
|
||||||
|
|
||||||
|
private int mSmartAlarmFrom = -1;
|
||||||
|
private int mSmartAlarmTo = -1;
|
||||||
|
private int mTimestampFrom = -1;
|
||||||
|
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(ACTION_REFRESH)) {
|
||||||
|
// TODO: use LimitLines to visualize smart alarms?
|
||||||
|
mSmartAlarmFrom = intent.getIntExtra("smartalarm_from", -1);
|
||||||
|
mSmartAlarmTo = intent.getIntExtra("smartalarm_to", -1);
|
||||||
|
mTimestampFrom = intent.getIntExtra("recording_base_timestamp", -1);
|
||||||
|
mSmartAlarmGoneOff = intent.getIntExtra("alarm_gone_off", -1);
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
||||||
|
filter.addAction(ACTION_REFRESH);
|
||||||
|
|
||||||
|
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);
|
||||||
|
|
||||||
|
mChart = (BarLineChartBase) rootView.findViewById(R.id.sleepchart);
|
||||||
|
|
||||||
|
setupChart();
|
||||||
|
|
||||||
|
return rootView;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupChart() {
|
||||||
|
mChart.setBackgroundColor(Color.rgb(24, 22, 24));
|
||||||
|
mChart.setDescriptionColor(Color.WHITE);
|
||||||
|
|
||||||
|
// 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.setDrawLimitLinesBehindData(true);
|
||||||
|
|
||||||
|
YAxis y = mChart.getAxisLeft();
|
||||||
|
y.setDrawGridLines(false);
|
||||||
|
// y.setDrawLabels(false);
|
||||||
|
// TODO: make fixed max value optional
|
||||||
|
y.setAxisMaxValue(1f);
|
||||||
|
y.setDrawTopYLabelEntry(false);
|
||||||
|
y.setTextColor(Color.WHITE);
|
||||||
|
|
||||||
|
// y.setTypeface(tf);
|
||||||
|
// y.setLabelCount(5);
|
||||||
|
y.setEnabled(true);
|
||||||
|
|
||||||
|
YAxis yAxisRight = mChart.getAxisRight();
|
||||||
|
yAxisRight.setDrawGridLines(false);
|
||||||
|
yAxisRight.setEnabled(false);
|
||||||
|
yAxisRight.setDrawLabels(false);
|
||||||
|
yAxisRight.setDrawTopYLabelEntry(false);
|
||||||
|
yAxisRight.setTextColor(Color.WHITE);
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
|
||||||
|
mChart.getLegend().setTextColor(Color.WHITE);
|
||||||
|
// mChart.getLegend().setEnabled(false);
|
||||||
|
//
|
||||||
|
// mChart.animateXY(2000, 2000);
|
||||||
|
|
||||||
|
// dont forget to refresh the drawing
|
||||||
|
mChart.invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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<GBActivitySample> 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<GBActivitySample> 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<GBActivitySample> samples = getTestSamples(mGBDevice, -1, -1);
|
||||||
|
ArrayList<GBActivitySample> samples = getSamples(mGBDevice, -1, -1);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.clear();
|
||||||
|
Date date;
|
||||||
|
String dateStringFrom = "";
|
||||||
|
String dateStringTo = "";
|
||||||
|
|
||||||
|
LOG.info("number of samples:" + samples.size());
|
||||||
|
if (samples.size() > 1) {
|
||||||
|
float movement_divisor;
|
||||||
|
boolean annotate = true;
|
||||||
|
boolean use_steps_as_movement;
|
||||||
|
switch (getProvider(mGBDevice)) {
|
||||||
|
case GBActivitySample.PROVIDER_MIBAND:
|
||||||
|
movement_divisor = 256.0f;
|
||||||
|
use_steps_as_movement = true;
|
||||||
|
break;
|
||||||
|
default: // Morpheuz
|
||||||
|
movement_divisor = 5000.0f;
|
||||||
|
use_steps_as_movement = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte last_type = GBActivitySample.TYPE_UNKNOWN;
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
|
||||||
|
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm");
|
||||||
|
|
||||||
|
int numEntries = samples.size();
|
||||||
|
List<String> xLabels = new ArrayList<>(numEntries);
|
||||||
|
// List<BarEntry> deepSleepEntries = new ArrayList<>(numEntries / 4);
|
||||||
|
// List<BarEntry> lightSleepEntries = new ArrayList<>(numEntries / 4);
|
||||||
|
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
|
||||||
|
List<Integer> colors = new ArrayList<>(numEntries); // this is kinda inefficient...
|
||||||
|
|
||||||
|
for (int i = 0; i < numEntries; i++) {
|
||||||
|
GBActivitySample sample = samples.get(i);
|
||||||
|
byte type = sample.getType();
|
||||||
|
|
||||||
|
// determine start and end dates
|
||||||
|
if (i == 0) {
|
||||||
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L); // make sure it's converted to long
|
||||||
|
date = cal.getTime();
|
||||||
|
dateStringFrom = dateFormat.format(date);
|
||||||
|
} else if (i == samples.size() - 1) {
|
||||||
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L); // same here
|
||||||
|
date = cal.getTime();
|
||||||
|
dateStringTo = dateFormat.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
short movement = sample.getIntensity();
|
||||||
|
|
||||||
|
float value;
|
||||||
|
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));
|
||||||
|
colors.add(akDeepSleep.color);
|
||||||
|
} else {
|
||||||
|
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
|
||||||
|
value = ((float) movement) / movement_divisor;
|
||||||
|
// value += Y_VALUE_LIGHT_SLEEP;
|
||||||
|
// value = Math.min(1.0f, Y_VALUE_LIGHT_SLEEP);
|
||||||
|
activityEntries.add(createEntry(value, i));
|
||||||
|
colors.add(akLightSleep.color);
|
||||||
|
} else {
|
||||||
|
byte steps = sample.getSteps();
|
||||||
|
if (use_steps_as_movement && steps != 0) {
|
||||||
|
// I'm not sure using steps for this is actually a good idea
|
||||||
|
movement = steps;
|
||||||
|
}
|
||||||
|
value = ((float) movement) / movement_divisor;
|
||||||
|
activityEntries.add(createEntry(value, i));
|
||||||
|
colors.add(akActivity.color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String xLabel = "";
|
||||||
|
if (annotate) {
|
||||||
|
cal.setTimeInMillis(sample.getTimestamp() * 1000L);
|
||||||
|
date = cal.getTime();
|
||||||
|
String dateString = annotationDateFormat.format(date);
|
||||||
|
xLabel = dateString;
|
||||||
|
// if (last_type != type) {
|
||||||
|
// if (isSleep(last_type) && !isSleep(type)) {
|
||||||
|
// // woken up
|
||||||
|
// LimitLine line = new LimitLine(i, dateString);
|
||||||
|
// line.enableDashedLine(8, 8, 0);
|
||||||
|
// line.setTextColor(Color.WHITE);
|
||||||
|
// line.setTextSize(15);
|
||||||
|
// mChart.getXAxis().addLimitLine(line);
|
||||||
|
// } else if (!isSleep(last_type) && isSleep(type)) {
|
||||||
|
// // fallen asleep
|
||||||
|
// LimitLine line = new LimitLine(i, dateString);
|
||||||
|
// line.enableDashedLine(8, 8, 0);
|
||||||
|
// line.setTextSize(15);
|
||||||
|
// line.setTextColor(Color.WHITE);
|
||||||
|
// mChart.getXAxis().addLimitLine(line);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
last_type = type;
|
||||||
|
}
|
||||||
|
xLabels.add(xLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
mChart.getXAxis().setValues(xLabels);
|
||||||
|
|
||||||
|
// BarDataSet deepSleepSet = createDeepSleepSet(deepSleepEntries, "Deep Sleep");
|
||||||
|
// BarDataSet lightSleepSet = createLightSleepSet(lightSleepEntries, "Light Sleep");
|
||||||
|
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
|
||||||
|
|
||||||
|
ArrayList<BarDataSet> dataSets = new ArrayList<>();
|
||||||
|
// dataSets.add(deepSleepSet);
|
||||||
|
// dataSets.add(lightSleepSet);
|
||||||
|
dataSets.add(activitySet);
|
||||||
|
|
||||||
|
// create a data object with the datasets
|
||||||
|
BarData data = new BarData(xLabels, dataSets);
|
||||||
|
data.setGroupSpace(0);
|
||||||
|
|
||||||
|
mChart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
|
||||||
|
// mChart.setDescriptionPosition(?, ?);
|
||||||
|
// set data
|
||||||
|
|
||||||
|
setupLegend(mChart);
|
||||||
|
|
||||||
|
mChart.setData(data);
|
||||||
|
|
||||||
|
mChart.animateX(1000, 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<Integer> legendColors = new ArrayList<>(3);
|
||||||
|
List<String> legendLabels = new ArrayList<>(3);
|
||||||
|
legendColors.add(akActivity.color);
|
||||||
|
legendLabels.add(akActivity.label);
|
||||||
|
legendColors.add(akLightSleep.color);
|
||||||
|
legendLabels.add(akLightSleep.label);
|
||||||
|
legendColors.add(akDeepSleep.color);
|
||||||
|
legendLabels.add(akDeepSleep.label);
|
||||||
|
chart.getLegend().setColors(legendColors);
|
||||||
|
chart.getLegend().setLabels(legendLabels);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BarEntry createEntry(float value, int index) {
|
||||||
|
return new BarEntry(value, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BarDataSet createActivitySet(List<BarEntry> values, List<Integer> 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<BarEntry> 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<BarEntry> 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
|
||||||
|
android:layout_width="match_parent" android:layout_height="match_parent"
|
||||||
|
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity" />
|
|
@ -1,16 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:orientation="vertical" android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:weightSum="1">
|
|
||||||
|
|
||||||
<SurfaceView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="200dp"
|
|
||||||
android:id="@+id/surfaceView" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:id="@+id/textView" />
|
|
||||||
</LinearLayout>
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
|
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity$PlaceholderFragment">
|
||||||
|
|
||||||
|
<nodomain.freeyourgadget.gadgetbridge.charts.CustomBarChart
|
||||||
|
android:id="@+id/sleepchart"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
/>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
|
@ -0,0 +1,12 @@
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
|
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity$PlaceholderFragment">
|
||||||
|
|
||||||
|
<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.ChartsActivity">
|
||||||
|
<item android:id="@+id/action_settings" android:title="@string/action_settings"
|
||||||
|
android:orderInCategory="100" app:showAsAction="never" />
|
||||||
|
</menu>
|
|
@ -136,5 +136,12 @@
|
||||||
<string name="control_center_find_device">Find Device…</string>
|
<string name="control_center_find_device">Find Device…</string>
|
||||||
<string name="control_center_find_lost_device">Find lost Device</string>
|
<string name="control_center_find_lost_device">Find lost Device</string>
|
||||||
<string name="control_center_cancel_to_stop_vibration">Cancel to stop vibration.</string>
|
<string name="control_center_cancel_to_stop_vibration">Cancel to stop vibration.</string>
|
||||||
|
<string name="title_activity_charts">ChartsActivity</string>
|
||||||
|
|
||||||
|
<string name="title_section1">Section 1</string>
|
||||||
|
<string name="title_section2">Section 2</string>
|
||||||
|
<string name="title_section3">Section 3</string>
|
||||||
|
|
||||||
|
<string name="hello_world">Hello world!</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue