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

Commit 6bcc18f7 authored by Hui Yu's avatar Hui Yu Committed by Android (Google) Code Review
Browse files

Merge "Add WakeLock.setStateListener() API."

parents d09b644d a7149247
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -31738,9 +31738,14 @@ package android.os {
    method public void release();
    method public void release(int);
    method public void setReferenceCounted(boolean);
    method public void setStateListener(@NonNull java.util.concurrent.Executor, @Nullable android.os.PowerManager.WakeLockStateListener);
    method public void setWorkSource(android.os.WorkSource);
  }
  public static interface PowerManager.WakeLockStateListener {
    method public void onStateChanged(boolean);
  }
  public class Process {
    ctor public Process();
    method public static final long getElapsedCpuTime();
+1 −0
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ filegroup {
        "android/os/IThermalStatusListener.aidl",
        "android/os/IThermalService.aidl",
        "android/os/IPowerManager.aidl",
        "android/os/IWakeLockCallback.aidl",
    ],
}

+4 −2
Original line number Diff line number Diff line
@@ -21,15 +21,16 @@ import android.os.BatterySaverPolicyConfig;
import android.os.ParcelDuration;
import android.os.PowerSaveState;
import android.os.WorkSource;
import android.os.IWakeLockCallback;

/** @hide */

interface IPowerManager
{
    void acquireWakeLock(IBinder lock, int flags, String tag, String packageName, in WorkSource ws,
            String historyTag, int displayId);
            String historyTag, int displayId, IWakeLockCallback callback);
    void acquireWakeLockWithUid(IBinder lock, int flags, String tag, String packageName,
            int uidtoblame, int displayId);
            int uidtoblame, int displayId, IWakeLockCallback callback);
    @UnsupportedAppUsage
    void releaseWakeLock(IBinder lock, int flags);
    void updateWakeLockUids(IBinder lock, in int[] uids);
@@ -40,6 +41,7 @@ interface IPowerManager
    boolean setPowerModeChecked(int mode, boolean enabled);

    void updateWakeLockWorkSource(IBinder lock, in WorkSource ws, String historyTag);
    void updateWakeLockCallback(IBinder lock, IWakeLockCallback callback);
    boolean isWakeLockLevelSupported(int level);

    void userActivity(int displayId, long time, int event, int flags);
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

/**
 * @hide
 */
oneway interface IWakeLockCallback {
    oneway void onStateChanged(boolean enabled);
}
+59 −1
Original line number Diff line number Diff line
@@ -2769,6 +2769,23 @@ public final class PowerManager {
     */
    public static final int PRE_IDLE_TIMEOUT_MODE_SHORT = 2;

    /**
     * A listener interface to get notified when the wakelock is enabled/disabled.
     */
    public interface WakeLockStateListener {
        /**
         * Frameworks could disable the wakelock because either device's power allowlist has
         * changed, or the app's wakelock has exceeded its quota, or the app goes into cached
         * state.
         * <p>
         * This callback is called whenever the wakelock's state has changed.
         * </p>
         *
         * @param enabled true is enabled, false is disabled.
         */
        void onStateChanged(boolean enabled);
    }

    /**
     * A wake lock is a mechanism to indicate that your application needs
     * to have the device stay on.
@@ -2800,6 +2817,8 @@ public final class PowerManager {
        private String mHistoryTag;
        private final String mTraceName;
        private final int mDisplayId;
        private WakeLockStateListener mListener;
        private IWakeLockCallback mCallback;

        private final Runnable mReleaser = () -> release(RELEASE_FLAG_TIMEOUT);

@@ -2890,7 +2909,7 @@ public final class PowerManager {
                Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0);
                try {
                    mService.acquireWakeLock(mToken, mFlags, mTag, mPackageName, mWorkSource,
                            mHistoryTag, mDisplayId);
                            mHistoryTag, mDisplayId, mCallback);
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
@@ -3083,6 +3102,45 @@ public final class PowerManager {
                }
            };
        }

        /**
         * Set the listener to get notified when the wakelock is enabled/disabled.
         *
         * @param executor {@link Executor} to handle listener callback.
         * @param listener listener to be added, set the listener to null to cancel a listener.
         */
        public void setStateListener(@NonNull @CallbackExecutor Executor executor,
                @Nullable WakeLockStateListener listener) {
            Preconditions.checkNotNull(executor, "executor cannot be null");
            synchronized (mToken) {
                if (listener != mListener) {
                    mListener = listener;
                    if (listener != null) {
                        mCallback = new IWakeLockCallback.Stub() {
                            public void onStateChanged(boolean enabled) {
                                final long token = Binder.clearCallingIdentity();
                                try {
                                    executor.execute(() -> {
                                        listener.onStateChanged(enabled);
                                    });
                                } finally {
                                    Binder.restoreCallingIdentity(token);
                                }
                            }
                        };
                    } else {
                        mCallback = null;
                    }
                    if (mHeld) {
                        try {
                            mService.updateWakeLockCallback(mToken, mCallback);
                        } catch (RemoteException e) {
                            throw e.rethrowFromSystemServer();
                        }
                    }
                }
            }
        }
    }

    /**
Loading