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

Commit f73881b7 authored by Danesh M's avatar Danesh M Committed by Gerrit Code Review
Browse files

ProximityWake : Add support for checking proximity when waking device

Change-Id: Ia5ff446d88435773acccc7467e66009000dc20cb
parent b5eaceaf
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -62,4 +62,5 @@ interface IPowerManager

	void setPowerProfile(String profile);
	String getPowerProfile();
	void wakeUpWithProximityCheck(long time);
}
+13 −0
Original line number Diff line number Diff line
@@ -546,6 +546,19 @@ public final class PowerManager {
        }
    }

    /**
     * Forces the device to wake up from sleep only if
     * nothing is blocking the proximity sensor
     * @see #wakeUp
     * @hide
     */
    public void wakeUpWithProximityCheck(long time) {
        try {
            mService.wakeUpFromKeyEvent(time);
        } catch (RemoteException e) {
        }
    }

    /**
     * Forces the device to start napping.
     * <p>
+5 −0
Original line number Diff line number Diff line
@@ -3298,6 +3298,11 @@ public final class Settings {
         */
        public static final String EXPANDED_DESKTOP_STATE = "expanded_desktop_state";

        /**
         * @hide
         */
        public static final String PROXIMITY_ON_WAKE = "proximity_on_wake";

        /**
         * Expanded desktop style (with status bar or without status bar)
         * @hide
+94 −9
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.SystemSensorManager;
import android.net.Uri;
@@ -91,6 +94,7 @@ public final class PowerManagerService extends IPowerManager.Stub
    private static final int MSG_SCREEN_ON_BLOCKER_RELEASED = 3;
    // Message: Sent to poll whether the boot animation has terminated.
    private static final int MSG_CHECK_IF_BOOT_ANIMATION_FINISHED = 4;
    private static final int MSG_WAKE_UP = 5;

    // Dirty bit: mWakeLocks changed
    private static final int DIRTY_WAKE_LOCKS = 1 << 0;
@@ -175,6 +179,9 @@ public final class PowerManagerService extends IPowerManager.Stub
    // Max time (microseconds) to allow a CPU boost for
    private static final int MAX_CPU_BOOST_TIME = 5000000;

    // Max time allowed for proximity check
    private static final int MAX_PROXIMITY_WAIT = 200;

    private Context mContext;
    private LightsService mLightsService;
    private BatteryService mBatteryService;
@@ -413,6 +420,9 @@ public final class PowerManagerService extends IPowerManager.Stub
    private boolean mKeyboardVisible = false;

    private PerformanceManager mPerformanceManager;
    private SensorManager mSensorManager;
    private Sensor mProximitySensor;
    private boolean mProximityWake;

    public PowerManagerService() {
        synchronized (mLock) {
@@ -446,6 +456,8 @@ public final class PowerManagerService extends IPowerManager.Stub
        mHandlerThread = new HandlerThread(TAG);
        mHandlerThread.start();
        mHandler = new PowerManagerHandler(mHandlerThread.getLooper());
        mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        Watchdog.getInstance().addMonitor(this);
        Watchdog.getInstance().addThread(mHandler, mHandlerThread.getName());
@@ -568,6 +580,9 @@ public final class PowerManagerService extends IPowerManager.Stub
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.BUTTON_BACKLIGHT_TIMEOUT),
                    false, mSettingsObserver, UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.PROXIMITY_ON_WAKE),
                    false, mSettingsObserver, UserHandle.USER_ALL);

            // Go.
            readConfigurationLocked();
@@ -617,6 +632,8 @@ public final class PowerManagerService extends IPowerManager.Stub
        mWakeUpWhenPluggedOrUnpluggedSetting = Settings.Global.getInt(resolver,
                Settings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
                (mWakeUpWhenPluggedOrUnpluggedConfig ? 1 : 0));
        mProximityWake = Settings.System.getInt(resolver,
                Settings.System.PROXIMITY_ON_WAKE, 0) == 1;

        final int oldScreenBrightnessSetting = mScreenBrightnessSetting;
        mScreenBrightnessSetting = Settings.System.getIntForUser(resolver,
@@ -1161,8 +1178,10 @@ public final class PowerManagerService extends IPowerManager.Stub
        }
    }

    @Override // Binder call
    public void wakeUp(long eventTime) {
    /**
     * @hide
     */
    private void wakeUp(final long eventTime, boolean checkProximity) {
        if (eventTime > SystemClock.uptimeMillis()) {
            throw new IllegalArgumentException("event time must not be in the future");
        }
@@ -1177,6 +1196,9 @@ public final class PowerManagerService extends IPowerManager.Stub

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);

        Runnable r = new Runnable() {
            @Override
            public void run() {
                final long ident = Binder.clearCallingIdentity();
                try {
                    wakeUpInternal(eventTime);
@@ -1184,11 +1206,71 @@ public final class PowerManagerService extends IPowerManager.Stub
                    Binder.restoreCallingIdentity(ident);
                }
            }
        };
        runWithProximityCheck(r);
    }

    private void runWithProximityCheck(Runnable r) {
        if (mHandler.hasMessages(MSG_WAKE_UP)) {
            // There is already a message queued;
            return;
        }
        if (mProximityWake && mProximitySensor != null) {
            Message msg = mHandler.obtainMessage(MSG_WAKE_UP);
            msg.obj = r;
            mHandler.sendMessageDelayed(msg, MAX_PROXIMITY_WAIT);
            runPostProximityCheck(r);
        } else {
            r.run();
        }
    }

    private void runPostProximityCheck(final Runnable r) {
        if (mSensorManager == null) {
            r.run();
            return;
        }
        mSensorManager.registerListener(new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                if (!mHandler.hasMessages(MSG_WAKE_UP)) {
                    // The sensor took too long to return and
                    // the wake event already triggered.
                    return;
                }
                mHandler.removeMessages(MSG_WAKE_UP);
                if (event.values[0] == mProximitySensor.getMaximumRange()) {
                    r.run();
                }
                mSensorManager.unregisterListener(this);
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {}

        }, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override // Binder call
    public void wakeUpWithProximityCheck(long eventTime) {
        wakeUp(eventTime, true);
    }

    @Override // Binder call
    public void wakeUp(long eventTime) {
        wakeUp(eventTime, false);
    }

    // Called from native code.
    private void wakeUpFromNative(long eventTime) {
    private void wakeUpFromNative(final long eventTime) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                wakeUpInternal(eventTime);
            }
        };
        runWithProximityCheck(r);
    }

    private void wakeUpInternal(long eventTime) {
        synchronized (mLock) {
@@ -2815,6 +2897,9 @@ public final class PowerManagerService extends IPowerManager.Stub
                case MSG_CHECK_IF_BOOT_ANIMATION_FINISHED:
                    checkIfBootAnimationFinished();
                    break;
                case MSG_WAKE_UP:
                    ((Runnable) msg.obj).run();
                    break;
            }
        }
    }