Skip to content

Commit

Permalink
Refacoring, removed unused code, changed version
Browse files Browse the repository at this point in the history
  • Loading branch information
Labo89 committed Feb 18, 2021
1 parent b5f4cf2 commit 2d69560
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 50 deletions.
2 changes: 1 addition & 1 deletion adbGUI/Forms/BackupRestore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void Cbo_BackupPackage_CheckedChanged(object sender, EventArgs e)
groupBox8.Enabled = false;
groupBox14.Enabled = false;

var output = HelperClass.ExecuteWithOutput("adb shell pm list packages -3");
var output = HelperClass.ExecuteWithOutput("adb", "shell pm list packages -3");

if (!string.IsNullOrEmpty(output))
{
Expand Down
2 changes: 1 addition & 1 deletion adbGUI/Forms/InstallUninstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void RefreshInstalledApps()

cbx_InstallUninstallPackageUninstall.Enabled = false;

var output = HelperClass.ExecuteWithOutput("adb shell pm list packages -3");
var output = HelperClass.ExecuteWithOutput("adb", "shell pm list packages -3");

if (!string.IsNullOrEmpty(output))
{
Expand Down
2 changes: 1 addition & 1 deletion adbGUI/Forms/LogcatAdvanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void Btn_LogcatAdvancedClearBuffers_Click(object sender, EventArgs e)

private void Btn_LogcatAdvancedStop_Click(object sender, EventArgs e)
{
CLI.AbortChildProcessesAsync();
CLI.KillChildProcessesAsync();
}

private void Btn_LogcatAdvancedNewBufferSize_Click(object sender, EventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions adbGUI/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void Btn_ConsoleClear_Click(object sender, EventArgs e)

private void Btn_consoleStop_Click(object sender, EventArgs e)
{
CLI.AbortChildProcessesAsync();
CLI.KillChildProcessesAsync();
}

private void Btn_executeCommand_Click(object sender, EventArgs e)
Expand All @@ -83,7 +83,7 @@ private void CloseApplication()
{
Debug.WriteLine("Shutting down...");

Debug.WriteLine(" > Killing child processes...");
Debug.WriteLine(" > Killing all child processes...");
CLI.StopWithShell();

Debug.WriteLine(" > Closing application...");
Expand All @@ -109,7 +109,7 @@ private void MainForm_KeyDown(object sender, KeyEventArgs e)
if ((!cbx_customCommand.Focused || String.IsNullOrWhiteSpace(cbx_customCommand.SelectedText)) && String.IsNullOrWhiteSpace(rtb_console.SelectedText))
{
Debug.WriteLine("Keypress detected: CTRL + C");
CLI.AbortChildProcessesAsync();
CLI.KillChildProcessesAsync();
e.Handled = e.SuppressKeyPress = true;
}

Expand Down
2 changes: 1 addition & 1 deletion adbGUI/Forms/ScreenRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void Btn_SreenRecordAbort_Click(object sender, EventArgs e)
{
timer.Enabled = false;
btn_screenRecordStart.Text = @"Start";
CLI.AbortChildProcesses();
CLI.KillChildProcesses();
}

private void Timer_Tick(object sender, EventArgs e)
Expand Down
81 changes: 55 additions & 26 deletions adbGUI/Methods/CLI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Management;
Expand Down Expand Up @@ -38,38 +39,71 @@ static CLI()

public static Process Commandline { get; set; } = new Process();

public static void AbortChildProcesses()
public static List<int> GetChildProcesses()
{
List<int> lst = new List<int>();

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ParentProcessID=" + Commandline.Id + " AND Caption != 'conhost.exe'"))
{
ManagementObjectCollection managementObjectCollection = searcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection)
{
int pid = Convert.ToInt32(managementObject["ProcessID"]);
Debug.WriteLine($"Killing {managementObject["Caption"]}({managementObject["ProcessID"]})");
Process.GetProcessById(pid).Kill();
lst.Add(Convert.ToInt32(managementObject["ProcessID"]));
}
}

return lst;
}

public static void AbortChildProcessesAsync()
public static void KillChildProcesses()
{
Task.Run(() => { AbortChildProcesses(); });
foreach (int pid in GetChildProcesses())
{
Debug.WriteLine($"Killing {pid})");
Process.GetProcessById(pid).Kill();
}
}

public static void AbortChildProcessesWithShell()
public static void KillChildProcessesAsync()
{
Task.Run(() => { KillChildProcesses(); });
}

public static void KillChildProcessesWithShell()
{
string input = "taskkill /F ";

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ParentProcessID=" + Commandline.Id + " AND Caption != 'conhost.exe'"))
foreach (int pid in GetChildProcesses())
{
ManagementObjectCollection managementObjectCollection = searcher.Get();
input += $"/PID {pid} ";
}

foreach (ManagementObject managementObject in managementObjectCollection)
{
int pid = Convert.ToInt32(managementObject["ProcessID"]);
input += $"/PID {pid} ";
}
if (input == "taskkill /F ") return;

Process cmd = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "cmd",
Arguments = "/c " + input,
UseShellExecute = false,
CreateNoWindow = true,
};

cmd.StartInfo = startInfo;

Debug.WriteLine("Executing: cmd /c " + input);

cmd.Start();
}

public static void KillAllAdbProcessesWithShell()
{
string input = "taskkill /F ";

foreach (Process process in Process.GetProcessesByName("adb"))
{
input += $"/PID {process.Id} ";
}

if (input == "taskkill /F ") return;
Expand All @@ -86,24 +120,25 @@ public static void AbortChildProcessesWithShell()

cmd.StartInfo = startInfo;

Debug.WriteLine("Executing: cmd /c" + input);
Debug.WriteLine("Executing: cmd /c " + input);

cmd.Start();

}

public static void Execute(string command)
{
Commandline.StandardInput.WriteLine(command);
}

public static string GetOutput(string command)
public static string GetOutput(string fileName, string arguments)
{
Process cmd = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "cmd",
Arguments = "/c " + command,
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
Expand All @@ -122,15 +157,9 @@ public static string GetOutput(string command)
return cmd.StandardOutput.ReadToEnd();
}

internal static void Stop()
{
AbortChildProcesses();
Commandline.Kill();
}

internal static void StopWithShell()
public static void StopWithShell()
{
AbortChildProcessesWithShell();
KillChildProcessesWithShell();
Commandline.Kill();
}
}
Expand Down
12 changes: 3 additions & 9 deletions adbGUI/Methods/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public static void Check()
{
case DialogResult.Yes:
DownloadFiles();
ExtractFiles();
MessageBox.Show(@"Files downloaded", @"Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;

default:
Environment.Exit(0);
break;
Expand All @@ -64,20 +67,11 @@ private static void DownloadFiles()
{
wc.DownloadFile(downloadUri, downloadedZipFile);
}

ExtractFiles();
}

private static void ExtractFiles()
{
if (Directory.Exists(tmpPlatformPath))
{
Directory.Delete(tmpPlatformPath, true);
}

ZipFile.ExtractToDirectory(downloadedZipFile, Path.GetTempPath());

MessageBox.Show(@"Files downloaded", @"Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private static void SetEnvVariable()
Expand Down
2 changes: 1 addition & 1 deletion adbGUI/Methods/DevicesWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Refresh()
{
while (true)
{
string output = CLI.GetOutput("adb devices");
string output = CLI.GetOutput("adb", "devices");

if (output != oldOutput)
{
Expand Down
8 changes: 4 additions & 4 deletions adbGUI/Methods/HelperClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public static void Execute(string command, bool withSerial = true)
}
}

public static string ExecuteWithOutput(string command, bool withSerial = true)
public static string ExecuteWithOutput(string fileName, string arguments, bool withSerial = true)
{
if (String.IsNullOrWhiteSpace(SelectedDevice) || withSerial == false)
{
return CLI.GetOutput(command);
return CLI.GetOutput(fileName, arguments);
}
else
{
string cmd = command.Replace("adb ", "adb -s " + SelectedDevice + " ");
return CLI.GetOutput(cmd);
arguments = "-s " + SelectedDevice + " " + arguments;
return CLI.GetOutput(fileName, arguments);
}
}

Expand Down
6 changes: 3 additions & 3 deletions adbGUI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Labo")]
[assembly: AssemblyProduct("adbGUI")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -34,5 +34,5 @@
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.0.1")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyVersion("2.2.0.0")]
[assembly: AssemblyFileVersion("2.2.0.0")]

0 comments on commit 2d69560

Please sign in to comment.