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

Commit 7ad45662 authored by Kenny Guy's avatar Kenny Guy Committed by Android (Google) Code Review
Browse files

Merge "Fix BrightnessTracker's handling of color temperature"

parents a1a76a37 2385d4fa
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -112,8 +112,8 @@ public final class ColorDisplayController {

    private final Context mContext;
    private final int mUserId;
    private final ContentObserver mContentObserver;

    private ContentObserver mContentObserver;
    private Callback mCallback;
    private MetricsLogger mMetricsLogger;

@@ -124,18 +124,6 @@ public final class ColorDisplayController {
    public ColorDisplayController(@NonNull Context context, int userId) {
        mContext = context.getApplicationContext();
        mUserId = userId;

        mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
            @Override
            public void onChange(boolean selfChange, Uri uri) {
                super.onChange(selfChange, uri);

                final String setting = uri == null ? null : uri.getLastPathSegment();
                if (setting != null) {
                    onSettingChanged(setting);
                }
            }
        };
    }

    /**
@@ -522,6 +510,20 @@ public final class ColorDisplayController {
        if (oldCallback != callback) {
            mCallback = callback;

            if (mContentObserver == null) {
                mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
                    @Override
                    public void onChange(boolean selfChange, Uri uri) {
                        super.onChange(selfChange, uri);

                        final String setting = uri == null ? null : uri.getLastPathSegment();
                        if (setting != null) {
                            onSettingChanged(setting);
                        }
                    }
                };
            }

            if (callback == null) {
                // Stop listening for changes now that there IS NOT a listener.
                mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+12 −7
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.util.Xml;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.ColorDisplayController;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.RingBuffer;
@@ -72,7 +73,6 @@ import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Deque;
import java.util.HashMap;
@@ -363,12 +363,9 @@ public class BrightnessTracker {
            return;
        }

        builder.setNightMode(mInjector.getSecureIntForUser(mContentResolver,
                Settings.Secure.NIGHT_DISPLAY_ACTIVATED, 0, UserHandle.USER_CURRENT)
                == 1);
        builder.setColorTemperature(mInjector.getSecureIntForUser(mContentResolver,
                Settings.Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE,
                0, UserHandle.USER_CURRENT));
        builder.setNightMode(mInjector.isNightModeActive(mContext, UserHandle.USER_CURRENT));
        builder.setColorTemperature(mInjector.getColorTemperature(mContext,
                UserHandle.USER_CURRENT));

        BrightnessChangeEvent event = builder.build();
        if (DEBUG) {
@@ -952,5 +949,13 @@ public class BrightnessTracker {
        public boolean isInteractive(Context context) {
            return context.getSystemService(PowerManager.class).isInteractive();
        }

        public int getColorTemperature(Context context, int userId) {
            return new ColorDisplayController(context, userId).getColorTemperature();
        }

        public boolean isNightModeActive(Context context, int userId) {
            return new ColorDisplayController(context, userId).isActivated();
        }
    }
}
+20 −1
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -82,6 +84,8 @@ public class BrightnessTrackerTest {
    private static HandlerThread sThread =
            new HandlerThread("brightness.test", android.os.Process.THREAD_PRIORITY_BACKGROUND);

    private int mDefaultNightModeColorTemperature;

    private static Handler ensureHandler() {
        synchronized (sHandlerLock) {
            if (sHandler == null) {
@@ -98,6 +102,9 @@ public class BrightnessTrackerTest {
        mInjector = new TestInjector(ensureHandler());

        mTracker = new BrightnessTracker(InstrumentationRegistry.getContext(), mInjector);
        mDefaultNightModeColorTemperature =
                InstrumentationRegistry.getContext().getResources().getInteger(
                R.integer.config_nightDisplayColorTemperatureDefault);
    }

    @Test
@@ -188,7 +195,7 @@ public class BrightnessTrackerTest {
        // System had no data so these should all be at defaults.
        assertEquals(Float.NaN, event.batteryLevel, 0.0);
        assertFalse(event.nightMode);
        assertEquals(0, event.colorTemperature);
        assertEquals(mDefaultNightModeColorTemperature, event.colorTemperature);
    }

    @Test
@@ -863,5 +870,17 @@ public class BrightnessTrackerTest {
        public boolean isInteractive(Context context) {
            return mInteractive;
        }

        @Override
        public int getColorTemperature(Context context, int userId) {
          return mSecureIntSettings.getOrDefault(Settings.Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE,
                  mDefaultNightModeColorTemperature);
        }

        @Override
        public boolean isNightModeActive(Context context, int userId) {
            return mSecureIntSettings.getOrDefault(Settings.Secure.NIGHT_DISPLAY_ACTIVATED,
                    0) == 1;
        }
    }
}