mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 09:38:52 -06:00
Write tests for IntentScheduler
This commit is contained in:
@@ -27,10 +27,9 @@ import android.os.Build.VERSION.*
|
||||
import android.os.Build.VERSION_CODES.*
|
||||
import android.util.*
|
||||
import org.isoron.androidbase.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.core.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.core.reminders.*
|
||||
import org.isoron.uhabits.core.reminders.ReminderScheduler.*
|
||||
import org.isoron.uhabits.core.utils.*
|
||||
import java.util.*
|
||||
import javax.inject.*
|
||||
@@ -40,36 +39,37 @@ class IntentScheduler
|
||||
@Inject constructor(
|
||||
@AppContext context: Context,
|
||||
private val pendingIntents: PendingIntentFactory
|
||||
) : ReminderScheduler.SystemScheduler {
|
||||
) : SystemScheduler {
|
||||
|
||||
private val manager =
|
||||
context.getSystemService(ALARM_SERVICE) as AlarmManager
|
||||
|
||||
fun schedule(timestamp: Long, intent: PendingIntent, alarmType: Int) {
|
||||
Log.d("IntentScheduler",
|
||||
"timestamp=" + timestamp + " current=" + System.currentTimeMillis())
|
||||
if (timestamp < System.currentTimeMillis()) {
|
||||
private fun schedule(timestamp: Long, intent: PendingIntent, alarmType: Int): SchedulerResult {
|
||||
val now = System.currentTimeMillis()
|
||||
Log.d("IntentScheduler", "timestamp=$timestamp now=$now")
|
||||
if (timestamp < now) {
|
||||
Log.e("IntentScheduler",
|
||||
"Ignoring attempt to schedule intent in the past.")
|
||||
return;
|
||||
return SchedulerResult.IGNORED
|
||||
}
|
||||
if (SDK_INT >= M)
|
||||
manager.setExactAndAllowWhileIdle(alarmType, timestamp, intent)
|
||||
else
|
||||
manager.setExact(alarmType, timestamp, intent)
|
||||
return SchedulerResult.OK
|
||||
}
|
||||
|
||||
override fun scheduleShowReminder(reminderTime: Long,
|
||||
habit: Habit,
|
||||
timestamp: Long) {
|
||||
timestamp: Long): SchedulerResult {
|
||||
val intent = pendingIntents.showReminder(habit, reminderTime, timestamp)
|
||||
schedule(reminderTime, intent, RTC_WAKEUP)
|
||||
logReminderScheduled(habit, reminderTime)
|
||||
return schedule(reminderTime, intent, RTC_WAKEUP)
|
||||
}
|
||||
|
||||
override fun scheduleWidgetUpdate(updateTime: Long) {
|
||||
override fun scheduleWidgetUpdate(updateTime: Long): SchedulerResult {
|
||||
val intent = pendingIntents.updateWidgets()
|
||||
schedule(updateTime, intent, RTC)
|
||||
return schedule(updateTime, intent, RTC)
|
||||
}
|
||||
|
||||
override fun log(componentName: String, msg: String) {
|
||||
|
||||
@@ -48,11 +48,14 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
|
||||
private static final String TAG = "ReminderReceiver";
|
||||
|
||||
private static Intent lastReceivedIntent = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(@Nullable final Context context, @Nullable Intent intent)
|
||||
{
|
||||
if (context == null || intent == null) return;
|
||||
if (intent.getAction() == null) return;
|
||||
lastReceivedIntent = intent;
|
||||
|
||||
HabitsApplication app = (HabitsApplication) context.getApplicationContext();
|
||||
HabitsApplicationComponent appComponent = app.getComponent();
|
||||
@@ -107,4 +110,14 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
Log.e(TAG, "could not process intent", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearLastReceivedIntent()
|
||||
{
|
||||
lastReceivedIntent = null;
|
||||
}
|
||||
|
||||
public static Intent getLastReceivedIntent()
|
||||
{
|
||||
return lastReceivedIntent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,8 @@ public class WidgetReceiver extends BroadcastReceiver
|
||||
|
||||
private static final String TAG = "WidgetReceiver";
|
||||
|
||||
private static Intent lastReceivedIntent = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent)
|
||||
{
|
||||
@@ -75,6 +77,7 @@ public class WidgetReceiver extends BroadcastReceiver
|
||||
WidgetUpdater widgetUpdater = app.getComponent().getWidgetUpdater();
|
||||
|
||||
Log.i(TAG, String.format("Received intent: %s", intent.toString()));
|
||||
lastReceivedIntent = intent;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -112,6 +115,7 @@ public class WidgetReceiver extends BroadcastReceiver
|
||||
controller.onRemoveRepetition(data.getHabit(),
|
||||
data.getTimestamp());
|
||||
break;
|
||||
|
||||
case ACTION_SET_NUMERICAL_VALUE:
|
||||
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
|
||||
Intent numberSelectorIntent = new Intent(context, NumericalCheckmarkWidgetActivity.class);
|
||||
@@ -121,6 +125,7 @@ public class WidgetReceiver extends BroadcastReceiver
|
||||
parser.copyIntentData(intent,numberSelectorIntent);
|
||||
context.startActivity(numberSelectorIntent);
|
||||
break;
|
||||
|
||||
case ACTION_UPDATE_WIDGETS_VALUE:
|
||||
widgetUpdater.updateWidgets();
|
||||
widgetUpdater.scheduleStartDayWidgetUpdate();
|
||||
@@ -139,4 +144,14 @@ public class WidgetReceiver extends BroadcastReceiver
|
||||
{
|
||||
WidgetBehavior getWidgetController();
|
||||
}
|
||||
|
||||
public static Intent getLastReceivedIntent()
|
||||
{
|
||||
return lastReceivedIntent;
|
||||
}
|
||||
|
||||
public static void clearLastReceivedIntent()
|
||||
{
|
||||
lastReceivedIntent = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user