mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-29 19:20:36 +02:00
* Cancel alarm and reschedule for next week, if data entered because alarm timestamp
* Remove notification on data input, if still present
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="android_app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="java-gradle" name="Java-Gradle">
|
||||
<configuration>
|
||||
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
|
||||
<option name="BUILDABLE" value="false" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@@ -1,122 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="android-gradle" name="Android-Gradle">
|
||||
<configuration>
|
||||
<option name="GRADLE_PROJECT_PATH" value=":app" />
|
||||
</configuration>
|
||||
</facet>
|
||||
<facet type="android" name="Android">
|
||||
<configuration>
|
||||
<option name="SELECTED_BUILD_VARIANT" value="debug" />
|
||||
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
|
||||
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
|
||||
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
|
||||
<afterSyncTasks>
|
||||
<task>generateDebugSources</task>
|
||||
</afterSyncTasks>
|
||||
<option name="ALLOW_USER_CONFIGURATION" value="false" />
|
||||
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
|
||||
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
|
||||
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
|
||||
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
|
||||
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/debug" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/debug" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/debug" isTestSource="false" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/debug" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/debug" isTestSource="true" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/androidTest/debug" isTestSource="true" generated="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/debug" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-resources" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" exported="" name="support-v4-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-compat-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-media-compat-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-core-ui-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="hellocharts-library-1.5.8" level="project" />
|
||||
<orderEntry type="library" exported="" name="appcompat-v7-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-annotations-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-vector-drawable-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-core-utils-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="support-fragment-25.2.0" level="project" />
|
||||
<orderEntry type="library" exported="" name="animated-vector-drawable-25.2.0" level="project" />
|
||||
</component>
|
||||
</module>
|
@@ -0,0 +1,8 @@
|
||||
package com.health.openscale.core;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public interface IScaleDatabaseEntryListener
|
||||
{
|
||||
void entryChanged(Context context, ScaleData data);
|
||||
}
|
@@ -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<>();
|
||||
|
@@ -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<IScaleDatabaseEntryListener> 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);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
package com.health.openscale.core.alarm;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public class AlarmEntry implements Comparable<AlarmEntry>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@@ -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<AlarmEntry> alarmEntries;
|
||||
private String alarmNotificationText;
|
||||
|
||||
private AlarmEntryReader(Set<AlarmEntry> alarmEntries, String alarmNotificationText)
|
||||
{
|
||||
this.alarmEntries = alarmEntries;
|
||||
this.alarmNotificationText = alarmNotificationText;
|
||||
}
|
||||
|
||||
public Set<AlarmEntry> getEntries()
|
||||
{
|
||||
return alarmEntries;
|
||||
}
|
||||
|
||||
public String getNotificationText()
|
||||
{
|
||||
return alarmNotificationText;
|
||||
}
|
||||
|
||||
public static AlarmEntryReader construct(Context context)
|
||||
{
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
Set<String> reminderWeekdays = prefs.getStringSet(PREFERENCE_KEY_REMINDER_WEEKDAYS, new HashSet<String>());
|
||||
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<AlarmEntry> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -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<AlarmEntry> alarmEntries = reader.getEntries();
|
||||
|
||||
disableAllAlarms(context);
|
||||
enableAlarms(context, alarmEntries);
|
||||
}
|
||||
|
||||
private void enableAlarms(Context context, Set<AlarmEntry> 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<PendingIntent> getWeekdaysPendingAlarmIntent(Context context)
|
||||
{
|
||||
final int[] dayOfWeeks =
|
||||
{Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY,
|
||||
Calendar.SATURDAY, Calendar.SUNDAY};
|
||||
List<PendingIntent> 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<PendingIntent> pendingIntents = getWeekdaysPendingAlarmIntent(context);
|
||||
for (PendingIntent pendingIntent : pendingIntents)
|
||||
alarmMgr.cancel(pendingIntent);
|
||||
}
|
||||
|
||||
public void cancelAndRescheduleAlarmForNextWeek(Context context, Calendar timestamp)
|
||||
{
|
||||
AlarmEntryReader reader = AlarmEntryReader.construct(context);
|
||||
Set<AlarmEntry> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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<PendingIntent> 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<String> reminderWeekdays = prefs.getStringSet(PREFERENCE_KEY_REMINDER_WEEKDAYS, new HashSet<String>());
|
||||
Long reminderTimeInMillis = prefs.getLong(PREFERENCE_KEY_REMINDER_TIME, System.currentTimeMillis());
|
||||
|
||||
Iterator<String> 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);
|
||||
|
@@ -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'
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="java-gradle" name="Java-Gradle">
|
||||
<configuration>
|
||||
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/build/classes/main" />
|
||||
<output-test url="file://$MODULE_DIR$/build/classes/test" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
|
Reference in New Issue
Block a user