Skip to main content

Overview

ClawScript is PhoneClaw’s JavaScript execution engine, powered by Mozilla Rhino. It enables natural language automation by converting voice commands into executable JavaScript code that controls Android device functions.

Architecture

JavaScript Engine: Mozilla Rhino

PhoneClaw uses Rhino 1.7.13, a pure Java implementation of JavaScript that runs directly on Android:
build.gradle.kts
Why Rhino? Rhino provides a sandboxed JavaScript environment that’s perfect for Android automation - it runs entirely in the JVM without requiring V8 or other native engines.

Script Execution Flow

  1. Voice Input → User speaks a command (“Click the login button”)
  2. AI Translation → OpenRouter converts command to JavaScript
  3. Code Extraction → Extract JavaScript from AI response
  4. Rhino Execution → Execute in sandboxed environment
  5. Android Actions → Trigger device automation via AndroidJSInterface

Core Implementation

Setting Up the Rhino Context

From MainActivity.kt:4169-4186:
Security Note: The ClassShutter prevents scripts from accessing dangerous Java reflection APIs. Never remove these restrictions in production.

JavaScript Bridge Interface

PhoneClaw exposes Android functionality through the Android object:

Available Functions

Magic Functions

These use vision AI to interact with UI elements:
Finds and clicks UI elements using natural language descriptions.
Uses Moondream API for vision-based element detection.
Extracts text content from the screen using vision AI.
Returns extracted text as a string.

Accessibility Functions

Direct UI interaction via Android Accessibility Service:

App Control

System Settings

Code Extraction

AI responses are automatically cleaned to extract pure JavaScript:

Error Handling

All script execution includes automatic error catching:

Performance Optimization

Optimization Level

Rhino’s optimization level is set to -1 (interpreted mode) for Android compatibility. Higher optimization levels require bytecode generation which isn’t reliable on Android.

Async Execution

Scripts run on a background dispatcher to prevent UI blocking:

Example Scripts

Simple Click Automation

Data Scraping

Conditional Logic

Limitations

Known Limitations:
  • No ES6+ features (arrow functions, async/await)
  • No Node.js modules or npm packages
  • Limited to synchronous execution (use Android.delay() for timing)
  • Cannot access Android SDK directly (use exposed Android interface)

Next Steps

Accessibility Service

Learn how ClawScript interacts with Android UI

Vision Targeting

Understand vision-based UI detection