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

Commit 7bd2af8d authored by DingYong's avatar DingYong Committed by Josep del Río
Browse files

Add keyboard volume mute LED. (1/2)

(cherry picked from https://partner-android-review.googlesource.com/q/commit:0454055b9eef247f5498b1c91491b0ac07805a49)

Change-Id: I78091ff5ab5ec956e03517e95b36a529d3ede3fa
Bug: 373556678
Flag: NONE external OEM contribution only active in their devices
parent b5517fd2
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -70,6 +70,12 @@ public final class Light implements Parcelable {
     */
    public static final int LIGHT_TYPE_KEYBOARD_MIC_MUTE = 10004;

    /**
     * Type for keyboard volume mute light.
     * @hide
     */
    public static final int LIGHT_TYPE_KEYBOARD_VOLUME_MUTE = 10005;

    /**
     * Capability for lights that could adjust its LED brightness. If the capability is not present
     * the LED can only be turned either on or off.
@@ -99,6 +105,7 @@ public final class Light implements Parcelable {
            LIGHT_TYPE_PLAYER_ID,
            LIGHT_TYPE_KEYBOARD_BACKLIGHT,
            LIGHT_TYPE_KEYBOARD_MIC_MUTE,
            LIGHT_TYPE_KEYBOARD_VOLUME_MUTE,
        })
    public @interface LightType {}

+62 −0
Original line number Diff line number Diff line
@@ -46,11 +46,13 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
    private static final String TAG = KeyboardLedController.class.getSimpleName();
    private static final int MSG_UPDATE_EXISTING_DEVICES = 1;
    private static final int MSG_UPDATE_MIC_MUTE_LED_STATE = 2;
    private static final int MSG_UPDATE_AUDIO_MUTE_LED_STATE = 3;

    private final Context mContext;
    private final Handler mHandler;
    private final NativeInputManagerService mNative;
    private final SparseArray<InputDevice> mKeyboardsWithMicMuteLed = new SparseArray<>();
    private final SparseArray<InputDevice> mKeyboardsWithVolumeMuteLed = new SparseArray<>();
    @NonNull
    private InputManager mInputManager;
    @NonNull
@@ -66,6 +68,17 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
                }
            };

    private BroadcastReceiver mVolumeMuteIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
            if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {
                Message msg = Message.obtain(mHandler, MSG_UPDATE_AUDIO_MUTE_LED_STATE);
                mHandler.sendMessage(msg);
            }
        }
    };

    KeyboardLedController(Context context, Looper looper,
            NativeInputManagerService nativeService) {
        mContext = context;
@@ -83,6 +96,9 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
            case MSG_UPDATE_MIC_MUTE_LED_STATE:
                updateMicMuteLedState();
                return true;
            case MSG_UPDATE_AUDIO_MUTE_LED_STATE:
                updateVolumeMuteLedState();
                return true;
        }
        return false;
    }
@@ -105,6 +121,21 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
        }
    }

    private void updateVolumeMuteLedState() {
        int color = mAudioManager.isStreamMute(AudioManager.USE_DEFAULT_STREAM_TYPE)
                ? Color.WHITE : Color.TRANSPARENT;
        for (int i = 0; i < mKeyboardsWithVolumeMuteLed.size(); i++) {
            InputDevice device = mKeyboardsWithVolumeMuteLed.valueAt(i);
            if (device != null) {
                int deviceId = device.getId();
                Light light = getKeyboardVolumeMuteLight(device);
                if (light != null) {
                    mNative.setLightColor(deviceId, light.getId(), color);
                }
            }
        }
    }

    private Light getKeyboardMicMuteLight(InputDevice device) {
        for (Light light : device.getLightsManager().getLights()) {
            if (light.getType() == Light.LIGHT_TYPE_KEYBOARD_MIC_MUTE
@@ -115,6 +146,16 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
        return null;
    }

    private Light getKeyboardVolumeMuteLight(InputDevice device) {
        for (Light light : device.getLightsManager().getLights()) {
            if (light.getType() == Light.LIGHT_TYPE_KEYBOARD_VOLUME_MUTE
                    && light.hasBrightnessControl()) {
                return light;
            }
        }
        return null;
    }

    /** Called when the system is ready for us to start third-party code. */
    public void systemRunning() {
        mSensorPrivacyManager = Objects.requireNonNull(
@@ -131,6 +172,12 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
                new IntentFilter(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED),
                null,
                mHandler);
        mContext.registerReceiverAsUser(
                mVolumeMuteIntentReceiver,
                UserHandle.ALL,
                new IntentFilter(AudioManager.STREAM_MUTE_CHANGED_ACTION),
                null,
                mHandler);
    }

    @Override
@@ -141,6 +188,7 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
    @Override
    public void onInputDeviceRemoved(int deviceId) {
        mKeyboardsWithMicMuteLed.remove(deviceId);
        mKeyboardsWithVolumeMuteLed.remove(deviceId);
    }

    @Override
@@ -154,6 +202,11 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
            Message msg = Message.obtain(mHandler, MSG_UPDATE_MIC_MUTE_LED_STATE);
            mHandler.sendMessage(msg);
        }
        if (getKeyboardVolumeMuteLight(inputDevice) != null) {
            mKeyboardsWithVolumeMuteLed.put(deviceId, inputDevice);
            Message msg = Message.obtain(mHandler, MSG_UPDATE_AUDIO_MUTE_LED_STATE);
            mHandler.sendMessage(msg);
        }
    }

    /** Dump the diagnostic information */
