Author: ks156
Date: 2009-01-14 16:32:34 +0100 (Wed, 14 Jan 2009)
New Revision: 3448

Modified:
   
software_suite_v2/software/gadgets/tuxdroid-gadget-audacious/trunk/tuxdroid-gadget-audacious/sources/audacious.java
Log:
* Added a PID checker to be able to stop the gadget when audacious is closed by
  the user.


Modified: 
software_suite_v2/software/gadgets/tuxdroid-gadget-audacious/trunk/tuxdroid-gadget-audacious/sources/audacious.java
===================================================================
--- 
software_suite_v2/software/gadgets/tuxdroid-gadget-audacious/trunk/tuxdroid-gadget-audacious/sources/audacious.java
 2009-01-14 15:19:56 UTC (rev 3447)
+++ 
software_suite_v2/software/gadgets/tuxdroid-gadget-audacious/trunk/tuxdroid-gadget-audacious/sources/audacious.java
 2009-01-14 15:32:34 UTC (rev 3448)
@@ -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;
 
@@ -47,45 +50,61 @@
        /**
         * Start audacious application.
         */
-       public int startAudacious(String path)
+        public int startAudacious(String path)
+        {
+            /* Check if the process is already active */
+            if (! isProcess("audacious"))
+            {
+                /* If not, start it */
+                try
+                {
+                    if(path == null)
+                        Runtime.getRuntime().exec("audacious");
+                    else
+                        Runtime.getRuntime().exec(new String[] {"audacious", 
path});
+
+                    return 0;
+                }
+                catch (java.io.IOException e)
+                {
+                    throwMessage("The gadget could not find audacious. For 
using this gadget, please make sure you have audacious installed.");
+                    return -1;
+                }
+            }
+            /* Otherwise, return 0 : all is OK */
+            else
+            {
+                return 0;
+            }
+        }
+       
+       /**
+        * Remote control 'standby' button pushed callback.
+        * @param value
+        * @param delay
+        */
+       public void onStandbyPushed(String value, Double delay)
        {
-           try 
-           {
-               if(path == null)
-               {
-                   Runtime.getRuntime().exec("audacious");
-               }
-               else
-               {
-                   Runtime.getRuntime().exec(new String[] {"audacious", path});
-               }
-               return 0;
-           }
-           catch (java.io.IOException e)
-           {
-               throwMessage("The gadget could not find audacious. For using 
this gadget, please make sure you have audacious installed.");
-               return -1;
-           }
+               this.tux.destroy();
+               System.exit(0);
        }
        
-       
        /**
         * Remote control 'next' button callback.
         * @param value
         * @param delay
         */
        public void onNextPushed(String value, Double delay){
-               new CommandThread("audacious -f").start();
+               this.evalCommand("audacious -f");
        }
        
-       
        /**
         * Remote control 'previous' button pushed callback.
         * @param value
         * @param delay
         */
        public void onPreviousPushed(String value, Double delay){
-               new CommandThread("audacious -r").start();
+               this.evalCommand("audacious -r");
        }
        
        
@@ -95,29 +114,17 @@
         * @param delay
         */
        public void onOKPushed(String value, Double delay){
-               new CommandThread("audacious --play-pause").start();
+               this.evalCommand("audacious --play-pause");
        }
        
        
        /**
-        * Remote control 'standby' button pushed callback.
-        * @param value
-        * @param delay
-        */
-       public void onStandbyPushed(String value, Double delay)
-       {
-               this.tux.destroy();
-               System.exit(0);
-       }
-       
-       
-       /**
         * Remote control 'ok' button pushed callback.
         * @param value
         * @param delay
         */
        public void onPlayPausePushed(String value, Double delay){
-               new CommandThread("audacious --play-pause").start();
+               this.evalCommand("audacious --play-pause");
        }
 
        
@@ -127,7 +134,7 @@
         * @param delay
         */
        public void onStopPushed(String value, Double delay){
-               new CommandThread("audacious -s").start();
+               this.evalCommand("audacious -s");
        }
        
        
@@ -137,7 +144,7 @@
         * @param delay
         */
        public void onPlayerNextPushed(String value, Double delay){
-               new CommandThread("audacious --fwd").start();
+               this.evalCommand("audacious --fwd");
        }
        
        
@@ -147,7 +154,7 @@
         * @param delay
         */
        public void onPlayerPreviousPushed(String value, Double delay){
-               new CommandThread("audacious --rew").start();
+               this.evalCommand("audacious --rew");
        }
        
        
@@ -172,6 +179,7 @@
                this.tux.button.left.registerEventOnPressed(this, 
"onNextPushed");
                this.tux.server.autoConnect(TuxAPIConst.CLIENT_LEVEL_FREE, 
"tuxdroid-gadget-audacious", "tuxdroid-gadget-audacious-2121");
                
+                /* Start audacious */
                if(this.configuration().userPlaylist)
                {
                        ret = this.startAudacious(this.configuration().path);
@@ -185,6 +193,8 @@
                    this.tux.destroy();
                    System.exit(1);
                }
+                /* Start a thread to monitor the instance of audacious */
+                new InstanceChecker("audacious").start();
        }
        
        
@@ -192,6 +202,56 @@
                new audacious().boot(args, new AudaciousConfiguration());
        }
        
+        /**
+         * 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("audacious"))
+                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.
@@ -244,4 +304,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 (!audacious.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

Reply via email to