-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.cs
More file actions
280 lines (251 loc) · 9.36 KB
/
Copy pathProgram.cs
File metadata and controls
280 lines (251 loc) · 9.36 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace AndroidController
{
static class Program
{
public static string defaultWindowName = "WINDOWBYCS9925834";
public static AdbClient AdbClient = new AdbClient();
public static Properties.Settings Settings = Properties.Settings.Default;
[STAThread]
static void Main()
{
Environment.CurrentDirectory =Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"tools");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AdbClient.adbPath = getAdbPath();
Translator.init();
Translator.chLang(Translator.SystemLanguage);
Console.Write("");
Application.Run(ProgressDialog.Schedule(e =>
{
e.ReportProgress(0, "StartingADB".t());
AdbClient.ensureAdbRunning();
var a = AdbClient.getDeviceList();
}));
Application.Run(new Form1());
Settings.Save();
}
public static string getAdbPath() {
foreach (Process item in Process.GetProcessesByName("adb"))
{
return item.MainModule.FileName;
}
foreach (string item in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
{
if (File.Exists(Path.Combine(item, "adb.exe"))) {
return Path.Combine(item, "adb.exe");
}
}
return "adb.exe";
}
public static void setDict<T>(this ComboBox cmb, Dictionary<String, T> dict)
{
cmb.Enabled = false;
cmb.DataSource = null;
cmb.Items.Clear();
cmb.Text = null;
if (dict != null && dict.Count > 0)
{
cmb.Enabled = true;
cmb.DataSource = new BindingSource() { DataSource = dict };
cmb.DisplayMember = "Key";
cmb.ValueMember = "Value";
}
}
}
public static class Translator
{
public static Dictionary<String, String> baseTran = new Dictionary<string, string>();
public static Dictionary<String, String> overTran = new Dictionary<string, string>();
public static Dictionary<String, String> langList = new Dictionary<string, string>();
public static string SystemLanguage {
get {
return System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower().Replace("-", "_");
}
}
public static void init()
{
Directory.EnumerateFiles("languages", "*.xml").ToList().ForEach(f => {
try
{
loadLang(f, baseTran);
langList.Add(XDocument.Load(f).Root.Attribute("name").Value, f);
}
catch { }
});
loadLang(langList["default"], baseTran);
}
public static void chLang(String langName)
{
if (langList.ContainsKey(langName))
{
loadLang(langList[langName], overTran);
}
}
public static void loadLang(String fn, Dictionary<String, String> dict)
{
dict.Clear();
XDocument.Load(fn).Root.Elements().ToList().ForEach(el => {
dict.Remove(el.Name.LocalName);
dict.Add(el.Name.LocalName, el.Value);
});
}
public static String t(this String s)
{
if (s == null || s == "") { return s; }
if (overTran.ContainsKey(s)) { return overTran[s]; }
if (baseTran.ContainsKey(s)) { return baseTran[s]; }
return "?" + s + "<";
}
}
public class FormTranslator {
Form form;
public FormTranslator(Form form)
{
this.form = form;
doScan(form);
doScan2(form);
TranslateAll();
}
public void doScan2(Form frm) {
frm.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(f => f.GetValue(frm) is ToolStrip ).Select(f => (ToolStrip)f.GetValue(frm)).ToList().ForEach(f =>
{
foreach (ToolStripMenuItem item in f.Items)
{
doScan3(item);
}
});
}
public void doScan3(ToolStripMenuItem tsmi) {
tranObjs.Add(new TransObj(tsmi, "Text"));
foreach (ToolStripMenuItem item in tsmi.DropDownItems)
{
doScan3(item);
}
}
public void doScan(Control ctl)
{
if (ctl is NumericUpDown || ctl is ComboBox || ctl is DateTimePicker)
{
return;
}
if (ctl is TextBox) {
TextBox t = ctl as TextBox;
if (t.ReadOnly) {
t.ReadOnly = false;
t.Text = t.Text.t();
t.ReadOnly = true;
}
return;
}
if (ctl is DataGridView)
{
foreach (DataGridViewColumn col in (ctl as DataGridView).Columns)
{
tranObjs.Add(new TransObj(col, "HeaderText"));
}
return;
}
tranObjs.Add(new TransObj(ctl, "Text"));
foreach (Control c in ctl.Controls)
{
doScan(c);
}
}
public void TranslateAll() => tranObjs.ForEach(s => s.Translate());
List<TransObj> tranObjs = new List<TransObj>();
public class TransObj
{
String key;
object obj;
String prop;
public TransObj(object obj, string prop)
{
this.obj = obj;
this.prop = prop;
key = obj.GetType().GetProperty(prop).GetValue(obj).ToString();
}
public void Translate()
{
obj.GetType().GetProperty(prop).SetValue(obj, key.t());
}
}
}
public class AdbClient {
public String adbPath = "adb.exe";
public void ensureAdbRunning() {
Console.WriteLine(runRawCommand("devices"));
}
public string runRawCommand(string cmd, bool includeErr = false) {
ProcessStartInfo psi = new ProcessStartInfo(adbPath, cmd);
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process adbp = Process.Start(psi);
adbp.WaitForExit();
StreamReader sr = (adbp.StandardOutput);
StreamReader sr2 =(adbp.StandardError);
string result = "";
result += sr.ReadToEnd();
string err = sr2.ReadToEnd();
if (err.Trim() != "" && includeErr) {
result+="\r\n"+err;
}
Console.WriteLine(result);
sr.Close();
sr2.Close();
return result;
}
public List<DeviceInfo> getDeviceList() {
return runRawCommand("devices -l")
.Split('\r', '\n')
.Where(l => l.Contains("model"))
.Select(t => splitAdbResult(t))
.Select(t => new DeviceInfo(t[1],t[0]))
.ToList();
}
private string[] splitAdbResult(string raw) {
int idx = raw.IndexOf(' ');
string str1 = raw.Substring(0, idx);
string str2 = raw.Substring(idx, raw.Length - idx);
return new string[] {str1,str2 };
}
public string runDeviceCommand(DeviceInfo dev, string command, bool includeErr = false) {
if (dev == null) {
return runRawCommand($"{command}", includeErr);
}
return runRawCommand($"-t {dev.TransportId} {command}", includeErr);
}
}
public class DeviceInfo
{
public string DeviceData;
public string DeviceSeries;
public string Product = "Android Device";
public string Model = "Android Device";
public string Device = "Android Device";
public string TransportId = "-1";
public DeviceInfo(string deviceName, string deviceSeries)
{
DeviceData = deviceName;
DeviceSeries = deviceSeries;
DeviceData.Split(' ').Where(d => d.Contains(":")).Select(d => d.Split(':')).ToList().ForEach(d =>
{
if (d[0] == "product") { Product = d[1]; }
if (d[0] == "model") { Model = d[1]; }
if (d[0] == "device") { Device = d[1]; }
if (d[0] == "transport_id") { TransportId = d[1]; }
});
}
}
}