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

Commit e4fed1e6 authored by Shaowei Shen's avatar Shaowei Shen Committed by Android (Google) Code Review
Browse files

Merge "[Physical Keyboard] Listen input devices change" into main

parents 8c3d699d 0c07f164
Loading
Loading
Loading
Loading
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 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.settings.inputmethod;

import android.hardware.input.InputManager;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.android.internal.util.Preconditions;
import com.android.settings.dashboard.DashboardFragment;

public abstract class InputDeviceDashboardFragment extends DashboardFragment
        implements InputManager.InputDeviceListener {
    private InputManager mInputManager;

    @Override
    public void onCreate(@NonNull Bundle icicle) {
        super.onCreate(icicle);
        mInputManager = Preconditions.checkNotNull(getActivity()
                .getSystemService(InputManager.class));
    }

    @Override
    public void onResume() {
        super.onResume();
        finishEarlyIfNeeded();
        mInputManager.registerInputDeviceListener(this /* listener */, null /* handler */);
    }

    @Override
    public void onPause() {
        super.onPause();
        mInputManager.unregisterInputDeviceListener(this /* listener */);
    }

    @Override
    public void onInputDeviceAdded(int deviceId) {
        finishEarlyIfNeeded();
    }

    @Override
    public void onInputDeviceRemoved(int deviceId) {
        finishEarlyIfNeeded();
    }

    @Override
    public void onInputDeviceChanged(int deviceId) {
        finishEarlyIfNeeded();
    }

    private void finishEarlyIfNeeded() {
        if (getActivity() == null) {
            return;
        }
        if (needToFinishEarly()) {
            getActivity().finish();
        }
    }

    /**
     * Returns whether the fragment should still be displayed given the input devices that are
     * currently connected.
     */
    protected abstract boolean needToFinishEarly();

    protected static boolean isTouchpadDetached() {
        return !InputPeripheralsSettingsUtils.isTouchpad();
    }

    protected static boolean isMouseDetached() {
        return !InputPeripheralsSettingsUtils.isMouse();
    }

    protected static boolean isHardKeyboardDetached() {
        return !InputPeripheralsSettingsUtils.isHardKeyboard();
    }
}
+23 −1
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package com.android.settings.inputmethod;

import static android.hardware.input.KeyboardLayoutSelectionResult.LAYOUT_SELECTION_CRITERIA_USER;
import static android.hardware.input.KeyboardLayoutSelectionResult.LAYOUT_SELECTION_CRITERIA_DEVICE;
import static android.hardware.input.KeyboardLayoutSelectionResult.LAYOUT_SELECTION_CRITERIA_USER;
import static android.hardware.input.KeyboardLayoutSelectionResult.LAYOUT_SELECTION_CRITERIA_VIRTUAL_KEYBOARD;

import android.annotation.NonNull;
@@ -55,6 +55,25 @@ public class InputPeripheralsSettingsUtils {
    static final String EXTRA_INPUT_METHOD_INFO = "input_method_info";
    static final String EXTRA_INPUT_METHOD_SUBTYPE = "input_method_subtype";

    /**
     * Returns whether any hard keyboard is connected.
     */
    static boolean isHardKeyboard() {
        for (int deviceId : InputDevice.getDeviceIds()) {
            final InputDevice device = InputDevice.getDevice(deviceId);
            if (device == null) {
                continue;
            }
            if (device.isFullKeyboard() && !device.isVirtual()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns whether any touchpad is connected.
     */
    static boolean isTouchpad() {
        for (int deviceId : InputDevice.getDeviceIds()) {
            final InputDevice device = InputDevice.getDevice(deviceId);
@@ -69,6 +88,9 @@ public class InputPeripheralsSettingsUtils {
        return false;
    }

    /**
     * Returns whether any mouse is connected.
     */
    static boolean isMouse() {
        for (int deviceId : InputDevice.getDeviceIds()) {
            final InputDevice device = InputDevice.getDevice(deviceId);
+6 −2
Original line number Diff line number Diff line
@@ -21,12 +21,11 @@ import android.content.Context;
import android.util.FeatureFlagUtils;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;

@SearchIndexable
public class ModifierKeysSettings extends DashboardFragment {
public class ModifierKeysSettings extends InputDeviceDashboardFragment {

    private static final String TAG = "ModifierKeysSettings";

@@ -52,6 +51,11 @@ public class ModifierKeysSettings extends DashboardFragment {
        return R.xml.modifier_keys_settings;
    }

    @Override
    protected boolean needToFinishEarly() {
        return isHardKeyboardDetached();
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.modifier_keys_settings) {
                @Override
+6 −2
Original line number Diff line number Diff line
@@ -20,13 +20,12 @@ import android.app.settings.SettingsEnums;
import android.content.Context;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.keyboard.Flags;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;

@SearchIndexable
public class MouseSettingFragment extends DashboardFragment {
public class MouseSettingFragment extends InputDeviceDashboardFragment {
    private static final String TAG = MouseSettingFragment.class.getSimpleName();

    @Override
@@ -52,4 +51,9 @@ public class MouseSettingFragment extends DashboardFragment {
                            && InputPeripheralsSettingsUtils.isMouse();
                }
            };

    @Override
    protected boolean needToFinishEarly() {
        return isMouseDetached();
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -23,13 +23,12 @@ import android.app.settings.SettingsEnums;
import android.content.Context;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;

/** Settings for pointer and touchpad. */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class PointerColorCustomizationFragment extends DashboardFragment {
public class PointerColorCustomizationFragment extends InputDeviceDashboardFragment {

    private static final String TAG = "PointerColorCustomizationFragment";

@@ -56,4 +55,9 @@ public class PointerColorCustomizationFragment extends DashboardFragment {
                    return isTouchpad() || isMouse();
                }
            };

    @Override
    protected boolean needToFinishEarly() {
        return isMouseDetached() && isTouchpadDetached();
    }
}
Loading