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

Commit 3fcdfa1b authored by Christine Franks's avatar Christine Franks Committed by android-build-merger
Browse files

Merge "Night display status restores on reboot/user change" into oc-dev am: 78e03b44

am: 0bd001e9

Change-Id: I83536aa49173091ed5fe8ad96e8f752a5313ef93
parents 438931ab 0bd001e9
Loading
Loading
Loading
Loading
+11 −1
Original line number Original line Diff line number Diff line
@@ -6811,7 +6811,8 @@ public final class Settings {
         * Represented as milliseconds from midnight (e.g. 79200000 == 10pm).
         * Represented as milliseconds from midnight (e.g. 79200000 == 10pm).
         * @hide
         * @hide
         */
         */
        public static final String NIGHT_DISPLAY_CUSTOM_START_TIME = "night_display_custom_start_time";
        public static final String NIGHT_DISPLAY_CUSTOM_START_TIME =
                "night_display_custom_start_time";


        /**
        /**
         * Custom time when Night display is scheduled to deactivate.
         * Custom time when Night display is scheduled to deactivate.
@@ -6820,6 +6821,14 @@ public final class Settings {
         */
         */
        public static final String NIGHT_DISPLAY_CUSTOM_END_TIME = "night_display_custom_end_time";
        public static final String NIGHT_DISPLAY_CUSTOM_END_TIME = "night_display_custom_end_time";


        /**
         * Time in milliseconds (since epoch) when Night display was last activated. Use to decide
         * whether to apply the current activated state after a reboot or user change.
         * @hide
         */
        public static final String NIGHT_DISPLAY_LAST_ACTIVATED_TIME =
                "night_display_last_activated_time";

        /**
        /**
         * Names of the service components that the current user has explicitly allowed to
         * Names of the service components that the current user has explicitly allowed to
         * be a VR mode listener, separated by ':'.
         * be a VR mode listener, separated by ':'.
@@ -7046,6 +7055,7 @@ public final class Settings {
            NIGHT_DISPLAY_CUSTOM_END_TIME,
            NIGHT_DISPLAY_CUSTOM_END_TIME,
            NIGHT_DISPLAY_COLOR_TEMPERATURE,
            NIGHT_DISPLAY_COLOR_TEMPERATURE,
            NIGHT_DISPLAY_AUTO_MODE,
            NIGHT_DISPLAY_AUTO_MODE,
            NIGHT_DISPLAY_LAST_ACTIVATED_TIME,
            NIGHT_DISPLAY_ACTIVATED,
            NIGHT_DISPLAY_ACTIVATED,
            SYNC_PARENT_SOUNDS,
            SYNC_PARENT_SOUNDS,
            CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED,
            CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED,
+57 −38
Original line number Original line Diff line number Diff line
@@ -47,7 +47,6 @@ import com.android.server.SystemService;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
import com.android.server.twilight.TwilightState;
import com.android.server.vr.VrManagerService;


import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Calendar;
import java.util.Calendar;
@@ -62,7 +61,6 @@ public final class NightDisplayService extends SystemService
        implements NightDisplayController.Callback {
        implements NightDisplayController.Callback {


    private static final String TAG = "NightDisplayService";
    private static final String TAG = "NightDisplayService";
    private static final boolean DEBUG = false;


    /**
    /**
     * The transition time, in milliseconds, for Night Display to turn on/off.
     * The transition time, in milliseconds, for Night Display to turn on/off.
@@ -151,8 +149,9 @@ public final class NightDisplayService extends SystemService


    @Override
    @Override
    public void onBootPhase(int phase) {
    public void onBootPhase(int phase) {
        if (phase == PHASE_SYSTEM_SERVICES_READY) {
        if (phase >= PHASE_SYSTEM_SERVICES_READY) {
            IVrManager vrManager = (IVrManager) getBinderService(Context.VR_SERVICE);
            final IVrManager vrManager = IVrManager.Stub.asInterface(
                    getBinderService(Context.VR_SERVICE));
            if (vrManager != null) {
            if (vrManager != null) {
                try {
                try {
                    vrManager.registerListener(mVrStateCallbacks);
                    vrManager.registerListener(mVrStateCallbacks);
@@ -160,7 +159,9 @@ public final class NightDisplayService extends SystemService
                    Slog.e(TAG, "Failed to register VR mode state listener: " + e);
                    Slog.e(TAG, "Failed to register VR mode state listener: " + e);
                }
                }
            }
            }
        } else if (phase == PHASE_BOOT_COMPLETED) {
        }

        if (phase >= PHASE_BOOT_COMPLETED) {
            mBootCompleted = true;
            mBootCompleted = true;


            // Register listeners now that boot is complete.
            // Register listeners now that boot is complete.
@@ -284,12 +285,18 @@ public final class NightDisplayService extends SystemService
        if (mIsActivated == null || mIsActivated != activated) {
        if (mIsActivated == null || mIsActivated != activated) {
            Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display");
            Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display");


            if (mAutoMode != null) {
            if (mIsActivated != null) {
                mAutoMode.onActivated(activated);
                Secure.putLongForUser(getContext().getContentResolver(),
                        Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, System.currentTimeMillis(),
                        mCurrentUser);
            }
            }


            mIsActivated = activated;
            mIsActivated = activated;


            if (mAutoMode != null) {
                mAutoMode.onActivated(activated);
            }

            applyTint(false);
            applyTint(false);
        }
        }
    }
    }
@@ -423,8 +430,22 @@ public final class NightDisplayService extends SystemService
        outTemp[10] = blue;
        outTemp[10] = blue;
    }
    }


    private Calendar getLastActivatedTime() {
        final ContentResolver cr = getContext().getContentResolver();
        final long lastActivatedTimeMillis = Secure.getLongForUser(
                cr, Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, -1, mCurrentUser);
        if (lastActivatedTimeMillis < 0) {
            return null;
        }

        final Calendar lastActivatedTime = Calendar.getInstance();
        lastActivatedTime.setTimeInMillis(lastActivatedTimeMillis);
        return lastActivatedTime;
    }

    private abstract class AutoMode implements NightDisplayController.Callback {
    private abstract class AutoMode implements NightDisplayController.Callback {
        public abstract void onStart();
        public abstract void onStart();

        public abstract void onStop();
        public abstract void onStop();
    }
    }


@@ -438,7 +459,7 @@ public final class NightDisplayService extends SystemService


        private Calendar mLastActivatedTime;
        private Calendar mLastActivatedTime;


        public CustomAutoMode() {
        CustomAutoMode() {
            mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
            mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
            mTimeChangedReceiver = new BroadcastReceiver() {
            mTimeChangedReceiver = new BroadcastReceiver() {
                @Override
                @Override
@@ -452,10 +473,10 @@ public final class NightDisplayService extends SystemService
            final Calendar now = Calendar.getInstance();
            final Calendar now = Calendar.getInstance();
            final Calendar startTime = mStartTime.getDateTimeBefore(now);
            final Calendar startTime = mStartTime.getDateTimeBefore(now);
            final Calendar endTime = mEndTime.getDateTimeAfter(startTime);
            final Calendar endTime = mEndTime.getDateTimeAfter(startTime);
            final boolean activated = now.before(endTime);


            boolean setActivated = mIsActivated == null || mLastActivatedTime == null;
            boolean activate = now.before(endTime);
            if (!setActivated && mIsActivated != activated) {
            if (mLastActivatedTime != null) {
                // Convert mLastActivatedTime to the current timezone if needed.
                final TimeZone currentTimeZone = now.getTimeZone();
                final TimeZone currentTimeZone = now.getTimeZone();
                if (!currentTimeZone.equals(mLastActivatedTime.getTimeZone())) {
                if (!currentTimeZone.equals(mLastActivatedTime.getTimeZone())) {
                    final int year = mLastActivatedTime.get(Calendar.YEAR);
                    final int year = mLastActivatedTime.get(Calendar.YEAR);
@@ -470,17 +491,16 @@ public final class NightDisplayService extends SystemService
                    mLastActivatedTime.set(Calendar.MINUTE, minute);
                    mLastActivatedTime.set(Calendar.MINUTE, minute);
                }
                }


                if (mIsActivated) {
                // Maintain the existing activated state if within the current period.
                    setActivated = now.before(mStartTime.getDateTimeBefore(mLastActivatedTime))
                if (mLastActivatedTime.before(now)
                            || now.after(mEndTime.getDateTimeAfter(mLastActivatedTime));
                        && mLastActivatedTime.after(startTime)
                } else {
                        && (mLastActivatedTime.after(endTime) || now.before(endTime))) {
                    setActivated = now.before(mEndTime.getDateTimeBefore(mLastActivatedTime))
                    activate = mController.isActivated();
                            || now.after(mStartTime.getDateTimeAfter(mLastActivatedTime));
                }
                }
            }
            }


            if (setActivated) {
            if (mIsActivated == null || mIsActivated != activate) {
                mController.setActivated(activated);
                mController.setActivated(activate);
            }
            }
            updateNextAlarm(mIsActivated, now);
            updateNextAlarm(mIsActivated, now);
        }
        }
@@ -502,6 +522,8 @@ public final class NightDisplayService extends SystemService
            mStartTime = mController.getCustomStartTime();
            mStartTime = mController.getCustomStartTime();
            mEndTime = mController.getCustomEndTime();
            mEndTime = mController.getCustomEndTime();


            mLastActivatedTime = getLastActivatedTime();

            // Force an update to initialize state.
            // Force an update to initialize state.
            updateActivated();
            updateActivated();
        }
        }
@@ -516,11 +538,8 @@ public final class NightDisplayService extends SystemService


        @Override
        @Override
        public void onActivated(boolean activated) {
        public void onActivated(boolean activated) {
            final Calendar now = Calendar.getInstance();
            mLastActivatedTime = getLastActivatedTime();
            if (mIsActivated != null) {
            updateNextAlarm(activated, Calendar.getInstance());
                mLastActivatedTime = now;
            }
            updateNextAlarm(activated, now);
        }
        }


        @Override
        @Override
@@ -550,33 +569,33 @@ public final class NightDisplayService extends SystemService


        private Calendar mLastActivatedTime;
        private Calendar mLastActivatedTime;


        public TwilightAutoMode() {
        TwilightAutoMode() {
            mTwilightManager = getLocalService(TwilightManager.class);
            mTwilightManager = getLocalService(TwilightManager.class);
        }
        }


        private void updateActivated(TwilightState state) {
        private void updateActivated(TwilightState state) {
            final boolean isNight = state != null && state.isNight();
            boolean activate = state != null && state.isNight();
            boolean setActivated = mIsActivated == null || mIsActivated != isNight;
            if (state != null && mLastActivatedTime != null) {
            if (setActivated && state != null && mLastActivatedTime != null) {
                final Calendar now = Calendar.getInstance();
                final Calendar sunrise = state.sunrise();
                final Calendar sunrise = state.sunrise();
                final Calendar sunset = state.sunset();
                final Calendar sunset = state.sunset();
                if (sunrise.before(sunset)) {

                    setActivated = mLastActivatedTime.before(sunrise)
                // Maintain the existing activated state if within the current period.
                            || mLastActivatedTime.after(sunset);
                if (mLastActivatedTime.before(now)
                } else {
                        && (mLastActivatedTime.after(sunrise) ^ mLastActivatedTime.after(sunset))) {
                    setActivated = mLastActivatedTime.before(sunset)
                    activate = mController.isActivated();
                            || mLastActivatedTime.after(sunrise);
                }
                }
            }
            }


            if (setActivated) {
            if (mIsActivated == null || mIsActivated != activate) {
                mController.setActivated(isNight);
                mController.setActivated(activate);
            }
            }
        }
        }


        @Override
        @Override
        public void onStart() {
        public void onStart() {
            mTwilightManager.registerListener(this, mHandler);
            mTwilightManager.registerListener(this, mHandler);
            mLastActivatedTime = getLastActivatedTime();


            // Force an update to initialize state.
            // Force an update to initialize state.
            updateActivated(mTwilightManager.getLastTwilightState());
            updateActivated(mTwilightManager.getLastTwilightState());
@@ -591,7 +610,7 @@ public final class NightDisplayService extends SystemService
        @Override
        @Override
        public void onActivated(boolean activated) {
        public void onActivated(boolean activated) {
            if (mIsActivated != null) {
            if (mIsActivated != null) {
                mLastActivatedTime = Calendar.getInstance();
                mLastActivatedTime = getLastActivatedTime();
            }
            }
        }
        }


+1 −1
Original line number Original line Diff line number Diff line
@@ -31,7 +31,7 @@ public final class TwilightState {
    private final long mSunriseTimeMillis;
    private final long mSunriseTimeMillis;
    private final long mSunsetTimeMillis;
    private final long mSunsetTimeMillis;


    TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) {
    public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) {
        mSunriseTimeMillis = sunriseTimeMillis;
        mSunriseTimeMillis = sunriseTimeMillis;
        mSunsetTimeMillis = sunsetTimeMillis;
        mSunsetTimeMillis = sunsetTimeMillis;
    }
    }
+1 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@
        package="com.android.frameworks.servicestests">
        package="com.android.frameworks.servicestests">


    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.ACCESS_VR_MANAGER" />
    <uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
    <uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+620 −0

File added.

Preview size limit exceeded, changes collapsed.