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

Commit 6b7dc168 authored by Marcelo Arteiro's avatar Marcelo Arteiro
Browse files

Add contrast listeners and getter methods to server

Flag: EXEMPT no logical change
Bug: 362682063
Bug: 440236342
Test: presubmit
Change-Id: I3d1f50403e852bb0131108e9972c4fd08eab288b
parent efea9d76
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server;

import java.util.concurrent.Executor;

/**
 * UiModeManager local system service interface.
 *
@@ -36,4 +38,15 @@ public abstract class UiModeManagerInternal {

    /** Returns the UI mode for the given display. */
    public abstract int getDisplayUiMode(int displayId);

    /** Returns contrast level for the given user. */
    public abstract float getContrast(int userId);

    public interface ContrastListenerInternal {
        /** Called when the contrast level changes. */
        void onContrastChange(int userId, float contrastLevel);
    }

    /** Adds a contrast listener for all users. */
    public abstract void addContrastListener(ContrastListenerInternal listener, Executor executor);
}
+33 −3
Original line number Diff line number Diff line
@@ -134,6 +134,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;

final class UiModeManagerService extends SystemService {
    private static final String TAG = UiModeManager.class.getSimpleName();
@@ -508,25 +509,38 @@ final class UiModeManagerService extends SystemService {
    private final ContentObserver mContrastObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            final SparseArray<Float> usersToNotify = new SparseArray<>();

            synchronized (mLock) {
                if (fixContrastAndForceInvertStateForMultiUser()) {
                    for (int i = 0; i < mUiModeManagerCallbacks.size(); i++) {
                        int userId = mUiModeManagerCallbacks.keyAt(i);
                        if (updateContrastLocked(userId)) {
                            float contrast = getContrastLocked(userId);
                            usersToNotify.append(userId, contrast);
                            mUiModeManagerCallbacks.valueAt(i).broadcast(ignoreRemoteException(
                                    callback -> callback.notifyContrastChanged(contrast)));
                        }
                    }
                    return;
                }
                if (updateContrastLocked()) {
                } else if (updateContrastLocked()) {
                    float contrast = getContrastLocked();
                    usersToNotify.append(mCurrentUser, contrast);
                    mUiModeManagerCallbacks.get(mCurrentUser, new RemoteCallbackList<>())
                            .broadcast(ignoreRemoteException(
                                    callback -> callback.notifyContrastChanged(contrast)));
                }
            }

            for (int i = 0; i < usersToNotify.size(); i++) {
                int userId = usersToNotify.keyAt(i);
                float contrast = usersToNotify.valueAt(i);
                for (Map.Entry<UiModeManagerInternal.ContrastListenerInternal, Executor> entry :
                        mLocalService.mContrastListeners.entrySet()) {
                    UiModeManagerInternal.ContrastListenerInternal listener = entry.getKey();
                    Executor executor = entry.getValue();
                    executor.execute(() -> listener.onContrastChange(userId, contrast));
                }
            }
        }
    };

@@ -2673,6 +2687,8 @@ final class UiModeManagerService extends SystemService {
    }

    public final class LocalService extends UiModeManagerInternal {
        private final HashMap<ContrastListenerInternal, Executor> mContrastListeners =
                new HashMap<>();

        @Override
        public boolean isNightMode(int displayId) {
@@ -2749,6 +2765,20 @@ final class UiModeManagerService extends SystemService {
                return uiMode;
            }
        }

        @Override
        public float getContrast(int userId) {
            synchronized (mLock) {
                return getContrastLocked(userId);
            }
        }

        @Override
        public void addContrastListener(ContrastListenerInternal listener, Executor executor) {
            synchronized (mLock) {
                mContrastListeners.put(listener, executor);
            }
        }
    }

    @VisibleForTesting(otherwise = VisibleForTesting.NONE)