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

Commit bf697fda authored by Adrian Roos's avatar Adrian Roos Committed by Android (Google) Code Review
Browse files

Merge "AOD: Fix AoD time tick after refactor"

parents 1d5b9778 fe54aa00
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.doze;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
@@ -25,10 +26,7 @@ import android.os.PowerManager;
import android.os.SystemClock;

import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.systemui.SystemUI;
import com.android.systemui.SystemUIApplication;
import com.android.systemui.plugins.PluginListener;
import com.android.systemui.plugins.PluginManager;
import com.android.systemui.plugins.doze.DozeProvider;
import com.android.systemui.statusbar.phone.DozeParameters;

@@ -45,6 +43,7 @@ public class DozeFactory {
        Context context = dozeService;
        SensorManager sensorManager = context.getSystemService(SensorManager.class);
        PowerManager powerManager = context.getSystemService(PowerManager.class);
        AlarmManager alarmManager = context.getSystemService(AlarmManager.class);

        DozeHost host = getHost(dozeService);
        AmbientDisplayConfiguration config = new AmbientDisplayConfiguration(context);
@@ -57,7 +56,7 @@ public class DozeFactory {
        machine.setParts(new DozeMachine.Part[]{
                createDozeTriggers(context, sensorManager, host, config, params, handler, wakeLock,
                        machine),
                createDozeUi(context, host, wakeLock, machine),
                createDozeUi(context, host, wakeLock, machine, handler, alarmManager),
        });

        return machine;
@@ -72,7 +71,7 @@ public class DozeFactory {
    }

    private DozeMachine.Part createDozeUi(Context context, DozeHost host, WakeLock wakeLock,
            DozeMachine machine) {
            DozeMachine machine, Handler handler, AlarmManager alarmManager) {
        if (mDozePlugin != null) {
            DozeProvider.DozeUi dozeUi = mDozePlugin.provideDozeUi(context,
                    pluginMachine(context, machine, host),
@@ -82,7 +81,7 @@ public class DozeFactory {
                        pluginState(newState));
            };
        } else {
            return new DozeUi(context, machine, wakeLock, host);
            return new DozeUi(context, alarmManager, machine, wakeLock, host, handler);
        }
    }

+70 −4
Original line number Diff line number Diff line
@@ -16,7 +16,13 @@

package com.android.systemui.doze;

import android.app.AlarmManager;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * The policy controlling doze.
@@ -24,16 +30,25 @@ import android.content.Context;
public class DozeUi implements DozeMachine.Part {

    private final Context mContext;
    private final AlarmManager mAlarmManager;
    private final DozeHost mHost;
    private DozeFactory.WakeLock mWakeLock;
    private DozeMachine mMachine;
    private final Handler mHandler;
    private final DozeFactory.WakeLock mWakeLock;
    private final DozeMachine mMachine;
    private final AlarmManager.OnAlarmListener mTimeTick;

    private boolean mTimeTickScheduled = false;

    public DozeUi(Context context, DozeMachine machine, DozeFactory.WakeLock wakeLock,
            DozeHost host) {
    public DozeUi(Context context, AlarmManager alarmManager, DozeMachine machine,
            DozeFactory.WakeLock wakeLock, DozeHost host, Handler handler) {
        mContext = context;
        mAlarmManager = alarmManager;
        mMachine = machine;
        mWakeLock = wakeLock;
        mHost = host;
        mHandler = handler;

        mTimeTick = this::onTimeTick;
    }

    private void pulseWhileDozing(int reason) {
@@ -54,6 +69,12 @@ public class DozeUi implements DozeMachine.Part {
    @Override
    public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
        switch (newState) {
            case DOZE_AOD:
                scheduleTimeTick();
                break;
            case DOZE:
                unscheduleTimeTick();
                break;
            case DOZE_REQUEST_PULSE:
                pulseWhileDozing(DozeLog.PULSE_REASON_NOTIFICATION /* TODO */);
                break;
@@ -62,7 +83,52 @@ public class DozeUi implements DozeMachine.Part {
                break;
            case FINISH:
                mHost.stopDozing();
                unscheduleTimeTick();
                break;
        }
    }

    private void scheduleTimeTick() {
        if (mTimeTickScheduled) {
            return;
        }

        long delta = roundToNextMinute(System.currentTimeMillis()) - System.currentTimeMillis();
        mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + delta, "doze_time_tick", mTimeTick, mHandler);

        mTimeTickScheduled = true;
    }

    private void unscheduleTimeTick() {
        if (!mTimeTickScheduled) {
            return;
        }
        mAlarmManager.cancel(mTimeTick);
    }

    private long roundToNextMinute(long timeInMillis) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.MINUTE, 1);

        return calendar.getTimeInMillis();
    }

    private void onTimeTick() {
        if (!mTimeTickScheduled) {
            // Alarm was canceled, but we still got the callback. Ignore.
            return;
        }

        mHost.dozeTimeTick();

        // Keep wakelock until a frame has been pushed.
        mHandler.post(mWakeLock.wrap(() -> {}));

        mTimeTickScheduled = false;
        scheduleTimeTick();
    }
}