Example #1
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static void Refresh ()
    {
      LoggingUtils.PrintFunction ();

      lock (m_updateLockMutex)
      {
        // 
        // Start an ADB instance, if required.
        // 

        using (SyncRedirectProcess adbStartServer = new SyncRedirectProcess (AndroidSettings.SdkRoot + @"\platform-tools\adb.exe", "start-server"))
        {
          adbStartServer.StartAndWaitForExit (30000);
        }

        using (SyncRedirectProcess adbDevices = new SyncRedirectProcess (AndroidSettings.SdkRoot + @"\platform-tools\adb.exe", "devices"))
        {
          adbDevices.StartAndWaitForExit (30000);

          // 
          // Parse 'devices' output, skipping headers and potential 'start-server' output.
          // 

          Dictionary<string, string> currentAdbDevices = new Dictionary<string, string> ();

          LoggingUtils.Print (string.Format ("[AndroidAdb] Devices output: {0}", adbDevices.StandardOutput));

          if (!String.IsNullOrEmpty (adbDevices.StandardOutput))
          {
            string [] deviceOutputLines = adbDevices.StandardOutput.Replace ("\r", "").Split (new char [] { '\n' });

            foreach (string line in deviceOutputLines)
            {
              if (Regex.IsMatch (line, "^[A-Za-z0-9.:\\-]+[\t][a-z]+$"))
              {
                string [] segments = line.Split (new char [] { '\t' });

                string deviceName = segments [0];

                string deviceType = segments [1];

                currentAdbDevices.Add (deviceName, deviceType);
              }
            }
          }

          // 
          // First identify any previously tracked devices which aren't in 'devices' output.
          // 

          HashSet<string> disconnectedDevices = new HashSet<string> ();

          foreach (string key in m_connectedDevices.Keys)
          {
            string deviceName = (string) key;

            if (!currentAdbDevices.ContainsKey (deviceName))
            {
              disconnectedDevices.Add (deviceName);
            }
          }

          // 
          // Identify whether any devices have changed state; connected/persisted/disconnected.
          // 

          foreach (KeyValuePair <string, string> devicePair in currentAdbDevices)
          {
            string deviceName = devicePair.Key;

            string deviceType = devicePair.Value;

            if (deviceType.Equals ("offline"))
            {
              disconnectedDevices.Add (deviceName);
            }
            else if (deviceType.Equals ("unauthorized"))
            {
              // User needs to allow USB debugging.
            }
            else
            {
              AndroidDevice connectedDevice;

              if (m_connectedDevices.TryGetValue (deviceName, out connectedDevice))
              {
                // 
                // Device is pervasive. Refresh internal properties.
                // 

                LoggingUtils.Print (string.Format ("[AndroidAdb] Device pervaded: {0} - {1}", deviceName, deviceType));

                connectedDevice.Refresh ();

                foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
                {
                  deviceListener.DevicePervasive (connectedDevice);
                }
              }
              else
              {
                // 
                // Device connected.
                // 

                LoggingUtils.Print (string.Format ("[AndroidAdb] Device connected: {0} - {1}", deviceName, deviceType));

                connectedDevice = new AndroidDevice (deviceName);

                connectedDevice.Refresh ();

                m_connectedDevices.Add (deviceName, connectedDevice);

                foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
                {
                  deviceListener.DeviceConnected (connectedDevice);
                }
              }
            }
          }

          // 
          // Finally, handle device disconnection.
          // 

          foreach (string deviceName in disconnectedDevices)
          {
            AndroidDevice disconnectedDevice;

            if (m_connectedDevices.TryGetValue (deviceName, out disconnectedDevice))
            {
              LoggingUtils.Print (string.Format ("[AndroidAdb] Device disconnected: {0}", deviceName));

              m_connectedDevices.Remove (deviceName);

              foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
              {
                deviceListener.DeviceDisconnected (disconnectedDevice);
              }
            }
          }
        }
      }
    }
