TwilightService v2.0
- Switched to using CalendarAstronomer for more accurate sunrise/sunset
times.
- Exposed sunrise/sunset times via TwilightState so that clients can
track the current twilight period and perform their own
interpolations.
- Adopted LocationRequest API for fused location updates:
(low power, min 1h, max 10m).
- TwilightService is now only activated when a listener is registered,
minimizing impact to system health on platforms / configurations
where twilight state is not needed.
Bug: 28588307
Bug: 30190450
Bug: 30282370
Bug: 30650316
Change-Id: Ic5c94d8608e8bb3a3d895e623676a1468d4abdcd
diff --git a/services/core/java/com/android/server/display/AutomaticBrightnessController.java b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
index c16dac2..15ae846 100644
--- a/services/core/java/com/android/server/display/AutomaticBrightnessController.java
+++ b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
@@ -22,6 +22,7 @@
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
+import android.annotation.Nullable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
@@ -268,7 +269,7 @@
pw.println();
pw.println("Automatic Brightness Controller State:");
pw.println(" mLightSensor=" + mLightSensor);
- pw.println(" mTwilight.getCurrentState()=" + mTwilight.getCurrentState());
+ pw.println(" mTwilight.getLastTwilightState()=" + mTwilight.getLastTwilightState());
pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
pw.println(" mLightSensorEnableTime=" + TimeUtils.formatUptime(mLightSensorEnableTime));
pw.println(" mAmbientLux=" + mAmbientLux);
@@ -495,12 +496,14 @@
}
if (mUseTwilight) {
- TwilightState state = mTwilight.getCurrentState();
+ TwilightState state = mTwilight.getLastTwilightState();
if (state != null && state.isNight()) {
- final long now = System.currentTimeMillis();
- gamma *= 1 + state.getAmount() * TWILIGHT_ADJUSTMENT_MAX_GAMMA;
+ final long duration = state.sunriseTimeMillis() - state.sunsetTimeMillis();
+ final long progress = System.currentTimeMillis() - state.sunsetTimeMillis();
+ final float amount = (float) Math.pow(2.0 * progress / duration - 1.0, 2.0);
+ gamma *= 1 + amount * TWILIGHT_ADJUSTMENT_MAX_GAMMA;
if (DEBUG) {
- Slog.d(TAG, "updateAutoBrightness: twilight amount=" + state.getAmount());
+ Slog.d(TAG, "updateAutoBrightness: twilight amount=" + amount);
}
}
}
@@ -621,7 +624,7 @@
private final TwilightListener mTwilightListener = new TwilightListener() {
@Override
- public void onTwilightStateChanged() {
+ public void onTwilightStateChanged(@Nullable TwilightState state) {
updateAutoBrightness(true /*sendUpdate*/);
}
};