Author: ks156
Date: 2009-01-14 16:19:56 +0100 (Wed, 14 Jan 2009)
New Revision: 3447
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-totem/trunk/tuxdroid-gadget-totem/sources/totem.java
Log:
* Added a PID checker. Now, when totem is closed, the gadget stops. This
prevents the multi instance of this gadget, which remains even when the
control center is closed.
* Added a protection to not send the command "totem" when totem is already
started. This fixes the problem with the playlist, which restart from the
beginning each time the gadget is started..
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-totem/trunk/tuxdroid-gadget-totem/sources/totem.java
===================================================================
---
software_suite_v2/software/gadgets/tuxdroid-gadget-totem/trunk/tuxdroid-gadget-totem/sources/totem.java
2009-01-14 13:03:22 UTC (rev 3446)
+++
software_suite_v2/software/gadgets/tuxdroid-gadget-totem/trunk/tuxdroid-gadget-totem/sources/totem.java
2009-01-14 15:19:56 UTC (rev 3447)
@@ -21,9 +21,12 @@
*/
import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadget;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadgetConfiguration;
+
import com.tuxisalive.api.TuxAPI;
import com.tuxisalive.api.TuxAPIConst;
@@ -49,37 +52,33 @@
*/
public int startTotem(String path)
{
- try
+ /* Check if the process is already active */
+ if (! isProcess("totem"))
{
- if(path == null)
+ /* If not, start it */
+ try
{
- Runtime.getRuntime().exec("totem");
+ if(path == null)
+ Runtime.getRuntime().exec("totem");
+ else
+ Runtime.getRuntime().exec(new String[] {"totem",
path});
+
+ return 0;
}
- else
+ catch (java.io.IOException e)
{
- Runtime.getRuntime().exec(new String[] { "totem", path});
+ throwMessage("The gadget could not find totem. For using
this gadget, please make sure you have totem installed.");
+ return -1;
}
- return 0;
}
- catch (java.io.IOException e)
+ /* Otherwise, return 0 : all is OK */
+ else
{
- throwMessage("The gadget could not find totem. For using this
gadget, please make sure you have totem installed.");
- return -1;
+ return 0;
}
}
-
/**
- * Remote control 'next' button callback.
- * @param value
- * @param delay
- */
- public void onNextPushed(String value, Double delay){
- new CommandThread("totem --next").start();
- }
-
-
- /**
* Remote control 'standby' button pushed callback.
* @param value
* @param delay
@@ -95,6 +94,14 @@
System.exit(0);
}
+ /**
+ * Remote control 'next' button callback.
+ * @param value
+ * @param delay
+ */
+ public void onNextPushed(String value, Double delay){
+ this.evalCommand("totem --next");
+ }
/**
* Remote control 'previous' button pushed callback.
@@ -102,7 +109,7 @@
* @param delay
*/
public void onPreviousPushed(String value, Double delay){
- new CommandThread("totem --previous").start();
+ this.evalCommand("totem --previous");
}
@@ -112,7 +119,7 @@
* @param delay
*/
public void onOKPushed(String value, Double delay){
- new CommandThread("totem --play-pause").start();
+ this.evalCommand("totem --play-pause");
}
@@ -122,7 +129,7 @@
* @param delay
*/
public void onPlayPausePushed(String value, Double delay){
- new CommandThread("totem --play-pause").start();
+ this.evalCommand("totem --play-pause");
}
@@ -132,7 +139,7 @@
* @param delay
*/
public void onStopPushed(String value, Double delay){
- new CommandThread("totem --pause").start();
+ this.evalCommand("totem --pause");
}
@@ -142,7 +149,7 @@
* @param delay
*/
public void onPlayerNextPushed(String value, Double delay){
- new CommandThread("totem --next").start();
+ this.evalCommand("totem --next");
}
@@ -152,7 +159,7 @@
* @param delay
*/
public void onPlayerPreviousPushed(String value, Double delay){
- new CommandThread("totem --previous").start();
+ this.evalCommand("totem --previous");
}
@@ -162,7 +169,7 @@
* @param delay
*/
public void onVolumePlusPushed(String value, Double delay){
- new CommandThread("totem --volume-up").start();
+ this.evalCommand("totem --volume-up");
}
@@ -172,7 +179,7 @@
* @param delay
*/
public void onVolumeMinusPushed(String value, Double delay){
- new CommandThread("totem --volume-down").start();
+ this.evalCommand("totem --volume-down");
}
@@ -182,7 +189,7 @@
* @param delay
*/
public void onReturnPushed(String value, Double delay){
- new CommandThread("totem --fullscreen").start();
+ this.evalCommand("totem --fullscreen");
}
@@ -192,7 +199,7 @@
* @param delay
*/
public void onSeekFPushed(String value, Double delay){
- new CommandThread("totem --seek-fwd").start();
+ this.evalCommand("totem --seek-fwd");
}
@@ -202,7 +209,7 @@
* @param delay
*/
public void onSeekBPushed(String value, Double delay){
- new CommandThread("totem --seek-bwd").start();
+ this.evalCommand("totem --seek-bwd");
}
/**
@@ -232,13 +239,16 @@
this.tux.button.left.registerEventOnPressed(this,
"onNextPushed");
this.tux.server.autoConnect(TuxAPIConst.CLIENT_LEVEL_FREE,
"tuxdroid-gadget-totem", "tuxdroid-gadget-totem-2122");
+ /* Start totem */
ret = this.startTotem(this.configuration().path);
if (ret < 0)
{
- this.tux.destroy();
- System.exit(1);
+ System.exit(0);
}
+
+ /* Start a thread to monitor the instance of totem */
+ new InstanceChecker("totem").start();
}
@@ -246,6 +256,56 @@
new totem().boot(args, new TotemConfiguration());
}
+ /**
+ * Evaluate if the command can be executed.
+ * If the process isn't running, the command shouldn't be sent, and the
+ * gadgt must be destroyed.
+ * @param command The command to send
+ */
+ private void evalCommand(String command)
+ {
+ if (isProcess("totem"))
+ new CommandThread(command).start();
+ else
+ System.exit(0);
+ }
+
+ /**
+ * Check for a process presence
+ * @param process The process to find
+ * @return true if the process exists
+ */
+ private static boolean isProcess(String process)
+ {
+ try {
+ // Retrieve the unix process
+ Process p = Runtime.getRuntime().exec("ps -e");
+
+ //Create a buffer to read the command output
+ BufferedReader stdInput = new BufferedReader(new
+ InputStreamReader(p.getInputStream()));
+
+ String s;
+ int count = 0;
+ while ((s = stdInput.readLine()) != null)
+ {
+ count++;
+ // Search for the process
+ if (s.contains(process))
+ {
+ stdInput.close();
+ return true;
+ }
+ }
+ stdInput.close();
+ return false;
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ return false;
+ }
+ }
/**
* Class that create a thread object for starting a system command.
@@ -298,4 +358,45 @@
}
}
+ /**
+ * Spy an instance
+ * This class check every second if the process to watch is alive.
+ * When the process die, the gadget stops its execution.
+ */
+ private class InstanceChecker extends Thread
+ {
+ String process = null;
+
+ /**
+ * Register a process to spy
+ * @param process
+ */
+ public InstanceChecker(String process)
+ {
+ this.process = process;
+ }
+
+ /**
+ * Mainloop :
+ * Each second, the process is searched. If it's not running,
+ * the gadget is killed.
+ */
+ public void run()
+ {
+ while (true)
+ {
+ if (!totem.isProcess(process))
+ {
+ System.exit(0);
+ break;
+ }
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (java.lang.InterruptedException e)
+ { }
+ }
+ }
+ }
}
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn