Files

6.8 KiB

MG Utility plugin SDK

The SDK lets an independently packaged plugin contribute pages, dashboard tiles, background work, audio, and key actions without coupling plugin code to MG Utility's Android activities. The API is Java 8 and Android API 28 compatible. SDK sources live in src/cloud/molberg/mgutility/sdk; the host implementation is under src/cloud/molberg/mgutility/plugin.

Minimal plugin

package com.example.speed;

import cloud.molberg.mgutility.sdk.Contribution;
import cloud.molberg.mgutility.sdk.Plugin;
import cloud.molberg.mgutility.sdk.PluginContext;

public final class SpeedPlugin implements Plugin {
    private PluginContext context;

    @Override public void onLoad(PluginContext context) {
        this.context = context;
        context.logger().info("Loaded");
    }

    @Override public void onEnable(PluginContext context) {
        context.contributions().register(new Contribution(
            "speed.tile", Contribution.Type.DASHBOARD_WIDGET,
            "Speed", "Show current speed", "speed://dashboard"));
    }

    @Override public void onDisable(PluginContext context) {
        context.contributions().remove("speed.tile");
    }

    @Override public void onUnload(PluginContext context) { }
}

Lifecycle callbacks are idempotent from the host's perspective. Register listeners/contributions in onEnable and remove them in onDisable; release threads and players in onUnload. Exceptions are caught by the host and mark the plugin as FAILED.

mg-plugin.json

Place this file in the plugin APK's assets/ directory:

{
  "schema": 1,
  "id": "com.example.speed",
  "name": "Speed display",
  "versionName": "1.0.0",
  "versionCode": 1,
  "entryPoint": "com.example.speed.SpeedPlugin",
  "sdkVersion": "1.0.0",
  "publisher": "Example",
  "description": "Displays vehicle speed on the dashboard.",
  "capabilities": ["DASHBOARD", "VEHICLE_DATA_READ"],
  "permissions": ["vehicle_data"]
}

id is a stable reverse-DNS-like identifier and must be unique. entryPoint is the class implementing Plugin. versionName and versionCode identify a release; sdkVersion is the SDK compatibility declaration. The parser accepts the legacy aliases version and entry while new plugins should use the explicit names above. Unknown manifest schema versions are rejected.

Capabilities are an allow-list requested by the plugin. The host may grant a smaller set (for example when a vehicle does not expose telemetry). Always check context.hasCapability(...) before using a capability. Available values are DASHBOARD, SETTINGS, AUDIO_PLAYBACK, VEHICLE_DATA_READ, KEY_REMAP, BACKGROUND_WORK, STORAGE, NETWORK, and NOTIFICATIONS.

Contributions and hooks

Use ContributionRegistry for UI or behaviour contributions. IDs are unique within a plugin. HookRegistry provides named, opaque host events and is safe to publish from a worker thread; listeners are invoked outside the registry's lock. Keep payload handling defensive because hosts may add fields over time.

ScopedPreferences is automatically namespaced by plugin ID. Never write to the host's mg_config store directly. runAsync supplies a host-managed executor; stop work when onDisable is called.

Standard hooks

HookRegistry supports both custom string hooks and typed standard hooks. Typed hooks use PluginHook plus an immutable HookEvent, so event names and common value accessors are discoverable in the SDK and payloads can grow without breaking old plugins. The standard catalog is:

Area Hooks
App/display APP_STARTED, APP_STOPPED, SCREEN_ON, SCREEN_OFF, DASHBOARD_REFRESH, THEME_CHANGED
Vehicle VEHICLE_CONNECTED, VEHICLE_DISCONNECTED, VEHICLE_STATE, SPEED_CHANGED, SOC_CHANGED, DRIVE_MODE_CHANGED, REGENERATION_CHANGED, CHARGING_STATE_CHANGED
Media/audio MEDIA_STATE_CHANGED, AUDIO_FFT, VOLUME_CHANGED
Input KEY_EVENT
Trips/arrival TRIP_STARTED, TRIP_UPDATED, TRIP_FINISHED, ARRIVAL_ENTERED, ARRIVAL_EXITED
Plugins PLUGIN_ENABLED, PLUGIN_DISABLED

Example typed subscription:

HookRegistry.TypedListener listener = new HookRegistry.TypedListener() {
    @Override public void onEvent(PluginHook hook, HookEvent event) {
        double speed = event.getDouble("speedKph", 0);
        context.logger().debug("speed=" + speed);
    }
};
context.hooks().subscribe(PluginHook.SPEED_CHANGED, listener);

The host publishes events on its own threads; listeners must be quick and non-blocking. Use runAsync for work. Unsubscribe in onDisable. Capability checks still apply: for example, vehicle hooks require VEHICLE_DATA_READ, audio hooks require AUDIO_PLAYBACK, and key hooks require KEY_REMAP.

Host code publishes typed events through PluginHostEvents.publish(HookEvent); this keeps event production decoupled from plugin loading and allows vehicle services, media listeners, and trip tracking to feed the same bus.

Building and testing

The current repository intentionally has no Gradle dependency. Add the SDK source files to a plugin module and compile with Java 8 against the Android SDK (android.jar). The JVM contract tests can be run with:

./tests/run_tests.sh

The test suite includes manifest schema/capability validation, semantic version ranges, contribution registration, and hook subscribe/unsubscribe behaviour.

To produce a reusable SDK jar for a plugin project, run:

./scripts/build-plugin-sdk.sh

The resulting build/plugin-sdk/mg-utility-plugin-sdk.jar contains only the Android-free contract classes. Compile the Android host adapter against android.jar when a plugin needs Android resources or activities.

Marketplace repository format

The marketplace consumes a JSON index containing schema and a plugins array. Each entry includes id, version, apkUrl, sha256, and optional sdkRange, entry, publisher, and description fields. APK URLs must use HTTPS and are checked against the SHA-256 digest before installation. A sample entry is:

{
  "schema": 1,
  "plugins": [{
    "id": "com.example.speed",
    "name": "Speed display",
    "version": "1.0.0",
    "apkUrl": "https://example.org/speed-1.0.0.apk",
    "sha256": "…64 lowercase hexadecimal characters…",
    "sdkRange": "1.0.0-2.0.0",
    "entry": "com.example.speed.SpeedPlugin"
  }]
}

Roadmap

Three existing features — speed-reactive audio, the soundboard, and the button-key remapper — will become SDK consumers during the next phase. They keep their current screens and behaviour until their code is rewritten against Contribution, HookRegistry, and ScopedPreferences and published from the official repository index. Removing in-app code only happens once a replacement plugin is signed and published, so functionality never disappears without a working install path.