mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-10-25 14:58:36 +00:00
Port MousePadPlugin to Kotlin
This commit is contained in:
@@ -1,217 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
*/
|
||||
|
||||
package org.kde.kdeconnect.Plugins.MousePadPlugin;
|
||||
|
||||
import static org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView.SpecialKeysMap;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.kde.kdeconnect.DeviceType;
|
||||
import org.kde.kdeconnect.NetworkPacket;
|
||||
import org.kde.kdeconnect.Plugins.Plugin;
|
||||
import org.kde.kdeconnect.Plugins.PluginFactory;
|
||||
import org.kde.kdeconnect.UserInterface.PluginSettingsFragment;
|
||||
import org.kde.kdeconnect_tp.R;
|
||||
|
||||
@PluginFactory.LoadablePlugin
|
||||
public class MousePadPlugin extends Plugin {
|
||||
|
||||
public final static String PACKET_TYPE_MOUSEPAD_REQUEST = "kdeconnect.mousepad.request";
|
||||
private final static String PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE = "kdeconnect.mousepad.keyboardstate";
|
||||
|
||||
private boolean keyboardEnabled = true;
|
||||
|
||||
@Override
|
||||
public boolean onPacketReceived(@NonNull NetworkPacket np) {
|
||||
|
||||
keyboardEnabled = np.getBoolean("state", true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getDisplayName() {
|
||||
return context.getString(R.string.pref_plugin_mousepad);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getDescription() {
|
||||
return context.getString(R.string.pref_plugin_mousepad_desc_nontv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @DrawableRes int getIcon() {
|
||||
return R.drawable.touchpad_plugin_action_24dp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSettings() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginSettingsFragment getSettingsFragment(Activity activity) {
|
||||
if (device.getDeviceType() == DeviceType.TV) {
|
||||
return PluginSettingsFragment.newInstance(getPluginKey(), R.xml.mousepadplugin_preferences, R.xml.mousepadplugin_preferences_tv);
|
||||
} else {
|
||||
return PluginSettingsFragment.newInstance(getPluginKey(), R.xml.mousepadplugin_preferences);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean displayAsButton(Context context) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startMainActivity(Activity parentActivity) {
|
||||
if (device.getDeviceType() == DeviceType.TV) {
|
||||
Intent intent = new Intent(parentActivity, BigscreenActivity.class);
|
||||
intent.putExtra("deviceId", device.getDeviceId());
|
||||
parentActivity.startActivity(intent);
|
||||
} else {
|
||||
Intent intent = new Intent(parentActivity, MousePadActivity.class);
|
||||
intent.putExtra("deviceId", device.getDeviceId());
|
||||
parentActivity.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String[] getSupportedPacketTypes() {
|
||||
return new String[]{PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String[] getOutgoingPacketTypes() {
|
||||
return new String[]{PACKET_TYPE_MOUSEPAD_REQUEST};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getActionName() {
|
||||
return context.getString(R.string.open_mousepad);
|
||||
}
|
||||
|
||||
public void sendMouseDelta(float dx, float dy) {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("dx", dx);
|
||||
np.set("dy", dy);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public Boolean hasMicPermission() {
|
||||
return isPermissionGranted(Manifest.permission.RECORD_AUDIO);
|
||||
}
|
||||
|
||||
public void sendLeftClick() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("singleclick", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendDoubleClick() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("doubleclick", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendMiddleClick() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("middleclick", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendRightClick() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("rightclick", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendSingleHold() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("singlehold", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendSingleRelease() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("singlerelease", true);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendScroll(float dx, float dy) {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("scroll", true);
|
||||
np.set("dx", dx);
|
||||
np.set("dy", dy);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendLeft() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_LEFT));
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendRight() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_RIGHT));
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendUp() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_UP));
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendDown() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_DOWN));
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendSelect() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_ENTER));
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendHome() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("alt", true);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_F4));
|
||||
getDevice().sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendBack() {
|
||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_ESCAPE));
|
||||
getDevice().sendPacket(np);
|
||||
}
|
||||
|
||||
public void sendText(String content) {
|
||||
NetworkPacket np = new NetworkPacket(MousePadPlugin.PACKET_TYPE_MOUSEPAD_REQUEST);
|
||||
np.set("key", content);
|
||||
sendPacket(np);
|
||||
}
|
||||
|
||||
void sendPacket(NetworkPacket np) {
|
||||
device.sendPacket(np);
|
||||
}
|
||||
|
||||
boolean isKeyboardEnabled() {
|
||||
return keyboardEnabled;
|
||||
}
|
||||
|
||||
}
|
||||
181
src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.kt
Normal file
181
src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.kt
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
*/
|
||||
package org.kde.kdeconnect.Plugins.MousePadPlugin
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.view.KeyEvent
|
||||
import androidx.annotation.DrawableRes
|
||||
import org.kde.kdeconnect.DeviceType
|
||||
import org.kde.kdeconnect.NetworkPacket
|
||||
import org.kde.kdeconnect.Plugins.Plugin
|
||||
import org.kde.kdeconnect.Plugins.PluginFactory.LoadablePlugin
|
||||
import org.kde.kdeconnect.UserInterface.PluginSettingsFragment
|
||||
import org.kde.kdeconnect.UserInterface.PluginSettingsFragment.Companion.newInstance
|
||||
import org.kde.kdeconnect_tp.R
|
||||
|
||||
@LoadablePlugin
|
||||
class MousePadPlugin : Plugin() {
|
||||
var isKeyboardEnabled: Boolean = true
|
||||
private set
|
||||
|
||||
override fun onPacketReceived(np: NetworkPacket): Boolean {
|
||||
this.isKeyboardEnabled = np.getBoolean("state", true)
|
||||
return true
|
||||
}
|
||||
|
||||
override val displayName: String
|
||||
get() = context.getString(R.string.pref_plugin_mousepad)
|
||||
|
||||
override val actionName: String
|
||||
get() = context.getString(R.string.open_mousepad)
|
||||
|
||||
override val description: String
|
||||
get() = context.getString(R.string.pref_plugin_mousepad_desc_nontv)
|
||||
|
||||
@get:DrawableRes
|
||||
override val icon: Int = R.drawable.touchpad_plugin_action_24dp
|
||||
|
||||
override fun displayAsButton(context: Context): Boolean = true
|
||||
|
||||
override fun hasSettings(): Boolean = true
|
||||
|
||||
override fun getSettingsFragment(activity: Activity): PluginSettingsFragment? {
|
||||
return if (device.deviceType == DeviceType.TV) {
|
||||
newInstance(pluginKey, R.xml.mousepadplugin_preferences, R.xml.mousepadplugin_preferences_tv)
|
||||
} else {
|
||||
newInstance(pluginKey, R.xml.mousepadplugin_preferences)
|
||||
}
|
||||
}
|
||||
|
||||
override fun startMainActivity(parentActivity: Activity) {
|
||||
val intent = if (device.deviceType == DeviceType.TV) {
|
||||
Intent(parentActivity, BigscreenActivity::class.java)
|
||||
} else {
|
||||
Intent(parentActivity, MousePadActivity::class.java)
|
||||
}
|
||||
intent.putExtra("deviceId", device.deviceId)
|
||||
parentActivity.startActivity(intent)
|
||||
}
|
||||
|
||||
fun sendMouseDelta(dx: Float, dy: Float) {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["dx"] = dx.toDouble()
|
||||
np["dy"] = dy.toDouble()
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun hasMicPermission(): Boolean {
|
||||
return isPermissionGranted(Manifest.permission.RECORD_AUDIO)
|
||||
}
|
||||
|
||||
fun sendLeftClick() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["singleclick"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendDoubleClick() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["doubleclick"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendMiddleClick() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["middleclick"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendRightClick() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["rightclick"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendSingleHold() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["singlehold"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendSingleRelease() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["singlerelease"] = true
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendScroll(dx: Float, dy: Float) {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["scroll"] = true
|
||||
np["dx"] = dx.toDouble()
|
||||
np["dy"] = dy.toDouble()
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendLeft() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_LEFT)
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendRight() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_RIGHT)
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendUp() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_UP)
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendDown() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_DPAD_DOWN)
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendSelect() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_ENTER)
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendHome() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["alt"] = true
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_F4)
|
||||
device.sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendBack() {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["specialKey"] = KeyListenerView.SpecialKeysMap.get(KeyEvent.KEYCODE_ESCAPE)
|
||||
device.sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendText(content: String?) {
|
||||
val np = NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
np["key"] = content
|
||||
sendPacket(np)
|
||||
}
|
||||
|
||||
fun sendPacket(np: NetworkPacket) {
|
||||
device.sendPacket(np)
|
||||
}
|
||||
|
||||
override val supportedPacketTypes = arrayOf(PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE)
|
||||
override val outgoingPacketTypes = arrayOf(PACKET_TYPE_MOUSEPAD_REQUEST)
|
||||
|
||||
companion object {
|
||||
const val PACKET_TYPE_MOUSEPAD_REQUEST: String = "kdeconnect.mousepad.request"
|
||||
private const val PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE = "kdeconnect.mousepad.keyboardstate"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user