2015-05-21 18:17:39 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Paint;
|
2015-05-27 00:59:05 +02:00
|
|
|
import android.graphics.RectF;
|
2015-05-21 18:17:39 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.SurfaceHolder;
|
|
|
|
import android.view.SurfaceView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
2015-05-30 17:28:03 +02:00
|
|
|
import java.util.ArrayList;
|
2015-05-21 18:17:39 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
public class SleepMonitorActivity extends Activity implements SurfaceHolder.Callback {
|
2015-05-21 18:17:39 +02:00
|
|
|
public static final String ACTION_REFRESH
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.sleepmonitor.action.refresh";
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(SleepMonitorActivity.class);
|
|
|
|
|
|
|
|
private SurfaceView surfaceView;
|
|
|
|
private TextView textView;
|
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
private int mSmartAlarmFrom = -1;
|
|
|
|
private int mSmartAlarmTo = -1;
|
|
|
|
private int mTimestampFrom = -1;
|
|
|
|
private int mSmartAlarmGoneOff = -1;
|
2015-06-01 16:57:45 +02:00
|
|
|
private GBDevice mGBDevice = null;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-05-21 18:17:39 +02:00
|
|
|
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)) {
|
2015-05-30 17:28:03 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void refresh() {
|
2015-06-01 16:57:45 +02:00
|
|
|
if (mGBDevice == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
if (mTimestampFrom == -1) {
|
|
|
|
Long ts = System.currentTimeMillis();
|
|
|
|
mTimestampFrom = (int) ((ts / 1000) - (24 * 60 * 60) & 0xffffffff); // -24 hours
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:57:45 +02:00
|
|
|
byte provider = -1;
|
|
|
|
switch (mGBDevice.getType()) {
|
|
|
|
case MIBAND:
|
|
|
|
provider = GBActivitySample.PROVIDER_MIBAND;
|
|
|
|
break;
|
|
|
|
case PEBBLE:
|
|
|
|
provider = GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ; // FIXME
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ArrayList<GBActivitySample> samples = GBApplication.getActivityDatabaseHandler().getGBActivitySamples(mTimestampFrom, -1, provider);
|
2015-05-30 17:28:03 +02:00
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
Date date;
|
|
|
|
String dateStringFrom = "";
|
|
|
|
String dateStringTo = "";
|
|
|
|
|
|
|
|
SurfaceHolder surfaceHolder = surfaceView.getHolder();
|
|
|
|
LOG.info("number of samples:" + samples.size());
|
|
|
|
if (surfaceHolder.getSurface().isValid() && samples.size() > 1) {
|
|
|
|
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
Canvas canvas = surfaceHolder.lockCanvas();
|
|
|
|
paint.setColor(Color.WHITE);
|
|
|
|
paint.setStrokeWidth(2);
|
|
|
|
paint.setTextSize(20);
|
|
|
|
paint.setStyle(Paint.Style.FILL);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawRGB(100, 100, 100);
|
|
|
|
int width = canvas.getWidth();
|
|
|
|
int height = canvas.getHeight();
|
|
|
|
|
|
|
|
RectF r = new RectF(0.0f, 0.0f, 0.0f, height);
|
2015-06-05 19:32:09 +02:00
|
|
|
float movement_divisor;
|
|
|
|
boolean annotate;
|
|
|
|
boolean use_steps_as_movement;
|
|
|
|
switch (provider) {
|
|
|
|
case GBActivitySample.PROVIDER_MIBAND:
|
|
|
|
movement_divisor = 256.0f;
|
|
|
|
annotate = false; // sample density to high?
|
|
|
|
use_steps_as_movement = true;
|
|
|
|
break;
|
|
|
|
default: // Morpheuz
|
|
|
|
movement_divisor = 5000.0f;
|
|
|
|
annotate = true;
|
|
|
|
use_steps_as_movement = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte last_type = GBActivitySample.TYPE_UNKNOWN;
|
|
|
|
|
2015-06-05 23:09:28 +02:00
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
|
|
|
|
SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm");
|
2015-05-30 17:28:03 +02:00
|
|
|
for (int i = 0; i < samples.size(); i++) {
|
|
|
|
GBActivitySample sample = samples.get(i);
|
2015-06-05 19:32:09 +02:00
|
|
|
byte type = sample.getType();
|
2015-05-30 17:28:03 +02:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
cal.setTimeInMillis((long) sample.getTimestamp() * 1000L);
|
|
|
|
date = cal.getTime();
|
2015-06-05 23:09:28 +02:00
|
|
|
dateStringFrom = dateFormat.format(date);
|
2015-05-30 17:28:03 +02:00
|
|
|
} else if (i == samples.size() - 1) {
|
|
|
|
cal.setTimeInMillis((long) sample.getTimestamp() * 1000L);
|
|
|
|
date = cal.getTime();
|
2015-06-05 23:09:28 +02:00
|
|
|
dateStringTo = dateFormat.format(date);
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
2015-06-05 19:32:09 +02:00
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
short movement = sample.getIntensity();
|
|
|
|
r.left = r.right;
|
|
|
|
r.right = (float) (i + 1) / (samples.size()) * width;
|
2015-06-05 19:32:09 +02:00
|
|
|
if (type == GBActivitySample.TYPE_DEEP_SLEEP) {
|
|
|
|
paint.setColor(Color.BLUE);
|
|
|
|
r.top = 0.98f * height;
|
2015-05-30 17:28:03 +02:00
|
|
|
} else {
|
2015-06-05 19:32:09 +02:00
|
|
|
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
|
|
|
|
paint.setColor(Color.CYAN);
|
|
|
|
} else {
|
|
|
|
if (use_steps_as_movement) {
|
|
|
|
movement = sample.getSteps();
|
|
|
|
}
|
|
|
|
paint.setColor(Color.YELLOW);
|
|
|
|
}
|
|
|
|
r.top = (1.0f - (float) movement / movement_divisor) * height;
|
2015-05-21 18:17:39 +02:00
|
|
|
}
|
2015-06-05 19:32:09 +02:00
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
canvas.drawRect(r, paint);
|
2015-06-05 19:32:09 +02:00
|
|
|
boolean annotate_this = false;
|
2015-05-30 17:28:03 +02:00
|
|
|
if (annotate) {
|
2015-06-05 19:32:09 +02:00
|
|
|
if (type != GBActivitySample.TYPE_DEEP_SLEEP && type != GBActivitySample.TYPE_LIGHT_SLEEP &&
|
|
|
|
(last_type == GBActivitySample.TYPE_DEEP_SLEEP || last_type == GBActivitySample.TYPE_LIGHT_SLEEP)) {
|
|
|
|
// seems that we woke up
|
|
|
|
annotate_this = true;
|
|
|
|
}
|
|
|
|
if (annotate_this) {
|
|
|
|
cal.setTimeInMillis((long) (sample.getTimestamp()) * 1000L);
|
|
|
|
date = cal.getTime();
|
2015-06-05 23:09:28 +02:00
|
|
|
String dateString = annotationDateFormat.format(date);
|
2015-06-05 19:32:09 +02:00
|
|
|
paint.setColor(Color.WHITE);
|
2015-06-05 19:59:58 +02:00
|
|
|
canvas.save();
|
|
|
|
canvas.rotate(-90.0f, r.left, r.top);
|
|
|
|
canvas.drawText(dateString, r.left + 10, r.top + 10, paint);
|
|
|
|
canvas.restore();
|
2015-06-05 19:32:09 +02:00
|
|
|
}
|
|
|
|
last_type = type;
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
2015-05-21 18:17:39 +02:00
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
textView.setText(dateStringFrom + " to " + dateStringTo);
|
|
|
|
|
|
|
|
surfaceHolder.unlockCanvasAndPost(canvas);
|
2015-05-21 18:17:39 +02:00
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
2015-05-21 18:17:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2015-06-01 16:57:45 +02:00
|
|
|
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
if (extras != null) {
|
|
|
|
mGBDevice = extras.getParcelable("device");
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:17:39 +02:00
|
|
|
setContentView(R.layout.activity_sleepmonitor);
|
|
|
|
|
|
|
|
textView = (TextView) findViewById(R.id.textView);
|
|
|
|
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
|
|
|
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(ControlCenter.ACTION_QUIT);
|
|
|
|
filter.addAction(ACTION_REFRESH);
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
2015-05-30 17:28:03 +02:00
|
|
|
surfaceView.getHolder().addCallback(this);
|
2015-05-21 18:17:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void surfaceCreated(SurfaceHolder holder) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
|
|
|
|
|
|
}
|
2015-05-21 18:17:39 +02:00
|
|
|
}
|