2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, José Rebelo
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-05-07 23:46:18 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
|
|
|
|
2023-09-02 11:13:14 +02:00
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
2015-05-07 23:46:18 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2023-09-02 11:13:14 +02:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.SystemClock;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2023-09-02 11:13:14 +02:00
|
|
|
import org.threeten.bp.Instant;
|
|
|
|
import org.threeten.bp.ZoneId;
|
|
|
|
import org.threeten.bp.zone.ZoneOffsetTransition;
|
|
|
|
import org.threeten.bp.zone.ZoneRules;
|
2015-05-07 23:46:18 +02:00
|
|
|
|
2015-09-05 00:14:09 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2024-02-17 18:34:09 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
2015-09-05 00:14:09 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
2023-09-02 11:13:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-05-07 23:46:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class TimeChangeReceiver extends BroadcastReceiver {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(TimeChangeReceiver.class);
|
2015-05-07 23:46:18 +02:00
|
|
|
|
2024-02-15 21:07:08 +01:00
|
|
|
public static final String ACTION_DST_CHANGED_OR_PERIODIC_SYNC = "nodomain.freeyourgadget.gadgetbridge.DST_CHANGED_OR_PERIODIC_SYNC";
|
|
|
|
public static final long PERIODIC_SYNC_INTERVAL_MS = 158003000; // 43:53:23.000
|
|
|
|
public static final long PERIODIC_SYNC_INTERVAL_MAX_MS = 172800000; // 48 hours
|
2023-09-02 11:13:14 +02:00
|
|
|
|
2015-05-07 23:46:18 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2023-09-02 11:13:14 +02:00
|
|
|
final Prefs prefs = GBApplication.getPrefs();
|
2015-05-07 23:46:18 +02:00
|
|
|
final String action = intent.getAction();
|
2023-09-02 11:13:14 +02:00
|
|
|
if (action == null) {
|
|
|
|
LOG.warn("Null action");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefs.getBoolean("datetime_synconconnect", true)) {
|
|
|
|
LOG.warn("Ignoring time change for {}, time sync is disabled", action);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case Intent.ACTION_TIME_CHANGED:
|
|
|
|
case Intent.ACTION_TIMEZONE_CHANGED:
|
2024-02-15 21:07:08 +01:00
|
|
|
case ACTION_DST_CHANGED_OR_PERIODIC_SYNC:
|
2023-09-02 11:13:14 +02:00
|
|
|
// Continue after the switch
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG.warn("Unknown action {}", action);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-17 18:34:09 +01:00
|
|
|
// acquire wake lock, otherwise device might enter deep sleep immediately after returning from onReceive()
|
|
|
|
AndroidUtils.acquirePartialWakeLock(context, "TimeSyncWakeLock", 10100);
|
|
|
|
|
2023-09-02 11:13:14 +02:00
|
|
|
final Date newTime = GregorianCalendar.getInstance().getTime();
|
2024-02-15 21:07:08 +01:00
|
|
|
LOG.info("Time/Timezone changed or periodic sync, syncing with device: {} ({}), {}", DateTimeUtils.formatDate(newTime), newTime.toGMTString(), intent.getAction());
|
2023-09-02 11:13:14 +02:00
|
|
|
GBApplication.deviceService().onSetTime();
|
|
|
|
|
2024-02-17 18:34:09 +01:00
|
|
|
// Reschedule the next DST change (since the timezone may have changed) or periodic sync
|
2024-02-15 21:07:08 +01:00
|
|
|
scheduleNextDstChangeOrPeriodicSync(context);
|
2023-09-02 11:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-15 21:07:08 +01:00
|
|
|
* Schedule an alarm to trigger on the next DST change, since ACTION_TIMEZONE_CHANGED is not broadcast otherwise
|
|
|
|
* or schedule an alarm to trigger after PERIODIC_SYNC_INTERVAL_MS (whichever is earlier).
|
2023-09-02 11:13:14 +02:00
|
|
|
*
|
|
|
|
* @param context the context
|
|
|
|
*/
|
2024-02-15 21:07:08 +01:00
|
|
|
public static void scheduleNextDstChangeOrPeriodicSync(final Context context) {
|
2023-09-02 11:13:14 +02:00
|
|
|
final ZoneId zoneId = ZoneId.systemDefault();
|
|
|
|
final ZoneRules zoneRules = zoneId.getRules();
|
|
|
|
final Instant now = Instant.now();
|
|
|
|
final ZoneOffsetTransition transition = zoneRules.nextTransition(now);
|
2023-09-04 15:12:59 +02:00
|
|
|
|
2024-02-15 21:07:08 +01:00
|
|
|
final Intent i = new Intent(ACTION_DST_CHANGED_OR_PERIODIC_SYNC);
|
2023-09-02 11:13:14 +02:00
|
|
|
final PendingIntent pi = PendingIntentUtils.getBroadcast(context, 0, i, 0, false);
|
|
|
|
|
|
|
|
final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
2024-02-15 21:07:08 +01:00
|
|
|
boolean exactAlarm = false;
|
|
|
|
long delayMillis = PERIODIC_SYNC_INTERVAL_MS;
|
|
|
|
|
|
|
|
if (transition != null) {
|
|
|
|
final long nextDstMillis = transition.getInstant().toEpochMilli();
|
|
|
|
final long dstDelayMillis = nextDstMillis - now.toEpochMilli() + 5000L;
|
|
|
|
if (dstDelayMillis < PERIODIC_SYNC_INTERVAL_MAX_MS) {
|
|
|
|
exactAlarm = canScheduleExactAlarms(context, am);
|
|
|
|
delayMillis = dstDelayMillis;
|
|
|
|
LOG.info("Scheduling next DST change: {} (in {} millis) (exact = {})", nextDstMillis, delayMillis, exactAlarm);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG.warn("No DST transition found for {}", zoneId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delayMillis == PERIODIC_SYNC_INTERVAL_MS) {
|
|
|
|
LOG.info("Scheduling next periodic time sync in {} millis (exact = {})", delayMillis, exactAlarm);
|
|
|
|
}
|
2023-09-02 11:13:14 +02:00
|
|
|
|
|
|
|
am.cancel(pi);
|
|
|
|
|
|
|
|
boolean scheduledExact = false;
|
|
|
|
if (exactAlarm) {
|
|
|
|
try {
|
2024-02-17 18:34:09 +01:00
|
|
|
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
|
2023-09-02 11:13:14 +02:00
|
|
|
scheduledExact = true;
|
|
|
|
} catch (final Exception e) {
|
2024-02-15 21:07:08 +01:00
|
|
|
LOG.error("Failed to schedule exact alarm for next DST change or periodic time sync", e);
|
2023-09-02 11:13:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to inexact alarm if the exact one failed
|
|
|
|
if (!scheduledExact) {
|
|
|
|
try {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
2024-02-17 18:34:09 +01:00
|
|
|
am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
|
2023-09-02 11:13:14 +02:00
|
|
|
} else {
|
2024-02-17 18:34:09 +01:00
|
|
|
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
|
2023-09-02 11:13:14 +02:00
|
|
|
}
|
|
|
|
} catch (final Exception e) {
|
2024-02-15 21:07:08 +01:00
|
|
|
LOG.error("Failed to schedule inexact alarm for next DST change or periodic time sync", e);
|
2023-09-02 11:13:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 23:46:18 +02:00
|
|
|
|
2024-02-15 21:32:44 +01:00
|
|
|
public static void ifEnabledScheduleNextDstChangeOrPeriodicSync(final Context context) {
|
|
|
|
if (GBApplication.getPrefs().getBoolean("datetime_synconconnect", true)) {
|
|
|
|
scheduleNextDstChangeOrPeriodicSync(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 11:13:14 +02:00
|
|
|
private static boolean canScheduleExactAlarms(final Context context, final AlarmManager am) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
|
|
return am.canScheduleExactAlarms();
|
|
|
|
} else {
|
|
|
|
return GB.checkPermission(context, "android.permission.SCHEDULE_EXACT_ALARM");
|
2015-05-07 23:46:18 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-02 11:13:14 +02:00
|
|
|
}
|