diff --git a/android_app/android_app.iml b/android_app/android_app.iml
deleted file mode 100644
index a0a56048..00000000
--- a/android_app/android_app.iml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/android_app/app/app.iml b/android_app/app/app.iml
deleted file mode 100644
index 01e031b8..00000000
--- a/android_app/app/app.iml
+++ /dev/null
@@ -1,122 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- generateDebugSources
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/android_app/app/src/main/java/com/health/openscale/core/IScaleDatabaseEntryListener.java b/android_app/app/src/main/java/com/health/openscale/core/IScaleDatabaseEntryListener.java
new file mode 100644
index 00000000..2286f5ca
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/IScaleDatabaseEntryListener.java
@@ -0,0 +1,8 @@
+package com.health.openscale.core;
+
+import android.content.Context;
+
+public interface IScaleDatabaseEntryListener
+{
+ void entryChanged(Context context, ScaleData data);
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java
index 0340d085..4530476d 100644
--- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java
+++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java
@@ -23,6 +23,7 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
+import com.health.openscale.core.alarm.AlarmDatabaseEntryListener;
import com.health.openscale.gui.FragmentUpdateListener;
import java.io.BufferedReader;
@@ -61,6 +62,7 @@ public class OpenScale {
private OpenScale(Context con) {
context = con;
scaleDB = new ScaleDatabase(context);
+ scaleDB.addEntryListener(new AlarmDatabaseEntryListener());
scaleUserDB = new ScaleUserDatabase(context);
btCom = null;
fragmentList = new ArrayList<>();
diff --git a/android_app/app/src/main/java/com/health/openscale/core/ScaleDatabase.java b/android_app/app/src/main/java/com/health/openscale/core/ScaleDatabase.java
index c1a55819..82ceaa8e 100644
--- a/android_app/app/src/main/java/com/health/openscale/core/ScaleDatabase.java
+++ b/android_app/app/src/main/java/com/health/openscale/core/ScaleDatabase.java
@@ -28,6 +28,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Locale;
public class ScaleDatabase extends SQLiteOpenHelper {
@@ -84,9 +86,14 @@ public class ScaleDatabase extends SQLiteOpenHelper {
private final SQLiteDatabase dbRead = getReadableDatabase();
private SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
+
+ private Context context;
+ private List entryListeners;
public ScaleDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ this.context = context;
+ entryListeners = new LinkedList<>();
}
@Override
@@ -140,6 +147,7 @@ public class ScaleDatabase extends SQLiteOpenHelper {
try
{
db.insertOrThrow(TABLE_NAME, null, values);
+ notifyEntryListeners(scaleData);
}
catch (SQLException e)
{
@@ -167,6 +175,7 @@ public class ScaleDatabase extends SQLiteOpenHelper {
values.put(COLUMN_NAME_ENABLE, 1);
dbWrite.update(TABLE_NAME, values, COLUMN_NAME_ID + "=" + id, null);
+ notifyEntryListeners(scaleData);
}
public ScaleData getDataEntry(long id)
@@ -319,4 +328,20 @@ public class ScaleDatabase extends SQLiteOpenHelper {
return scaleData;
}
+
+ public void addEntryListener(IScaleDatabaseEntryListener listener)
+ {
+ if (!entryListeners.contains(listener)) entryListeners.add(listener);
+ }
+
+ public void removeEntryListener( IScaleDatabaseEntryListener listener)
+ {
+ entryListeners.remove(listener);
+ }
+
+ private void notifyEntryListeners(ScaleData data)
+ {
+ for (IScaleDatabaseEntryListener listener : entryListeners)
+ listener.entryChanged(context, data);
+ }
}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/Util.java b/android_app/app/src/main/java/com/health/openscale/core/Util.java
new file mode 100644
index 00000000..ef657189
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/Util.java
@@ -0,0 +1,16 @@
+package com.health.openscale.core;
+
+import java.util.Calendar;
+
+public class Util
+{
+ public static boolean isSameDate(Calendar c1, Calendar c2)
+ {
+ int[] dateFields = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH};
+ for (int dateField : dateFields)
+ {
+ if (c1.get(dateField) != c2.get(dateField)) return false;
+ }
+ return true;
+ }
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmDatabaseEntryListener.java b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmDatabaseEntryListener.java
new file mode 100644
index 00000000..ec6c0d1a
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmDatabaseEntryListener.java
@@ -0,0 +1,28 @@
+package com.health.openscale.core.alarm;
+
+import android.content.Context;
+
+import com.health.openscale.core.IScaleDatabaseEntryListener;
+import com.health.openscale.core.ScaleData;
+import com.health.openscale.core.Util;
+
+import java.util.Calendar;
+
+public class AlarmDatabaseEntryListener implements IScaleDatabaseEntryListener
+{
+ @Override
+ public void entryChanged(Context context, ScaleData data)
+ {
+ long dataMillis = data.date_time.getTime();
+
+ Calendar dataTimestamp = Calendar.getInstance();
+ dataTimestamp.setTimeInMillis(dataMillis);
+
+ if(Util.isSameDate(dataTimestamp, Calendar.getInstance()))
+ {
+ AlarmHandler alarmHandler = new AlarmHandler();
+ alarmHandler.cancelAlarmNotification(context);
+ alarmHandler.cancelAndRescheduleAlarmForNextWeek( context, dataTimestamp );
+ }
+ }
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntry.java b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntry.java
new file mode 100644
index 00000000..7656936f
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntry.java
@@ -0,0 +1,77 @@
+package com.health.openscale.core.alarm;
+
+import android.support.annotation.NonNull;
+
+import java.util.Calendar;
+
+public class AlarmEntry implements Comparable
+{
+ private final int dayOfWeek;
+ private final long timeInMillis;
+
+ public AlarmEntry(int dayOfWeek, long timeInMillis)
+ {
+ this.dayOfWeek = dayOfWeek;
+ this.timeInMillis = timeInMillis;
+ }
+
+ public int getDayOfWeek()
+ {
+ return dayOfWeek;
+ }
+
+ private long getTimeInMillis()
+ {
+ return timeInMillis;
+ }
+
+ public Calendar getNextTimestamp()
+ {
+ // We just want the time *not* the date
+ Calendar nextAlarmTimestamp = Calendar.getInstance();
+ nextAlarmTimestamp.setTimeInMillis(getTimeInMillis());
+
+ Calendar alarmCal = Calendar.getInstance();
+ alarmCal.set(Calendar.HOUR_OF_DAY, nextAlarmTimestamp.get(Calendar.HOUR_OF_DAY));
+ alarmCal.set(Calendar.MINUTE, nextAlarmTimestamp.get(Calendar.MINUTE));
+ alarmCal.set(Calendar.SECOND, 0);
+ alarmCal.set(Calendar.DAY_OF_WEEK, getDayOfWeek());
+
+ // Check we aren't setting it in the past which would trigger it to fire instantly
+ if (alarmCal.before(Calendar.getInstance())) alarmCal.add(Calendar.DAY_OF_YEAR, 7);
+ return alarmCal;
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ AlarmEntry that = (AlarmEntry) o;
+
+ if (dayOfWeek != that.dayOfWeek) return false;
+ return timeInMillis == that.timeInMillis;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = dayOfWeek;
+ result = 31 * result + (int) (timeInMillis ^ (timeInMillis >>> 32));
+ return result;
+ }
+
+ @Override
+ public int compareTo(@NonNull AlarmEntry o)
+ {
+ int rc = compare(dayOfWeek, o.dayOfWeek);
+ if (rc == 0) rc = compare(timeInMillis, o.timeInMillis);
+ return rc;
+ }
+
+ private int compare(long x, long y)
+ {
+ return (x < y) ? -1 : ((x == y) ? 0 : 1);
+ }
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntryReader.java b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntryReader.java
new file mode 100644
index 00000000..8a0197c7
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmEntryReader.java
@@ -0,0 +1,91 @@
+package com.health.openscale.core.alarm;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+import com.health.openscale.R;
+
+import java.util.Calendar;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.TreeSet;
+
+import static com.health.openscale.gui.preferences.ReminderPreferences.PREFERENCE_KEY_REMINDER_NOTIFY_TEXT;
+import static com.health.openscale.gui.preferences.ReminderPreferences.PREFERENCE_KEY_REMINDER_TIME;
+import static com.health.openscale.gui.preferences.ReminderPreferences.PREFERENCE_KEY_REMINDER_WEEKDAYS;
+
+public class AlarmEntryReader
+{
+ private Set alarmEntries;
+ private String alarmNotificationText;
+
+ private AlarmEntryReader(Set alarmEntries, String alarmNotificationText)
+ {
+ this.alarmEntries = alarmEntries;
+ this.alarmNotificationText = alarmNotificationText;
+ }
+
+ public Set getEntries()
+ {
+ return alarmEntries;
+ }
+
+ public String getNotificationText()
+ {
+ return alarmNotificationText;
+ }
+
+ public static AlarmEntryReader construct(Context context)
+ {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+
+ Set reminderWeekdays = prefs.getStringSet(PREFERENCE_KEY_REMINDER_WEEKDAYS, new HashSet());
+ Long reminderTimeInMillis = prefs.getLong(PREFERENCE_KEY_REMINDER_TIME, System.currentTimeMillis());
+ String notifyText = prefs.getString(PREFERENCE_KEY_REMINDER_NOTIFY_TEXT,
+ context.getResources().getString(R.string.default_value_reminder_notify_text));
+
+ Set alarms = new TreeSet<>();
+
+ for (String dayOfWeek : reminderWeekdays)
+ {
+ AlarmEntry alarm = getAlarmEntry(dayOfWeek, reminderTimeInMillis);
+ alarms.add(alarm);
+ }
+
+ return new AlarmEntryReader(alarms, notifyText);
+ }
+
+ private static AlarmEntry getAlarmEntry(String dayOfWeek, Long reminderTimeInMillis)
+ {
+ AlarmEntry alarmEntry;
+ switch (dayOfWeek)
+ {
+ case "Monday":
+ alarmEntry = new AlarmEntry(Calendar.MONDAY, reminderTimeInMillis);
+ break;
+ case "Tuesday":
+ alarmEntry = new AlarmEntry(Calendar.TUESDAY, reminderTimeInMillis);
+ break;
+ case "Wednesday":
+ alarmEntry = new AlarmEntry(Calendar.WEDNESDAY, reminderTimeInMillis);
+ break;
+ case "Thursday":
+ alarmEntry = new AlarmEntry(Calendar.THURSDAY, reminderTimeInMillis);
+ break;
+ case "Friday":
+ alarmEntry = new AlarmEntry(Calendar.FRIDAY, reminderTimeInMillis);
+ break;
+ case "Saturday":
+ alarmEntry = new AlarmEntry(Calendar.SATURDAY, reminderTimeInMillis);
+ break;
+ default:
+ case "Sunday":
+ alarmEntry = new AlarmEntry(Calendar.SUNDAY, reminderTimeInMillis);
+ break;
+ }
+ return alarmEntry;
+ }
+
+
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmHandler.java b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmHandler.java
new file mode 100644
index 00000000..5042f82d
--- /dev/null
+++ b/android_app/app/src/main/java/com/health/openscale/core/alarm/AlarmHandler.java
@@ -0,0 +1,149 @@
+package com.health.openscale.core.alarm;
+
+import android.app.AlarmManager;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+import android.service.notification.StatusBarNotification;
+import android.support.v4.app.NotificationCompat;
+import android.util.Log;
+
+import com.health.openscale.R;
+import com.health.openscale.core.Util;
+import com.health.openscale.gui.MainActivity;
+import com.health.openscale.gui.ReminderBootReceiver;
+
+import java.util.Calendar;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import static android.content.Context.NOTIFICATION_SERVICE;
+
+public class AlarmHandler
+{
+ public static final String INTENT_EXTRA_ALARM = "alarmIntent";
+ private static final int ALARM_NOTIFICATION_ID = 0x01;
+ private static final String LOG_TAG = "AlarmBuilder";
+
+ public void scheduleAlarms(Context context)
+ {
+ AlarmEntryReader reader = AlarmEntryReader.construct(context);
+ Set alarmEntries = reader.getEntries();
+
+ disableAllAlarms(context);
+ enableAlarms(context, alarmEntries);
+ }
+
+ private void enableAlarms(Context context, Set alarmEntries)
+ {
+ for (AlarmEntry alarmEntry : alarmEntries)
+ enableAlarm(context, alarmEntry);
+ }
+
+ private void enableAlarm(Context context, AlarmEntry alarmEntry)
+ {
+ int dayOfWeek = alarmEntry.getDayOfWeek();
+ Calendar nextAlarmTimestamp = alarmEntry.getNextTimestamp();
+
+ setRepeatingAlarm(context, dayOfWeek, nextAlarmTimestamp);
+ }
+
+ private void setRepeatingAlarm(Context context, int dayOfWeek, Calendar nextAlarmTimestamp)
+ {
+ Log.d(LOG_TAG, "Set repeating alarm for " + nextAlarmTimestamp.getTime());
+ PendingIntent alarmPendingIntent = getPendingAlarmIntent(context, dayOfWeek);
+ AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, nextAlarmTimestamp.getTimeInMillis(),
+ AlarmManager.INTERVAL_DAY * 7, alarmPendingIntent);
+ }
+
+ private List getWeekdaysPendingAlarmIntent(Context context)
+ {
+ final int[] dayOfWeeks =
+ {Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY,
+ Calendar.SATURDAY, Calendar.SUNDAY};
+ List pendingIntents = new LinkedList<>();
+ for (int dayOfWeek : dayOfWeeks)
+ pendingIntents.add(getPendingAlarmIntent(context, dayOfWeek));
+ return pendingIntents;
+ }
+
+ private PendingIntent getPendingAlarmIntent(Context context, int dayOfWeek)
+ {
+ Intent alarmIntent = new Intent(context, ReminderBootReceiver.class);
+ alarmIntent.putExtra(INTENT_EXTRA_ALARM, true);
+
+ return PendingIntent.getBroadcast(context, dayOfWeek, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ }
+
+ public void disableAllAlarms(Context context)
+ {
+ AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ List pendingIntents = getWeekdaysPendingAlarmIntent(context);
+ for (PendingIntent pendingIntent : pendingIntents)
+ alarmMgr.cancel(pendingIntent);
+ }
+
+ public void cancelAndRescheduleAlarmForNextWeek(Context context, Calendar timestamp)
+ {
+ AlarmEntryReader reader = AlarmEntryReader.construct(context);
+ Set alarmEntries = reader.getEntries();
+ for (AlarmEntry entry : alarmEntries)
+ {
+ Calendar nextAlarmTimestamp = entry.getNextTimestamp();
+
+ if (Util.isSameDate(timestamp, nextAlarmTimestamp))
+ {
+ int dayOfWeek = entry.getDayOfWeek();
+ PendingIntent alarmPendingIntent = getPendingAlarmIntent(context, dayOfWeek);
+ AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ alarmMgr.cancel(alarmPendingIntent);
+
+ nextAlarmTimestamp.add(Calendar.DATE, 7);
+ setRepeatingAlarm(context, dayOfWeek, nextAlarmTimestamp);
+ }
+ }
+ }
+
+ public void showAlarmNotification(Context context)
+ {
+ AlarmEntryReader reader = AlarmEntryReader.construct(context);
+ String notifyText = reader.getNotificationText();
+
+ Intent notifyIntent = new Intent(context, MainActivity.class);
+
+ PendingIntent notifyPendingIntent =
+ PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+
+ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
+ Notification notification = mBuilder.setSmallIcon(R.drawable.ic_launcher)
+ .setContentTitle(context.getString(R.string.app_name))
+ .setContentText(notifyText)
+ .setAutoCancel(true)
+ .setContentIntent(notifyPendingIntent)
+ .build();
+
+ NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
+ mNotifyMgr.notify(ALARM_NOTIFICATION_ID, notification);
+ }
+
+ public void cancelAlarmNotification(Context context)
+ {
+ NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
+ {
+ StatusBarNotification[] activeNotifications = mNotifyMgr.getActiveNotifications();
+ for (StatusBarNotification notification : activeNotifications)
+ {
+ if (notification.getId() == ALARM_NOTIFICATION_ID) mNotifyMgr.cancel(ALARM_NOTIFICATION_ID);
+ }
+ }
+ else mNotifyMgr.cancel(ALARM_NOTIFICATION_ID);
+ }
+
+
+}
diff --git a/android_app/app/src/main/java/com/health/openscale/gui/ReminderBootReceiver.java b/android_app/app/src/main/java/com/health/openscale/gui/ReminderBootReceiver.java
index 7ac484e8..f43bce46 100644
--- a/android_app/app/src/main/java/com/health/openscale/gui/ReminderBootReceiver.java
+++ b/android_app/app/src/main/java/com/health/openscale/gui/ReminderBootReceiver.java
@@ -16,51 +16,31 @@
package com.health.openscale.gui;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
-import android.preference.PreferenceManager;
-import android.support.v4.app.NotificationCompat;
-import com.health.openscale.R;
-import com.health.openscale.gui.preferences.ReminderPreferences;
-
-import static android.content.Context.NOTIFICATION_SERVICE;
+import com.health.openscale.core.alarm.AlarmHandler;
public class ReminderBootReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
- if (intent.hasExtra(ReminderPreferences.INTENT_EXTRA_ALARM))
- {
- //Log.d(ReminderBootReceiver.class.getSimpleName(), "Received alarm intent");
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ if (intent.hasExtra(AlarmHandler.INTENT_EXTRA_ALARM)) handleAlarm(context);
- String notifyText = prefs.getString(ReminderPreferences.PREFERENCE_KEY_REMINDER_NOTIFY_TEXT,
- context.getResources().getString(R.string.default_value_reminder_notify_text));
+ if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) scheduleAlarms(context);
+ }
- NotificationCompat.Builder mBuilder =
- new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
- .setContentTitle(context.getString(R.string.app_name))
- .setContentText(notifyText)
- .setAutoCancel(true);
+ private void handleAlarm(Context context)
+ {
+ AlarmHandler alarmHandler = new AlarmHandler();
+ alarmHandler.showAlarmNotification(context);
+ }
- Intent notifyIntent = new Intent(context, MainActivity.class);
-
- PendingIntent notifyPendingIntent =
- PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-
- mBuilder.setContentIntent(notifyPendingIntent);
-
- NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
- mNotifyMgr.notify(0x01, mBuilder.build());
- }
-
- if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
- ReminderPreferences.scheduleAlarms(context);
+ private void scheduleAlarms(Context context)
+ {
+ AlarmHandler alarmHandler = new AlarmHandler();
+ alarmHandler.scheduleAlarms(context);
}
}
diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/ReminderPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/ReminderPreferences.java
index 08f91ce5..eb354c2f 100644
--- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/ReminderPreferences.java
+++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/ReminderPreferences.java
@@ -15,11 +15,7 @@
*/
package com.health.openscale.gui.preferences;
-import android.app.AlarmManager;
-import android.app.PendingIntent;
import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
@@ -30,120 +26,29 @@ import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
-import android.preference.PreferenceManager;
import com.health.openscale.R;
+import com.health.openscale.core.alarm.AlarmHandler;
import com.health.openscale.gui.ReminderBootReceiver;
import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ReminderPreferences extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener
{
- public static final String INTENT_EXTRA_ALARM = "alarmIntent";
+
public static final String PREFERENCE_KEY_REMINDER_NOTIFY_TEXT = "reminderNotifyText";
-
+ public static final String PREFERENCE_KEY_REMINDER_WEEKDAYS = "reminderWeekdays";
+ public static final String PREFERENCE_KEY_REMINDER_TIME = "reminderTime";
private static final String PREFERENCE_KEY_REMINDER_ENABLE = "reminderEnable";
- private static final String PREFERENCE_KEY_REMINDER_WEEKDAYS = "reminderWeekdays";
- private static final String PREFERENCE_KEY_REMINDER_TIME = "reminderTime";
-
-
- private static ArrayList pendingAlarms = new ArrayList<>();
private CheckBoxPreference reminderEnable;
private MultiSelectListPreference reminderWeekdays;
private TimePreferenceDialog reminderTime;
private EditTextPreference reminderNotifyText;
- public static void scheduleAlarms(Context context)
- {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
-
- Set reminderWeekdays = prefs.getStringSet(PREFERENCE_KEY_REMINDER_WEEKDAYS, new HashSet());
- Long reminderTimeInMillis = prefs.getLong(PREFERENCE_KEY_REMINDER_TIME, System.currentTimeMillis());
-
- Iterator iterWeekdays = reminderWeekdays.iterator();
-
- disableAllAlarms(context);
-
- while (iterWeekdays.hasNext())
- {
- String strWeekdays = iterWeekdays.next();
- switch (strWeekdays)
- {
- case "Monday":
- pendingAlarms.add(enableAlarm(context, Calendar.MONDAY, reminderTimeInMillis));
- break;
- case "Tuesday":
- pendingAlarms.add(enableAlarm(context, Calendar.TUESDAY, reminderTimeInMillis));
- break;
- case "Wednesday":
- pendingAlarms.add(enableAlarm(context, Calendar.WEDNESDAY, reminderTimeInMillis));
- break;
- case "Thursday":
- pendingAlarms.add(enableAlarm(context, Calendar.THURSDAY, reminderTimeInMillis));
- break;
- case "Friday":
- pendingAlarms.add(enableAlarm(context, Calendar.FRIDAY, reminderTimeInMillis));
- break;
- case "Saturday":
- pendingAlarms.add(enableAlarm(context, Calendar.SATURDAY, reminderTimeInMillis));
- break;
- case "Sunday":
- pendingAlarms.add(enableAlarm(context, Calendar.SUNDAY, reminderTimeInMillis));
- break;
- }
- }
- }
-
- public static PendingIntent enableAlarm(Context context, int dayOfWeek, long timeInMillis)
- {
- // We just want the time *not* the date
- Calendar timeCal = Calendar.getInstance();
- timeCal.setTimeInMillis(timeInMillis);
-
- Calendar alarmCal = Calendar.getInstance();
- alarmCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
- alarmCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
- alarmCal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
-
- // Check we aren't setting it in the past which would trigger it to fire instantly
- if (alarmCal.before(Calendar.getInstance()))
- {
- alarmCal.add(Calendar.DAY_OF_YEAR, 7);
- }
-
- //Log.d(ReminderPreferences.class.getSimpleName(), "Set " + dayOfWeek + " alarm to " + alarmCal.getTime());
- AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-
- Intent alarmIntent = new Intent(context, ReminderBootReceiver.class);
- alarmIntent.putExtra(INTENT_EXTRA_ALARM, true);
-
- PendingIntent alarmPendingIntent =
- PendingIntent.getBroadcast(context, dayOfWeek, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-
- alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
- alarmPendingIntent);
-
- return alarmPendingIntent;
- }
-
- public static void disableAllAlarms(Context context)
- {
- AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-
- for (int i = 0; i < pendingAlarms.size(); i++)
- {
- alarmMgr.cancel(pendingAlarms.get(i));
- }
-
- pendingAlarms.clear();
- }
@Override
public void onCreate(Bundle savedInstanceState)
@@ -203,9 +108,10 @@ public class ReminderPreferences extends PreferenceFragment
ComponentName receiver = new ComponentName(getActivity().getApplicationContext(), ReminderBootReceiver.class);
PackageManager pm = getActivity().getApplicationContext().getPackageManager();
+ AlarmHandler alarmHandler = new AlarmHandler();
if (reminderEnable.isChecked())
{
- scheduleAlarms(getActivity());
+ alarmHandler.scheduleAlarms(getActivity());
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
@@ -216,7 +122,7 @@ public class ReminderPreferences extends PreferenceFragment
}
else
{
- disableAllAlarms(getActivity());
+ alarmHandler.disableAllAlarms(getActivity());
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
diff --git a/android_app/build.gradle b/android_app/build.gradle
index 97e8b8e3..1c2d0680 100644
--- a/android_app/build.gradle
+++ b/android_app/build.gradle
@@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.0'
+ classpath 'com.android.tools.build:gradle:2.3.1'
}
}
diff --git a/android_app/openScale.iml b/android_app/openScale.iml
deleted file mode 100644
index 2a022014..00000000
--- a/android_app/openScale.iml
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-