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

Commit 3d6b8c10 authored by Matthew Qin's avatar Matthew Qin Committed by Steve Kondik
Browse files

Alarm: Add one more RTC alarm type for poweroff alarm.

Now we use type of RTC_WAKEUP for the poweroff alarm.
Actually lots of other applications also use this type.
But these applications never expect the device will
power on after it has been shut down. So we need to
add dedicated alarm type for the Deskclock or similar
applications.

Change-Id: I5df2465c46de8fe875d1581bdefe26021c8ffb33
parent 0178be08
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -93,6 +93,12 @@ public class AlarmManager
     * wakes up.
     */
    public static final int ELAPSED_REALTIME = 3;
    /** @hide
     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
     * (wall clock time in UTC), which will wake up the device when
     * it goes off. And it will power on the devices when it shuts down.
     */
    public static final int RTC_POWEROFF_WAKEUP = 4;

    /** @hide */
    public static final long WINDOW_EXACT = 0;
+38 −5
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import static android.app.AlarmManager.RTC_WAKEUP;
import static android.app.AlarmManager.RTC;
import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP;
import static android.app.AlarmManager.ELAPSED_REALTIME;
import static android.app.AlarmManager.RTC_POWEROFF_WAKEUP;

import com.android.internal.util.LocalLog;

@@ -73,6 +74,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
    private static final int RTC_MASK = 1 << RTC;
    private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << ELAPSED_REALTIME_WAKEUP; 
    private static final int ELAPSED_REALTIME_MASK = 1 << ELAPSED_REALTIME;
    private static final int RTC_POWEROFF_WAKEUP_MASK = 1 << RTC_POWEROFF_WAKEUP;
    private static final int TIME_CHANGED_MASK = 1 << 16;
    private static final int IS_WAKEUP_MASK = RTC_WAKEUP_MASK|ELAPSED_REALTIME_WAKEUP_MASK;

@@ -101,6 +103,7 @@ class AlarmManagerService extends IAlarmManager.Stub {

    private int mDescriptor;
    private long mNextWakeup;
    private long mNextRtcWakeup;
    private long mNextNonWakeup;
    private int mBroadcastRefCount = 0;
    private PowerManager.WakeLock mWakeLock;
@@ -287,6 +290,18 @@ class AlarmManagerService extends IAlarmManager.Stub {
            return false;
        }

        boolean isRtcPowerOffWakeup() {
            final int N = alarms.size();
            for (int i = 0; i < N; i++) {
                Alarm a = alarms.get(i);
                // non-wakeup alarms are types 1 and 3, i.e. have the low bit set
                if (a.type == RTC_POWEROFF_WAKEUP) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public String toString() {
            StringBuilder b = new StringBuilder(40);
@@ -322,7 +337,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
    private final ArrayList<Batch> mAlarmBatches = new ArrayList<Batch>();

    static long convertToElapsed(long when, int type) {
        final boolean isRtc = (type == RTC || type == RTC_WAKEUP);
        final boolean isRtc = (type == RTC || type == RTC_WAKEUP || type == RTC_POWEROFF_WAKEUP);
        if (isRtc) {
            when -= System.currentTimeMillis() - SystemClock.elapsedRealtime();
        }
@@ -465,7 +480,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
    public AlarmManagerService(Context context) {
        mContext = context;
        mDescriptor = init();
        mNextWakeup = mNextNonWakeup = 0;
        mNextWakeup = mNextRtcWakeup = mNextNonWakeup = 0;

        // We have to set current TimeZone info to kernel
        // because kernel doesn't keep this after reboot
@@ -535,7 +550,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
            windowLength = AlarmManager.INTERVAL_HOUR;
        }

        if (type < RTC_WAKEUP || type > ELAPSED_REALTIME) {
        if (type < RTC_WAKEUP || type > RTC_POWEROFF_WAKEUP) {
            throw new IllegalArgumentException("Invalid alarm type " + type);
        }

@@ -651,6 +666,17 @@ class AlarmManagerService extends IAlarmManager.Stub {
        }
        return null;
    }
    private Batch findFirstRtcWakeupBatchLocked() {
        final int N = mAlarmBatches.size();
        for (int i = 0; i < N; i++) {
            Batch b = mAlarmBatches.get(i);
            if (b.isRtcPowerOffWakeup()) {
                return b;
            }
        }
        return null;
    }


    private void rescheduleKernelAlarmsLocked() {
        // Schedule the next upcoming wakeup alarm.  If there is a deliverable batch
@@ -658,6 +684,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
        if (mAlarmBatches.size() > 0) {
            final Batch firstWakeup = findFirstWakeupBatchLocked();
            final Batch firstBatch = mAlarmBatches.get(0);
            final Batch firstRtcWakeup = findFirstRtcWakeupBatchLocked();
            if (firstWakeup != null && mNextWakeup != firstWakeup.start) {
                mNextWakeup = firstWakeup.start;
                setLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup.start);
@@ -666,6 +693,11 @@ class AlarmManagerService extends IAlarmManager.Stub {
                mNextNonWakeup = firstBatch.start;
                setLocked(ELAPSED_REALTIME, firstBatch.start);
            }
            if (firstRtcWakeup != null && mNextRtcWakeup != firstRtcWakeup.start) {
                mNextRtcWakeup = firstRtcWakeup.start;
                setLocked(RTC_POWEROFF_WAKEUP, firstRtcWakeup.start - SystemClock.elapsedRealtime());
            }

        }
    }

@@ -996,6 +1028,7 @@ class AlarmManagerService extends IAlarmManager.Stub {
        case RTC_WAKEUP : return "RTC_WAKEUP";
        case ELAPSED_REALTIME : return "ELAPSED";
        case ELAPSED_REALTIME_WAKEUP: return "ELAPSED_WAKEUP";
        case RTC_POWEROFF_WAKEUP : return "RTC_POWEROFF_WAKEUP";
        default:
            break;
        }
@@ -1197,7 +1230,6 @@ class AlarmManagerService extends IAlarmManager.Stub {
                            recordWakeupAlarms(mAlarmBatches, nowELAPSED, nowRTC);
                        }
                    }

                    triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC);
                    rescheduleKernelAlarmsLocked();

@@ -1238,7 +1270,8 @@ class AlarmManagerService extends IAlarmManager.Stub {
                                fs.nesting++;
                            }
                            if (alarm.type == ELAPSED_REALTIME_WAKEUP
                                    || alarm.type == RTC_WAKEUP) {
                                    || alarm.type == RTC_WAKEUP
                                    || alarm.type == RTC_POWEROFF_WAKEUP) {
                                bs.numWakeup++;
                                fs.numWakeup++;
                                ActivityManagerNative.noteWakeupAlarm(