165 lines
4.9 KiB
Java
165 lines
4.9 KiB
Java
|
|
package com.rubenvandeven.heartbeatstreamer;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.rubenvandeven.heartbeatstreamer.heartrate.Activity_AsyncScanHeartRateSampler;
|
|
import com.rubenvandeven.heartbeatstreamer.heartrate.Activity_SearchUiHeartRateSampler;
|
|
import com.rubenvandeven.heartbeatstreamer.heartrate.HeartRateService;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
private final String TAG = MainActivity.class.getSimpleName();
|
|
|
|
private TextView statusLabel;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
statusLabel = findViewById(R.id.status_msg);
|
|
|
|
setup();
|
|
|
|
startHeartRateMonitor();
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
setup();
|
|
}
|
|
|
|
public void setup() {
|
|
if (HeartRateService.isRunning()) {
|
|
statusLabel.setText("Running");
|
|
} else {
|
|
statusLabel.setText("Stopped");
|
|
}
|
|
registerBroadcastReceiver();
|
|
}
|
|
|
|
public void startHeartRateMonitor() {
|
|
if(HeartRateService.isRunning()) {
|
|
Toast.makeText(this, "Already running", Toast.LENGTH_LONG);
|
|
} else {
|
|
Intent intent = new Intent(MainActivity.this, HeartRateService.class);
|
|
startService(intent);
|
|
}
|
|
}
|
|
|
|
public void stopHeartRateMonitor() {
|
|
// stop tracking
|
|
Intent intent = new Intent(MainActivity.this, HeartRateService.class);
|
|
stopService(intent);
|
|
}
|
|
|
|
/**
|
|
* Register broadcast receiver for synchronization
|
|
* and tracking status updates
|
|
*/
|
|
private void registerBroadcastReceiver() {
|
|
Log.d(TAG, "register broadcastreceiver");
|
|
IntentFilter filter = new IntentFilter();
|
|
filter.addAction(HeartRateService.BROADCAST_MONITOR_CONNECTED);
|
|
filter.addAction(HeartRateService.BROADCAST_MONITOR_DISCONNECTED);
|
|
filter.addAction(HeartRateService.BROADCAST_MONITOR_UPDATE);
|
|
filter.addAction(HeartRateService.BROADCAST_MONITOR_UNKNOWN);
|
|
filter.addAction(HeartRateService.BROADCAST_SOCKET_SENT);
|
|
registerReceiver(mBroadcastReceiver, filter);
|
|
}
|
|
|
|
/**
|
|
* On pause
|
|
*/
|
|
@Override
|
|
protected void onPause() {
|
|
unregisterReceiver(mBroadcastReceiver);
|
|
// if (db != null) {
|
|
// db.close();
|
|
// }
|
|
super.onPause();
|
|
}
|
|
|
|
/**
|
|
* Broadcast receiver
|
|
*/
|
|
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
Log.d(TAG, "[broadcast received " + intent + "]");
|
|
if (intent == null || intent.getAction() == null) {
|
|
return;
|
|
}
|
|
switch (intent.getAction()) {
|
|
case HeartRateService.BROADCAST_MONITOR_CONNECTED:
|
|
statusLabel.setText("Running");
|
|
break;
|
|
case HeartRateService.BROADCAST_MONITOR_DISCONNECTED:
|
|
statusLabel.setText("Stopped");
|
|
break;
|
|
case HeartRateService.BROADCAST_MONITOR_UPDATE:
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
|
String currentDateandTime = sdf.format(new Date());
|
|
statusLabel.setText("Last update: %s".format(currentDateandTime));
|
|
break;
|
|
|
|
case HeartRateService.BROADCAST_SOCKET_SENT:
|
|
String msg = intent.getStringExtra("msg");
|
|
statusLabel.setText("Last msg: %s".format(msg));
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get the token for use with the syncing.
|
|
* @return
|
|
*/
|
|
public String getToken() {
|
|
String json = null;
|
|
String token = null;
|
|
|
|
try {
|
|
InputStream is = getAssets().open("token.json");
|
|
int size = is.available();
|
|
byte[] buffer = new byte[size];
|
|
is.read(buffer);
|
|
is.close();
|
|
json = new String(buffer, "UTF-8");
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
return null;
|
|
}
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(json);
|
|
token = jsonObject.getString("token'");
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
return token;
|
|
}
|
|
|
|
}
|