Example #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static void Refresh()
        {
            LoggingUtils.PrintFunction();

            lock (m_updateLockMutex)
            {
                //
                // Start an ADB instance, if required.
                //

                using (SyncRedirectProcess adbStartServer = new SyncRedirectProcess(AndroidSettings.SdkRoot + @"\platform-tools\adb.exe", "start-server"))
                {
                    adbStartServer.StartAndWaitForExit(30000);
                }

                using (SyncRedirectProcess adbDevices = new SyncRedirectProcess(AndroidSettings.SdkRoot + @"\platform-tools\adb.exe", "devices"))
                {
                    adbDevices.StartAndWaitForExit(30000);

                    //
                    // Parse 'devices' output, skipping headers and potential 'start-server' output.
                    //

                    Dictionary <string, string> currentAdbDevices = new Dictionary <string, string> ();

                    LoggingUtils.Print(string.Concat("[AndroidAdb] Devices output: ", adbDevices.StandardOutput));

                    if (!String.IsNullOrEmpty(adbDevices.StandardOutput))
                    {
                        string [] deviceOutputLines = adbDevices.StandardOutput.Replace("\r", "").Split(new char [] { '\n' });

                        foreach (string line in deviceOutputLines)
                        {
                            if (Regex.IsMatch(line, "^[A-Za-z0-9.:\\-]+[\t][A-Za-z]+$"))
                            {
                                string [] segments = line.Split(new char [] { '\t' });

                                string deviceName = segments [0];

                                string deviceType = segments [1];

                                currentAdbDevices.Add(deviceName, deviceType);
                            }
                        }
                    }

                    //
                    // First identify any previously tracked devices which aren't in 'devices' output.
                    //

                    HashSet <string> disconnectedDevices = new HashSet <string> ();

                    foreach (string key in m_connectedDevices.Keys)
                    {
                        string deviceName = (string)key;

                        if (!currentAdbDevices.ContainsKey(deviceName))
                        {
                            disconnectedDevices.Add(deviceName);
                        }
                    }

                    //
                    // Identify whether any devices have changed state; connected/persisted/disconnected.
                    //

                    foreach (KeyValuePair <string, string> devicePair in currentAdbDevices)
                    {
                        string deviceName = devicePair.Key;

                        string deviceType = devicePair.Value;

                        if (deviceType.Equals("offline", StringComparison.InvariantCultureIgnoreCase))
                        {
                            disconnectedDevices.Add(deviceName);
                        }
                        else if (deviceType.Equals("unauthorized", StringComparison.InvariantCultureIgnoreCase))
                        {
                            // User needs to allow USB debugging.
                        }
                        else
                        {
                            AndroidDevice connectedDevice;

                            if (m_connectedDevices.TryGetValue(deviceName, out connectedDevice))
                            {
                                //
                                // Device is pervasive. Refresh internal properties.
                                //

                                LoggingUtils.Print(string.Format("[AndroidAdb] Device pervaded: {0} - {1}", deviceName, deviceType));

                                connectedDevice.Refresh();

                                foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
                                {
                                    deviceListener.DevicePervasive(connectedDevice);
                                }
                            }
                            else
                            {
                                //
                                // Device connected.
                                //

                                LoggingUtils.Print(string.Format("[AndroidAdb] Device connected: {0} - {1}", deviceName, deviceType));

                                connectedDevice = new AndroidDevice(deviceName);

                                connectedDevice.Refresh();

                                m_connectedDevices.Add(deviceName, connectedDevice);

                                foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
                                {
                                    deviceListener.DeviceConnected(connectedDevice);
                                }
                            }
                        }
                    }

                    //
                    // Finally, handle device disconnection.
                    //

                    foreach (string deviceName in disconnectedDevices)
                    {
                        AndroidDevice disconnectedDevice;

                        if (m_connectedDevices.TryGetValue(deviceName, out disconnectedDevice))
                        {
                            LoggingUtils.Print(string.Concat("[AndroidAdb] Device disconnected: ", deviceName));

                            m_connectedDevices.Remove(deviceName);

                            foreach (IStateListener deviceListener in m_registeredDeviceStateListeners)
                            {
                                deviceListener.DeviceDisconnected(disconnectedDevice);
                            }
                        }
                    }
                }
            }
        }