|
1 | 1 | using Microsoft.Win32;
|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
| 4 | +using System.ComponentModel; |
4 | 5 | using System.Diagnostics;
|
5 | 6 | using System.IO;
|
6 | 7 | using System.Linq;
|
@@ -1278,6 +1279,56 @@ public static void AddContextMenuRegistry(string contextRegRoot)
|
1278 | 1279 | }
|
1279 | 1280 | }
|
1280 | 1281 |
|
| 1282 | + public static void AddContextMenuRegistryAPKInstall(string contextRegRoot) |
| 1283 | + { |
| 1284 | + // Define the registry key path for .apk file association |
| 1285 | + string apkFileTypeRegPath = @"Software\Classes\.apk"; |
| 1286 | + |
| 1287 | + // Open or create the registry key for .apk files |
| 1288 | + RegistryKey apkKey = Registry.CurrentUser.OpenSubKey(apkFileTypeRegPath, true); |
| 1289 | + |
| 1290 | + if (apkKey == null) |
| 1291 | + { |
| 1292 | + apkKey = Registry.CurrentUser.CreateSubKey(apkFileTypeRegPath); |
| 1293 | + } |
| 1294 | + |
| 1295 | + if (apkKey != null) |
| 1296 | + { |
| 1297 | + // Create or open the Shell subkey for context menu options |
| 1298 | + RegistryKey shellKey = apkKey.CreateSubKey("shell", true); |
| 1299 | + |
| 1300 | + if (shellKey != null) |
| 1301 | + { |
| 1302 | + var appName = "UnityLauncherPro"; |
| 1303 | + // Create a subkey for the app's context menu item |
| 1304 | + RegistryKey appKey = shellKey.CreateSubKey(appName, true); |
| 1305 | + |
| 1306 | + if (appKey != null) |
| 1307 | + { |
| 1308 | + appKey.SetValue("", "Install with " + appName); // Display name in context menu |
| 1309 | + appKey.SetValue("Icon", "\"" + Process.GetCurrentProcess().MainModule.FileName + "\""); |
| 1310 | + appKey.SetValue("Position", "Bottom"); // Set position to adjust order |
| 1311 | + |
| 1312 | + // Create the command subkey to specify the action |
| 1313 | + RegistryKey commandKey = appKey.CreateSubKey("command", true); |
| 1314 | + |
| 1315 | + if (commandKey != null) |
| 1316 | + { |
| 1317 | + // Build the command string to launch with -install argument |
| 1318 | + var executeString = "\"" + Process.GetCurrentProcess().MainModule.FileName + "\" -install \"%1\""; |
| 1319 | + commandKey.SetValue("", executeString); |
| 1320 | + } |
| 1321 | + } |
| 1322 | + } |
| 1323 | + } |
| 1324 | + else |
| 1325 | + { |
| 1326 | + Console.WriteLine("Error> Cannot create or access registry key for .apk file association: " + apkFileTypeRegPath); |
| 1327 | + } |
| 1328 | + } |
| 1329 | + |
| 1330 | + |
| 1331 | + |
1281 | 1332 | /// <summary>
|
1282 | 1333 | /// uninstall context menu item from registry
|
1283 | 1334 | /// </summary>
|
@@ -1305,6 +1356,37 @@ public static void RemoveContextMenuRegistry(string contextRegRoot)
|
1305 | 1356 | }
|
1306 | 1357 | }
|
1307 | 1358 |
|
| 1359 | + public static void RemoveContextMenuRegistryAPKInstall(string contextRegRoot) |
| 1360 | + { |
| 1361 | + // Define the registry key path for .apk file association |
| 1362 | + string apkFileTypeRegPath = @"Software\Classes\.apk\shell"; |
| 1363 | + |
| 1364 | + // Open the registry key for the shell context menu |
| 1365 | + RegistryKey shellKey = Registry.CurrentUser.OpenSubKey(apkFileTypeRegPath, true); |
| 1366 | + |
| 1367 | + if (shellKey != null) |
| 1368 | + { |
| 1369 | + var appName = "UnityLauncherPro"; |
| 1370 | + |
| 1371 | + // Check if the app's context menu key exists |
| 1372 | + RegistryKey appKey = shellKey.OpenSubKey(appName, false); |
| 1373 | + if (appKey != null) |
| 1374 | + { |
| 1375 | + // Delete the app's context menu key |
| 1376 | + shellKey.DeleteSubKeyTree(appName); |
| 1377 | + Console.WriteLine("Removed context menu for .apk files."); |
| 1378 | + } |
| 1379 | + else |
| 1380 | + { |
| 1381 | + Console.WriteLine("No context menu found for .apk files."); |
| 1382 | + } |
| 1383 | + } |
| 1384 | + else |
| 1385 | + { |
| 1386 | + Console.WriteLine("Error> Cannot find registry key for .apk shell context: " + apkFileTypeRegPath); |
| 1387 | + } |
| 1388 | + } |
| 1389 | + |
1308 | 1390 | /// <summary>
|
1309 | 1391 | /// reads .git/HEAD file from the project to get current branch name
|
1310 | 1392 | /// </summary>
|
@@ -2505,6 +2587,99 @@ internal static string GetSRP(string projectPath)
|
2505 | 2587 | }
|
2506 | 2588 |
|
2507 | 2589 | }
|
| 2590 | + |
| 2591 | + internal static void InstallAPK(string ApkPath) |
| 2592 | + { |
| 2593 | + try |
| 2594 | + { |
| 2595 | + var cmd = "cmd.exe"; |
| 2596 | + var pars = $"/C adb install -r \"{ApkPath}\""; // Use /C to execute and close the window after finishing |
| 2597 | + |
| 2598 | + var processStartInfo = new ProcessStartInfo |
| 2599 | + { |
| 2600 | + FileName = cmd, |
| 2601 | + Arguments = pars, |
| 2602 | + RedirectStandardOutput = true, // Capture output to wait for completion |
| 2603 | + RedirectStandardError = true, |
| 2604 | + UseShellExecute = false, |
| 2605 | + CreateNoWindow = false |
| 2606 | + }; |
| 2607 | + |
| 2608 | + string installOutput = null; |
| 2609 | + string installError = null; |
| 2610 | + |
| 2611 | + using (var installProcess = Process.Start(processStartInfo)) |
| 2612 | + { |
| 2613 | + installOutput = installProcess.StandardOutput.ReadToEnd(); |
| 2614 | + installError = installProcess.StandardError.ReadToEnd(); |
| 2615 | + installProcess.WaitForExit(); |
| 2616 | + |
| 2617 | + if (installProcess.ExitCode != 0 || !string.IsNullOrEmpty(installError)) |
| 2618 | + { |
| 2619 | + SetStatus($"Error installing APK: {installError.Trim()}\n{installOutput.Trim()}"); |
| 2620 | + return; |
| 2621 | + } |
| 2622 | + } |
| 2623 | + |
| 2624 | + // Attempt to extract package name using aapt |
| 2625 | + var aaptCmd = $"aapt dump badging \"{ApkPath}\" | findstr package:"; |
| 2626 | + var aaptProcessStartInfo = new ProcessStartInfo |
| 2627 | + { |
| 2628 | + FileName = "cmd.exe", |
| 2629 | + Arguments = $"/C {aaptCmd}", |
| 2630 | + RedirectStandardOutput = true, |
| 2631 | + UseShellExecute = false, |
| 2632 | + CreateNoWindow = true |
| 2633 | + }; |
| 2634 | + |
| 2635 | + string packageName = null; |
| 2636 | + using (var process = Process.Start(aaptProcessStartInfo)) |
| 2637 | + { |
| 2638 | + var output = process.StandardOutput.ReadToEnd(); |
| 2639 | + process.WaitForExit(); |
| 2640 | + |
| 2641 | + var match = System.Text.RegularExpressions.Regex.Match(output, "package: name='(.*?)'"); |
| 2642 | + if (match.Success) |
| 2643 | + { |
| 2644 | + packageName = match.Groups[1].Value; |
| 2645 | + } |
| 2646 | + } |
| 2647 | + |
| 2648 | + if (!string.IsNullOrEmpty(packageName)) |
| 2649 | + { |
| 2650 | + // Run the application using adb |
| 2651 | + var runPars = $"/C adb shell monkey -p {packageName} 1"; |
| 2652 | + var runProcessStartInfo = new ProcessStartInfo |
| 2653 | + { |
| 2654 | + FileName = cmd, |
| 2655 | + Arguments = runPars, |
| 2656 | + UseShellExecute = true, |
| 2657 | + CreateNoWindow = false, |
| 2658 | + WindowStyle = ProcessWindowStyle.Normal |
| 2659 | + }; |
| 2660 | + Process.Start(runProcessStartInfo); |
| 2661 | + |
| 2662 | + SetStatus($"Installed and launched APK with package name: {packageName}"); |
| 2663 | + } |
| 2664 | + else |
| 2665 | + { |
| 2666 | + SetStatus("ADB install completed, but failed to extract package name. Application not launched."); |
| 2667 | + } |
| 2668 | + } |
| 2669 | + catch (Win32Exception ex) |
| 2670 | + { |
| 2671 | + // Handle case where adb or aapt is not found |
| 2672 | + SetStatus($"Error: Required tool not found. Ensure adb and aapt are installed and added to PATH. Details: {ex.Message}"); |
| 2673 | + } |
| 2674 | + catch (Exception ex) |
| 2675 | + { |
| 2676 | + // Handle other unexpected exceptions |
| 2677 | + SetStatus($"An unexpected error occurred: {ex.Message}"); |
| 2678 | + } |
| 2679 | + } |
| 2680 | + |
| 2681 | + |
| 2682 | + |
2508 | 2683 | } // class
|
2509 | 2684 |
|
2510 | 2685 | } // namespace
|
0 commit comments