Skip to content

Commit 8075092

Browse files
authored
feature: delete android emulator (#104)
* added delete option for emulator * fix deleting booted devices * lint issue fixes
1 parent 2a991ce commit 8075092

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

MiniSim/MenuItems/SubMenuItem.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ enum SubMenuItems {
119119
accessibilityDescription: "Delete simulator"
120120
)
121121
}
122+
123+
struct DeleteEmulator: SubMenuActionItem {
124+
let title = NSLocalizedString("Delete emulator", comment: "")
125+
let tag = Tags.delete.rawValue
126+
let bootsDevice = false
127+
let needBootedDevice = false
128+
let image = NSImage(
129+
systemSymbolName: "trash",
130+
accessibilityDescription: "Delete emulator"
131+
)
132+
}
122133
}
123134

124135
extension SubMenuItems {
@@ -131,7 +142,8 @@ extension SubMenuItems {
131142
ColdBoot(),
132143
NoAudio(),
133144
ToggleA11y(),
134-
Paste()
145+
Paste(),
146+
DeleteEmulator()
135147
]
136148

137149
static var ios: [SubMenuItem] = [

MiniSim/Service/Adb.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class ADB: ADBProtocol {
2424
case home = "/Android/sdk"
2525
case emulator = "/emulator/emulator"
2626
case adb = "/platform-tools/adb"
27+
case avd = "/cmdline-tools/latest/bin/avdmanager"
2728
}
2829

2930
/**
@@ -47,6 +48,10 @@ final class ADB: ADBProtocol {
4748
try getAndroidHome() + Paths.adb.rawValue
4849
}
4950

51+
static func getAvdPath() throws -> String {
52+
try getAndroidHome() + Paths.avd.rawValue
53+
}
54+
5055
/**
5156
Checks if passed path exists and points to `ANDROID_HOME`.
5257
*/

MiniSim/Service/DeviceService.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,19 @@ extension DeviceService {
413413
try shellOut(to: "\(adbPath) -s \(deviceId) shell input text \"\(formattedText)\"")
414414
}
415415

416+
static func deleteEmulator(device: Device) throws {
417+
let avdPath = try ADB.getAvdPath()
418+
let adbPath = try ADB.getAdbPath()
419+
if device.booted {
420+
guard let deviceId = device.identifier else {
421+
throw DeviceError.deviceNotFound
422+
}
423+
try shellOut(to: "\(adbPath) -s \(deviceId) emu kill")
424+
}
425+
try shellOut(to: "\(avdPath) delete avd -n \"\(device.name)\"")
426+
}
427+
416428
static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) {
417-
queue.async {
418429
do {
419430
switch commandTag {
420431
case .coldBoot:
@@ -447,12 +458,27 @@ extension DeviceService {
447458
if let command = DeviceService.getCustomCommand(platform: .android, commandName: itemName) {
448459
try DeviceService.runCustomCommand(device, command: command)
449460
}
461+
462+
case .delete:
463+
let result = !NSAlert.showQuestionDialog(
464+
title: "Are you sure?",
465+
message: "Are you sure you want to delete this Emulator?"
466+
)
467+
if result { return }
468+
queue.async {
469+
do {
470+
try DeviceService.deleteEmulator(device: device)
471+
DeviceService.showSuccessMessage(title: "Emulator deleted!", message: device.name)
472+
NotificationCenter.default.post(name: .deviceDeleted, object: nil)
473+
} catch {
474+
NSAlert.showError(message: error.localizedDescription)
475+
}
476+
}
450477
default:
451478
break
452479
}
453480
} catch {
454481
NSAlert.showError(message: error.localizedDescription)
455482
}
456-
}
457483
}
458484
}

0 commit comments

Comments
 (0)