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

Commit d8546dce authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge "KeyboardBacklightController: Remove sysfs node change detection" into main

parents acd55a7a 36859f99
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -510,8 +510,7 @@ public class InputManagerService extends IInputManager.Stub

        KeyboardBacklightControllerInterface getKeyboardBacklightController(
                NativeInputManagerService nativeService) {
            return new KeyboardBacklightController(mContext, nativeService, mLooper,
                    mUEventManager);
            return new KeyboardBacklightController(mContext, nativeService, mLooper);
        }
    }

+3 −30
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import android.os.Looper;
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;
@@ -83,8 +82,6 @@ final class KeyboardBacklightController implements
    private static final long TRANSITION_ANIMATION_DURATION_MILLIS =
            Duration.ofSeconds(1).toMillis();

    private static final String UEVENT_KEYBOARD_BACKLIGHT_TAG = "kbd_backlight";

    @VisibleForTesting
    static final int[] DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL =
            new int[DEFAULT_NUM_BRIGHTNESS_CHANGE_STEPS + 1];
@@ -93,7 +90,6 @@ final class KeyboardBacklightController implements
    private final NativeInputManagerService mNative;
    private final Handler mHandler;
    private final AnimatorFactory mAnimatorFactory;
    private final UEventManager mUEventManager;
    // Always access on handler thread or need to lock this for synchronization.
    private final SparseArray<KeyboardBacklightState> mKeyboardBacklights = new SparseArray<>(1);
    // Maintains state if all backlights should be on or turned off
@@ -124,19 +120,18 @@ final class KeyboardBacklightController implements
    }

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

    @VisibleForTesting
    KeyboardBacklightController(Context context, NativeInputManagerService nativeService,
            Looper looper, AnimatorFactory animatorFactory, UEventManager uEventManager) {
            Looper looper, AnimatorFactory animatorFactory) {
        mContext = context;
        mNative = nativeService;
        mHandler = new Handler(looper, this::handleMessage);
        mAnimatorFactory = animatorFactory;
        mAmbientController = new AmbientKeyboardBacklightController(context, looper);
        mUEventManager = uEventManager;
        Resources res = mContext.getResources();
        mUserInactivityThresholdMs = res.getInteger(
                com.android.internal.R.integer.config_keyboardBacklightTimeoutMs);
@@ -154,17 +149,6 @@ final class KeyboardBacklightController implements
                inputManager.getInputDeviceIds());
        mHandler.sendMessage(msg);

        // Observe UEvents for "kbd_backlight" sysfs nodes.
        // We want to observe creation of such LED nodes since they might be created after device
        // FD created and InputDevice creation logic doesn't initialize LED nodes which leads to
        // backlight not working.
        mUEventManager.addListener(new UEventManager.UEventListener() {
            @Override
            public void onUEvent(UEventObserver.UEvent event) {
                onKeyboardBacklightUEvent(event);
            }
        }, UEVENT_KEYBOARD_BACKLIGHT_TAG);

        // Start ambient backlight controller
        mAmbientController.systemRunning();
    }
