Merge "KeyguardLifecyclesDispatcher improvements" into udc-qpr-dev

This commit is contained in:
Treehugger Robot
2023-05-25 18:59:40 +00:00
committed by Android (Google) Code Review

View File

@@ -16,11 +16,19 @@
package com.android.systemui.keyguard;
import android.annotation.IntDef;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Trace;
import android.os.TraceNameSupplier;
import androidx.annotation.NonNull;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;
@@ -29,7 +37,6 @@ import javax.inject.Inject;
*/
@SysUISingleton
public class KeyguardLifecyclesDispatcher {
static final int SCREEN_TURNING_ON = 0;
static final int SCREEN_TURNED_ON = 1;
static final int SCREEN_TURNING_OFF = 2;
@@ -39,19 +46,46 @@ public class KeyguardLifecyclesDispatcher {
static final int FINISHED_WAKING_UP = 5;
static final int STARTED_GOING_TO_SLEEP = 6;
static final int FINISHED_GOING_TO_SLEEP = 7;
private static final String TAG = "KeyguardLifecyclesDispatcher";
private final ScreenLifecycle mScreenLifecycle;
private final WakefulnessLifecycle mWakefulnessLifecycle;
@Inject
public KeyguardLifecyclesDispatcher(ScreenLifecycle screenLifecycle,
WakefulnessLifecycle wakefulnessLifecycle) {
mScreenLifecycle = screenLifecycle;
mWakefulnessLifecycle = wakefulnessLifecycle;
@IntDef({
SCREEN_TURNING_ON,
SCREEN_TURNED_ON,
SCREEN_TURNING_OFF,
SCREEN_TURNED_OFF,
STARTED_WAKING_UP,
FINISHED_WAKING_UP,
STARTED_GOING_TO_SLEEP,
FINISHED_GOING_TO_SLEEP,
})
@Retention(RetentionPolicy.SOURCE)
public @interface KeyguardLifecycleMessageType {
}
void dispatch(int what) {
private static String getNameOfMessage(@KeyguardLifecycleMessageType int what) {
return switch (what) {
case SCREEN_TURNING_ON -> "SCREEN_TURNING_ON";
case SCREEN_TURNED_ON -> "SCREEN_TURNED_ON";
case SCREEN_TURNING_OFF -> "SCREEN_TURNING_OFF";
case SCREEN_TURNED_OFF -> "SCREEN_TURNED_OFF";
case STARTED_WAKING_UP -> "STARTED_WAKING_UP";
case FINISHED_WAKING_UP -> "FINISHED_WAKING_UP";
case STARTED_GOING_TO_SLEEP -> "STARTED_GOING_TO_SLEEP";
case FINISHED_GOING_TO_SLEEP -> "FINISHED_GOING_TO_SLEEP";
default -> "UNKNOWN";
};
}
private final Handler mHandler;
@Inject
public KeyguardLifecyclesDispatcher(
@Main Looper mainLooper,
ScreenLifecycle screenLifecycle,
WakefulnessLifecycle wakefulnessLifecycle) {
mHandler = new KeyguardLifecycleHandler(mainLooper, screenLifecycle, wakefulnessLifecycle);
}
void dispatch(@KeyguardLifecycleMessageType int what) {
mHandler.obtainMessage(what).sendToTarget();
}
@@ -60,7 +94,7 @@ public class KeyguardLifecyclesDispatcher {
* @param pmReason Reason this message was triggered - this should be a value from either
* {@link PowerManager.WakeReason} or {@link PowerManager.GoToSleepReason}.
*/
void dispatch(int what, int pmReason) {
void dispatch(@KeyguardLifecycleMessageType int what, int pmReason) {
final Message message = mHandler.obtainMessage(what);
message.arg1 = pmReason;
message.sendToTarget();
@@ -70,44 +104,48 @@ public class KeyguardLifecyclesDispatcher {
* @param what Message to send.
* @param object Object to send with the message
*/
void dispatch(int what, Object object) {
void dispatch(@KeyguardLifecycleMessageType int what, Object object) {
mHandler.obtainMessage(what, object).sendToTarget();
}
private Handler mHandler = new Handler() {
private static class KeyguardLifecycleHandler extends Handler {
private static final String TAG = "KeyguardLifecycleHandler";
private final ScreenLifecycle mScreenLifecycle;
private final WakefulnessLifecycle mWakefulnessLifecycle;
public KeyguardLifecycleHandler(Looper looper,
ScreenLifecycle screenLifecycle,
WakefulnessLifecycle wakefulnessLifecycle) {
super(looper);
mScreenLifecycle = screenLifecycle;
mWakefulnessLifecycle = wakefulnessLifecycle;
}
@NonNull
@Override
public void handleMessage(Message msg) {
public String getTraceName(@NonNull Message msg) {
if (msg.getCallback() instanceof TraceNameSupplier || msg.getCallback() != null) {
return super.getTraceName(msg);
}
return TAG + "#" + getNameOfMessage(msg.what);
}
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case SCREEN_TURNING_ON:
Trace.beginSection("KeyguardLifecyclesDispatcher#SCREEN_TURNING_ON");
mScreenLifecycle.dispatchScreenTurningOn();
Trace.endSection();
break;
case SCREEN_TURNED_ON:
mScreenLifecycle.dispatchScreenTurnedOn();
break;
case SCREEN_TURNING_OFF:
mScreenLifecycle.dispatchScreenTurningOff();
break;
case SCREEN_TURNED_OFF:
mScreenLifecycle.dispatchScreenTurnedOff();
break;
case STARTED_WAKING_UP:
mWakefulnessLifecycle.dispatchStartedWakingUp(msg.arg1 /* pmReason */);
break;
case FINISHED_WAKING_UP:
mWakefulnessLifecycle.dispatchFinishedWakingUp();
break;
case STARTED_GOING_TO_SLEEP:
mWakefulnessLifecycle.dispatchStartedGoingToSleep(msg.arg1 /* pmReason */);
break;
case FINISHED_GOING_TO_SLEEP:
mWakefulnessLifecycle.dispatchFinishedGoingToSleep();
break;
default:
throw new IllegalArgumentException("Unknown message: " + msg);
case SCREEN_TURNING_ON -> mScreenLifecycle.dispatchScreenTurningOn();
case SCREEN_TURNED_ON -> mScreenLifecycle.dispatchScreenTurnedOn();
case SCREEN_TURNING_OFF -> mScreenLifecycle.dispatchScreenTurningOff();
case SCREEN_TURNED_OFF -> mScreenLifecycle.dispatchScreenTurnedOff();
case STARTED_WAKING_UP ->
mWakefulnessLifecycle.dispatchStartedWakingUp(msg.arg1 /* pmReason */);
case FINISHED_WAKING_UP -> mWakefulnessLifecycle.dispatchFinishedWakingUp();
case STARTED_GOING_TO_SLEEP ->
mWakefulnessLifecycle.dispatchStartedGoingToSleep(msg.arg1 /* pmReason */);
case FINISHED_GOING_TO_SLEEP ->
mWakefulnessLifecycle.dispatchFinishedGoingToSleep();
default -> throw new IllegalArgumentException("Unknown message: " + msg);
}
}
};
}
}