-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron-main.js
More file actions
369 lines (333 loc) · 12.3 KB
/
electron-main.js
File metadata and controls
369 lines (333 loc) · 12.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const { app, BrowserWindow, Menu, ipcMain, dialog } = require('electron');
const path = require('path');
const { spawn } = require('child_process');
const fs = require('fs');
class PhoneControlElectron {
constructor() {
this.mainWindow = null;
this.serverProcess = null;
this.isDev = process.env.NODE_ENV === 'development';
}
async createWindow() {
// Create the browser window
this.mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
icon: path.join(__dirname, 'Software', 'icon.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webSecurity: false
},
show: false,
titleBarStyle: 'default'
});
// Start the backend server
await this.startServer();
// Load the app
if (this.isDev) {
this.mainWindow.loadURL('http://localhost:3000');
this.mainWindow.webContents.openDevTools();
} else {
this.mainWindow.loadURL(`http://localhost:3000`);
}
// Show window when ready
this.mainWindow.once('ready-to-show', () => {
this.mainWindow.show();
if (this.isDev) {
this.mainWindow.webContents.openDevTools();
}
});
// Handle window closed
this.mainWindow.on('closed', () => {
this.mainWindow = null;
this.stopServer();
});
this.setupMenu();
this.setupIpcHandlers();
}
async startServer() {
return new Promise((resolve) => {
const serverPath = path.join(__dirname, 'src', 'server.js');
this.serverProcess = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env, PORT: '3000' }
});
this.serverProcess.stdout.on('data', (data) => {
console.log(`Server: ${data}`);
if (data.toString().includes('running on')) {
resolve();
}
});
this.serverProcess.stderr.on('data', (data) => {
console.error(`Server Error: ${data}`);
});
this.serverProcess.on('close', (code) => {
console.log(`Server process exited with code ${code}`);
});
// Timeout fallback
setTimeout(resolve, 3000);
});
}
stopServer() {
if (this.serverProcess) {
this.serverProcess.kill();
this.serverProcess = null;
}
}
setupMenu() {
const template = [
{
label: 'File',
submenu: [
{
label: 'Open Device Manager',
accelerator: 'CmdOrCtrl+D',
click: () => {
this.mainWindow.webContents.send('open-device-manager');
}
},
{
label: 'Take Screenshot',
accelerator: 'CmdOrCtrl+S',
click: () => {
this.mainWindow.webContents.send('take-screenshot');
}
},
{ type: 'separator' },
{
label: 'Exit',
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
click: () => {
app.quit();
}
}
]
},
{
label: 'Device',
submenu: [
{
label: 'Refresh Devices',
accelerator: 'F5',
click: () => {
this.mainWindow.webContents.send('refresh-devices');
}
},
{
label: 'Start Screen Mirror',
accelerator: 'CmdOrCtrl+M',
click: () => {
this.mainWindow.webContents.send('start-mirror');
}
},
{
label: 'Enable WiFi Debugging',
click: () => {
this.mainWindow.webContents.send('enable-wifi');
}
}
]
},
{
label: 'Tools',
submenu: [
{
label: 'ADB Shell',
accelerator: 'CmdOrCtrl+T',
click: () => {
this.mainWindow.webContents.send('open-shell');
}
},
{
label: 'File Transfer',
accelerator: 'CmdOrCtrl+F',
click: () => {
this.mainWindow.webContents.send('file-transfer');
}
},
{ type: 'separator' },
{
label: 'Settings',
click: () => {
this.openSettings();
}
}
]
},
{
label: 'Help',
submenu: [
{
label: 'About',
click: () => {
this.showAbout();
}
},
{
label: 'Documentation',
click: () => {
require('electron').shell.openExternal('https://github.com/jtgsystems/Phone-Control-Project');
}
}
]
}
];
// macOS specific menu adjustments
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ label: 'About ' + app.getName(), role: 'about' },
{ type: 'separator' },
{ label: 'Services', role: 'services', submenu: [] },
{ type: 'separator' },
{ label: 'Hide ' + app.getName(), accelerator: 'Command+H', role: 'hide' },
{ label: 'Hide Others', accelerator: 'Command+Shift+H', role: 'hideothers' },
{ label: 'Show All', role: 'unhide' },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click: () => app.quit() }
]
});
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
setupIpcHandlers() {
ipcMain.handle('select-file', async () => {
const result = await dialog.showOpenDialog(this.mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'APK Files', extensions: ['apk'] },
{ name: 'All Files', extensions: ['*'] }
]
});
return result;
});
ipcMain.handle('select-directory', async () => {
const result = await dialog.showOpenDialog(this.mainWindow, {
properties: ['openDirectory']
});
return result;
});
ipcMain.handle('save-file', async (event, defaultName) => {
const result = await dialog.showSaveDialog(this.mainWindow, {
defaultPath: defaultName,
filters: [
{ name: 'PNG Images', extensions: ['png'] },
{ name: 'All Files', extensions: ['*'] }
]
});
return result;
});
ipcMain.handle('show-notification', (event, title, body) => {
new Notification({ title, body }).show();
});
}
openSettings() {
// Create settings window
const settingsWindow = new BrowserWindow({
width: 600,
height: 400,
parent: this.mainWindow,
modal: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
const settingsHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Settings</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; padding: 20px; }
.setting { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: 500; }
input, select { width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px; }
button { background: #007acc; color: white; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; }
button:hover { background: #005a9e; }
</style>
</head>
<body>
<h2>Phone Control Settings</h2>
<div class="setting">
<label>Default Video Bitrate:</label>
<select id="bitrate">
<option value="4M">4 Mbps</option>
<option value="8M" selected>8 Mbps</option>
<option value="16M">16 Mbps</option>
</select>
</div>
<div class="setting">
<label>Max Screen Size:</label>
<select id="maxSize">
<option value="720">720p</option>
<option value="1080" selected>1080p</option>
<option value="1440">1440p</option>
<option value="0">Original</option>
</select>
</div>
<div class="setting">
<label>Renderer:</label>
<select id="renderer">
<option value="opengl" selected>OpenGL</option>
<option value="software">Software</option>
</select>
</div>
<button onclick="saveSettings()">Save Settings</button>
<script>
function saveSettings() {
const settings = {
bitrate: document.getElementById('bitrate').value,
maxSize: document.getElementById('maxSize').value,
renderer: document.getElementById('renderer').value
};
localStorage.setItem('phoneControlSettings', JSON.stringify(settings));
window.close();
}
</script>
</body>
</html>
`;
settingsWindow.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(settingsHTML));
}
showAbout() {
dialog.showMessageBox(this.mainWindow, {
type: 'info',
title: 'About Phone Control Project',
message: 'Phone Control Project v2.0',
detail: 'Advanced Android device control with modern web interface.\n\nFeatures:\n• Wireless ADB connection\n• Screen mirroring with scrcpy\n• File transfer\n• Remote shell access\n• Cross-platform support\n\nDeveloped by JTG Systems'
});
}
}
// App event handlers
const phoneControl = new PhoneControlElectron();
app.whenReady().then(() => {
phoneControl.createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
phoneControl.createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('before-quit', () => {
phoneControl.stopServer();
});
// Handle certificate errors for local development
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
if (url.startsWith('https://localhost') || url.startsWith('https://127.0.0.1')) {
event.preventDefault();
callback(true);
} else {
callback(false);
}
});