mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Update widgets at midnight (#634)
This commit is contained in:
@@ -62,6 +62,7 @@ class HabitsApplication : Application() {
|
|||||||
|
|
||||||
widgetUpdater = component.widgetUpdater
|
widgetUpdater = component.widgetUpdater
|
||||||
widgetUpdater.startListening()
|
widgetUpdater.startListening()
|
||||||
|
widgetUpdater.scheduleStartDayWidgetUpdate()
|
||||||
|
|
||||||
reminderScheduler = component.reminderScheduler
|
reminderScheduler = component.reminderScheduler
|
||||||
reminderScheduler.startListening()
|
reminderScheduler.startListening()
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class IntentScheduler
|
|||||||
private val manager =
|
private val manager =
|
||||||
context.getSystemService(ALARM_SERVICE) as AlarmManager
|
context.getSystemService(ALARM_SERVICE) as AlarmManager
|
||||||
|
|
||||||
fun schedule(timestamp: Long, intent: PendingIntent) {
|
fun schedule(timestamp: Long, intent: PendingIntent, alarmType: Int) {
|
||||||
Log.d("IntentScheduler",
|
Log.d("IntentScheduler",
|
||||||
"timestamp=" + timestamp + " current=" + System.currentTimeMillis())
|
"timestamp=" + timestamp + " current=" + System.currentTimeMillis())
|
||||||
if (timestamp < System.currentTimeMillis()) {
|
if (timestamp < System.currentTimeMillis()) {
|
||||||
@@ -54,19 +54,24 @@ class IntentScheduler
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (SDK_INT >= M)
|
if (SDK_INT >= M)
|
||||||
manager.setExactAndAllowWhileIdle(RTC_WAKEUP, timestamp, intent)
|
manager.setExactAndAllowWhileIdle(alarmType, timestamp, intent)
|
||||||
else
|
else
|
||||||
manager.setExact(RTC_WAKEUP, timestamp, intent)
|
manager.setExact(alarmType, timestamp, intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun scheduleShowReminder(reminderTime: Long,
|
override fun scheduleShowReminder(reminderTime: Long,
|
||||||
habit: Habit,
|
habit: Habit,
|
||||||
timestamp: Long) {
|
timestamp: Long) {
|
||||||
val intent = pendingIntents.showReminder(habit, reminderTime, timestamp)
|
val intent = pendingIntents.showReminder(habit, reminderTime, timestamp)
|
||||||
schedule(reminderTime, intent)
|
schedule(reminderTime, intent, RTC_WAKEUP)
|
||||||
logReminderScheduled(habit, reminderTime)
|
logReminderScheduled(habit, reminderTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun scheduleWidgetUpdate(updateTime: Long) {
|
||||||
|
val intent = pendingIntents.updateWidgets()
|
||||||
|
schedule(updateTime, intent, RTC)
|
||||||
|
}
|
||||||
|
|
||||||
override fun log(componentName: String, msg: String) {
|
override fun log(componentName: String, msg: String) {
|
||||||
Log.d(componentName, msg)
|
Log.d(componentName, msg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.isoron.androidbase.*
|
|||||||
import org.isoron.uhabits.core.*
|
import org.isoron.uhabits.core.*
|
||||||
import org.isoron.uhabits.core.models.*
|
import org.isoron.uhabits.core.models.*
|
||||||
import org.isoron.uhabits.receivers.*
|
import org.isoron.uhabits.receivers.*
|
||||||
|
import org.isoron.uhabits.widgets.*
|
||||||
import javax.inject.*
|
import javax.inject.*
|
||||||
|
|
||||||
@AppScope
|
@AppScope
|
||||||
@@ -118,4 +119,12 @@ class PendingIntentFactory
|
|||||||
if (timestamp != null) putExtra("timestamp", timestamp)
|
if (timestamp != null) putExtra("timestamp", timestamp)
|
||||||
},
|
},
|
||||||
FLAG_UPDATE_CURRENT)
|
FLAG_UPDATE_CURRENT)
|
||||||
|
|
||||||
|
fun updateWidgets(): PendingIntent =
|
||||||
|
PendingIntent.getBroadcast(
|
||||||
|
context, 0,
|
||||||
|
Intent(context, WidgetReceiver::class.java).apply {
|
||||||
|
action = WidgetReceiver.ACTION_UPDATE_WIDGETS_VALUE
|
||||||
|
},
|
||||||
|
FLAG_UPDATE_CURRENT)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.isoron.uhabits.*;
|
|||||||
import org.isoron.uhabits.core.preferences.*;
|
import org.isoron.uhabits.core.preferences.*;
|
||||||
import org.isoron.uhabits.core.ui.widgets.*;
|
import org.isoron.uhabits.core.ui.widgets.*;
|
||||||
import org.isoron.uhabits.intents.*;
|
import org.isoron.uhabits.intents.*;
|
||||||
|
import org.isoron.uhabits.widgets.*;
|
||||||
import org.isoron.uhabits.widgets.activities.*;
|
import org.isoron.uhabits.widgets.activities.*;
|
||||||
|
|
||||||
import dagger.*;
|
import dagger.*;
|
||||||
@@ -52,6 +53,9 @@ public class WidgetReceiver extends BroadcastReceiver
|
|||||||
public static final String ACTION_SET_NUMERICAL_VALUE =
|
public static final String ACTION_SET_NUMERICAL_VALUE =
|
||||||
"org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE";
|
"org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE";
|
||||||
|
|
||||||
|
public static final String ACTION_UPDATE_WIDGETS_VALUE =
|
||||||
|
"org.isoron.uhabits.ACTION_UPDATE_WIDGETS_VALUE";
|
||||||
|
|
||||||
private static final String TAG = "WidgetReceiver";
|
private static final String TAG = "WidgetReceiver";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -68,13 +72,17 @@ public class WidgetReceiver extends BroadcastReceiver
|
|||||||
IntentParser parser = app.getComponent().getIntentParser();
|
IntentParser parser = app.getComponent().getIntentParser();
|
||||||
WidgetBehavior controller = component.getWidgetController();
|
WidgetBehavior controller = component.getWidgetController();
|
||||||
Preferences prefs = app.getComponent().getPreferences();
|
Preferences prefs = app.getComponent().getPreferences();
|
||||||
|
WidgetUpdater widgetUpdater = app.getComponent().getWidgetUpdater();
|
||||||
|
|
||||||
Log.i(TAG, String.format("Received intent: %s", intent.toString()));
|
Log.i(TAG, String.format("Received intent: %s", intent.toString()));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
IntentParser.CheckmarkIntentData data;
|
IntentParser.CheckmarkIntentData data = null;
|
||||||
|
if (intent.getAction() != ACTION_UPDATE_WIDGETS_VALUE)
|
||||||
|
{
|
||||||
data = parser.parseCheckmarkIntent(intent);
|
data = parser.parseCheckmarkIntent(intent);
|
||||||
|
}
|
||||||
|
|
||||||
switch (intent.getAction())
|
switch (intent.getAction())
|
||||||
{
|
{
|
||||||
@@ -112,6 +120,10 @@ public class WidgetReceiver extends BroadcastReceiver
|
|||||||
parser.copyIntentData(intent,numberSelectorIntent);
|
parser.copyIntentData(intent,numberSelectorIntent);
|
||||||
context.startActivity(numberSelectorIntent);
|
context.startActivity(numberSelectorIntent);
|
||||||
break;
|
break;
|
||||||
|
case ACTION_UPDATE_WIDGETS_VALUE:
|
||||||
|
widgetUpdater.updateWidgets();
|
||||||
|
widgetUpdater.scheduleStartDayWidgetUpdate();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (RuntimeException e)
|
catch (RuntimeException e)
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import org.isoron.androidbase.*
|
|||||||
import org.isoron.uhabits.core.commands.*
|
import org.isoron.uhabits.core.commands.*
|
||||||
import org.isoron.uhabits.core.preferences.*
|
import org.isoron.uhabits.core.preferences.*
|
||||||
import org.isoron.uhabits.core.tasks.*
|
import org.isoron.uhabits.core.tasks.*
|
||||||
|
import org.isoron.uhabits.core.utils.*
|
||||||
|
import org.isoron.uhabits.intents.*
|
||||||
import javax.inject.*
|
import javax.inject.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,7 +38,8 @@ class WidgetUpdater
|
|||||||
@AppContext private val context: Context,
|
@AppContext private val context: Context,
|
||||||
private val commandRunner: CommandRunner,
|
private val commandRunner: CommandRunner,
|
||||||
private val taskRunner: TaskRunner,
|
private val taskRunner: TaskRunner,
|
||||||
private val widgetPrefs: WidgetPreferences
|
private val widgetPrefs: WidgetPreferences,
|
||||||
|
private val intentScheduler: IntentScheduler
|
||||||
) : CommandRunner.Listener {
|
) : CommandRunner.Listener {
|
||||||
|
|
||||||
override fun onCommandExecuted(command: Command, refreshKey: Long?) {
|
override fun onCommandExecuted(command: Command, refreshKey: Long?) {
|
||||||
@@ -60,6 +63,11 @@ class WidgetUpdater
|
|||||||
commandRunner.removeListener(this)
|
commandRunner.removeListener(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun scheduleStartDayWidgetUpdate() {
|
||||||
|
val timestamp = DateUtils.getStartOfTomorrow()
|
||||||
|
intentScheduler.scheduleWidgetUpdate(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
fun updateWidgets(modifiedHabitId: Long?) {
|
fun updateWidgets(modifiedHabitId: Long?) {
|
||||||
taskRunner.execute {
|
taskRunner.execute {
|
||||||
updateWidgets(modifiedHabitId, CheckmarkWidgetProvider::class.java)
|
updateWidgets(modifiedHabitId, CheckmarkWidgetProvider::class.java)
|
||||||
|
|||||||
@@ -164,6 +164,8 @@ public class ReminderScheduler implements CommandRunner.Listener
|
|||||||
{
|
{
|
||||||
void scheduleShowReminder(long reminderTime, Habit habit, long timestamp);
|
void scheduleShowReminder(long reminderTime, Habit habit, long timestamp);
|
||||||
|
|
||||||
|
void scheduleWidgetUpdate(long updateTime);
|
||||||
|
|
||||||
void log(String componentName, String msg);
|
void log(String componentName, String msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,9 +175,14 @@ public abstract class DateUtils
|
|||||||
return getStartOfDay(getLocalTime());
|
return getStartOfDay(getLocalTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static long getStartOfTomorrow()
|
||||||
|
{
|
||||||
|
return getUpcomingTimeInMillis(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public static long millisecondsUntilTomorrow()
|
public static long millisecondsUntilTomorrow()
|
||||||
{
|
{
|
||||||
return getStartOfToday() + DAY_LENGTH - getLocalTime();
|
return getStartOfTomorrow() - getLocalTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GregorianCalendar getStartOfTodayCalendar()
|
public static GregorianCalendar getStartOfTodayCalendar()
|
||||||
|
|||||||
@@ -164,6 +164,7 @@ public class DateUtilsTest extends BaseUnitTest
|
|||||||
@Test
|
@Test
|
||||||
public void testMillisecondsUntilTomorrow() throws Exception
|
public void testMillisecondsUntilTomorrow() throws Exception
|
||||||
{
|
{
|
||||||
|
DateUtils.setFixedTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 23, 59));
|
DateUtils.setFixedLocalTime(unixTime(2017, JANUARY, 1, 23, 59));
|
||||||
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(60000L));
|
assertThat(DateUtils.millisecondsUntilTomorrow(), equalTo(60000L));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user