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