diff --git a/api/api.d.ts b/api/api.d.ts index 63692f6..ec56d7b 100644 --- a/api/api.d.ts +++ b/api/api.d.ts @@ -100,7 +100,7 @@ declare interface MediaService { } declare interface LocationService { - get location(): Location + get current(): Location } declare function require(module: "watches"): WatchesService; diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt index bcaaa4a..a7bbce0 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt @@ -13,13 +13,13 @@ import kotlinx.coroutines.newSingleThreadContext import net.pipe01.pinepartner.data.AppDatabase import net.pipe01.pinepartner.data.Plugin import net.pipe01.pinepartner.scripting.api.Finalizeable -import net.pipe01.pinepartner.scripting.api.HTTP -import net.pipe01.pinepartner.scripting.api.Location -import net.pipe01.pinepartner.scripting.api.Media -import net.pipe01.pinepartner.scripting.api.Notifications +import net.pipe01.pinepartner.scripting.api.HTTPService +import net.pipe01.pinepartner.scripting.api.LocationService +import net.pipe01.pinepartner.scripting.api.MediaService +import net.pipe01.pinepartner.scripting.api.NotificationsService import net.pipe01.pinepartner.scripting.api.Require -import net.pipe01.pinepartner.scripting.api.Volume -import net.pipe01.pinepartner.scripting.api.Watches +import net.pipe01.pinepartner.scripting.api.VolumeService +import net.pipe01.pinepartner.scripting.api.WatchesService import net.pipe01.pinepartner.scripting.api.adapters.BLECharacteristicAdapter import net.pipe01.pinepartner.scripting.api.adapters.BLEServiceAdapter import net.pipe01.pinepartner.scripting.api.adapters.LocationAdapter @@ -97,12 +97,12 @@ class Runner(val plugin: Plugin, deps: ScriptDependencies) { ctx.initSafeStandardObjects() } - ScriptableObject.defineClass(scope, Watches::class.java) - ScriptableObject.defineClass(scope, Notifications::class.java) - ScriptableObject.defineClass(scope, HTTP::class.java) - ScriptableObject.defineClass(scope, Volume::class.java) - ScriptableObject.defineClass(scope, Media::class.java) - ScriptableObject.defineClass(scope, Location::class.java) + ScriptableObject.defineClass(scope, WatchesService::class.java) + ScriptableObject.defineClass(scope, NotificationsService::class.java) + ScriptableObject.defineClass(scope, HTTPService::class.java) + ScriptableObject.defineClass(scope, VolumeService::class.java) + ScriptableObject.defineClass(scope, MediaService::class.java) + ScriptableObject.defineClass(scope, LocationService::class.java) ScriptableObject.defineClass(scope, WatchAdapter::class.java) ScriptableObject.defineClass(scope, NotificationAdapter::class.java) diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/ApiScriptableObject.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/ApiScriptableObject.kt index b6c69ef..fbccf86 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/ApiScriptableObject.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/ApiScriptableObject.kt @@ -14,12 +14,13 @@ import org.mozilla.javascript.Function import org.mozilla.javascript.ScriptRuntime import org.mozilla.javascript.ScriptableObject import java.util.concurrent.atomic.AtomicBoolean +import kotlin.reflect.KClass interface Finalizeable { fun finalize() } -abstract class ApiScriptableObject(private val className: String) : ScriptableObject(), Finalizeable { +abstract class ApiScriptableObject(private val clazz: KClass<*>) : ScriptableObject(), Finalizeable { private lateinit var contextFactory: ContextFactory private lateinit var dispatcher: CoroutineDispatcher private lateinit var onEvent: OnLogEvent @@ -29,7 +30,7 @@ abstract class ApiScriptableObject(private val className: String) : ScriptableOb private val listeners = mutableMapOf, () -> Unit>() private val children = mutableListOf() - override fun getClassName() = className + override fun getClassName() = clazz.simpleName!! fun initSuper(contextFactory: ContextFactory, dispatcher: CoroutineDispatcher, onEvent: OnLogEvent) { this.contextFactory = contextFactory @@ -97,11 +98,11 @@ abstract class ApiScriptableObject(private val className: String) : ScriptableOb } } - fun newObject(name: String, init: (T.() -> Unit)? = null): T { + fun newObject(clazz: KClass, init: (T.() -> Unit)? = null): T { checkFinalized() val scope = getTopLevelScope(this) - val obj = Context.getCurrentContext().newObject(scope, name) as? T ?: throw IllegalArgumentException("Invalid class") + val obj = Context.getCurrentContext().newObject(scope, clazz.simpleName!!) as? T ?: throw IllegalArgumentException("Invalid class") obj.initSuper(contextFactory, dispatcher, onEvent) diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTP.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt similarity index 96% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTP.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt index cf0e4b8..4f6a1b3 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTP.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/HTTPService.kt @@ -9,7 +9,7 @@ import org.mozilla.javascript.NativeObject import org.mozilla.javascript.Undefined import org.mozilla.javascript.annotations.JSFunction -class HTTP : ApiScriptableObject("HTTP") { +class HTTPService : ApiScriptableObject(HTTPService::class) { @JSFunction fun request(method: String, url: String, arg1: Any, arg2: Any?) { val options: NativeObject? diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Location.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/LocationService.kt similarity index 81% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/Location.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/LocationService.kt index 6464a9d..b06a257 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Location.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/LocationService.kt @@ -8,11 +8,7 @@ import kotlinx.coroutines.tasks.await import net.pipe01.pinepartner.scripting.api.adapters.LocationAdapter import org.mozilla.javascript.annotations.JSGetter -class Location : ApiScriptableObject(CLASS_NAME) { - companion object { - const val CLASS_NAME = "Location" - } - +class LocationService : ApiScriptableObject(LocationService::class) { private lateinit var fusedLocationClient: FusedLocationProviderClient fun init(fusedLocationClient: FusedLocationProviderClient) { @@ -28,7 +24,7 @@ class Location : ApiScriptableObject(CLASS_NAME) { task.await() } - return newObject(LocationAdapter.CLASS_NAME).apply { + return newObject(LocationAdapter::class).apply { init(location) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Media.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/MediaService.kt similarity index 91% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/Media.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/MediaService.kt index 02678c7..b9c297d 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Media.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/MediaService.kt @@ -8,11 +8,7 @@ import net.pipe01.pinepartner.scripting.api.adapters.PlaybackStateAdapter import org.mozilla.javascript.annotations.JSFunction import org.mozilla.javascript.annotations.JSGetter -class Media : ApiScriptableObject(CLASS_NAME) { - companion object { - val CLASS_NAME = "Media" - } - +class MediaService : ApiScriptableObject(MediaService::class) { private lateinit var mediaSessionManager: MediaSessionManager fun init(mediaSessionManager: MediaSessionManager) { @@ -47,7 +43,7 @@ class Media : ApiScriptableObject(CLASS_NAME) { return null return sessions[0].playbackState?.let { state -> - newObject(PlaybackStateAdapter.CLASS_NAME) { + newObject(PlaybackStateAdapter::class) { init(state, sessions[0].metadata) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Notifications.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/NotificationsService.kt similarity index 88% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/Notifications.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/NotificationsService.kt index 24d9d7a..47af812 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Notifications.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/NotificationsService.kt @@ -6,7 +6,7 @@ import org.mozilla.javascript.Context import org.mozilla.javascript.Function import org.mozilla.javascript.annotations.JSFunction -class Notifications : ApiScriptableObject("Notifications") { +class NotificationsService : ApiScriptableObject(NotificationsService::class) { private lateinit var notifManager: NotificationsManager fun init(notifManager: NotificationsManager) { @@ -17,7 +17,7 @@ class Notifications : ApiScriptableObject("Notifications") { fun addEventListener(event: String, cb: Function) { when (event) { "received" -> addListener(notifManager.notificationReceived, event, cb) { - newObject("Notification") { + newObject(NotificationAdapter::class) { init(it) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Require.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/Require.kt index ace209a..d47c2a3 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Require.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/Require.kt @@ -38,7 +38,7 @@ class Require( return when (className) { "watches" -> { - createInstance(cx, scope) { + createInstance(cx, scope) { init(deps.db, deps.deviceManager) } } @@ -46,7 +46,7 @@ class Require( "notifications" -> { checkPermission(cx, Permission.RECEIVE_NOTIFICATIONS) - createInstance(cx, scope) { + createInstance(cx, scope) { init(deps.notifManager) } } @@ -54,13 +54,13 @@ class Require( "http" -> { checkPermission(cx, Permission.HTTP) - createInstance(cx, scope) { } + createInstance(cx, scope) { } } "volume" -> { checkPermission(cx, Permission.VOLUME_CONTROL) - createInstance(cx, scope) { + createInstance(cx, scope) { init(deps.audioManager) } } @@ -68,7 +68,7 @@ class Require( "media" -> { checkPermission(cx, Permission.MEDIA_CONTROL) - createInstance(cx, scope) { + createInstance(cx, scope) { init(deps.mediaSessionManager) } } @@ -76,7 +76,7 @@ class Require( "location" -> { checkPermission(cx, Permission.LOCATION) - createInstance(cx, scope) { + createInstance(cx, scope) { init(deps.fusedLocationProviderClient) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Volume.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/VolumeService.kt similarity index 89% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/Volume.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/VolumeService.kt index 016e318..9a775ab 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Volume.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/VolumeService.kt @@ -4,7 +4,7 @@ import android.media.AudioManager import net.pipe01.pinepartner.scripting.api.adapters.VolumeStreamAdapter import org.mozilla.javascript.annotations.JSGetter -class Volume : ApiScriptableObject("Volume") { +class VolumeService : ApiScriptableObject(VolumeService::class) { private lateinit var audioManager: AudioManager fun init(audioManager: AudioManager) { @@ -33,7 +33,7 @@ class Volume : ApiScriptableObject("Volume") { fun getAccessibilityStream() = getStream(AudioManager.STREAM_ACCESSIBILITY) private fun getStream(index: Int): VolumeStreamAdapter { - return newObject("VolumeStream").also { + return newObject(VolumeStreamAdapter::class).also { it.init(index, audioManager) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Watches.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/WatchesService.kt similarity index 93% rename from app/src/main/java/net/pipe01/pinepartner/scripting/api/Watches.kt rename to app/src/main/java/net/pipe01/pinepartner/scripting/api/WatchesService.kt index 8cd6a16..ef788d7 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/Watches.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/WatchesService.kt @@ -9,7 +9,7 @@ import org.mozilla.javascript.Function import org.mozilla.javascript.annotations.JSFunction import org.mozilla.javascript.annotations.JSGetter -class Watches : ApiScriptableObject("Watches") { +class WatchesService : ApiScriptableObject(WatchesService::class) { private lateinit var db: AppDatabase private lateinit var deviceManager: DeviceManager @@ -19,7 +19,7 @@ class Watches : ApiScriptableObject("Watches") { } private fun getWatch(dev: Device): WatchAdapter { - return newObject("Watch") { + return newObject(WatchAdapter::class) { init(dev, deviceManager) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLECharacteristicAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLECharacteristicAdapter.kt index 987c96d..1629b45 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLECharacteristicAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLECharacteristicAdapter.kt @@ -18,7 +18,7 @@ import org.mozilla.javascript.typedarrays.NativeUint8Array import java.nio.ByteBuffer import java.nio.ByteOrder -class BLECharacteristicAdapter : ApiScriptableObject("BLECharacteristic") { +class BLECharacteristicAdapter : ApiScriptableObject(BLECharacteristicAdapter::class) { private lateinit var characteristic: ClientBleGattCharacteristic private val subscriptionScopes = mutableMapOf() diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLEServiceAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLEServiceAdapter.kt index c023d83..5958ed3 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLEServiceAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/BLEServiceAdapter.kt @@ -6,7 +6,7 @@ import org.mozilla.javascript.annotations.JSFunction import org.mozilla.javascript.annotations.JSGetter import java.util.UUID -class BLEServiceAdapter : ApiScriptableObject("BLEService") { +class BLEServiceAdapter : ApiScriptableObject(BLEServiceAdapter::class) { private lateinit var service: ClientBleGattService fun init(service: ClientBleGattService) { @@ -21,7 +21,7 @@ class BLEServiceAdapter : ApiScriptableObject("BLEService") { val uuid = UUID.fromString(uuidStr) val characteristic = service.findCharacteristic(uuid) ?: return null - return newObject("BLECharacteristic") { + return newObject(BLECharacteristicAdapter::class) { init(characteristic) } } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/LocationAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/LocationAdapter.kt index 67af6d6..2a36773 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/LocationAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/LocationAdapter.kt @@ -4,11 +4,7 @@ import android.location.Location import net.pipe01.pinepartner.scripting.api.ApiScriptableObject import org.mozilla.javascript.annotations.JSGetter -class LocationAdapter : ApiScriptableObject(CLASS_NAME) { - companion object { - const val CLASS_NAME = "LocationAdapter" - } - +class LocationAdapter : ApiScriptableObject(LocationAdapter::class) { private lateinit var location: Location fun init(location: Location) { diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/NotificationAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/NotificationAdapter.kt index ce179ea..86a889d 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/NotificationAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/NotificationAdapter.kt @@ -4,7 +4,7 @@ import net.pipe01.pinepartner.scripting.api.ApiScriptableObject import net.pipe01.pinepartner.service.Notification import org.mozilla.javascript.annotations.JSGetter -class NotificationAdapter : ApiScriptableObject("Notification") { +class NotificationAdapter : ApiScriptableObject(NotificationAdapter::class) { private lateinit var notification: Notification internal fun init(notification: Notification) { diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/PlaybackStateAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/PlaybackStateAdapter.kt index 7492c76..685ad0e 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/PlaybackStateAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/PlaybackStateAdapter.kt @@ -6,10 +6,8 @@ import net.pipe01.pinepartner.scripting.api.ApiScriptableObject import org.mozilla.javascript.annotations.JSGetter import kotlin.properties.Delegates -class PlaybackStateAdapter : ApiScriptableObject(CLASS_NAME) { +class PlaybackStateAdapter : ApiScriptableObject(PlaybackStateAdapter::class) { companion object { - val CLASS_NAME = "PlaybackState" - private val emptyMetadata = MediaMetadata.Builder().build() } diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/VolumeStreamAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/VolumeStreamAdapter.kt index 2f2ba69..b85cba2 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/VolumeStreamAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/VolumeStreamAdapter.kt @@ -7,7 +7,7 @@ import org.mozilla.javascript.annotations.JSGetter import org.mozilla.javascript.annotations.JSSetter import kotlin.properties.Delegates -class VolumeStreamAdapter : ApiScriptableObject("VolumeStream") { +class VolumeStreamAdapter : ApiScriptableObject(VolumeStreamAdapter::class) { private var volumeStream by Delegates.notNull() private lateinit var audioManager: AudioManager diff --git a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/WatchAdapter.kt b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/WatchAdapter.kt index cd2f1bb..13dc5e8 100644 --- a/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/WatchAdapter.kt +++ b/app/src/main/java/net/pipe01/pinepartner/scripting/api/adapters/WatchAdapter.kt @@ -11,7 +11,7 @@ import java.time.LocalDateTime import java.time.ZoneId import java.util.UUID -class WatchAdapter : ApiScriptableObject("Watch") { +class WatchAdapter : ApiScriptableObject(WatchAdapter::class) { private lateinit var device: Device private lateinit var deviceManager: DeviceManager @@ -61,7 +61,7 @@ class WatchAdapter : ApiScriptableObject("Watch") { fun getService(uuid: String): BLEServiceAdapter? { val service = device.getBLEService(UUID.fromString(uuid)) ?: return null - return newObject("BLEService") { + return newObject(BLEServiceAdapter::class) { init(service) } }