mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 08:05:55 +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) -->
|
<!-- Schedule exact alarms (eg. for DST changes) -->
|
||||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
<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 -->
|
<!-- Read loyalty cards from Catima -->
|
||||||
<uses-permission android:name="me.hackerchick.catima.READ_CARDS"/>
|
<uses-permission android:name="me.hackerchick.catima.READ_CARDS"/>
|
||||||
<uses-permission android:name="me.hackerchick.catima.debug.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 java.util.GregorianCalendar;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
|
||||||
@ -73,11 +74,14 @@ public class TimeChangeReceiver extends BroadcastReceiver {
|
|||||||
return;
|
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();
|
final Date newTime = GregorianCalendar.getInstance().getTime();
|
||||||
LOG.info("Time/Timezone changed or periodic sync, syncing with device: {} ({}), {}", DateTimeUtils.formatDate(newTime), newTime.toGMTString(), intent.getAction());
|
LOG.info("Time/Timezone changed or periodic sync, syncing with device: {} ({}), {}", DateTimeUtils.formatDate(newTime), newTime.toGMTString(), intent.getAction());
|
||||||
GBApplication.deviceService().onSetTime();
|
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);
|
scheduleNextDstChangeOrPeriodicSync(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +126,7 @@ public class TimeChangeReceiver extends BroadcastReceiver {
|
|||||||
boolean scheduledExact = false;
|
boolean scheduledExact = false;
|
||||||
if (exactAlarm) {
|
if (exactAlarm) {
|
||||||
try {
|
try {
|
||||||
am.setExact(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis, pi);
|
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
|
||||||
scheduledExact = true;
|
scheduledExact = true;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Failed to schedule exact alarm for next DST change or periodic time sync", 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) {
|
if (!scheduledExact) {
|
||||||
try {
|
try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
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 {
|
} else {
|
||||||
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis, pi);
|
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pi);
|
||||||
}
|
}
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Failed to schedule inexact alarm for next DST change or periodic time sync", 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.Environment;
|
||||||
import android.os.ParcelUuid;
|
import android.os.ParcelUuid;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.provider.DocumentsContract;
|
import android.provider.DocumentsContract;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@ -337,4 +338,16 @@ public class AndroidUtils {
|
|||||||
}
|
}
|
||||||
GBApplication.getContext().startActivity(launchIntent);
|
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