wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*  Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, José Rebelo
 
    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
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents;
 
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.time.Instant;
import java.time.ZoneId;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
import java.util.Date;
import java.util.GregorianCalendar;
 
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
 
 
public class TimeChangeReceiver extends BroadcastReceiver {
    private static final Logger LOG = LoggerFactory.getLogger(TimeChangeReceiver.class);
 
    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
 
    @Override
    public void onReceive(Context context, Intent intent) {
        final Prefs prefs = GBApplication.getPrefs();
        final String action = intent.getAction();
        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:
            case ACTION_DST_CHANGED_OR_PERIODIC_SYNC:
                // Continue after the switch
                break;
            default:
                LOG.warn("Unknown action {}", action);
                return;
        }
 
        // acquire wake lock, otherwise device might enter deep sleep immediately after returning from onReceive()
        AndroidUtils.acquirePartialWakeLock(context, "TimeSyncWakeLock", 10100);
 
        final Date newTime = GregorianCalendar.getInstance().getTime();
        LOG.info("Time/Timezone changed or periodic sync, syncing with device: {} ({}), {}", DateTimeUtils.formatDate(newTime), newTime.toGMTString(), intent.getAction());
        GBApplication.deviceService().onSetTime();
 
        // Reschedule the next DST change (since the timezone may have changed) or periodic sync
        scheduleNextDstChangeOrPeriodicSync(context);
    }
 
    /**
     * 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).
     *
     * @param context the context
     */
    public static void scheduleNextDstChangeOrPeriodicSync(final Context context) {
        final ZoneId zoneId = ZoneId.systemDefault();
        final ZoneRules zoneRules = zoneId.getRules();
        final Instant now = Instant.now();
        final ZoneOffsetTransition transition = zoneRules.nextTransition(now);
 
        final Intent i = new Intent(ACTION_DST_CHANGED_OR_PERIODIC_SYNC);
        i.setPackage(BuildConfig.APPLICATION_ID);
        final PendingIntent pi = PendingIntentUtils.getBroadcast(context, 0, i, 0, false);
 
        final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
 
        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);
        }
 
        am.cancel(pi);
 
        boolean scheduledExact = false;
        if (exactAlarm) {
            try {
                am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
                scheduledExact = true;
            } catch (final Exception e) {
                LOG.error("Failed to schedule exact alarm for next DST change or periodic time sync", e);
            }
        }
 
        // Fallback to inexact alarm if the exact one failed
        if (!scheduledExact) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
                } else {
                    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
                }
            } catch (final Exception e) {
                LOG.error("Failed to schedule inexact alarm for next DST change or periodic time sync", e);
            }
        }
    }
 
    public static void ifEnabledScheduleNextDstChangeOrPeriodicSync(final Context context) {
        if (GBApplication.getPrefs().getBoolean("datetime_synconconnect", true)) {
            scheduleNextDstChangeOrPeriodicSync(context);
        }
    }
 
    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");
        }
    }
}