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

Commit 71e003eb authored by Christine Franks's avatar Christine Franks
Browse files

Replace ColorDisplayController references

with ColorDisplayManager, where possible

Bug: 123352608
Test: atest FrameworksServicesTests:ColorDisplayServiceTest

Change-Id: I4feb67197462d49002582931aee285abe85054e6
parent d6e59160
Loading
Loading
Loading
Loading
+153 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hardware.display;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings.Secure;

import java.time.LocalTime;

/**
 * @hide
 */
public class NightDisplayListener {

    private final Context mContext;
    private final int mUserId;
    private final ColorDisplayManager mManager;

    private ContentObserver mContentObserver;
    private Callback mCallback;

    public NightDisplayListener(@NonNull Context context) {
        this(context, ActivityManager.getCurrentUser());
    }

    public NightDisplayListener(@NonNull Context context, @UserIdInt int userId) {
        mContext = context.getApplicationContext();
        mUserId = userId;
        mManager = mContext.getSystemService(ColorDisplayManager.class);
    }

    /**
     * Register a callback to be invoked whenever the Night display settings are changed.
     */
    public void setCallback(Callback callback) {
        final Callback oldCallback = mCallback;
        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);
                        onSettingChanged(uri);
                    }
                };
            }

            if (callback == null) {
                // Stop listening for changes now that there IS NOT a callback.
                mContext.getContentResolver().unregisterContentObserver(mContentObserver);
            } else if (oldCallback == null) {
                // Start listening for changes now that there IS a callback.
                final ContentResolver cr = mContext.getContentResolver();
                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_ACTIVATED),
                        false /* notifyForDescendants */, mContentObserver, mUserId);
                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_AUTO_MODE),
                        false /* notifyForDescendants */, mContentObserver, mUserId);
                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_START_TIME),
                        false /* notifyForDescendants */, mContentObserver, mUserId);
                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_END_TIME),
                        false /* notifyForDescendants */, mContentObserver, mUserId);
                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE),
                        false /* notifyForDescendants */, mContentObserver, mUserId);
            }
        }
    }

    private void onSettingChanged(Uri uri) {
        final String setting = uri == null ? null : uri.getLastPathSegment();
        if (setting == null || mCallback == null) {
            return;
        }

        switch (setting) {
            case Secure.NIGHT_DISPLAY_ACTIVATED:
                mCallback.onActivated(mManager.isNightDisplayActivated());
                break;
            case Secure.NIGHT_DISPLAY_AUTO_MODE:
                mCallback.onAutoModeChanged(mManager.getNightDisplayAutoMode());
                break;
            case Secure.NIGHT_DISPLAY_CUSTOM_START_TIME:
                mCallback.onCustomStartTimeChanged(mManager.getNightDisplayCustomStartTime());
                break;
            case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME:
                mCallback.onCustomEndTimeChanged(mManager.getNightDisplayCustomEndTime());
                break;
            case Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE:
                mCallback.onColorTemperatureChanged(mManager.getNightDisplayColorTemperature());
                break;
        }
    }

    /**
     * Callback invoked whenever the Night display settings are changed.
     */
    public interface Callback {
        /**
         * Callback invoked when the activated state changes.
         *
         * @param activated {@code true} if Night display is activated
         */
        default void onActivated(boolean activated) {}
        /**
         * Callback invoked when the auto mode changes.
         *
         * @param autoMode the auto mode to use
         */
        default void onAutoModeChanged(int autoMode) {}
        /**
         * Callback invoked when the time to automatically activate Night display changes.
         *
         * @param startTime the local time to automatically activate Night display
         */
        default void onCustomStartTimeChanged(LocalTime startTime) {}
        /**
         * Callback invoked when the time to automatically deactivate Night display changes.
         *
         * @param endTime the local time to automatically deactivate Night display
         */
        default void onCustomEndTimeChanged(LocalTime endTime) {}

        /**
         * Callback invoked when the color temperature changes.
         *
         * @param colorTemperature the color temperature to tint the screen
         */
        default void onColorTemperatureChanged(int colorTemperature) {}
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -963,7 +963,7 @@
    <bool name="config_nightDisplayAvailable">@bool/config_setColorTransformAccelerated</bool>

    <!-- Default mode to control how Night display is automatically activated.
         One of the following values (see ColorDisplayController.java):
         One of the following values (see ColorDisplayManager.java):
             0 - AUTO_MODE_DISABLED
             1 - AUTO_MODE_CUSTOM_TIME
             2 - AUTO_MODE_TWILIGHT
@@ -1052,7 +1052,7 @@
    </string-array>


    <!-- Indicate available ColorDisplayController.COLOR_MODE_xxx. -->
    <!-- Indicate available ColorDisplayManager.COLOR_MODE_xxx. -->
    <integer-array name="config_availableColorModes">
        <!-- Example:
        <item>0</item>
+3 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.SensorPrivacyManager;
import android.hardware.display.NightDisplayListener;
import android.os.Handler;
import android.os.Looper;
import android.util.ArrayMap;
@@ -25,7 +26,6 @@ import android.util.DisplayMetrics;
import android.view.IWindowManager;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.ColorDisplayController;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.statusbar.IStatusBarService;
import com.android.internal.util.Preconditions;
@@ -206,7 +206,7 @@ public class Dependency extends SystemUI {
    @Inject Lazy<UserInfoController> mUserInfoController;
    @Inject Lazy<KeyguardMonitor> mKeyguardMonitor;
    @Inject Lazy<BatteryController> mBatteryController;
    @Inject Lazy<ColorDisplayController> mColorDisplayController;
    @Inject Lazy<NightDisplayListener> mNightDisplayListener;
    @Inject Lazy<ManagedProfileController> mManagedProfileController;
    @Inject Lazy<NextAlarmController> mNextAlarmController;
    @Inject Lazy<DataSaverController> mDataSaverController;
@@ -330,7 +330,7 @@ public class Dependency extends SystemUI {

        mProviders.put(BatteryController.class, mBatteryController::get);

        mProviders.put(ColorDisplayController.class, mColorDisplayController::get);
        mProviders.put(NightDisplayListener.class, mNightDisplayListener::get);

        mProviders.put(ManagedProfileController.class, mManagedProfileController::get);

+3 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.systemui.Dependency.TIME_TICK_HANDLER_NAME;
import android.annotation.Nullable;
import android.content.Context;
import android.hardware.SensorPrivacyManager;
import android.hardware.display.NightDisplayListener;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
@@ -34,7 +35,6 @@ import android.util.DisplayMetrics;
import android.view.IWindowManager;
import android.view.WindowManagerGlobal;

import com.android.internal.app.ColorDisplayController;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.statusbar.IStatusBarService;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -152,8 +152,8 @@ public class DependencyProvider {

    @Singleton
    @Provides
    public ColorDisplayController provideColorDisplayController(Context context) {
        return new ColorDisplayController(context);
    public NightDisplayListener provideNightDisplayListener(Context context) {
        return new NightDisplayListener(context);
    }

    @Singleton
+24 −20
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.Nullable;
import android.app.ActivityManager;
import android.content.Intent;
import android.hardware.display.ColorDisplayManager;
import android.hardware.display.NightDisplayListener;
import android.metrics.LogMaker;
import android.provider.Settings;
import android.service.quicksettings.Tile;
@@ -31,7 +32,6 @@ import android.widget.Switch;

import androidx.annotation.StringRes;

import com.android.internal.app.ColorDisplayController;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
@@ -46,8 +46,9 @@ import java.util.TimeZone;

import javax.inject.Inject;

public class NightDisplayTile extends QSTileImpl<BooleanState>
        implements ColorDisplayController.Callback {
/** Quick settings tile: Night display **/
public class NightDisplayTile extends QSTileImpl<BooleanState> implements
        NightDisplayListener.Callback {

    /**
     * Pattern for {@link java.time.format.DateTimeFormatter} used to approximate the time to the
@@ -57,13 +58,15 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>
    private static final String PATTERN_HOUR_MINUTE = "h:mm a";
    private static final String PATTERN_HOUR_NINUTE_24 = "HH:mm";

    private ColorDisplayController mController;
    private final ColorDisplayManager mManager;
    private NightDisplayListener mListener;
    private boolean mIsListening;

    @Inject
    public NightDisplayTile(QSHost host) {
        super(host);
        mController = new ColorDisplayController(mContext, ActivityManager.getCurrentUser());
        mManager = mContext.getSystemService(ColorDisplayManager.class);
        mListener = new NightDisplayListener(mContext, ActivityManager.getCurrentUser());
    }

    @Override
@@ -81,27 +84,27 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>
        // Enroll in forced auto mode if eligible.
        if ("1".equals(Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE))
                && mController.getAutoModeRaw() == -1) {
            mController.setAutoMode(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME);
                && mManager.getNightDisplayAutoModeRaw() == -1) {
            mManager.setNightDisplayAutoMode(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME);
            Log.i("NightDisplayTile", "Enrolled in forced night display auto mode");
        }

        // Change current activation state.
        final boolean activated = !mState.value;
        mController.setActivated(activated);
        mManager.setNightDisplayActivated(activated);
    }

    @Override
    protected void handleUserSwitch(int newUserId) {
        // Stop listening to the old controller.
        if (mIsListening) {
            mController.setListener(null);
            mListener.setCallback(null);
        }

        // Make a new controller for the new user.
        mController = new ColorDisplayController(mContext, newUserId);
        mListener = new NightDisplayListener(mContext, newUserId);
        if (mIsListening) {
            mController.setListener(this);
            mListener.setCallback(this);
        }

        super.handleUserSwitch(newUserId);
@@ -109,7 +112,7 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>

    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        state.value = mController.isActivated();
        state.value = mManager.isNightDisplayActivated();
        state.label = mContext.getString(R.string.quick_settings_night_display_label);
        state.icon = ResourceIcon.get(com.android.internal.R.drawable.ic_qs_night_display_on);
        state.expandedAccessibilityClassName = Switch.class.getName();
@@ -121,12 +124,12 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>
    }

    /**
     * Returns a {@link String} for the secondary label that reflects when the light will be turned
     * on or off based on the current auto mode and night light activated status.
     * Returns a String for the secondary label that reflects when the light will be turned on or
     * off based on the current auto mode and night light activated status.
     */
    @Nullable
    private String getSecondaryLabel(boolean isNightLightActivated) {
        switch(mController.getAutoMode()) {
        switch (mManager.getNightDisplayAutoMode()) {
            case ColorDisplayManager.AUTO_MODE_TWILIGHT:
                // Auto mode related to sunrise & sunset. If the light is on, it's guaranteed to be
                // turned off at sunrise. If it's off, it's guaranteed to be turned on at sunset.
@@ -143,10 +146,10 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>
                final DateTimeFormatter toggleTimeFormat;

                if (isNightLightActivated) {
                    toggleTime = mController.getCustomEndTime();
                    toggleTime = mManager.getNightDisplayCustomEndTime();
                    toggleTimeStringRes = R.string.quick_settings_secondary_label_until;
                } else {
                    toggleTime = mController.getCustomStartTime();
                    toggleTime = mManager.getNightDisplayCustomStartTime();
                    toggleTimeStringRes = R.string.quick_settings_night_secondary_label_on_at;
                }

@@ -175,7 +178,8 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>

    @Override
    public LogMaker populate(LogMaker logMaker) {
        return super.populate(logMaker).addTaggedData(FIELD_QS_MODE, mController.getAutoModeRaw());
        return super.populate(logMaker)
                .addTaggedData(FIELD_QS_MODE, mManager.getNightDisplayAutoModeRaw());
    }

    @Override
@@ -187,10 +191,10 @@ public class NightDisplayTile extends QSTileImpl<BooleanState>
    protected void handleSetListening(boolean listening) {
        mIsListening = listening;
        if (listening) {
            mController.setListener(this);
            mListener.setCallback(this);
            refreshState();
        } else {
            mController.setListener(null);
            mListener.setCallback(null);
        }
    }

Loading