Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0a22b32 authored by Christopher Tate's avatar Christopher Tate
Browse files

Batch alarms to reduce device wakeups

The default Alarm Manager behavior for KLP+ apps will be to aggressively
coalesce alarms, trading exact timeliness of delivery for minimizing the
number of alarm-delivery points, especially wakeup points.

There is new API in AlarmManager, setExact() and setExactRepeating(),
for use by apps that absolutely *must* get their alarms at a specific
point in time.

Bug 9532215

Change-Id: I40b4eea90220211cc958172d2629664b921ff051
parent 209bede6
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -3052,17 +3052,18 @@ package android.app {
  public class AlarmManager {
    method public void cancel(android.app.PendingIntent);
    method public void set(int, long, android.app.PendingIntent);
    method public void setInexactRepeating(int, long, long, android.app.PendingIntent);
    method public void setExact(int, long, android.app.PendingIntent);
    method public deprecated void setInexactRepeating(int, long, long, android.app.PendingIntent);
    method public void setRepeating(int, long, long, android.app.PendingIntent);
    method public void setTime(long);
    method public void setTimeZone(java.lang.String);
    field public static final int ELAPSED_REALTIME = 3; // 0x3
    field public static final int ELAPSED_REALTIME_WAKEUP = 2; // 0x2
    field public static final long INTERVAL_DAY = 86400000L; // 0x5265c00L
    field public static final long INTERVAL_FIFTEEN_MINUTES = 900000L; // 0xdbba0L
    field public static final long INTERVAL_HALF_DAY = 43200000L; // 0x2932e00L
    field public static final long INTERVAL_HALF_HOUR = 1800000L; // 0x1b7740L
    field public static final long INTERVAL_HOUR = 3600000L; // 0x36ee80L
    field public static final deprecated long INTERVAL_DAY = 86400000L; // 0x5265c00L
    field public static final deprecated long INTERVAL_FIFTEEN_MINUTES = 900000L; // 0xdbba0L
    field public static final deprecated long INTERVAL_HALF_DAY = 43200000L; // 0x2932e00L
    field public static final deprecated long INTERVAL_HALF_HOUR = 1800000L; // 0x1b7740L
    field public static final deprecated long INTERVAL_HOUR = 3600000L; // 0x36ee80L
    field public static final int RTC = 1; // 0x1
    field public static final int RTC_WAKEUP = 0; // 0x0
  }
+56 −13
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package android.app;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.RemoteException;

/**
@@ -52,6 +54,8 @@ import android.os.RemoteException;
 */
public class AlarmManager
{
    private static final String TAG = "AlarmManager";

    /**
     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
     * (wall clock time in UTC), which will wake up the device when
@@ -81,12 +85,16 @@ public class AlarmManager
    public static final int ELAPSED_REALTIME = 3;

    private final IAlarmManager mService;
    private final boolean mAlwaysExact;

    /**
     * package private on purpose
     */
    AlarmManager(IAlarmManager service) {
    AlarmManager(IAlarmManager service, Context ctx) {
        mService = service;

        final int sdkVersion = ctx.getApplicationInfo().targetSdkVersion;
        mAlwaysExact = (sdkVersion < Build.VERSION_CODES.KEY_LIME_PIE);
    }
    
    /**
@@ -133,10 +141,7 @@ public class AlarmManager
     * @see #RTC_WAKEUP
     */
    public void set(int type, long triggerAtMillis, PendingIntent operation) {
        try {
            mService.set(type, triggerAtMillis, operation);
        } catch (RemoteException ex) {
        }
        setImpl(type, triggerAtMillis, 0, operation, mAlwaysExact);
    }

    /**
@@ -188,20 +193,58 @@ public class AlarmManager
     */
    public void setRepeating(int type, long triggerAtMillis,
            long intervalMillis, PendingIntent operation) {
        setImpl(type, triggerAtMillis, intervalMillis, operation, mAlwaysExact);
    }

    /**
     * TBW: new 'exact' alarm that must be delivered as nearly as possible
     * to the precise time specified.
     */
    public void setExact(int type, long triggerAtMillis, PendingIntent operation) {
        setImpl(type, triggerAtMillis, 0, operation, true);
    }

    private void setImpl(int type, long triggerAtMillis, long intervalMillis,
            PendingIntent operation, boolean isExact) {
        try {
            mService.setRepeating(type, triggerAtMillis, intervalMillis, operation);
            mService.set(type, triggerAtMillis, intervalMillis, operation, isExact);
        } catch (RemoteException ex) {
        }
    }

    /**
     * Available inexact recurrence intervals recognized by
     * {@link #setInexactRepeating(int, long, long, PendingIntent)} 
     * @deprecated setInexactRepeating() is deprecated; as of API 19 all
     * repeating alarms are inexact.
     */
    @Deprecated
    public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;

    /**
     * @deprecated setInexactRepeating() is deprecated; as of API 19 all
     * repeating alarms are inexact.
     */
    @Deprecated
    public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;

    /**
     * @deprecated setInexactRepeating() is deprecated; as of API 19 all
     * repeating alarms are inexact.
     */
    @Deprecated
    public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;

    /**
     * @deprecated setInexactRepeating() is deprecated; as of API 19 all
     * repeating alarms are inexact.
     */
    @Deprecated
    public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;

    /**
     * @deprecated setInexactRepeating() is deprecated; as of API 19 all
     * repeating alarms are inexact.
     */
    @Deprecated
    public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;

    /**
@@ -236,6 +279,8 @@ public class AlarmManager
     * typically comes from {@link PendingIntent#getBroadcast
     * IntentSender.getBroadcast()}.
     *
     * @deprecated As of API 19, all repeating alarms are inexact.
     *
     * @see android.os.Handler
     * @see #set
     * @see #cancel
@@ -252,12 +297,10 @@ public class AlarmManager
     * @see #INTERVAL_HALF_DAY
     * @see #INTERVAL_DAY
     */
    @Deprecated
    public void setInexactRepeating(int type, long triggerAtMillis,
            long intervalMillis, PendingIntent operation) {
        try {
            mService.setInexactRepeating(type, triggerAtMillis, intervalMillis, operation);
        } catch (RemoteException ex) {
        }
        setRepeating(type, triggerAtMillis, intervalMillis, operation);
    }
    
    /**
+3 −3
Original line number Diff line number Diff line
@@ -309,11 +309,11 @@ class ContextImpl extends Context {
                    return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
                }});

        registerService(ALARM_SERVICE, new StaticServiceFetcher() {
                public Object createStaticService() {
        registerService(ALARM_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    IBinder b = ServiceManager.getService(ALARM_SERVICE);
                    IAlarmManager service = IAlarmManager.Stub.asInterface(b);
                    return new AlarmManager(service);
                    return new AlarmManager(service, ctx);
                }});

        registerService(AUDIO_SERVICE, new ServiceFetcher() {
+1 −3
Original line number Diff line number Diff line
@@ -24,9 +24,7 @@ import android.app.PendingIntent;
 * {@hide}
 */
interface IAlarmManager {
    void set(int type, long triggerAtTime, in PendingIntent operation);
    void setRepeating(int type, long triggerAtTime, long interval, in PendingIntent operation);
    void setInexactRepeating(int type, long triggerAtTime, long interval, in PendingIntent operation);
    void set(int type, long triggerAtTime, long interval, in PendingIntent operation, boolean isExact);
    void setTime(long millis);
    void setTimeZone(String zone);
    void remove(in PendingIntent operation);
+497 −314

File changed.

Preview size limit exceeded, changes collapsed.

Loading