Skip to content

Commit

Permalink
Fix fro hanging service..
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr committed Nov 28, 2023
1 parent 0202503 commit 8f39ba3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 29 deletions.
5 changes: 2 additions & 3 deletions SleepGarmin-android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 33
versionCode 39
versionName "20230805"
versionCode 40
versionName "20231128"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
signingConfigs {
Expand All @@ -34,7 +34,6 @@ android {
repositories {
mavenLocal()
mavenCentral()
jcenter()
google()
flatDir {
dirs 'libs'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Constants {
public final static String TO_WATCH_ALARM_SET = "SetAlarm";
public final static String TO_WATCH_HINT = "Hint";
public final static String TO_WATCH_TRACKING_START_HR = "StartHRTracking";
public final static String TO_WATCH_TRACKING_START_OXI = "StartOxiTracking";
public final static String TO_WATCH_TRACKING_START = "StartTracking";

// From watch to plugin
Expand All @@ -36,6 +37,9 @@ public class Constants {
// From sleep to plugin
final static String START_WATCH_APP = "com.urbandroid.sleep.watch.START_TRACKING";
final static String DO_HR_MONITORING = "DO_HR_MONITORING";

final static String DO_OXIMETER_MONITORING = "DO_OXIMETER_MONITORING";

final static String STOP_WATCH_APP = "com.urbandroid.sleep.watch.STOP_TRACKING";
final static String SET_PAUSE = "com.urbandroid.sleep.watch.SET_PAUSE";
final static String SET_BATCH_SIZE = "com.urbandroid.sleep.watch.SET_BATCH_SIZE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,11 @@ protected void onCreate(Bundle savedInstanceState) {
startAppInfo(this, getPackageName());
});

if (ContextCompat.checkSelfPermission(this, PERMISSION_POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
findViewById(R.id.card_allow_notifications).setVisibility(View.GONE);
} else {
findViewById(R.id.card_allow_notifications).setVisibility(View.VISIBLE);
}

findViewById(R.id.card_battery_optimization_opt_out).setVisibility(Utils.isUnrestrictedBatteryNotificationNeeded(this) ? View.VISIBLE : View.GONE);

sendUnrestrictedBatteryNotificationWithPermissionCheck();
}



private void sendUnrestrictedBatteryNotificationWithPermissionCheck() {
if (ContextCompat.checkSelfPermission(this, PERMISSION_POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
// You can use the API that requires the permission.
Expand Down Expand Up @@ -284,6 +278,13 @@ protected void onResume() {
findViewById(R.id.card_install_gcm).setVisibility(!gcmInstalled ? View.VISIBLE : View.GONE);
findViewById(R.id.card_install_watchsleepstarter).setVisibility(!watchsleepstarterInstalled ? View.VISIBLE : View.GONE);

if (ContextCompat.checkSelfPermission(this, PERMISSION_POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
findViewById(R.id.card_allow_notifications).setVisibility(View.GONE);
} else {
findViewById(R.id.card_allow_notifications).setVisibility(View.VISIBLE);
}

findViewById(R.id.card_battery_optimization_opt_out).setVisibility(Utils.isUnrestrictedBatteryNotificationNeeded(this) ? View.VISIBLE : View.GONE);
}

private void setupSleep() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.urbandroid.sleep.garmin.Constants.DATA_WITH_EXTRA;
import static com.urbandroid.sleep.garmin.Constants.DISMISS_ACTION_NAME;
import static com.urbandroid.sleep.garmin.Constants.DO_HR_MONITORING;
import static com.urbandroid.sleep.garmin.Constants.DO_OXIMETER_MONITORING;
import static com.urbandroid.sleep.garmin.Constants.EXTRA_DATA_BATCH;
import static com.urbandroid.sleep.garmin.Constants.EXTRA_DATA_FRAMERATE;
import static com.urbandroid.sleep.garmin.Constants.EXTRA_DATA_RR;
Expand Down Expand Up @@ -33,6 +34,7 @@
import static com.urbandroid.sleep.garmin.Constants.TO_WATCH_STOP;
import static com.urbandroid.sleep.garmin.Constants.TO_WATCH_TRACKING_START;
import static com.urbandroid.sleep.garmin.Constants.TO_WATCH_TRACKING_START_HR;
import static com.urbandroid.sleep.garmin.Constants.TO_WATCH_TRACKING_START_OXI;
import static com.urbandroid.sleep.garmin.Constants.UPDATE_ALARM;
import static com.urbandroid.sleep.garmin.Utils.dumpIntent;

Expand All @@ -58,6 +60,8 @@ public static MessageHandler getInstance() {
private MessageHandler() {
}



private final static String TAG = "MessageHandler: ";

private float[] maxFloatValues = null;
Expand Down Expand Up @@ -204,12 +208,19 @@ public void handleMessageFromSleep(Intent intent, final Context context) {
Logger.logDebug(TAG + "START_WATCH_APP");
dumpIntent(intent);


if (intent.hasExtra(DO_HR_MONITORING)) {
queueToWatch.enqueue(new MessageToWatch(TO_WATCH_TRACKING_START_HR));
Logger.logInfo(TAG + "TO_WATCH_TRACKING_START_HR");
}

if (intent.hasExtra(DO_OXIMETER_MONITORING)) {
queueToWatch.enqueue(new MessageToWatch(TO_WATCH_TRACKING_START_OXI));
Logger.logInfo(TAG + "TO_WATCH_TRACKING_START_OXI");
}

queueToWatch.enqueue(new MessageToWatch(TO_WATCH_TRACKING_START));

Logger.logDebug(TAG + "TO_WATCH_TRACKING_START");
}

Expand Down Expand Up @@ -250,6 +261,9 @@ public void handleMessageFromSleep(Intent intent, final Context context) {
}
if (action.equals(CHECK_CONNECTED)) {
queueToWatch.remove(new MessageToWatch(TO_WATCH_STOP));



try {
if (watchAppOpenTime == -1 || System.currentTimeMillis() - watchAppOpenTime >= 10000) {
watchAppOpenTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.urbandroid.sleep.garmin;

import static com.urbandroid.sleep.garmin.Constants.ACTION_STOP_SELF;
import static com.urbandroid.sleep.garmin.Constants.CHECK_CONNECTED;
import static com.urbandroid.sleep.garmin.Constants.PACKAGE_SLEEP_WATCH_STARTER;
import static com.urbandroid.sleep.garmin.Notifications.NOTIFICATION_CHANNEL_ID_TRACKING;
import static com.urbandroid.sleep.garmin.Notifications.getPendingIntentFlags;
Expand All @@ -9,6 +10,7 @@
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;
Expand Down Expand Up @@ -48,6 +50,8 @@ public void onCreate() {
ciqManager.resetState();

startHttpServer();

handler = new Handler();
}

private void startHttpServer() {
Expand All @@ -63,16 +67,27 @@ private void startHttpServer() {
}
}

private Handler handler;

private Runnable connectionTimeout = () -> ServiceRecoveryManager.getInstance().stopSelfAndDontScheduleRecovery("CONNECTIVITY_CHECK timeout");


@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
Logger.logDebug(TAG + "onStartCommand, intent " + ((intent != null && intent.getAction() != null) ? intent.getAction() : "null"));

startForeground();
RUNNING = true;

if (intent != null && intent.getAction() != null && ACTION_STOP_SELF.equals(intent.getAction())) {
ServiceRecoveryManager.getInstance().stopSelfAndDontScheduleRecovery("STOP_SELF intent received");
return START_NOT_STICKY;
if (intent != null) {
if (ACTION_STOP_SELF.equals(intent.getAction())) {
ServiceRecoveryManager.getInstance().stopSelfAndDontScheduleRecovery("STOP_SELF intent received");
return START_NOT_STICKY;
} else if (CHECK_CONNECTED.equals(intent.getAction())) {
handler.postDelayed(connectionTimeout, 60000);
} else {
handler.removeCallbacks(connectionTimeout);
}
}

ciqManager.init(this, intent);
Expand Down Expand Up @@ -119,7 +134,7 @@ private void startForeground() {
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name_long));
}

notificationBuilder.setSmallIcon(R.drawable.ic_action_track);
notificationBuilder.setSmallIcon(R.drawable.ic_action_watch);

startForeground(1349, notificationBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<LinearLayout
android:padding="20dp"
android:background="@color/tint_dark"
android:background="@color/tint"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
12 changes: 6 additions & 6 deletions SleepGarmin-android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="colorAccent">#7d9420</color>
<color name="primaryDark">#7d9420</color>
<color name="colorPrimary">#7d9420</color>
<color name="colorPrimaryDark">#7d9420</color>
<color name="colorAccent">#26a69a</color>
<color name="primaryDark">#26a69a</color>
<color name="colorPrimary">#26a69a</color>
<color name="colorPrimaryDark">#26a69a</color>

<color name="primaryBright">#ffffffff</color>
<color name="primary">#ffffffff</color>
Expand All @@ -15,8 +15,8 @@
<color name="white">#ffffffff</color>
<color name="trans_white">#99ffffff</color>
<color name="transparent">#00000000</color>
<color name="tint">#ffa6d725</color>
<color name="tint_dark">#7d9420</color>
<color name="tint">#26a69a</color>
<color name="tint_dark">#00897b</color>
<color name="normal">#4DB6AC</color>
<color name="red">#ffdf4545</color>
<color name="red_widget">#ffd50000</color>
Expand Down
21 changes: 14 additions & 7 deletions SleepGarmin-watch2/source/SensorManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class SensorManager {

const SENSOR_PERIOD_SEC = 4;
const OXI_READING_PERIOD_SEC = 4;
const SENSOR_FREQ = 15;
const SENSOR_AGGREG_WINDOW = 10;
const SENSOR_FREQ = 10;
const SENSOR_AGGREG_WINDOW_SEC = 10;

var ctx;

Expand All @@ -30,11 +30,11 @@ class SensorManager {
}

function startHr() {
// Sensor.enableSensorType(Sensor.SensorType.SENSOR_HEARTRATE)
Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);
}

function startOxi() {
// Sensor.enableSensorType(Sensor.SensorType.SENSOR_PULSE_OXIMETRY)
Sensor.setEnabledSensors([Sensor.SENSOR_PULSE_OXIMETRY]);
}

function start() {
Expand All @@ -45,11 +45,18 @@ class SensorManager {
:accelerometer => {
:enabled => true,
:sampleRate => SENSOR_FREQ
}
};
},
:heartBeatIntervals => {
:enabled => true
}};

Sensor.registerSensorDataListener(SensorManager.method(:onData), options);
}

// function stop() {
// Sensor.unregisterSensorDataListener()
// }

// argument is of type SensorData
public function onData(sensorData as Sensor.SensorData) as Void {
DebugManager.log("SensorManager onData");
Expand Down Expand Up @@ -82,7 +89,7 @@ class SensorManager {
accYBuf.addAll(yArr);
accZBuf.addAll(zArr);

var maxCount = SENSOR_AGGREG_WINDOW * SENSOR_FREQ; // Maximum number of values to go into one aggregate (sampleRate [Hz] x batchPeriod [s])
var maxCount = SENSOR_AGGREG_WINDOW_SEC * SENSOR_FREQ; // Maximum number of values to go into one aggregate (sampleRate [Hz] x batchPeriod [s])

// Since maximum sensor batching period is 4 seconds and we need to have aggregate period of 10 seconds, we need to aggregate two and half sensor batches. Then we need to retain the remaining half of the third batch.
// In order to do that, we first add all the data from three batches into one array and then aggregate just first 100 values, deleting them from the batch arrays.
Expand Down

0 comments on commit 8f39ba3

Please sign in to comment.