2024-11-18 20:43:19 +01:00
|
|
|
/**
|
|
|
|
* The Android Polyfill will attempt to communicate with the Anrdoid Shell.
|
|
|
|
* If this is successful, it will patch some webAPIs
|
|
|
|
*/
|
|
|
|
import { registerPlugin } from "@capacitor/core"
|
2024-12-12 00:46:24 +01:00
|
|
|
import { Store, UIEventSource } from "../UIEventSource"
|
|
|
|
import { OsmConnection } from "../Osm/OsmConnection"
|
2024-12-10 04:28:50 +01:00
|
|
|
|
|
|
|
export interface DatabridgePlugin {
|
2024-12-19 13:49:14 +01:00
|
|
|
request<T extends (string | object) = string | object>(options: { key: string }): Promise<{ value: T }>;
|
2024-12-10 04:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const DatabridgePluginSingleton = registerPlugin<DatabridgePlugin>("Databridge", {
|
|
|
|
web: () => {
|
|
|
|
return <DatabridgePlugin>{
|
2024-12-12 00:46:24 +01:00
|
|
|
async request(options: { key: string }): Promise<{ value: string | object }> {
|
2024-12-10 04:28:50 +01:00
|
|
|
return { value: "web" }
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2024-11-18 20:43:19 +01:00
|
|
|
|
|
|
|
export class AndroidPolyfill {
|
2024-12-10 04:28:50 +01:00
|
|
|
private readonly databridgePlugin: DatabridgePlugin = DatabridgePluginSingleton
|
2024-12-12 00:46:24 +01:00
|
|
|
private static readonly _inAndroid: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
|
|
|
public static readonly inAndroid: Store<boolean> = AndroidPolyfill._inAndroid
|
|
|
|
private static readonly _geolocationPermission: UIEventSource<"granted" | "denied" | "prompt"> = new UIEventSource("prompt")
|
|
|
|
public static readonly geolocationPermission: Store<"granted" | "denied" | "prompt"> = this._geolocationPermission
|
2024-12-10 04:28:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers 'navigator.'
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private backfillGeolocation(databridgePlugin: DatabridgePlugin) {
|
|
|
|
const origQueryFunc = navigator?.permissions?.query
|
2024-12-12 00:46:24 +01:00
|
|
|
const src = UIEventSource.FromPromise(databridgePlugin.request({ key: "location:request-permission" }))
|
|
|
|
src.addCallbackAndRunD(permission => {
|
|
|
|
AndroidPolyfill._geolocationPermission.set(<"granted" | "denied">permission.value)
|
|
|
|
})
|
2024-11-18 20:43:19 +01:00
|
|
|
}
|
|
|
|
|
2024-12-10 04:28:50 +01:00
|
|
|
public async init() {
|
|
|
|
console.log("Sniffing shell version")
|
2024-11-18 20:43:19 +01:00
|
|
|
const shell = await this.databridgePlugin.request({ key: "meta" })
|
2024-12-10 04:28:50 +01:00
|
|
|
if (shell.value === "web") {
|
|
|
|
console.log("Not initing Android polyfill as not in a shell; web detected")
|
2024-11-18 20:43:19 +01:00
|
|
|
return
|
|
|
|
}
|
2024-12-12 00:46:24 +01:00
|
|
|
AndroidPolyfill._inAndroid.set(true)
|
2024-11-18 20:43:19 +01:00
|
|
|
console.log("Detected shell:", shell.value)
|
2024-12-10 04:28:50 +01:00
|
|
|
this.backfillGeolocation(this.databridgePlugin)
|
2024-11-18 20:43:19 +01:00
|
|
|
}
|
|
|
|
|
2024-12-31 19:55:08 +01:00
|
|
|
public static async requestLoginCodes() {
|
2024-12-19 13:49:14 +01:00
|
|
|
const result = await DatabridgePluginSingleton.request<{oauth_token: string}>({ key: "request:login" })
|
|
|
|
const token: string = result.value.oauth_token
|
2024-12-31 19:55:08 +01:00
|
|
|
console.log("AndroidPolyfill: received oauth_token; trying to pass them to the oauth lib",token)
|
|
|
|
return token
|
2024-12-12 00:46:24 +01:00
|
|
|
}
|
2024-11-18 20:43:19 +01:00
|
|
|
}
|
2024-12-19 13:49:14 +01:00
|
|
|
|