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

Commit 5ff52995 authored by Vaibhav Devmurari's avatar Vaibhav Devmurari Committed by Android (Google) Code Review
Browse files

Merge "Cleanup old sysprop flags for KeyboardBacklightController" into main

parents 73cd4c07 53d5dddd
Loading
Loading
Loading
Loading
+0 −101
Original line number Diff line number Diff line
/*
 * Copyright 2023 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 com.android.server.input;

import android.sysprop.InputProperties;

import java.util.Optional;

/**
 * A component of {@link InputManagerService} responsible for managing the input sysprop flags
 *
 * @hide
 */
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public final class InputFeatureFlagProvider {

    // To disable Keyboard backlight control via Framework, run:
    // 'adb shell setprop persist.input.keyboard_backlight_control.enabled false' (requires restart)
    private static final boolean KEYBOARD_BACKLIGHT_CONTROL_ENABLED =
            InputProperties.enable_keyboard_backlight_control().orElse(true);

    // To disable Framework controlled keyboard backlight animation run:
    // adb shell setprop persist.input.keyboard.backlight_animation.enabled false (requires restart)
    private static final boolean KEYBOARD_BACKLIGHT_ANIMATION_ENABLED =
            InputProperties.enable_keyboard_backlight_animation().orElse(false);

    // To disable Custom keyboard backlight levels support via IDC files run:
    // adb shell setprop persist.input.keyboard.backlight_custom_levels.enabled false (requires
    // restart)
    private static final boolean KEYBOARD_BACKLIGHT_CUSTOM_LEVELS_ENABLED =
            InputProperties.enable_keyboard_backlight_custom_levels().orElse(true);

    // To disable als based ambient keyboard backlight control run:
    // adb shell setprop persist.input.keyboard.ambient_backlight_control.enabled false (requires
    // restart)
    private static final boolean AMBIENT_KEYBOARD_BACKLIGHT_CONTROL_ENABLED =
            InputProperties.enable_ambient_keyboard_backlight_control().orElse(true);

    private static Optional<Boolean> sKeyboardBacklightControlOverride = Optional.empty();
    private static Optional<Boolean> sKeyboardBacklightAnimationOverride = Optional.empty();
    private static Optional<Boolean> sKeyboardBacklightCustomLevelsOverride = Optional.empty();
    private static Optional<Boolean> sAmbientKeyboardBacklightControlOverride = Optional.empty();

    public static boolean isKeyboardBacklightControlEnabled() {
        return sKeyboardBacklightControlOverride.orElse(KEYBOARD_BACKLIGHT_CONTROL_ENABLED);
    }

    public static boolean isKeyboardBacklightAnimationEnabled() {
        return sKeyboardBacklightAnimationOverride.orElse(KEYBOARD_BACKLIGHT_ANIMATION_ENABLED);
    }

    public static boolean isKeyboardBacklightCustomLevelsEnabled() {
        return sKeyboardBacklightCustomLevelsOverride.orElse(
                KEYBOARD_BACKLIGHT_CUSTOM_LEVELS_ENABLED);
    }

    public static boolean isAmbientKeyboardBacklightControlEnabled() {
        return sAmbientKeyboardBacklightControlOverride.orElse(
                AMBIENT_KEYBOARD_BACKLIGHT_CONTROL_ENABLED);
    }

    public static void setKeyboardBacklightControlEnabled(boolean enabled) {
        sKeyboardBacklightControlOverride = Optional.of(enabled);
    }

    public static void setKeyboardBacklightAnimationEnabled(boolean enabled) {
        sKeyboardBacklightAnimationOverride = Optional.of(enabled);
    }

    public static void setKeyboardBacklightCustomLevelsEnabled(boolean enabled) {
        sKeyboardBacklightCustomLevelsOverride = Optional.of(enabled);
    }

    public static void setAmbientKeyboardBacklightControlEnabled(boolean enabled) {
        sAmbientKeyboardBacklightControlOverride = Optional.of(enabled);
    }

    /**
     * Clears all input feature flag overrides.
     */
    public static void clearOverrides() {
        sKeyboardBacklightControlOverride = Optional.empty();
        sKeyboardBacklightAnimationOverride = Optional.empty();
        sKeyboardBacklightCustomLevelsOverride = Optional.empty();
        sAmbientKeyboardBacklightControlOverride = Optional.empty();
    }
}
+4 −6
Original line number Diff line number Diff line
@@ -471,11 +471,9 @@ public class InputManagerService extends IInputManager.Stub
        }

        KeyboardBacklightControllerInterface getKeyboardBacklightController(
                NativeInputManagerService nativeService, PersistentDataStore dataStore) {
            return InputFeatureFlagProvider.isKeyboardBacklightControlEnabled()
                    ? new KeyboardBacklightController(mContext, nativeService, dataStore,
                    mLooper, mUEventManager)
                    : new KeyboardBacklightControllerInterface() {};
                NativeInputManagerService nativeService) {
            return new KeyboardBacklightController(mContext, nativeService, mLooper,
                    mUEventManager);
        }
    }

