diff --git a/KeyHandler/Android.bp b/KeyHandler/Android.bp index 7c8766f..be66272 100644 --- a/KeyHandler/Android.bp +++ b/KeyHandler/Android.bp @@ -1,7 +1,7 @@ android_app { name: "KeyHandler", - srcs: ["src/**/*.java"], + srcs: ["src/**/*.kt"], certificate: "platform", platform_apis: true, diff --git a/KeyHandler/src/org/lineageos/settings/device/KeyHandler.java b/KeyHandler/src/org/lineageos/settings/device/KeyHandler.java deleted file mode 100644 index 1d3a296..0000000 --- a/KeyHandler/src/org/lineageos/settings/device/KeyHandler.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2018 The LineageOS 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 org.lineageos.settings.device; - -import android.content.Context; -import android.media.AudioManager; -import android.os.VibrationEffect; -import android.os.Vibrator; -import android.view.KeyEvent; - -import com.android.internal.os.DeviceKeyHandler; - -public class KeyHandler implements DeviceKeyHandler { - private static final String TAG = KeyHandler.class.getSimpleName(); - - // Slider key codes - private static final int MODE_NORMAL = 601; - private static final int MODE_VIBRATION = 602; - private static final int MODE_SILENCE = 603; - - // Vibration effects - private static final VibrationEffect MODE_NORMAL_EFFECT = - VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK); - private static final VibrationEffect MODE_VIBRATION_EFFECT = - VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK); - - private final Context mContext; - private final AudioManager mAudioManager; - private final Vibrator mVibrator; - - public KeyHandler(Context context) { - mContext = context; - - mAudioManager = mContext.getSystemService(AudioManager.class); - mVibrator = mContext.getSystemService(Vibrator.class); - } - - public KeyEvent handleKeyEvent(KeyEvent event) { - if (event.getAction() != KeyEvent.ACTION_DOWN) { - return event; - } - - int scanCode = event.getScanCode(); - - switch (scanCode) { - case MODE_NORMAL: - mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL); - doHapticFeedback(MODE_NORMAL_EFFECT); - break; - case MODE_VIBRATION: - mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_VIBRATE); - doHapticFeedback(MODE_VIBRATION_EFFECT); - break; - case MODE_SILENCE: - mAudioManager.setRingerModeInternal(AudioManager.RINGER_MODE_SILENT); - break; - default: - return event; - } - - return null; - } - - private void doHapticFeedback(VibrationEffect effect) { - if (mVibrator != null && mVibrator.hasVibrator()) { - mVibrator.vibrate(effect); - } - } -} diff --git a/KeyHandler/src/org/lineageos/settings/device/KeyHandler.kt b/KeyHandler/src/org/lineageos/settings/device/KeyHandler.kt new file mode 100644 index 0000000..6fbd6d3 --- /dev/null +++ b/KeyHandler/src/org/lineageos/settings/device/KeyHandler.kt @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 The LineageOS Project + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.lineageos.settings.device + +import android.content.Context +import android.media.AudioManager +import android.os.VibrationEffect +import android.os.Vibrator +import android.view.KeyEvent +import com.android.internal.os.DeviceKeyHandler + +class KeyHandler(context: Context) : DeviceKeyHandler { + private val audioManager = context.getSystemService(AudioManager::class.java) + private val vibrator = context.getSystemService(Vibrator::class.java) + + override fun handleKeyEvent(event: KeyEvent): KeyEvent { + if (event.action == KeyEvent.ACTION_DOWN) { + when (event.scanCode) { + MODE_NORMAL -> { + audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL) + vibrator.vibrate(MODE_NORMAL_EFFECT) + } + MODE_VIBRATION -> { + audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_VIBRATE) + vibrator.vibrate(MODE_VIBRATION_EFFECT) + } + MODE_SILENCE -> { + audioManager.setRingerModeInternal(AudioManager.RINGER_MODE_SILENT) + } + } + } + return event + } + + companion object { + private const val TAG = "KeyHandler" + + // Slider key codes + private const val MODE_NORMAL = 601 + private const val MODE_VIBRATION = 602 + private const val MODE_SILENCE = 603 + + // Vibration effects + private val MODE_NORMAL_EFFECT = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK) + private val MODE_VIBRATION_EFFECT = VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK) + } +}