Skip to content

Commit

Permalink
Rename API services with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Mar 23, 2024
1 parent d88ee93 commit 0be46fd
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 57 deletions.
2 changes: 1 addition & 1 deletion api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ declare interface MediaService {
}

declare interface LocationService {
get location(): Location
get current(): Location
}

declare function require(module: "watches"): WatchesService;
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +30,7 @@ abstract class ApiScriptableObject(private val className: String) : ScriptableOb
private val listeners = mutableMapOf<Pair<String, Function>, () -> Unit>()
private val children = mutableListOf<Finalizeable>()

override fun getClassName() = className
override fun getClassName() = clazz.simpleName!!

fun initSuper(contextFactory: ContextFactory, dispatcher: CoroutineDispatcher, onEvent: OnLogEvent) {
this.contextFactory = contextFactory
Expand Down Expand Up @@ -97,11 +98,11 @@ abstract class ApiScriptableObject(private val className: String) : ScriptableOb
}
}

fun <T : ApiScriptableObject> newObject(name: String, init: (T.() -> Unit)? = null): T {
fun <T : ApiScriptableObject> newObject(clazz: KClass<T>, 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,7 +24,7 @@ class Location : ApiScriptableObject(CLASS_NAME) {
task.await()
}

return newObject<LocationAdapter>(LocationAdapter.CLASS_NAME).apply {
return newObject(LocationAdapter::class).apply {
init(location)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -17,7 +17,7 @@ class Notifications : ApiScriptableObject("Notifications") {
fun addEventListener(event: String, cb: Function) {
when (event) {
"received" -> addListener(notifManager.notificationReceived, event, cb) {
newObject<NotificationAdapter>("Notification") {
newObject(NotificationAdapter::class) {
init(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,45 @@ class Require(

return when (className) {
"watches" -> {
createInstance<Watches>(cx, scope) {
createInstance<WatchesService>(cx, scope) {
init(deps.db, deps.deviceManager)
}
}

"notifications" -> {
checkPermission(cx, Permission.RECEIVE_NOTIFICATIONS)

createInstance<Notifications>(cx, scope) {
createInstance<NotificationsService>(cx, scope) {
init(deps.notifManager)
}
}

"http" -> {
checkPermission(cx, Permission.HTTP)

createInstance<HTTP>(cx, scope) { }
createInstance<HTTPService>(cx, scope) { }
}

"volume" -> {
checkPermission(cx, Permission.VOLUME_CONTROL)

createInstance<Volume>(cx, scope) {
createInstance<VolumeService>(cx, scope) {
init(deps.audioManager)
}
}

"media" -> {
checkPermission(cx, Permission.MEDIA_CONTROL)

createInstance<Media>(cx, scope) {
createInstance<MediaService>(cx, scope) {
init(deps.mediaSessionManager)
}
}

"location" -> {
checkPermission(cx, Permission.LOCATION)

createInstance<Location>(cx, scope) {
createInstance<LocationService>(cx, scope) {
init(deps.fusedLocationProviderClient)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -33,7 +33,7 @@ class Volume : ApiScriptableObject("Volume") {
fun getAccessibilityStream() = getStream(AudioManager.STREAM_ACCESSIBILITY)

private fun getStream(index: Int): VolumeStreamAdapter {
return newObject<VolumeStreamAdapter>("VolumeStream").also {
return newObject(VolumeStreamAdapter::class).also {
it.init(index, audioManager)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,7 +19,7 @@ class Watches : ApiScriptableObject("Watches") {
}

private fun getWatch(dev: Device): WatchAdapter {
return newObject("Watch") {
return newObject(WatchAdapter::class) {
init(dev, deviceManager)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function, CoroutineScope>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>()
private lateinit var audioManager: AudioManager

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 0be46fd

Please sign in to comment.