Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit fd756fb

Browse files
author
TebbeUbben
committed
Ability to automatically synchronize the pump's time.
1 parent c390cec commit fd756fb

10 files changed

Lines changed: 162 additions & 7 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
<service
101101
android:name=".services.OngoingNotificationService"
102102
android:enabled="true" />
103+
<service
104+
android:name=".services.TimeSynchronizationService"
105+
android:enabled="true" />
103106
<service
104107
android:name="sugar.free.sightparser.handling.SightService"
105108
android:exported="true"

app/src/main/java/sugar/free/sightremote/SightRemote.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sugar.free.sightremote.services.AlertService;
1414
import sugar.free.sightremote.services.HistorySyncService;
1515
import sugar.free.sightremote.services.OngoingNotificationService;
16+
import sugar.free.sightremote.services.TimeSynchronizationService;
1617
import sugar.free.sightremote.utils.NotificationCenter;
1718
import sugar.free.sightremote.utils.Preferences;
1819

@@ -39,5 +40,7 @@ public void onCreate() {
3940
startService(new Intent(this, SightService.class));
4041
startService(new Intent(this, HistorySyncService.class));
4142
startService(new Intent(this, AlertService.class));
43+
if (Preferences.getBooleanPref(Preferences.PREF_BOOLEAN_AUTO_ADJUST_TIME))
44+
startService(new Intent(this, TimeSynchronizationService.class));
4245
}
4346
}

app/src/main/java/sugar/free/sightremote/activities/SettingsActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sugar.free.sightremote.dialogs.ChangePINDialog;
1414
import sugar.free.sightremote.dialogs.ConfirmPINDialog;
1515
import sugar.free.sightremote.dialogs.ConfirmationDialog;
16+
import sugar.free.sightremote.services.TimeSynchronizationService;
1617
import sugar.free.sightremote.utils.HTMLUtil;
1718

