essential-actions

Clone or download

Merge pull request #54 from j4ckofalltrades/issue-53

Use ListValue for media key states

Modified Files

--- 'a/mediakeyaction/README.md'
+++ b/mediakeyaction/README.md
@@ -1,3 +1,17 @@
# Media Key Action
-Documentation is WIP
\ No newline at end of file
+Control playback with the following media keys:
+
+- Play / Pause toggle
+- Stop
+- Mute
+- Play Previous Track
+- Play Next Track
+- Volume Up
+- Volume Down
+
+## Dependencies
+
+### on Linux
+
+This action needs [xdotool](https://github.com/jordansissel/xdotool) to be installed.
--- 'a/mediakeyaction/src/main/java/com/stream_pi/mediakeyaction/MediaKeyAction.java'
+++ b/mediakeyaction/src/main/java/com/stream_pi/mediakeyaction/MediaKeyAction.java
@@ -1,5 +1,6 @@
package com.stream_pi.mediakeyaction;
+import com.stream_pi.action_api.actionproperty.property.ListValue;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.externalplugin.NormalAction;
@@ -21,13 +22,20 @@ public class MediaKeyAction extends Norm
setAuthor("rnayabed");
setServerButtonGraphic("fas-volume-up");
setHelpLink("https://github.com/stream-pi/essentialactions/tree/master/mediakeyaction");
- setVersion(new Version(1,1,1));
+ setVersion(new Version(1, 1, 1));
}
@Override
public void initProperties() throws MinorException
{
- List<String> states = List.of("Play/Pause", "Previous", "Next", "Vol Up", "Vol Down", "Mute", "Stop");
+ final List<ListValue> states = List.of(
+ new ListValue("Play/Pause"),
+ new ListValue("Previous"),
+ new ListValue("Next"),
+ new ListValue("Vol Up"),
+ new ListValue("Vol Down"),
+ new ListValue("Mute"),
+ new ListValue("Stop"));
Property mediaKeyTypeProperty = new Property("media_key_type", Type.LIST);
mediaKeyTypeProperty.setListValue(states);
@@ -41,24 +49,26 @@ public class MediaKeyAction extends Norm
{
try
{
- if(getServerConnection().getPlatform() == Platform.WINDOWS)
+ if (getServerConnection().getPlatform() == Platform.WINDOWS)
+ {
extractKeyEXEToTmpDirectory();
- }
- catch (IOException e)
+ }
+ } catch (IOException e)
{
e.printStackTrace();
- throw new MinorException("Error","IOException occurred. \n\n"+e.getMessage());
+ throw new MinorException("Error", "IOException occurred. \n\n" + e.getMessage());
}
}
private static String absolutePathToExe;
+
private void extractKeyEXEToTmpDirectory() throws IOException
{
String tmpDirectory = System.getProperty("java.io.tmpdir");
InputStream is = Objects.requireNonNull(getClass().getResource("key.exe")).openStream();
- absolutePathToExe = tmpDirectory+"/key.exe";
+ absolutePathToExe = tmpDirectory + "/key.exe";
File file = new File(absolutePathToExe);
file.deleteOnExit();
OutputStream os = new FileOutputStream(file);
@@ -66,7 +76,8 @@ public class MediaKeyAction extends Norm
byte[] b = new byte[2048];
int length;
- while ((length = is.read(b)) != -1) {
+ while ((length = is.read(b)) != -1)
+ {
os.write(b, 0, length);
}
@@ -79,18 +90,18 @@ public class MediaKeyAction extends Norm
{
int state = getClientProperties().getSingleProperty("media_key_type").getSelectedIndex();
- switch(getServerConnection().getPlatform())
+ switch (getServerConnection().getPlatform())
{
- case LINUX :
+ case LINUX:
linuxHandler(state);
break;
case WINDOWS:
windowsHandler(state);
break;
-
+
default:
- othersHandler();
+ othersHandler();
}
}
@@ -105,23 +116,36 @@ public class MediaKeyAction extends Norm
{
try
{
- switch(state)
+ switch (state)
{
- case 0: runCommand("xdotool key XF86AudioPlay"); break;
- case 1: runCommand("xdotool key XF86AudioPrev"); break;
- case 2: runCommand("xdotool key XF86AudioNext"); break;
- case 3: runCommand("xdotool key XF86AudioRaiseVolume"); break;
- case 4: runCommand("xdotool key XF86AudioLowerVolume"); break;
- case 5: runCommand("xdotool key XF86AudioMute"); break;
- case 6: runCommand("xdotool key XF86AudioStop"); break;
+ case 0:
+ runCommand("xdotool key XF86AudioPlay");
+ break;
+ case 1:
+ runCommand("xdotool key XF86AudioPrev");
+ break;
+ case 2:
+ runCommand("xdotool key XF86AudioNext");
+ break;
+ case 3:
+ runCommand("xdotool key XF86AudioRaiseVolume");
+ break;
+ case 4:
+ runCommand("xdotool key XF86AudioLowerVolume");
+ break;
+ case 5:
+ runCommand("xdotool key XF86AudioMute");
+ break;
+ case 6:
+ runCommand("xdotool key XF86AudioStop");
+ break;
}
- }
- catch (Exception e)
+ } catch (Exception e)
{
e.printStackTrace();
-
+
throw new UnsupportedOperationException(
- "It looks like 'xdotool' is not installed in this computer. Please install it and try again."
+ "It looks like 'xdotool' is not installed in this computer. Please install it and try again."
);
}
}
@@ -130,19 +154,32 @@ public class MediaKeyAction extends Norm
{
try
{
- String exe = "\""+absolutePathToExe+"\"";
- switch(state)
+ String exe = "\"" + absolutePathToExe + "\"";
+ switch (state)
{
- case 0: runCommand(exe+" 0xB3"); break;
- case 1: runCommand(exe+" 0xB1"); break;
- case 2: runCommand(exe+" 0xB0"); break;
- case 3: runCommand(exe+" 0xAF"); break;
- case 4: runCommand(exe+" 0xAE"); break;
- case 5: runCommand(exe+" 0xAD"); break;
- case 6: runCommand(exe+" 0xB2"); break;
+ case 0:
+ runCommand(exe + " 0xB3");
+ break;
+ case 1:
+ runCommand(exe + " 0xB1");
+ break;
+ case 2:
+ runCommand(exe + " 0xB0");
+ break;
+ case 3:
+ runCommand(exe + " 0xAF");
+ break;
+ case 4:
+ runCommand(exe + " 0xAE");
+ break;
+ case 5:
+ runCommand(exe + " 0xAD");
+ break;
+ case 6:
+ runCommand(exe + " 0xB2");
+ break;
}
- }
- catch (Exception e)
+ } catch (Exception e)
{
e.printStackTrace();