android_uiautomator_toggle_checkbox
Toggle Android checkbox elements by resource ID to automate UI testing and device control through ADB commands.
Instructions
Toggle a checkbox element by resource ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resourceId | Yes | Resource ID of the checkbox element | |
| deviceSerial | No | Specific device serial number to target (optional) |
Implementation Reference
- src/handlers.ts:499-523 (handler)Main handler function that validates arguments, invokes the ADB toggle method, and formats the MCP response.export async function uiautomatorToggleCheckboxHandler( adb: ADBWrapper, args: any ): Promise<{ content: Array<{ type: string; text: string }> }> { const { resourceId, deviceSerial } = args as UIAutomatorToggleCheckboxArgs; if (!resourceId || typeof resourceId !== 'string') { throw new Error('Invalid resource ID: resourceId must be a non-empty string'); } try { await adb.toggleCheckboxByResourceId(resourceId, deviceSerial); return { content: [ { type: 'text', text: `Successfully toggled checkbox with resource-id: ${resourceId}`, }, ], }; } catch (error) { throw new Error(`UIAutomator toggle checkbox failed: ${error instanceof Error ? error.message : String(error)}`); } }
- src/adb-wrapper.ts:700-724 (helper)Core logic: Dumps UI hierarchy XML, extracts bounds via regex matching resource-id, computes center coordinates, and simulates touch to toggle the checkbox.async toggleCheckboxByResourceId(resourceId: string, deviceSerial?: string): Promise<void> { const device = await this.getTargetDevice(deviceSerial); const hierarchyFile = '/sdcard/window_dump.xml'; await this.exec(['shell', 'uiautomator', 'dump', hierarchyFile], device); const { stdout } = await this.exec(['shell', 'cat', hierarchyFile], device); await this.exec(['shell', 'rm', hierarchyFile], device); const boundsRegex = new RegExp(`resource-id="${resourceId}"[^>]*bounds="\\[(\\d+),(\\d+)\\]\\[(\\d+),(\\d+)\\]"`); const match = stdout.match(boundsRegex); if (match) { const x1 = parseInt(match[1], 10); const y1 = parseInt(match[2], 10); const x2 = parseInt(match[3], 10); const y2 = parseInt(match[4], 10); const centerX = Math.floor((x1 + x2) / 2); const centerY = Math.floor((y1 + y2) / 2); await this.touch(centerX, centerY, 100, device); } else { throw new Error(`Element with resource-id ${resourceId} not found in UI hierarchy`); } }
- src/index.ts:306-323 (schema)Tool schema definition including name, description, and input schema with resourceId (required) and optional deviceSerial.{ name: 'android_uiautomator_toggle_checkbox', description: 'Toggle a checkbox element by resource ID', inputSchema: { type: 'object', properties: { resourceId: { type: 'string', description: 'Resource ID of the checkbox element', }, deviceSerial: { type: 'string', description: 'Specific device serial number to target (optional)', }, }, required: ['resourceId'], }, },
- src/index.ts:490-491 (registration)Switch case registration that maps the tool name to the handler function in the CallToolRequestSchema handler.case 'android_uiautomator_toggle_checkbox': return await uiautomatorToggleCheckboxHandler(this.adb, args);