@@ -167,5 +220,14 @@ public final class KeyboardLedController implements InputManager.InputDeviceList
                    + getKeyboardMicMuteLight(inputDevice).toString());
        }
        ipw.decreaseIndent();
        ipw.println(TAG + ": " + mKeyboardsWithVolumeMuteLed.size()
                + " keyboard volume mute lights");
        ipw.increaseIndent();
        for (int i = 0; i < mKeyboardsWithVolumeMuteLed.size(); i++) {
            InputDevice inputDevice = mKeyboardsWithVolumeMuteLed.valueAt(i);
            ipw.println(i + " " + inputDevice.getName() + ": "
                    + getKeyboardVolumeMuteLight(inputDevice).toString());
        }
        ipw.decreaseIndent();
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -210,6 +210,7 @@ static struct {
    jfieldID lightTypePlayerId;
    jfieldID lightTypeKeyboardBacklight;
    jfieldID lightTypeKeyboardMicMute;
    jfieldID lightTypeKeyboardVolumeMute;
    jfieldID lightCapabilityBrightness;
    jfieldID lightCapabilityColorRgb;
} gLightClassInfo;
@@ -2630,6 +2631,9 @@ static jobject nativeGetLights(JNIEnv* env, jobject nativeImplObj, jint deviceId
        } else if (lightInfo.type == InputDeviceLightType::KEYBOARD_MIC_MUTE) {
            jTypeId = env->GetStaticIntField(gLightClassInfo.clazz,
                                             gLightClassInfo.lightTypeKeyboardMicMute);
        } else if (lightInfo.type == InputDeviceLightType::KEYBOARD_VOLUME_MUTE) {
            jTypeId = env->GetStaticIntField(gLightClassInfo.clazz,
                                             gLightClassInfo.lightTypeKeyboardVolumeMute);
        } else {
            ALOGW("Unknown light type %s", ftl::enum_string(lightInfo.type).c_str());
            continue;
@@ -3420,6 +3424,8 @@ int register_android_server_InputManager(JNIEnv* env) {
            env->GetStaticFieldID(gLightClassInfo.clazz, "LIGHT_TYPE_KEYBOARD_BACKLIGHT", "I");
    gLightClassInfo.lightTypeKeyboardMicMute =
            env->GetStaticFieldID(gLightClassInfo.clazz, "LIGHT_TYPE_KEYBOARD_MIC_MUTE", "I");
    gLightClassInfo.lightTypeKeyboardVolumeMute =
            env->GetStaticFieldID(gLightClassInfo.clazz, "LIGHT_TYPE_KEYBOARD_VOLUME_MUTE", "I");
    gLightClassInfo.lightCapabilityBrightness =
            env->GetStaticFieldID(gLightClassInfo.clazz, "LIGHT_CAPABILITY_BRIGHTNESS", "I");
    gLightClassInfo.lightCapabilityColorRgb =