Skip to content

Commit

Permalink
fix(#7): make scheduler respect user SIM enabled/disabled state if ti…
Browse files Browse the repository at this point in the history
…me matches current system time (#11)

Signed-off-by: iusmac <[email protected]>
  • Loading branch information
iusmac authored Apr 10, 2024
1 parent 5b0e4ba commit 1e63492
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void onReceive(final Context context, final Intent intent) {
// Need to schedule the next iteration processing of weekly repeat schedules after
// the device has finished booting. Note that, this call should only happen after
// syncing
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context, now);
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context,
now.plusMinutes(1));
break;

case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED:
Expand All @@ -73,10 +74,12 @@ public void onReceive(final Context context, final Intent intent) {
/*overrideUserPreference=*/ false);

// Need to re-schedule the next iteration processing of weekly repeat schedules
// after syncing. Note that, if the SIM card was ejected, the SIM subscription won't
// exist as well, so there will be nothing to sync, but we still need to do
// re-scheduling as there can be other SIM cards in the system with their schedules
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context, now);
// after syncing. Note that, if the SIM card was disabled on devices using legacy
// RIL, the SIM subscription won't exist as well, so there will be nothing to sync,
// but we still need to do re-scheduling as there can be other SIM cards in the
// system with their schedules, otherwise it will cancel existing alarm
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context,
now.plusMinutes(1));
break;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/com/github/iusmac/sevensim/SystemBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public void onReceive(final Context context, final Intent intent) {
// Need to reschedule the next weekly repeat schedule processing iteration, as it
// relies on a RTC-based alarm, which, in turn is independent of any alteration to
// the system time. Note that, this call should only happen after syncing
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context, now);
ForegroundService.updateNextWeeklyRepeatScheduleProcessingIter(context,
now.plusMinutes(1));
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ private PendingIntent getPendingIntent() {
}

/**
* Determine the expected SIM subscription enabled state using an opened interval
* ({@code a<x<b}) of two opposite schedules.
* Determine the expected SIM subscription enabled state using a closed interval
* ({@code a<=x<=b}) of two opposite schedules.
*
* @param sub The subscription for which to determine the expected enabled state.
* @param startDateTime The interval start date-time value. This is expected to be date-time of
Expand All @@ -398,7 +398,8 @@ private PendingIntent getPendingIntent() {
* @param overrideUserPreference Whether the user's preference should NOT take precedence over
* schedule intervals. For instance, if the SIM subscription is expected to be disabled, but the
* user enabled it manually, then pass {@code false} to keep the state within the allowed
* period.
* period. Note that, when passed in {@code true}, an opened interval ({@code a<x<b}) will be
* used instead for comparison.
* @return {@code true} if the SIM subscription is expected to be enabled, {@code false}
* otherwise.
*/
Expand All @@ -408,7 +409,8 @@ private static boolean getSubscriptionExpectedEnabledState(final Subscription su

if (sub.isSimEnabled()) {
return endDateTime.map((end) -> {
if (!overrideUserPreference && sub.getLastActivatedTime().isAfter(end)) {
if (!overrideUserPreference && (sub.getLastActivatedTime().equals(end) ||
sub.getLastActivatedTime().isAfter(end))) {
return true;
}

Expand All @@ -430,7 +432,8 @@ private static boolean getSubscriptionExpectedEnabledState(final Subscription su
});
}
return startDateTime.map((start) -> {
if (!overrideUserPreference && sub.getLastDeactivatedTime().isAfter(start)) {
if (!overrideUserPreference && (sub.getLastDeactivatedTime().equals(start) ||
sub.getLastDeactivatedTime().isAfter(start))) {
return false;
}
return endDateTime.map((end) -> end.isBefore(start)).orElse(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ public SubscriptionSchedulerSummaryBuilder(final @ApplicationContext Context con
// Since we don't support seconds and milliseconds, drop them off to avoid inexact summaries
dateTime = dateTime.truncatedTo(ChronoUnit.MINUTES);

// NOTE: if the SIM subscription state change time matches the target date-time, then we'll
// start seeking for the next weekly repeat schedule date-time that happens no earlier than
// one minute from the target date-time. This to avoid showing a summary for the schedule
// that just happened
final LocalDateTime dateTime2 = sub.getLastActivatedTime().equals(dateTime) ||
sub.getLastDeactivatedTime().equals(dateTime) ? dateTime.plusMinutes(1) : dateTime;
final LocalDateTime nearestScheduleDateTime =
SubscriptionScheduler.getDateTimeAfter(nearestSchedule.get(), dateTime).get();
SubscriptionScheduler.getDateTimeAfter(nearestSchedule.get(), dateTime2).get();

// NOTE: we *must* isolate the usage of the Formatter to avoid result aggregation from
// concurrent threads, also protect the StringBuilder length resetting
Expand Down
5 changes: 3 additions & 2 deletions src/com/github/iusmac/sevensim/telephony/Subscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -103,15 +104,15 @@ public LocalDateTime getLastActivatedTime() {
}

public void setLastActivatedTime(final LocalDateTime lastActivatedTime) {
mLastActivatedTime = lastActivatedTime;
mLastActivatedTime = lastActivatedTime.truncatedTo(ChronoUnit.MINUTES);
}

public LocalDateTime getLastDeactivatedTime() {
return mLastDeactivatedTime;
}

public void setLastDeactivatedTime(final LocalDateTime lastDeactivatedTime) {
mLastDeactivatedTime = lastDeactivatedTime;
mLastDeactivatedTime = lastDeactivatedTime.truncatedTo(ChronoUnit.MINUTES);
}

public Boolean getKeepDisabledAcrossBoots() {
Expand Down

0 comments on commit 1e63492

Please sign in to comment.