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

Commit bde98075 authored by Josh Tsuji's avatar Josh Tsuji
Browse files

Use the existing wallpaper command API to send wake/sleep events to the wallpaper.

This is already used for similar events such as icon drops and user taps. Using it to indicate that the user has woken/slept the device (and passing along location information) makes sense!

This does require an API modification to add the new string constants to WallpaperManager. This change is being requested by 1P apps (the Live Wallpapers, which are not bundled with the OS and thus can't use system APIs).

Bug: 169693662
Test: locally with modified wallpaper, it receives events
Change-Id: I0cc1bc70dbea509a049b08f8119ed875ffc2e8d5
parent bbf0d298
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -190,4 +190,18 @@ interface IWallpaperManager {
     * Called from SystemUI when it shows the AoD UI.
     */
    oneway void setInAmbientMode(boolean inAmbientMode, long animationDuration);

    /**
     * Called from SystemUI when the device is waking up.
     *
     * @hide
     */
    oneway void notifyWakingUp(int x, int y, in Bundle extras);

    /**
     * Called from SystemUI when the device is going to sleep.
     *
     * @hide
     */
    void notifyGoingToSleep(int x, int y, in Bundle extras);
}
+24 −0
Original line number Diff line number Diff line
@@ -189,6 +189,30 @@ public class WallpaperManager {
     */
    public static final String COMMAND_DROP = "android.home.drop";

    /**
     * Command for {@link #sendWallpaperCommand}: reported by System UI when the device is waking
     * up. The x and y arguments are a location (possibly very roughly) corresponding to the action
     * that caused the device to wake up. For example, if the power button was pressed, this will be
     * the location on the screen nearest the power button.
     *
     * If the location is unknown or not applicable, x and y will be -1.
     *
     * @hide
     */
    public static final String COMMAND_WAKING_UP = "android.wallpaper.wakingup";

    /**
     * Command for {@link #sendWallpaperCommand}: reported by System UI when the device is going to
     * sleep. The x and y arguments are a location (possibly very roughly) corresponding to the
     * action that caused the device to go to sleep. For example, if the power button was pressed,
     * this will be the location on the screen nearest the power button.
     *
     * If the location is unknown or not applicable, x and y will be -1.
     *
     * @hide
     */
    public static final String COMMAND_GOING_TO_SLEEP = "android.wallpaper.goingtosleep";

    /**
     * Command for {@link #sendWallpaperCommand}: reported when the wallpaper that was already
     * set is re-applied by the user.
+101 −2
Original line number Diff line number Diff line
@@ -17,10 +17,20 @@
package com.android.systemui.keyguard;

import android.annotation.IntDef;
import android.app.IWallpaperManager;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.Trace;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;

import com.android.systemui.Dumpable;
import com.android.systemui.R;
import com.android.systemui.dagger.SysUISingleton;

import java.io.FileDescriptor;
@@ -31,7 +41,7 @@ import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;

/**
 * Tracks the wakefulness lifecycle.
 * Tracks the wakefulness lifecycle, including why we're waking or sleeping.
 */
@SysUISingleton
public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements
@@ -51,13 +61,30 @@ public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observe
    public static final int WAKEFULNESS_AWAKE = 2;
    public static final int WAKEFULNESS_GOING_TO_SLEEP = 3;

    private final Context mContext;
    private final DisplayMetrics mDisplayMetrics;
    private final IWallpaperManager mWallpaperManagerService;

    private int mWakefulness = WAKEFULNESS_ASLEEP;

    private @PowerManager.WakeReason int mLastWakeReason = PowerManager.WAKE_REASON_UNKNOWN;

    @Nullable
    private Point mLastWakeOriginLocation = null;

    private @PowerManager.GoToSleepReason int mLastSleepReason =
            PowerManager.GO_TO_SLEEP_REASON_MIN;

    @Nullable
    private Point mLastSleepOriginLocation = null;

    @Inject
    public WakefulnessLifecycle() {
    public WakefulnessLifecycle(
            Context context,
            @Nullable IWallpaperManager wallpaperManagerService) {
        mContext = context;
        mDisplayMetrics = context.getResources().getDisplayMetrics();
        mWallpaperManagerService = wallpaperManagerService;
    }

    public @Wakefulness int getWakefulness() {
@@ -85,6 +112,15 @@ public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observe
        }
        setWakefulness(WAKEFULNESS_WAKING);
        mLastWakeReason = pmWakeReason;
        updateLastWakeOriginLocation();

        try {
            mWallpaperManagerService.notifyWakingUp(
                    mLastWakeOriginLocation.x, mLastWakeOriginLocation.y, new Bundle());
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        dispatch(Observer::onStartedWakingUp);
    }

@@ -102,6 +138,15 @@ public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observe
        }
        setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
        mLastSleepReason = pmSleepReason;
        updateLastSleepOriginLocation();

        try {
            mWallpaperManagerService.notifyGoingToSleep(
                    mLastSleepOriginLocation.x, mLastSleepOriginLocation.y, new Bundle());
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        dispatch(Observer::onStartedGoingToSleep);
    }

@@ -124,6 +169,60 @@ public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observe
        Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
    }

    private void updateLastWakeOriginLocation() {
        mLastWakeOriginLocation = null;

        switch (mLastWakeReason) {
            case PowerManager.WAKE_REASON_POWER_BUTTON:
                mLastWakeOriginLocation = getPowerButtonOrigin();
                break;
            default:
                mLastWakeOriginLocation = getDefaultWakeSleepOrigin();
                break;
        }
    }

    private void updateLastSleepOriginLocation() {
        mLastSleepOriginLocation = null;

        switch (mLastSleepReason) {
            case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON:
                mLastSleepOriginLocation = getPowerButtonOrigin();
                break;
            default:
                mLastSleepOriginLocation = getDefaultWakeSleepOrigin();
                break;
        }
    }

    /**
     * Returns the point on the screen closest to the physical power button.
     */
    private Point getPowerButtonOrigin() {
        final boolean isPortrait = mContext.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT;

        if (isPortrait) {
            return new Point(
                    mDisplayMetrics.widthPixels,
                    mContext.getResources().getDimensionPixelSize(
                            R.dimen.physical_power_button_center_screen_location_y));
        } else {
            return new Point(
                    mContext.getResources().getDimensionPixelSize(
                            R.dimen.physical_power_button_center_screen_location_y),
                    mDisplayMetrics.heightPixels);
        }
    }

    /**
     * Returns the point on the screen used as the default origin for wake/sleep events. This is the
     * middle-bottom of the screen.
     */
    private Point getDefaultWakeSleepOrigin() {
        return new Point(mDisplayMetrics.widthPixels / 2, mDisplayMetrics.heightPixels);
    }

    public interface Observer {
        default void onStartedWakingUp() {}
        default void onFinishedWakingUp() {}
+5 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import android.app.IWallpaperManager;
import android.os.PowerManager;
import android.testing.AndroidTestingRunner;

@@ -43,9 +44,12 @@ public class WakefulnessLifecycleTest extends SysuiTestCase {
    private WakefulnessLifecycle mWakefulness;
    private WakefulnessLifecycle.Observer mWakefulnessObserver;

    private IWallpaperManager mWallpaperManager;

    @Before
    public void setUp() throws Exception {
        mWakefulness = new WakefulnessLifecycle();
        mWallpaperManager = mock(IWallpaperManager.class);
        mWakefulness = new WakefulnessLifecycle(mContext, mWallpaperManager);
        mWakefulnessObserver = mock(WakefulnessLifecycle.Observer.class);
        mWakefulness.addObserver(mWakefulnessObserver);
    }
+4 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.IWallpaperManager;
import android.app.Notification;
import android.app.StatusBarManager;
import android.app.trust.TrustManager;
@@ -265,6 +266,7 @@ public class StatusBarTest extends SysuiTestCase {
    @Mock private OngoingCallController mOngoingCallController;
    @Mock private TunerService mTunerService;
    @Mock private FeatureFlags mFeatureFlags;
    @Mock private IWallpaperManager mWallpaperManager;
    private ShadeController mShadeController;
    private FakeExecutor mUiBgExecutor = new FakeExecutor(new FakeSystemClock());
    private InitController mInitController = new InitController();
@@ -323,7 +325,8 @@ public class StatusBarTest extends SysuiTestCase {

        when(mRemoteInputManager.getController()).thenReturn(mRemoteInputController);

        WakefulnessLifecycle wakefulnessLifecycle = new WakefulnessLifecycle();
        WakefulnessLifecycle wakefulnessLifecycle =
                new WakefulnessLifecycle(mContext, mWallpaperManager);
        wakefulnessLifecycle.dispatchStartedWakingUp(PowerManager.WAKE_REASON_UNKNOWN);
        wakefulnessLifecycle.dispatchFinishedWakingUp();

Loading