forked from nerdunit/androidsideloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADB.cs
129 lines (112 loc) · 4.53 KB
/
ADB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
namespace AndroidSideloader
{
class ADB
{
static Process adb = new Process();
public static string adbFolderPath = Environment.CurrentDirectory + "\\adb";
public static string adbFilePath = adbFolderPath + "\\adb.exe";
public static string DeviceID = "";
public static ProcessOutput RunAdbCommandToString(string command)
{
if (DeviceID.Length > 1)
command = $" -s {DeviceID} {command}";
Logger.Log($"Running command {command}");
adb.StartInfo.FileName = adbFilePath;
adb.StartInfo.Arguments = command;
adb.StartInfo.RedirectStandardError = true;
adb.StartInfo.RedirectStandardInput = true;
adb.StartInfo.RedirectStandardOutput = true;
adb.StartInfo.CreateNoWindow = true;
adb.StartInfo.UseShellExecute = false;
adb.StartInfo.WorkingDirectory = adbFolderPath;
adb.Start();
adb.StandardInput.WriteLine(command);
adb.StandardInput.Flush();
adb.StandardInput.Close();
string output = "";
string error = "";
try
{
output = adb.StandardOutput.ReadToEnd();
error = adb.StandardError.ReadToEnd();
}
catch { }
adb.WaitForExit();
Logger.Log(output);
Logger.Log(error);
return new ProcessOutput(output, error);
}
public static ProcessOutput UninstallPackage(string package)
{
WakeDevice();
ProcessOutput output = new ProcessOutput("", "");
output += RunAdbCommandToString($"shell pm uninstall -k --user 0 {package}");
output += RunAdbCommandToString($"shell pm uninstall {package}");
return output;
}
public static string GetAvailableSpace()
{
long totalSize = 0;
long usedSize = 0;
long freeSize = 0;
WakeDevice();
var output = RunAdbCommandToString("shell df").Output.Split('\n');
foreach (string currLine in output)
{
if (currLine.StartsWith("/data/media"))
{
var foo = currLine.Split(' ');
int i = 0;
foreach (string curr in foo)
{
if (curr.Length > 1)
{
switch (i)
{
case 0:
break;
case 1:
totalSize = Int64.Parse(curr) / 1000;
break;
case 2:
usedSize = Int64.Parse(curr) / 1000;
break;
case 3:
freeSize = Int64.Parse(curr) / 1000;
break;
default:
break;
}
i++;
}
}
}
}
return $"Total space: {String.Format("{0:0.00}", (double)totalSize / 1000)}GB\nUsed space: {String.Format("{0:0.00}", (double)usedSize / 1000)}GB\nFree space: {String.Format("{0:0.00}", (double)freeSize / 1000)}GB";
}
public static void WakeDevice()
{
RunAdbCommandToString("shell input keyevent KEYCODE_WAKEUP");
}
public static ProcessOutput Sideload(string path)
{
WakeDevice();
ProcessOutput ret = new ProcessOutput();
AndroidSideloader.Program.form.ChangeTitle($"Sideloading {path}");
ret = RunAdbCommandToString($"install -g -r \"{path}\"");
AndroidSideloader.Program.form.ChangeTitle($"Sideload done");
return ret;
}
public static ProcessOutput CopyOBB(string path)
{
WakeDevice();
if (SideloaderUtilities.CheckFolderIsObb(path))
return RunAdbCommandToString($"push \"{path}\" /sdcard/Android/obb");
return new ProcessOutput();
}
}
}