@@ -414,17 +398,6 @@ final class KeyboardBacklightController implements
        }
    }

    @VisibleForTesting
    public void onKeyboardBacklightUEvent(UEventObserver.UEvent event) {
        if ("ADD".equalsIgnoreCase(event.get("ACTION")) && "LEDS".equalsIgnoreCase(
                event.get("SUBSYSTEM"))) {
            final String devPath = event.get("DEVPATH");
            if (isValidBacklightNodePath(devPath)) {
                mNative.sysfsNodeChanged("/sys" + devPath);
            }
        }
    }

    private void updateAmbientLightListener() {
        boolean needToListenAmbientLightSensor = false;
        for (int i = 0; i < mKeyboardBacklights.size(); i++) {
+1 −82
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.hardware.input.IKeyboardBacklightState
import android.hardware.input.InputManager
import android.hardware.lights.Light
import android.os.SystemProperties
import android.os.UEventObserver
import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import android.util.TypedValue
@@ -98,14 +97,12 @@ class KeyboardBacklightControllerTests {
    @get:Rule val inputManagerRule = MockInputManagerRule()

    @Mock private lateinit var native: NativeInputManagerService
    @Mock private lateinit var uEventManager: UEventManager
    @Mock private lateinit var resources: Resources
    private lateinit var keyboardBacklightController: KeyboardBacklightController
    private lateinit var context: Context
    private lateinit var testLooper: TestLooper
    private var lightColorMap: HashMap<Int, Int> = HashMap()
    private var lastBacklightState: KeyboardBacklightState? = null
    private var sysfsNodeChanges = 0
    private var lastAnimationValues = IntArray(2)

    @Before
@@ -126,7 +123,6 @@ class KeyboardBacklightControllerTests {
            lightColorMap.getOrDefault(args[1] as Int, 0)
        }
        lightColorMap.clear()
        `when`(native.sysfsNodeChanged(any())).then { sysfsNodeChanges++ }
    }

    private fun setupConfig() {
@@ -158,13 +154,7 @@ class KeyboardBacklightControllerTests {

    private fun setupController() {
        keyboardBacklightController =
            KeyboardBacklightController(
                context,
                native,
                testLooper.looper,
                FakeAnimatorFactory(),
                uEventManager,
            )
            KeyboardBacklightController(context, native, testLooper.looper, FakeAnimatorFactory())
    }

    @Test
@@ -317,77 +307,6 @@ class KeyboardBacklightControllerTests {
        )
    }

    @Test
    fun testKeyboardBacklightSysfsNodeAdded_AfterInputDeviceAdded() {
        setupController()
        var counter = sysfsNodeChanges
        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=add\u0000SUBSYSTEM=leds\u0000DEVPATH=/xyz/leds/abc::no_backlight\u0000"
            )
        )
        assertEquals(
            "Should not reload sysfs node if UEvent path doesn't contain kbd_backlight",
            counter,
            sysfsNodeChanges,
        )

        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=add\u0000SUBSYSTEM=power\u0000DEVPATH=/xyz/leds/abc::kbd_backlight\u0000"
            )
        )
        assertEquals(
            "Should not reload sysfs node if UEvent doesn't belong to subsystem LED",
            counter,
            sysfsNodeChanges,
        )

        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=remove\u0000SUBSYSTEM=leds\u0000DEVPATH=/xyz/leds/abc::kbd_backlight\u0000"
            )
        )
        assertEquals(
            "Should not reload sysfs node if UEvent doesn't have ACTION(add)",
            counter,
            sysfsNodeChanges,
        )

        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=add\u0000SUBSYSTEM=leds\u0000DEVPATH=/xyz/pqr/abc::kbd_backlight\u0000"
            )
        )
        assertEquals(
            "Should not reload sysfs node if UEvent path doesn't belong to leds/ directory",
            counter,
            sysfsNodeChanges,
        )

        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=add\u0000SUBSYSTEM=leds\u0000DEVPATH=/xyz/leds/abc::kbd_backlight\u0000"
            )
        )
        assertEquals(
            "Should reload sysfs node if a valid Keyboard backlight LED UEvent occurs",
            ++counter,
            sysfsNodeChanges,
        )

        keyboardBacklightController.onKeyboardBacklightUEvent(
            UEventObserver.UEvent(
                "ACTION=add\u0000SUBSYSTEM=leds\u0000DEVPATH=/xyz/leds/abc:kbd_backlight:red\u0000"
            )
        )
        assertEquals(
            "Should reload sysfs node if a valid Keyboard backlight LED UEvent occurs",
            ++counter,
            sysfsNodeChanges,
        )
    }

    @Test
    @UiThreadTest
    fun testKeyboardBacklightAnimation_onChangeLevels() {