/// <summary> /// Scans a line resulting from 'adb version' for a potential version number. /// </summary> /// <param name="line">The line to scan.</param> /// <returns><c>true</c> if a version number was found (whether it is acceptable or not).</returns> /// <remarks>If a version number is found, it checks the version number against what is expected /// by this version of ddms.</remarks> private bool ScanVersionLine(String line) { if (!string.IsNullOrEmpty(line)) { Match matcher = Regex.Match(line, ADB_VERSION_PATTERN); if (matcher.Success) { int majorVersion = int.Parse(matcher.Groups[1].Value); int minorVersion = int.Parse(matcher.Groups[2].Value); int microVersion = int.Parse(matcher.Groups[3].Value); // check only the micro version for now. if (microVersion < ADB_VERSION_MICRO_MIN) { String message = String.Format("Required minimum version of adb: {0}.{1}.{2}. Current version is {0}.{1}.{3}", majorVersion, minorVersion, ADB_VERSION_MICRO_MIN, microVersion); Log.LogAndDisplay(LogLevel.Error, ADB, message); } else if (ADB_VERSION_MICRO_MAX != -1 && microVersion > ADB_VERSION_MICRO_MAX) { String message = String.Format("Required maximum version of adb: {0}.{1}.{2}. Current version is {0}.{1}.{3}", majorVersion, minorVersion, ADB_VERSION_MICRO_MAX, microVersion); Log.LogAndDisplay(LogLevel.Error, ADB, message); } else { VersionCheck = true; } return(true); } } return(false); }
/// <summary> /// Restarts adb, but not the services around it. /// </summary> /// <returns><c>true</c> if success.</returns> public bool Restart( ) { if (string.IsNullOrEmpty(AdbOsLocation)) { Log.e(ADB, "Cannot restart adb when AndroidDebugBridge is created without the location of adb."); return(false); } if (!VersionCheck) { Log.LogAndDisplay(LogLevel.Error, ADB, "Attempting to restart adb, but version check failed!"); return(false); } lock (this) { StopAdb( ); bool restart = StartAdb( ); if (restart && DeviceMonitor == null) { DeviceMonitor = new DeviceMonitor(this); DeviceMonitor.Start( ); } return(restart); } }
/// <summary> /// Queries adb for its version number and checks it against #MIN_VERSION_NUMBER and MAX_VERSION_NUMBER /// </summary> private void CheckAdbVersion( ) { // default is bad check VersionCheck = false; if (string.IsNullOrEmpty(AdbOsLocation)) { Log.w(TAG, "AdbOsLocation is Empty"); return; } try { Log.d(DDMS, String.Format("Checking '{0} version'", AdbOsLocation)); ProcessStartInfo psi = new ProcessStartInfo(AdbOsLocation, "version"); psi.WindowStyle = ProcessWindowStyle.Hidden; psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardOutput = true; List <String> errorOutput = new List <String> ( ); List <String> stdOutput = new List <String> ( ); using (Process proc = Process.Start(psi)) { int status = GrabProcessOutput(proc, errorOutput, stdOutput, true /* waitForReaders */); if (status != 0) { StringBuilder builder = new StringBuilder("'adb version' failed!"); builder.AppendLine(string.Empty); foreach (String error in errorOutput) { builder.AppendLine(error); } Log.LogAndDisplay(LogLevel.Error, TAG, builder.ToString( )); } } // check both stdout and stderr bool versionFound = false; foreach (String line in stdOutput) { versionFound = ScanVersionLine(line); if (versionFound) { break; } } if (!versionFound) { foreach (String line in errorOutput) { versionFound = ScanVersionLine(line); if (versionFound) { break; } } } if (!versionFound) { // if we get here, we failed to parse the output. Log.LogAndDisplay(LogLevel.Error, ADB, "Failed to parse the output of 'adb version'"); } } catch (IOException e) { Log.LogAndDisplay(LogLevel.Error, ADB, "Failed to get the adb version: " + e.Message); } }