am faf3f370: Merge "Fix sensor usage model" into cw-e-dev

* commit 'faf3f3706140c3b90c805e77c3c72c0978b0d61a':
  Fix sensor usage model
This commit is contained in:
Nick Vaccaro
2015-09-25 21:44:26 +00:00
committed by Android Git Automerger

View File

@@ -31,6 +31,8 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.TriggerEvent; import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener; import android.hardware.TriggerEventListener;
import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManager;
@@ -111,7 +113,7 @@ public class DeviceIdleController extends SystemService
private INetworkPolicyManager mNetworkPolicyManager; private INetworkPolicyManager mNetworkPolicyManager;
private DisplayManager mDisplayManager; private DisplayManager mDisplayManager;
private SensorManager mSensorManager; private SensorManager mSensorManager;
private Sensor mSigMotionSensor; private Sensor mMotionSensor;
private LocationManager mLocationManager; private LocationManager mLocationManager;
private LocationRequest mLocationRequest; private LocationRequest mLocationRequest;
private PendingIntent mSensingAlarmIntent; private PendingIntent mSensingAlarmIntent;
@@ -123,7 +125,6 @@ public class DeviceIdleController extends SystemService
private boolean mForceIdle; private boolean mForceIdle;
private boolean mScreenOn; private boolean mScreenOn;
private boolean mCharging; private boolean mCharging;
private boolean mSigMotionActive;
private boolean mSensing; private boolean mSensing;
private boolean mNotMoving; private boolean mNotMoving;
private boolean mLocating; private boolean mLocating;
@@ -268,13 +269,57 @@ public class DeviceIdleController extends SystemService
} }
}; };
private final TriggerEventListener mSigMotionListener = new TriggerEventListener() { private final class MotionListener extends TriggerEventListener
@Override public void onTrigger(TriggerEvent event) { implements SensorEventListener {
boolean active = false;
@Override
public void onTrigger(TriggerEvent event) {
synchronized (DeviceIdleController.this) { synchronized (DeviceIdleController.this) {
significantMotionLocked(); active = false;
motionLocked();
} }
} }
};
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (DeviceIdleController.this) {
mSensorManager.unregisterListener(this, mMotionSensor);
active = false;
motionLocked();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public boolean registerLocked() {
boolean success = false;
if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) {
success = mSensorManager.requestTriggerSensor(mMotionListener, mMotionSensor);
} else {
success = mSensorManager.registerListener(
mMotionListener, mMotionSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
if (success) {
active = true;
} else {
Slog.e(TAG, "Unable to register for " + mMotionSensor);
}
return success;
}
public void unregisterLocked() {
if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) {
mSensorManager.cancelTriggerSensor(mMotionListener, mMotionSensor);
} else {
mSensorManager.unregisterListener(mMotionListener);
}
active = false;
}
}
private final MotionListener mMotionListener = new MotionListener();
private final LocationListener mGenericLocationListener = new LocationListener() { private final LocationListener mGenericLocationListener = new LocationListener() {
@Override @Override
@@ -349,7 +394,7 @@ public class DeviceIdleController extends SystemService
* This is the time, after becoming inactive, at which we start looking at the * This is the time, after becoming inactive, at which we start looking at the
* motion sensor to determine if the device is being left alone. We don't do this * motion sensor to determine if the device is being left alone. We don't do this
* immediately after going inactive just because we don't want to be continually running * immediately after going inactive just because we don't want to be continually running
* the significant motion sensor whenever the screen is off. * the motion sensor whenever the screen is off.
* @see Settings.Global#DEVICE_IDLE_CONSTANTS * @see Settings.Global#DEVICE_IDLE_CONSTANTS
* @see #KEY_INACTIVE_TIMEOUT * @see #KEY_INACTIVE_TIMEOUT
*/ */
@@ -392,7 +437,7 @@ public class DeviceIdleController extends SystemService
/** /**
* This is the time, after the inactive timeout elapses, that we will wait looking * This is the time, after the inactive timeout elapses, that we will wait looking
* for significant motion until we truly consider the device to be idle. * for motion until we truly consider the device to be idle.
* @see Settings.Global#DEVICE_IDLE_CONSTANTS * @see Settings.Global#DEVICE_IDLE_CONSTANTS
* @see #KEY_IDLE_AFTER_INACTIVE_TIMEOUT * @see #KEY_IDLE_AFTER_INACTIVE_TIMEOUT
*/ */
@@ -886,18 +931,19 @@ public class DeviceIdleController extends SystemService
int sigMotionSensorId = getContext().getResources().getInteger( int sigMotionSensorId = getContext().getResources().getInteger(
com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor); com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor);
if (sigMotionSensorId > 0) { if (sigMotionSensorId > 0) {
mSigMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true); mMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true);
} }
if (mSigMotionSensor == null && getContext().getResources().getBoolean( if (mMotionSensor == null && getContext().getResources().getBoolean(
com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) { com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) {
mSigMotionSensor = mSensorManager.getDefaultSensor( mMotionSensor = mSensorManager.getDefaultSensor(
Sensor.TYPE_WRIST_TILT_GESTURE); Sensor.TYPE_WRIST_TILT_GESTURE, true);
} }
if (mSigMotionSensor == null) { if (mMotionSensor == null) {
// As a last ditch, fall back to SMD. // As a last ditch, fall back to SMD.
mSigMotionSensor = mSensorManager.getDefaultSensor( mMotionSensor = mSensorManager.getDefaultSensor(
Sensor.TYPE_SIGNIFICANT_MOTION); Sensor.TYPE_SIGNIFICANT_MOTION, true);
} }
if (getContext().getResources().getBoolean( if (getContext().getResources().getBoolean(
com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) { com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) {
mLocationManager = (LocationManager) getContext().getSystemService( mLocationManager = (LocationManager) getContext().getSystemService(
@@ -1242,7 +1288,7 @@ public class DeviceIdleController extends SystemService
cancelAlarmLocked(); cancelAlarmLocked();
cancelSensingAlarmLocked(); cancelSensingAlarmLocked();
cancelLocatingLocked(); cancelLocatingLocked();
stopMonitoringSignificantMotion(); stopMonitoringMotionLocked();
mAnyMotionDetector.stop(); mAnyMotionDetector.stop();
} }
@@ -1271,8 +1317,8 @@ public class DeviceIdleController extends SystemService
switch (mState) { switch (mState) {
case STATE_INACTIVE: case STATE_INACTIVE:
// We have now been inactive long enough, it is time to start looking // We have now been inactive long enough, it is time to start looking
// for significant motion and sleep some more while doing so. // for motion and sleep some more while doing so.
startMonitoringSignificantMotion(); startMonitoringMotionLocked();
scheduleAlarmLocked(mConstants.IDLE_AFTER_INACTIVE_TIMEOUT, false); scheduleAlarmLocked(mConstants.IDLE_AFTER_INACTIVE_TIMEOUT, false);
// Reset the upcoming idle delays. // Reset the upcoming idle delays.
mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT; mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT;
@@ -1353,17 +1399,16 @@ public class DeviceIdleController extends SystemService
} }
} }
void significantMotionLocked() { void motionLocked() {
if (DEBUG) Slog.d(TAG, "significantMotionLocked()"); if (DEBUG) Slog.d(TAG, "motionLocked()");
// When the sensor goes off, its trigger is automatically removed. // The motion sensor will have been disabled at this point
mSigMotionActive = false;
handleMotionDetectedLocked(mConstants.MOTION_INACTIVE_TIMEOUT, "motion"); handleMotionDetectedLocked(mConstants.MOTION_INACTIVE_TIMEOUT, "motion");
} }
void handleMotionDetectedLocked(long timeout, String type) { void handleMotionDetectedLocked(long timeout, String type) {
// The device is not yet active, so we want to go back to the pending idle // The device is not yet active, so we want to go back to the pending idle
// state to wait again for no motion. Note that we only monitor for significant // state to wait again for no motion. Note that we only monitor for motion
// motion after moving out of the inactive state, so no need to worry about that. // after moving out of the inactive state, so no need to worry about that.
if (mState != STATE_ACTIVE) { if (mState != STATE_ACTIVE) {
scheduleReportActiveLocked(type, Process.myUid()); scheduleReportActiveLocked(type, Process.myUid());
mState = STATE_ACTIVE; mState = STATE_ACTIVE;
@@ -1405,19 +1450,17 @@ public class DeviceIdleController extends SystemService
} }
} }
void startMonitoringSignificantMotion() { void startMonitoringMotionLocked() {
if (DEBUG) Slog.d(TAG, "startMonitoringSignificantMotion()"); if (DEBUG) Slog.d(TAG, "startMonitoringMotionLocked()");
if (mSigMotionSensor != null && !mSigMotionActive) { if (mMotionSensor != null && !mMotionListener.active) {
mSensorManager.requestTriggerSensor(mSigMotionListener, mSigMotionSensor); mMotionListener.registerLocked();
mSigMotionActive = true;
} }
} }
void stopMonitoringSignificantMotion() { void stopMonitoringMotionLocked() {
if (DEBUG) Slog.d(TAG, "stopMonitoringSignificantMotion()"); if (DEBUG) Slog.d(TAG, "stopMonitoringMotionLocked()");
if (mSigMotionActive) { if (mMotionSensor != null && mMotionListener.active) {
mSensorManager.cancelTriggerSensor(mSigMotionListener, mSigMotionSensor); mMotionListener.unregisterLocked();
mSigMotionActive = false;
} }
} }
@@ -1446,7 +1489,7 @@ public class DeviceIdleController extends SystemService
void scheduleAlarmLocked(long delay, boolean idleUntil) { void scheduleAlarmLocked(long delay, boolean idleUntil) {
if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")"); if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")");
if (mSigMotionSensor == null) { if (mMotionSensor == null) {
// If there is no motion sensor on this device, then we won't schedule // If there is no motion sensor on this device, then we won't schedule
// alarms, because we can't determine if the device is not moving. This effectively // alarms, because we can't determine if the device is not moving. This effectively
// turns off normal execution of device idling, although it is still possible to // turns off normal execution of device idling, although it is still possible to
@@ -1929,11 +1972,11 @@ public class DeviceIdleController extends SystemService
pw.print(" mEnabled="); pw.println(mEnabled); pw.print(" mEnabled="); pw.println(mEnabled);
pw.print(" mForceIdle="); pw.println(mForceIdle); pw.print(" mForceIdle="); pw.println(mForceIdle);
pw.print(" mSigMotionSensor="); pw.println(mSigMotionSensor); pw.print(" mMotionSensor="); pw.println(mMotionSensor);
pw.print(" mCurDisplay="); pw.println(mCurDisplay); pw.print(" mCurDisplay="); pw.println(mCurDisplay);
pw.print(" mScreenOn="); pw.println(mScreenOn); pw.print(" mScreenOn="); pw.println(mScreenOn);
pw.print(" mCharging="); pw.println(mCharging); pw.print(" mCharging="); pw.println(mCharging);
pw.print(" mSigMotionActive="); pw.println(mSigMotionActive); pw.print(" mMotionActive="); pw.println(mMotionListener.active);
pw.print(" mSensing="); pw.print(mSensing); pw.print(" mNotMoving="); pw.print(" mSensing="); pw.print(mSensing); pw.print(" mNotMoving=");
pw.println(mNotMoving); pw.println(mNotMoving);
pw.print(" mLocating="); pw.print(mLocating); pw.print(" mHasGps="); pw.print(" mLocating="); pw.print(mLocating); pw.print(" mHasGps=");