File indexing completed on 2024-12-22 04:41:40
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Ahmed I. Khalil <ahmedibrahimkhali@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.PresenterPlugin; 0008 0009 0010 import static org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView.SpecialKeysMap; 0011 0012 import android.app.Activity; 0013 import android.content.Context; 0014 import android.content.Intent; 0015 import android.view.KeyEvent; 0016 0017 import androidx.annotation.DrawableRes; 0018 import androidx.annotation.NonNull; 0019 0020 import org.apache.commons.lang3.ArrayUtils; 0021 import org.kde.kdeconnect.DeviceType; 0022 import org.kde.kdeconnect.NetworkPacket; 0023 import org.kde.kdeconnect.Plugins.Plugin; 0024 import org.kde.kdeconnect.Plugins.PluginFactory; 0025 import org.kde.kdeconnect_tp.R; 0026 0027 @PluginFactory.LoadablePlugin 0028 public class PresenterPlugin extends Plugin { 0029 0030 private final static String PACKET_TYPE_PRESENTER = "kdeconnect.presenter"; 0031 private final static String PACKET_TYPE_MOUSEPAD_REQUEST = "kdeconnect.mousepad.request"; 0032 0033 public boolean isPointerSupported() { 0034 return device.supportsPacketType(PACKET_TYPE_PRESENTER); 0035 } 0036 0037 @Override 0038 public @NonNull String getDisplayName() { 0039 return context.getString(R.string.pref_plugin_presenter); 0040 } 0041 0042 @Override 0043 public boolean isCompatible() { 0044 return !device.getDeviceType().equals(DeviceType.PHONE) && super.isCompatible(); 0045 } 0046 0047 @Override 0048 public @NonNull String getDescription() { 0049 return context.getString(R.string.pref_plugin_presenter_desc); 0050 } 0051 0052 @Override 0053 public @DrawableRes int getIcon() { 0054 return R.drawable.ic_presenter_24dp; 0055 } 0056 0057 @Override 0058 public boolean hasSettings() { 0059 return false; 0060 } 0061 0062 @Override 0063 public boolean displayAsButton(Context context) { 0064 return true; 0065 } 0066 0067 @Override 0068 public void startMainActivity(Activity parentActivity) { 0069 Intent intent = new Intent(parentActivity, PresenterActivity.class); 0070 intent.putExtra("deviceId", device.getDeviceId()); 0071 parentActivity.startActivity(intent); 0072 } 0073 0074 @Override 0075 public @NonNull String[] getSupportedPacketTypes() { return ArrayUtils.EMPTY_STRING_ARRAY; } 0076 0077 @Override 0078 public @NonNull String[] getOutgoingPacketTypes() { 0079 return new String[]{PACKET_TYPE_MOUSEPAD_REQUEST, PACKET_TYPE_PRESENTER}; 0080 } 0081 0082 @Override 0083 public @NonNull String getActionName() { 0084 return context.getString(R.string.pref_plugin_presenter); 0085 } 0086 0087 public void sendNext() { 0088 NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST); 0089 np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_PAGE_DOWN)); 0090 device.sendPacket(np); 0091 } 0092 0093 public void sendPrevious() { 0094 NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST); 0095 np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_PAGE_UP)); 0096 device.sendPacket(np); 0097 } 0098 0099 public void sendFullscreen() { 0100 NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST); 0101 np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_F5)); 0102 device.sendPacket(np); 0103 } 0104 0105 public void sendEsc() { 0106 NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST); 0107 np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_ESCAPE)); 0108 device.sendPacket(np); 0109 } 0110 0111 public void sendPointer(float xDelta, float yDelta) { 0112 NetworkPacket np = device.getAndRemoveUnsentPacket(NetworkPacket.PACKET_REPLACEID_PRESENTERPOINTER); 0113 if (np == null) { 0114 np = new NetworkPacket(PACKET_TYPE_PRESENTER); 0115 } else { 0116 // TODO: In my tests we never get here. Decide if it's worth keeping the logic to replace unsent packets. 0117 xDelta += np.getInt("dx"); 0118 yDelta += np.getInt("dy"); 0119 } 0120 np.set("dx", xDelta); 0121 np.set("dy", yDelta); 0122 device.sendPacket(np, NetworkPacket.PACKET_REPLACEID_PRESENTERPOINTER); 0123 } 0124 0125 public void stopPointer() { 0126 device.getAndRemoveUnsentPacket(NetworkPacket.PACKET_REPLACEID_PRESENTERPOINTER); 0127 NetworkPacket np = new NetworkPacket(PACKET_TYPE_PRESENTER); 0128 np.set("stop", true); 0129 device.sendPacket(np); 0130 } 0131 }