mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 17:11:56 +01: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:
parent
0ad7ffd113
commit
067f1d5d78
@ -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"/>
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user