1819
import static sugar.free.sightremote.utils.Preferences.*;
@@ -60,6 +61,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
6061
getPreferenceScreen().findPreference("enable_confirmation_challenges").setOnPreferenceClickListener(this);
6162
getPreferenceScreen().findPreference(PREF_BOOLEAN_ENABLE_CONFIRMATION_CHALLENGES).setOnPreferenceChangeListener(this);
6263
getPreferenceScreen().findPreference(PREF_BOOLEAN_CONFIRMATION_USE_FINGERPRINT).setOnPreferenceChangeListener(this);
64+
getPreferenceScreen().findPreference(PREF_BOOLEAN_AUTO_ADJUST_TIME).setOnPreferenceChangeListener(this);
6365
getPreferenceScreen().findPreference(PREF_BOOLEAN_CONFIRMATION_USE_PIN).setOnPreferenceChangeListener(this);
6466
getPreferenceScreen().findPreference(PREF_STRING_CONFIRMATION_PIN).setOnPreferenceClickListener(this);
6567
if (!ConfirmationDialog.isFingerprintScannerAvailable()) {
@@ -97,7 +99,7 @@ private SettingsActivity getSettingsActivity() {
9799
}
98100

99101
@Override
100-
public boolean onPreferenceChange(Preference preference, Object o) {
102+
public boolean onPreferenceChange(Preference preference, Object newValue) {
101103
if (preference.getKey().equals(PREF_BOOLEAN_CONFIRMATION_USE_PIN)) {
102104
if (getBooleanPref(PREF_BOOLEAN_CONFIRMATION_USE_PIN)) {
103105
getSettingsActivity().dialog = new ConfirmPINDialog(getSettingsActivity(), () -> {
@@ -130,6 +132,9 @@ public boolean onPreferenceChange(Preference preference, Object o) {
130132
}).show();
131133
return false;
132134
}
135+
} else if (preference.getKey().equals(PREF_BOOLEAN_AUTO_ADJUST_TIME)) {
136+
if ((Boolean) newValue) getActivity().startService(new Intent(getActivity(), TimeSynchronizationService.class));
137+
else getActivity().stopService(new Intent(getActivity(), TimeSynchronizationService.class));
133138
}
134139
return true;
135140
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package sugar.free.sightremote.services;
2+
3+
import android.app.AlarmManager;
4+
import android.app.PendingIntent;
5+
import android.app.Service;
6+
import android.content.Intent;
7+
import android.os.IBinder;
8+
import android.support.annotation.Nullable;
9+
import sugar.free.sightparser.applayer.messages.configuration.WriteDateTimeMessage;
10+
import sugar.free.sightparser.applayer.messages.status.ReadDateTimeMessage;
11+
import sugar.free.sightparser.handling.SightServiceConnector;
12+
import sugar.free.sightparser.handling.SingleMessageTaskRunner;
13+
import sugar.free.sightparser.handling.StatusCallback;
14+
import sugar.free.sightparser.handling.TaskRunner;
15+
import sugar.free.sightparser.pipeline.Status;
16+
import sugar.free.sightremote.utils.NotificationCenter;
17+
18+
import java.util.Calendar;
19+
20+
public class TimeSynchronizationService extends Service implements StatusCallback, TaskRunner.ResultCallback {
21+
22+
private SightServiceConnector serviceConnector;
23+
private AlarmManager alarmManager;
24+
private PendingIntent pendingIntent;
25+
26+
@Nullable
27+
@Override
28+
public IBinder onBind(Intent intent) {
29+
return null;
30+
}
31+
32+
@Override
33+
public void onCreate() {
34+
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
35+
serviceConnector = new SightServiceConnector(this);
36+
serviceConnector.addStatusCallback(this);
37+
serviceConnector.connectToService();
38+
}
39+
40+
@Override
41+
public int onStartCommand(Intent intent, int flags, int startId) {
42+
if (serviceConnector.isConnectedToService()) {
43+
serviceConnector.connect();
44+
if (serviceConnector.getStatus() == Status.CONNECTED)
45+
onStatusChange(serviceConnector.getStatus());
46+
}
47+
Intent serviceIntent = new Intent(this, TimeSynchronizationService.class);
48+
pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
49+
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 60 * 1000, pendingIntent);
50+
return START_STICKY;
51+
}
52+
53+
@Override
54+
public void onDestroy() {
55+
if (pendingIntent != null) alarmManager.cancel(pendingIntent);
56+
if (serviceConnector.isConnectedToService()) serviceConnector.disconnect();
57+
serviceConnector.disconnectFromService();
58+
}
59+
60+
@Override
61+
public void onStatusChange(Status status) {
62+
if (status == Status.CONNECTED) {
63+
serviceConnector.connect();
64+
new SingleMessageTaskRunner(serviceConnector, new ReadDateTimeMessage()).fetch(this);
65+
} else serviceConnector.disconnect();
66+
}
67+
68+
@Override
69+
public void onResult(Object result) {
70+
if (result instanceof ReadDateTimeMessage) {
71+
ReadDateTimeMessage dateTime = (ReadDateTimeMessage) result;
72+
if (Math.abs(parseDateTime(dateTime) - System.currentTimeMillis()) >= 1000 * 30) {
73+
new SingleMessageTaskRunner(serviceConnector, createWriteMessage()).fetch(this);
74+
}
75+
} else if (result instanceof WriteDateTimeMessage) {
76+
NotificationCenter.showTimeSynchronizedNotification();
77+
serviceConnector.disconnect();
78+
}
79+
}
80+
81+
private WriteDateTimeMessage createWriteMessage() {
82+
Calendar calendar = Calendar.getInstance();
83+
WriteDateTimeMessage writeMessage = new WriteDateTimeMessage();
84+
writeMessage.setYear(calendar.get(Calendar.YEAR));
85+
writeMessage.setMonth(calendar.get(Calendar.MONTH));
86+
writeMessage.setDay(calendar.get(Calendar.DAY_OF_MONTH));
87+
writeMessage.setHour(calendar.get(Calendar.HOUR_OF_DAY));
88+
writeMessage.setMinute(calendar.get(Calendar.MINUTE));
89+
writeMessage.setSecond(calendar.get(Calendar.SECOND));
90+
return writeMessage;
91+
}
92+
93+
private long parseDateTime(ReadDateTimeMessage dateTime) {
94+
Calendar calendar = Calendar.getInstance();
95+
calendar.set(Calendar.YEAR, dateTime.getYear());
96+
calendar.set(Calendar.MONTH, dateTime.getMonth());
97+
calendar.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
98+
calendar.set(Calendar.HOUR_OF_DAY, dateTime.getHour());
99+
calendar.set(Calendar.MINUTE, dateTime.getMinute());
100+
calendar.set(Calendar.SECOND, dateTime.getSecond());
101+
return calendar.getTime().getTime();
102+
}
103+
104+
@Override
105+
public void onError(Exception e) {
106+
serviceConnector.disconnect();
107+
}
108+
}

app/src/main/java/sugar/free/sightremote/utils/NotificationCenter.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@
1212

1313
public final class NotificationCenter {
1414

15-
private static final String ONGOING_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.OTHER";
15+
private static final String ONGOING_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.ONGOING";
16+
private static final String TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.TIME_SYNCHRONIZED";
1617
public static final int ONGOING_NOTIFICATION_ID = 156;
18+
public static final int TIME_SYNCHRONIZED_NOTIFICATION_ID = 157;
1719

1820
@Getter
1921
private static Notification ongoingNotification;
2022

2123
public static void setupUpChannels() {
2224
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
23-
NotificationChannel notificationChannel = new NotificationChannel(ONGOING_NOTIFICATION_CHANNEL_ID,
25+
NotificationManager notificationManager = (NotificationManager) SightRemote.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
26+
27+
NotificationChannel ongoingNotificationChannel = new NotificationChannel(ONGOING_NOTIFICATION_CHANNEL_ID,
2428
SightRemote.getInstance().getString(R.string.ongoing_notification_name),
2529
NotificationManager.IMPORTANCE_MIN);
26-
notificationChannel.setDescription(SightRemote.getInstance().getString(R.string.ongoing_notification_description));
27-
NotificationManager notificationManager = (NotificationManager) SightRemote.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
28-
notificationManager.createNotificationChannel(notificationChannel);
30+
ongoingNotificationChannel.setDescription(SightRemote.getInstance().getString(R.string.ongoing_notification_description));
31+
notificationManager.createNotificationChannel(ongoingNotificationChannel);
32+
33+
NotificationChannel timeSynchronizedNotificationChannel = new NotificationChannel(TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID,
34+
SightRemote.getInstance().getString(R.string.pump_time_synchronized),
35+
NotificationManager.IMPORTANCE_DEFAULT);
36+
timeSynchronizedNotificationChannel.setDescription(SightRemote.getInstance().getString(R.string.notifies_you_when_pump_time_is_changed));
37+
notificationManager.createNotificationChannel(timeSynchronizedNotificationChannel);
2938
}
3039
}
3140

@@ -40,4 +49,15 @@ public static void showOngoingNotification(int text) {
4049
NotificationManagerCompat.from(SightRemote.getInstance()).notify(ONGOING_NOTIFICATION_ID, ongoingNotification = builder.build());
4150
}
4251

52+
public static void showTimeSynchronizedNotification() {
53+
NotificationCompat.Builder builder = new NotificationCompat.Builder(SightRemote.getInstance(), TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID);
54+
builder.setSmallIcon(R.drawable.ic_notification);
55+
builder.setContentTitle(SightRemote.getInstance().getString(R.string.pump_time_synchronized));
56+
builder.setContentText(SightRemote.getInstance().getString(R.string.the_time_on_your_pump_was_inaccurate_and_has_been_synchronized));
57+
builder.setPriority(NotificationManagerCompat.IMPORTANCE_DEFAULT);
58+
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(SightRemote.getInstance().getString(R.string.the_time_on_your_pump_was_inaccurate_and_has_been_synchronized)));
59+
60+
NotificationManagerCompat.from(SightRemote.getInstance()).notify(TIME_SYNCHRONIZED_NOTIFICATION_ID, ongoingNotification = builder.build());
61+
}
62+
4363
}

app/src/main/java/sugar/free/sightremote/utils/Preferences.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Preferences {
1414
public static String PREF_BOOLEAN_ENABLE_CONFIRMATION_CHALLENGES = "enable_confirmation_challenges";
1515
public static String PREF_BOOLEAN_CONFIRMATION_USE_FINGERPRINT = "confirmation_use_fingerprint";
1616
public static String PREF_BOOLEAN_CONFIRMATION_USE_PIN = "confirmation_use_pin";
17+
public static String PREF_BOOLEAN_AUTO_ADJUST_TIME = "auto_adjust_time";
1718

1819
private static SharedPreferences sharedPreferences;
1920

app/src/main/res/values-de/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@
166166
<string name="sightremote_is_active">SightRemote ist aktiv</string>
167167
<string name="ongoing_notification_name">Laufende Benachrichtigung</string>
168168
<string name="ongoing_notification_description">Zeigt an, ob das Telefon mit deiner Pumpe verbunden ist.</string>
169+
<string name="automatically_adjust_pump_time">Uhrzeit der Pumpe automatisch anpassen</string>
170+
<string name="pump_time_synchronized">Uhrzeit der Pumpe synchronisiert</string>
171+
<string name="the_time_on_your_pump_was_inaccurate_and_has_been_synchronized">Die Uhrzeit auf deiner Pumpe war ungenau und wurde synchronisiert.</string>
172+
<string name="automatically_adjust_the_pumps_time_if_its_inaccurate">Automatisch die Uhrzeit der Pumpe anpassen, falls diese ungenau ist</string>
173+
<string name="notifies_you_when_pump_time_is_changed">Benachrichtigt dich, wenn die Uhrzeit der Pumpe geändert wurde.</string>
169174
</resources>

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,9 @@
168168
<string name="sightremote_is_active">SightRemote is active</string>
169169
<string name="ongoing_notification_name">Ongoing notification</string>
170170
<string name="ongoing_notification_description">Indicates whether your phone is connected to your pump.</string>
171+
<string name="automatically_adjust_pump_time">Automatically adjust pump time</string>
172+
<string name="automatically_adjust_the_pumps_time_if_its_inaccurate">Automatically adjust the pump\'s time if it\'s inaccurate</string>
173+
<string name="pump_time_synchronized">Pump time synchronized</string>
174+
<string name="the_time_on_your_pump_was_inaccurate_and_has_been_synchronized">The time on your pump was inaccurate and has been synchronized.</string>
175+
<string name="notifies_you_when_pump_time_is_changed">Notifies you when the pump\'s time was changed</string>
171176
</resources>

app/src/main/res/xml/settings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
android:key="background_sync_enabled"
1212
android:summary="@string/synchronize_pump_data_automatically_in_the_background"
1313
android:title="@string/background_sync" />
14+
<CheckBoxPreference
15+
android:defaultValue="false"
16+
android:key="auto_adjust_time"
17+
android:summary="@string/automatically_adjust_the_pumps_time_if_its_inaccurate"
18+
android:title="@string/automatically_adjust_pump_time" />
1419

1520
<PreferenceCategory
1621
android:title="@string/confirmation">

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ext {
66

77
versionMajor = 1
88
versionMinor = 5
9-
versionRevision = 3
9+
versionRevision = 4
1010
versionCompatiblity = 'asclepius'
1111

1212
versionCode = 1

0 commit comments

Comments
 (0)