forked from MapComplete/MapComplete
Android: setup data bridge, polyfill geolocation
This commit is contained in:
parent
17450deb82
commit
76e9381650
23 changed files with 187 additions and 106 deletions
|
@ -3,36 +3,62 @@
|
|||
* If this is successful, it will patch some webAPIs
|
||||
*/
|
||||
import { registerPlugin } from "@capacitor/core"
|
||||
|
||||
export class AndroidPolyfill {
|
||||
private readonly databridgePlugin: DatabridgePlugin
|
||||
|
||||
constructor() {
|
||||
this.databridgePlugin = registerPlugin<DatabridgePlugin>("Databridge", {
|
||||
web: () => {
|
||||
return <DatabridgePlugin>{
|
||||
async request(options: { key: string }): Promise<{ value: string }> {
|
||||
return { value: "web" }
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
public async init(){
|
||||
const shell = await this.databridgePlugin.request({ key: "meta" })
|
||||
if(shell.value === "web"){
|
||||
console.log("Not initing Android polyfill; web detected")
|
||||
return
|
||||
}
|
||||
console.log("Detected shell:", shell.value)
|
||||
}
|
||||
|
||||
}
|
||||
import { UIEventSource } from "../UIEventSource"
|
||||
|
||||
export interface DatabridgePlugin {
|
||||
request(options: { key: string }): Promise<{ value: string }>;
|
||||
}
|
||||
|
||||
new AndroidPolyfill().init()
|
||||
const DatabridgePluginSingleton = registerPlugin<DatabridgePlugin>("Databridge", {
|
||||
web: () => {
|
||||
return <DatabridgePlugin>{
|
||||
async request(options: { key: string }): Promise<{ value: string }> {
|
||||
return { value: "web" }
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export class AndroidPolyfill {
|
||||
private readonly databridgePlugin: DatabridgePlugin = DatabridgePluginSingleton
|
||||
|
||||
/**
|
||||
* Registers 'navigator.'
|
||||
* @private
|
||||
*/
|
||||
private backfillGeolocation(databridgePlugin: DatabridgePlugin) {
|
||||
const origQueryFunc = navigator?.permissions?.query
|
||||
navigator.permissions.query = async (descr: PermissionDescriptor) => {
|
||||
if (descr.name === "geolocation") {
|
||||
console.log("Got a geolocation permission request")
|
||||
const src = UIEventSource.FromPromise(databridgePlugin.request({ key: "location:request-permission" }))
|
||||
|
||||
return <PermissionStatus>{
|
||||
state: undefined,
|
||||
addEventListener(key: "change", f: (value: "granted" | "denied") => void) {
|
||||
src.addCallbackAndRunD(v => {
|
||||
const content = <"granted" | "denied">v.value
|
||||
f(content)
|
||||
return true
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
if (origQueryFunc) {
|
||||
return await origQueryFunc(descr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async init() {
|
||||
console.log("Sniffing shell version")
|
||||
const shell = await this.databridgePlugin.request({ key: "meta" })
|
||||
if (shell.value === "web") {
|
||||
console.log("Not initing Android polyfill as not in a shell; web detected")
|
||||
return
|
||||
}
|
||||
console.log("Detected shell:", shell.value)
|
||||
this.backfillGeolocation(this.databridgePlugin)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue