1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-16 10:00:08 +02:00

Add wake lock and wakeup for time sync

Wake lock with around 10 second timeout is a quick and dirty solution,
however as the time sync should happen once per several days the 10
second wake time should not be an issue.
This commit is contained in:
Davis Mosenkovs 2024-02-17 19:34:09 +02:00 committed by José Rebelo
parent cc5078332b
commit 7a79a1d0f5
3 changed files with 24 additions and 4 deletions

View File

@ -35,6 +35,9 @@
<!-- Schedule exact alarms (eg. for DST changes) -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<!-- Take wake locks (e.g. for time sync) -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Read loyalty cards from Catima -->
<uses-permission android:name="me.hackerchick.catima.READ_CARDS"/>
<uses-permission android:name="me.hackerchick.catima.debug.READ_CARDS"/>

View File

@ -35,6 +35,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
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;
@ -73,11 +74,14 @@ public class TimeChangeReceiver extends BroadcastReceiver {
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
// Reschedule the next DST change (since the timezone may have changed) or periodic sync
scheduleNextDstChangeOrPeriodicSync(context);
}
@ -122,7 +126,7 @@ public class TimeChangeReceiver extends BroadcastReceiver {
boolean scheduledExact = false;
if (exactAlarm) {
try {
am.setExact(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis, pi);
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);
@ -133,9 +137,9 @@ public class TimeChangeReceiver extends BroadcastReceiver {
if (!scheduledExact) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis, pi);
am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
} else {
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis, pi);
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);

View File

@ -32,6 +32,7 @@ import android.net.Uri;
import android.os.Environment;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.os.PowerManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
@ -337,4 +338,16 @@ public class AndroidUtils {
}
GBApplication.getContext().startActivity(launchIntent);
}
public static PowerManager.WakeLock acquirePartialWakeLock(Context context, String tag, long timeout) {
try {
PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = powermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Gadgetbridge:" + tag);
wl.acquire(timeout);
return wl;
} catch (final Exception e) {
LOG.error("Failed to take partial wake lock {}: ", tag, e);
return null;
}
}
}