@@ -500,7 +498,7 @@ public class InputManagerService extends IInputManager.Stub
                        injector.getLooper(), this) : null;
        mBatteryController = new BatteryController(mContext, mNative, injector.getLooper(),
                injector.getUEventManager());
        mKeyboardBacklightController = injector.getKeyboardBacklightController(mNative, mDataStore);
        mKeyboardBacklightController = injector.getKeyboardBacklightController(mNative);
        mStickyModifierStateController = new StickyModifierStateController();
        mKeyGestureController = new KeyGestureController(mContext, injector.getLooper());
        mKeyboardLedController = new KeyboardLedController(mContext, injector.getLooper(),
+14 −67
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UEventObserver;
import android.sysprop.InputProperties;
import android.text.TextUtils;
import android.util.IndentingPrintWriter;
import android.util.Log;
@@ -47,7 +48,6 @@ import java.io.PrintWriter;
import java.time.Duration;
import java.util.Arrays;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.TreeSet;

/**
@@ -63,6 +63,10 @@ final class KeyboardBacklightController implements
    // 'adb shell setprop log.tag.KbdBacklightController DEBUG' (requires restart)
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    // To disable Framework controlled keyboard backlight animation run:
    // adb shell setprop persist.input.keyboard.backlight_animation.enabled false (requires restart)
    private final boolean mKeyboardBacklightAnimationEnabled;

    private enum Direction {
        DIRECTION_UP, DIRECTION_DOWN
    }
@@ -87,9 +91,6 @@ final class KeyboardBacklightController implements

    private final Context mContext;
    private final NativeInputManagerService mNative;
    // The PersistentDataStore should be locked before use.
    @GuardedBy("mDataStore")
    private final PersistentDataStore mDataStore;
    private final Handler mHandler;
    private final AnimatorFactory mAnimatorFactory;
    private final UEventManager mUEventManager;
@@ -123,17 +124,15 @@ final class KeyboardBacklightController implements
    }

    KeyboardBacklightController(Context context, NativeInputManagerService nativeService,
            PersistentDataStore dataStore, Looper looper, UEventManager uEventManager) {
        this(context, nativeService, dataStore, looper, ValueAnimator::ofInt, uEventManager);
            Looper looper, UEventManager uEventManager) {
        this(context, nativeService, looper, ValueAnimator::ofInt, uEventManager);
    }

    @VisibleForTesting
    KeyboardBacklightController(Context context, NativeInputManagerService nativeService,
            PersistentDataStore dataStore, Looper looper, AnimatorFactory animatorFactory,
            UEventManager uEventManager) {
            Looper looper, AnimatorFactory animatorFactory, UEventManager uEventManager) {
        mContext = context;
        mNative = nativeService;
        mDataStore = dataStore;
        mHandler = new Handler(looper, this::handleMessage);
        mAnimatorFactory = animatorFactory;
        mAmbientController = new AmbientKeyboardBacklightController(context, looper);
@@ -141,6 +140,8 @@ final class KeyboardBacklightController implements
        Resources res = mContext.getResources();
        mUserInactivityThresholdMs = res.getInteger(
                com.android.internal.R.integer.config_keyboardBacklightTimeoutMs);
        mKeyboardBacklightAnimationEnabled =
                InputProperties.enable_keyboard_backlight_animation().orElse(false);
    }

    @Override
@@ -164,11 +165,9 @@ final class KeyboardBacklightController implements
            }
        }, UEVENT_KEYBOARD_BACKLIGHT_TAG);

        if (InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) {
        // Start ambient backlight controller
        mAmbientController.systemRunning();
    }
    }

    @Override
    public void incrementKeyboardBacklight(int deviceId) {
@@ -229,9 +228,6 @@ final class KeyboardBacklightController implements
        // level through keyboard up/down button
        updateAmbientLightListener();

        maybeBackupBacklightBrightness(inputDevice, state.mLight,
                state.mBrightnessValueForLevel[newBrightnessLevel]);

        if (DEBUG) {
            Slog.d(TAG,
                    "Changing state from " + state.mBrightnessLevel + " to " + newBrightnessLevel);
@@ -248,47 +244,6 @@ final class KeyboardBacklightController implements
        }
    }

    private void maybeBackupBacklightBrightness(InputDevice inputDevice, Light keyboardBacklight,
            int brightnessValue) {
        // Don't back up or restore when ALS based keyboard backlight is enabled
        if (InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) {
            return;
        }
        synchronized (mDataStore) {
            try {
                mDataStore.setKeyboardBacklightBrightness(inputDevice.getDescriptor(),
                        keyboardBacklight.getId(),
                        brightnessValue);
            } finally {
                mDataStore.saveIfNeeded();
            }
        }
    }

    private void maybeRestoreBacklightBrightness(InputDevice inputDevice, Light keyboardBacklight) {
        // Don't back up or restore when ALS based keyboard backlight is enabled
        if (InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) {
            return;
        }
        KeyboardBacklightState state = mKeyboardBacklights.get(inputDevice.getId());
        OptionalInt brightness;
        synchronized (mDataStore) {
            brightness = mDataStore.getKeyboardBacklightBrightness(
                    inputDevice.getDescriptor(), keyboardBacklight.getId());
        }
        if (state != null && brightness.isPresent()) {
            int brightnessValue = Math.max(0, Math.min(MAX_BRIGHTNESS, brightness.getAsInt()));
            int newLevel = Arrays.binarySearch(state.mBrightnessValueForLevel, brightnessValue);
            if (newLevel < 0) {
                newLevel = Math.min(state.getNumBrightnessChangeSteps(), -(newLevel + 1));
            }
            state.setBrightnessLevel(newLevel);
            if (DEBUG) {
                Slog.d(TAG, "Restoring brightness level " + brightness.getAsInt());
            }
        }
    }

    private void handleUserActivity() {
        // Ignore user activity if device is not interactive. When device becomes interactive, we
        // will send another user activity to turn backlight on.
@@ -393,7 +348,6 @@ final class KeyboardBacklightController implements
        }
        // The keyboard backlight was added or changed.
        mKeyboardBacklights.put(deviceId, new KeyboardBacklightState(deviceId, keyboardBacklight));
        maybeRestoreBacklightBrightness(inputDevice, keyboardBacklight);
    }

    private InputDevice getInputDevice(int deviceId) {
@@ -472,9 +426,6 @@ final class KeyboardBacklightController implements
    }

    private void updateAmbientLightListener() {
        if (!InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled()) {
            return;
        }
        boolean needToListenAmbientLightSensor = false;
        for (int i = 0; i < mKeyboardBacklights.size(); i++) {
            needToListenAmbientLightSensor |= mKeyboardBacklights.valueAt(i).mUseAmbientController;
@@ -555,8 +506,7 @@ final class KeyboardBacklightController implements
        private int mBrightnessLevel;
        private ValueAnimator mAnimator;
        private final int[] mBrightnessValueForLevel;
        private boolean mUseAmbientController =
                InputFeatureFlagProvider.isAmbientKeyboardBacklightControlEnabled();
        private boolean mUseAmbientController = true;

        KeyboardBacklightState(int deviceId, Light light) {
            mDeviceId = deviceId;
@@ -565,9 +515,6 @@ final class KeyboardBacklightController implements
        }

        private int[] setupBrightnessLevels() {
            if (!InputFeatureFlagProvider.isKeyboardBacklightCustomLevelsEnabled()) {
                return DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL;
            }
            int[] customLevels = mLight.getPreferredBrightnessLevels();
            if (customLevels == null || customLevels.length == 0) {
                return DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL;
@@ -627,7 +574,7 @@ final class KeyboardBacklightController implements
            if (fromValue == toValue) {
                return;
            }
            if (InputFeatureFlagProvider.isKeyboardBacklightAnimationEnabled()) {
            if (mKeyboardBacklightAnimationEnabled) {
                startAnimation(fromValue, toValue);
            } else {
                mNative.setLightColor(mDeviceId, mLight.getId(), Color.argb(toValue, 0, 0, 0));
+1 −2
Original line number Diff line number Diff line
@@ -156,8 +156,7 @@ class InputManagerServiceTests {
                }

                override fun getKeyboardBacklightController(
                    nativeService: NativeInputManagerService?,
                    dataStore: PersistentDataStore?
                    nativeService: NativeInputManagerService?
                ): InputManagerService.KeyboardBacklightControllerInterface {
                    return kbdController
                }
+251 −508

File changed.

Preview size limit exceeded, changes collapsed.