Skip to content

Commit

Permalink
Ability to automatically synchronize the pump's time.
Browse files Browse the repository at this point in the history
  • Loading branch information
TebbeUbben committed Apr 6, 2018
1 parent c390cec commit fd756fb
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<service
android:name=".services.OngoingNotificationService"
android:enabled="true" />
<service
android:name=".services.TimeSynchronizationService"
android:enabled="true" />
<service
android:name="sugar.free.sightparser.handling.SightService"
android:exported="true"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/sugar/free/sightremote/SightRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sugar.free.sightremote.services.AlertService;
import sugar.free.sightremote.services.HistorySyncService;
import sugar.free.sightremote.services.OngoingNotificationService;
import sugar.free.sightremote.services.TimeSynchronizationService;
import sugar.free.sightremote.utils.NotificationCenter;
import sugar.free.sightremote.utils.Preferences;

Expand All @@ -39,5 +40,7 @@ public void onCreate() {
startService(new Intent(this, SightService.class));
startService(new Intent(this, HistorySyncService.class));
startService(new Intent(this, AlertService.class));
if (Preferences.getBooleanPref(Preferences.PREF_BOOLEAN_AUTO_ADJUST_TIME))
startService(new Intent(this, TimeSynchronizationService.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sugar.free.sightremote.dialogs.ChangePINDialog;
import sugar.free.sightremote.dialogs.ConfirmPINDialog;
import sugar.free.sightremote.dialogs.ConfirmationDialog;
import sugar.free.sightremote.services.TimeSynchronizationService;
import sugar.free.sightremote.utils.HTMLUtil;

import static sugar.free.sightremote.utils.Preferences.*;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
getPreferenceScreen().findPreference("enable_confirmation_challenges").setOnPreferenceClickListener(this);
getPreferenceScreen().findPreference(PREF_BOOLEAN_ENABLE_CONFIRMATION_CHALLENGES).setOnPreferenceChangeListener(this);
getPreferenceScreen().findPreference(PREF_BOOLEAN_CONFIRMATION_USE_FINGERPRINT).setOnPreferenceChangeListener(this);
getPreferenceScreen().findPreference(PREF_BOOLEAN_AUTO_ADJUST_TIME).setOnPreferenceChangeListener(this);
getPreferenceScreen().findPreference(PREF_BOOLEAN_CONFIRMATION_USE_PIN).setOnPreferenceChangeListener(this);
getPreferenceScreen().findPreference(PREF_STRING_CONFIRMATION_PIN).setOnPreferenceClickListener(this);
if (!ConfirmationDialog.isFingerprintScannerAvailable()) {
Expand Down Expand Up @@ -97,7 +99,7 @@ private SettingsActivity getSettingsActivity() {
}

@Override
public boolean onPreferenceChange(Preference preference, Object o) {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(PREF_BOOLEAN_CONFIRMATION_USE_PIN)) {
if (getBooleanPref(PREF_BOOLEAN_CONFIRMATION_USE_PIN)) {
getSettingsActivity().dialog = new ConfirmPINDialog(getSettingsActivity(), () -> {
Expand Down Expand Up @@ -130,6 +132,9 @@ public boolean onPreferenceChange(Preference preference, Object o) {
}).show();
return false;
}
} else if (preference.getKey().equals(PREF_BOOLEAN_AUTO_ADJUST_TIME)) {
if ((Boolean) newValue) getActivity().startService(new Intent(getActivity(), TimeSynchronizationService.class));
else getActivity().stopService(new Intent(getActivity(), TimeSynchronizationService.class));
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package sugar.free.sightremote.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import sugar.free.sightparser.applayer.messages.configuration.WriteDateTimeMessage;
import sugar.free.sightparser.applayer.messages.status.ReadDateTimeMessage;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.SingleMessageTaskRunner;
import sugar.free.sightparser.handling.StatusCallback;
import sugar.free.sightparser.handling.TaskRunner;
import sugar.free.sightparser.pipeline.Status;
import sugar.free.sightremote.utils.NotificationCenter;

import java.util.Calendar;

public class TimeSynchronizationService extends Service implements StatusCallback, TaskRunner.ResultCallback {

private SightServiceConnector serviceConnector;
private AlarmManager alarmManager;
private PendingIntent pendingIntent;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
serviceConnector = new SightServiceConnector(this);
serviceConnector.addStatusCallback(this);
serviceConnector.connectToService();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (serviceConnector.isConnectedToService()) {
serviceConnector.connect();
if (serviceConnector.getStatus() == Status.CONNECTED)
onStatusChange(serviceConnector.getStatus());
}
Intent serviceIntent = new Intent(this, TimeSynchronizationService.class);
pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 60 * 1000, pendingIntent);
return START_STICKY;
}

@Override
public void onDestroy() {
if (pendingIntent != null) alarmManager.cancel(pendingIntent);
if (serviceConnector.isConnectedToService()) serviceConnector.disconnect();
serviceConnector.disconnectFromService();
}

@Override
public void onStatusChange(Status status) {
if (status == Status.CONNECTED) {
serviceConnector.connect();
new SingleMessageTaskRunner(serviceConnector, new ReadDateTimeMessage()).fetch(this);
} else serviceConnector.disconnect();
}

@Override
public void onResult(Object result) {
if (result instanceof ReadDateTimeMessage) {
ReadDateTimeMessage dateTime = (ReadDateTimeMessage) result;
if (Math.abs(parseDateTime(dateTime) - System.currentTimeMillis()) >= 1000 * 30) {
new SingleMessageTaskRunner(serviceConnector, createWriteMessage()).fetch(this);
}
} else if (result instanceof WriteDateTimeMessage) {
NotificationCenter.showTimeSynchronizedNotification();
serviceConnector.disconnect();
}
}

private WriteDateTimeMessage createWriteMessage() {
Calendar calendar = Calendar.getInstance();
WriteDateTimeMessage writeMessage = new WriteDateTimeMessage();
writeMessage.setYear(calendar.get(Calendar.YEAR));
writeMessage.setMonth(calendar.get(Calendar.MONTH));
writeMessage.setDay(calendar.get(Calendar.DAY_OF_MONTH));
writeMessage.setHour(calendar.get(Calendar.HOUR_OF_DAY));
writeMessage.setMinute(calendar.get(Calendar.MINUTE));
writeMessage.setSecond(calendar.get(Calendar.SECOND));
return writeMessage;
}

private long parseDateTime(ReadDateTimeMessage dateTime) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, dateTime.getYear());
calendar.set(Calendar.MONTH, dateTime.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
calendar.set(Calendar.HOUR_OF_DAY, dateTime.getHour());
calendar.set(Calendar.MINUTE, dateTime.getMinute());
calendar.set(Calendar.SECOND, dateTime.getSecond());
return calendar.getTime().getTime();
}

@Override
public void onError(Exception e) {
serviceConnector.disconnect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@

public final class NotificationCenter {

private static final String ONGOING_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.OTHER";
private static final String ONGOING_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.ONGOING";
private static final String TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID = "sugar.free.sightremote.TIME_SYNCHRONIZED";
public static final int ONGOING_NOTIFICATION_ID = 156;
public static final int TIME_SYNCHRONIZED_NOTIFICATION_ID = 157;

@Getter
private static Notification ongoingNotification;

public static void setupUpChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(ONGOING_NOTIFICATION_CHANNEL_ID,
NotificationManager notificationManager = (NotificationManager) SightRemote.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);

NotificationChannel ongoingNotificationChannel = new NotificationChannel(ONGOING_NOTIFICATION_CHANNEL_ID,
SightRemote.getInstance().getString(R.string.ongoing_notification_name),
NotificationManager.IMPORTANCE_MIN);
notificationChannel.setDescription(SightRemote.getInstance().getString(R.string.ongoing_notification_description));
NotificationManager notificationManager = (NotificationManager) SightRemote.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
ongoingNotificationChannel.setDescription(SightRemote.getInstance().getString(R.string.ongoing_notification_description));
notificationManager.createNotificationChannel(ongoingNotificationChannel);

NotificationChannel timeSynchronizedNotificationChannel = new NotificationChannel(TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID,
SightRemote.getInstance().getString(R.string.pump_time_synchronized),
NotificationManager.IMPORTANCE_DEFAULT);
timeSynchronizedNotificationChannel.setDescription(SightRemote.getInstance().getString(R.string.notifies_you_when_pump_time_is_changed));
notificationManager.createNotificationChannel(timeSynchronizedNotificationChannel);
}
}

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

public static void showTimeSynchronizedNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(SightRemote.getInstance(), TIME_SYNCHRONIZED_NOTIFICATION_CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_notification);
builder.setContentTitle(SightRemote.getInstance().getString(R.string.pump_time_synchronized));
builder.setContentText(SightRemote.getInstance().getString(R.string.the_time_on_your_pump_was_inaccurate_and_has_been_synchronized));
builder.setPriority(NotificationManagerCompat.IMPORTANCE_DEFAULT);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(SightRemote.getInstance().getString(R.string.the_time_on_your_pump_was_inaccurate_and_has_been_synchronized)));

NotificationManagerCompat.from(SightRemote.getInstance()).notify(TIME_SYNCHRONIZED_NOTIFICATION_ID, ongoingNotification = builder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Preferences {
public static String PREF_BOOLEAN_ENABLE_CONFIRMATION_CHALLENGES = "enable_confirmation_challenges";
public static String PREF_BOOLEAN_CONFIRMATION_USE_FINGERPRINT = "confirmation_use_fingerprint";
public static String PREF_BOOLEAN_CONFIRMATION_USE_PIN = "confirmation_use_pin";
public static String PREF_BOOLEAN_AUTO_ADJUST_TIME = "auto_adjust_time";

private static SharedPreferences sharedPreferences;

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,9 @@
<string name="sightremote_is_active">SightRemote ist aktiv</string>
<string name="ongoing_notification_name">Laufende Benachrichtigung</string>
<string name="ongoing_notification_description">Zeigt an, ob das Telefon mit deiner Pumpe verbunden ist.</string>
<string name="automatically_adjust_pump_time">Uhrzeit der Pumpe automatisch anpassen</string>
<string name="pump_time_synchronized">Uhrzeit der Pumpe synchronisiert</string>
<string name="the_time_on_your_pump_was_inaccurate_and_has_been_synchronized">Die Uhrzeit auf deiner Pumpe war ungenau und wurde synchronisiert.</string>
<string name="automatically_adjust_the_pumps_time_if_its_inaccurate">Automatisch die Uhrzeit der Pumpe anpassen, falls diese ungenau ist</string>
<string name="notifies_you_when_pump_time_is_changed">Benachrichtigt dich, wenn die Uhrzeit der Pumpe geändert wurde.</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@
<string name="sightremote_is_active">SightRemote is active</string>
<string name="ongoing_notification_name">Ongoing notification</string>
<string name="ongoing_notification_description">Indicates whether your phone is connected to your pump.</string>
<string name="automatically_adjust_pump_time">Automatically adjust pump time</string>
<string name="automatically_adjust_the_pumps_time_if_its_inaccurate">Automatically adjust the pump\'s time if it\'s inaccurate</string>
<string name="pump_time_synchronized">Pump time synchronized</string>
<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>
<string name="notifies_you_when_pump_time_is_changed">Notifies you when the pump\'s time was changed</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
android:key="background_sync_enabled"
android:summary="@string/synchronize_pump_data_automatically_in_the_background"
android:title="@string/background_sync" />
<CheckBoxPreference
android:defaultValue="false"
android:key="auto_adjust_time"
android:summary="@string/automatically_adjust_the_pumps_time_if_its_inaccurate"
android:title="@string/automatically_adjust_pump_time" />

<PreferenceCategory
android:title="@string/confirmation">
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ext {

versionMajor = 1
versionMinor = 5
versionRevision = 3
versionRevision = 4
versionCompatiblity = 'asclepius'

versionCode = 1
Expand Down

0 comments on commit fd756fb

Please sign in to comment.