-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
200 lines (179 loc) · 7.61 KB
/
Main.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
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpAdbClient;
using System.IO;
using IniParser.Model;
namespace QuestHome.UI
{
public partial class MainForm : Form
{
private WebClient webClient;
private ADB adb;
private void InvokeUI(Action a)
{
BeginInvoke(new MethodInvoker(a));
}
public MainForm()
{
Logger.Trace("START");
if (Program.Arguments.IgnoreSSLErrors)
{
Logger.Warn("\"--ignoresslerrors\" is set, ignoring SSL Errors!");
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
}
webClient = new WebClient();
if (Program.FirstRun || !Program.config.Sections.ContainsSection("ADB"))
{
new UI.Popups.Welcome().ShowDialog();
var adbPicker = new UI.ADBPicker();
adbPicker.ShowDialog();
}
adb = new ADB();
Task.Factory.StartNew(() => adb.Initialize());
// AdbClient.Instance.Install(adb.currentDevice.Data, );
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
if (Program.config.Sections.ContainsSection("Window"))
{
var state = Program.config["Window"]["State"]; var loc = Program.config["Window"]["Location"].Split(':'); var size = Program.config["Window"]["Size"].Split(':');
Logger.Debug("Was {} Location: {} Size: {}", Program.config["Window"]["State"], loc.ToJson(false), size.ToJson(false));
switch (state)
{
case "Maximized":
WindowState = FormWindowState.Maximized;
break;
default:
Location = new Point(int.Parse(loc[0]), int.Parse(loc[1]));
Size = new Size(int.Parse(size[1]), int.Parse(size[0]));
break;
}
}
var devList = adb.GetDevices();
if (devList.Count > 0) adb.currentDevice = devList.First();
adb.currentDeviceChanged += Adb_currentDeviceChanged;
Logger.Trace("MainForm fully loaded");
}
private void Adb_currentDeviceChanged(object sender, EventArgs e)
{
if (adb.currentDevice is null) return;
UpdateBatteryPercentages();
}
private void UpdateBatteryPercentages()
{
var level = int.Parse(adb.SendCommand("dumpsys battery | grep level").Replace("level: ", ""));
InvokeUI(() =>
{
txt_debug.Text = adb.currentDevice.ToJson();
status_battery_hmd.Text = $"{level} %";
if (level >= 99) status_battery_hmd.ForeColor = Color.Green;
else if (level <= 75) status_battery_hmd.ForeColor = Color.Blue;
else if (level <= 50) status_battery_hmd.ForeColor = Color.Yellow;
else if (level <= 25) status_battery_hmd.ForeColor = Color.Orange;
else if (level <= 10) status_battery_hmd.ForeColor = Color.Red;
// status_battery_hmd.ToolTipText = status_battery_hmd.Text + "(not charging)";
});
}
private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!(adb.currentDevice is null)) UpdateBatteryPercentages();
if (tabs_main.SelectedTab == tab_homes)
{
LoadAvailableHomes();
FillHomes();
}
else if (tabs_main.SelectedTab == tab_firmware)
{
PreloadFirmwares();
FillFirmwares();
}
}
private void tabs_main_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void tabs_main_Selected(object sender, TabControlEventArgs e)
{
if (e.TabPage == tab_firmware)
{
FillFirmwares();
}
}
private void tabs_main_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage == tab_news)
{
e.Cancel = true;
}
else if (e.TabPage == tab_homes)
{
InitializeHomes();
}
else if (e.TabPage == tab_firmware)
{
PreloadFirmwares();
}
}
private void tabs_main_TabIndexChanged(object sender, EventArgs e)
{
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Logger.Debug("called");
if (WindowState == FormWindowState.Normal)
{
Program.config["Window"]["Location"] = Location.X.ToString() + ":" + Location.Y.ToString();
Program.config["Window"]["Size"] = Height.ToString() + ":" + Width.ToString();
}
Program.config["Window"]["State"] = WindowState.ToString();
// Config.Save(Program.config);
Application.Exit();
}
private void changeDeviceToolStripMenuItem_Click(object sender, EventArgs e)
{
new Popups.ADBDevicePicker(adb).Show();
}
private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = MessageBox.Show($"Are you sure you want to shut down {adb.currentDevice.Serial}?", "Shutdown device?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) adb.SendCommand("reboot -s");
}
private void rebootSystemToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = MessageBox.Show($"Are you sure you want to reboot {adb.currentDevice.Serial}?", "Reboot device?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) adb.SendCommand("reboot");
}
private void rebootBootloaderToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = MessageBox.Show($"Are you sure you want to reboot {adb.currentDevice.Serial} into bootloader?", "Reboot bootloader device?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) adb.SendCommand("reboot bootloader");
}
private void rebootRecoveryToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = MessageBox.Show($"Are you sure you want to reboot {adb.currentDevice.Serial} into recovery?", "Reboot recovery device?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) adb.SendCommand("reboot recovery");
}
private void rebootFastbootToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = MessageBox.Show($"Are you sure you want to reboot {adb.currentDevice.Serial} into fastboot?", "Fastboot device?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) adb.SendCommand("reboot fastboot");
}
private void shellToolStripMenuItem_Click(object sender, EventArgs e)
{
new Popups.ADBShell(adb).Show();
}
private void recordSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
new Popups.DeviceSettings(adb).Show();
}
}
}