-
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android): generate kotlin files at build time (#671)
* feat(android): generate kotlin files at build time * changefile * Update kotlin-files.md * fix android ci * Add option for extra code when generating files * rerun build script when env changes * change it to a class code, prepare var name for future additions * Delete MainActivity.kt * uppercase [skip ci] * fix android detection Co-authored-by: Lucas Nogueira <[email protected]>
- Loading branch information
1 parent
1b26d60
commit b478903
Showing
7 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wry": "minor" | ||
--- | ||
|
||
WRY will now generate the needed kotlin files at build time but you need to set `WRY_ANDROID_REVERSED_DOMAIN`, `WRY_ANDROID_APP_NAME_SNAKE_CASE` and `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` env vars. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package {{app-domain-reversed}}.{{app-name-snake-case}} | ||
|
||
import android.webkit.* | ||
|
||
class Ipc { | ||
@JavascriptInterface | ||
fun postMessage(message: String) { | ||
this.ipc(message) | ||
} | ||
|
||
companion object { | ||
init { | ||
System.loadLibrary("{{app-name-snake-case}}") | ||
} | ||
} | ||
|
||
private external fun ipc(message: String) | ||
|
||
{{class-extension}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package {{app-domain-reversed}}.{{app-name-snake-case}} | ||
|
||
import android.graphics.Bitmap | ||
import android.webkit.* | ||
|
||
class RustWebViewClient(initScripts: Array<String>): WebViewClient() { | ||
private val initializationScripts: Array<String> | ||
|
||
init { | ||
initializationScripts = initScripts | ||
} | ||
|
||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { | ||
for (script in initializationScripts) { | ||
view?.evaluateJavascript(script, null) | ||
} | ||
super.onPageStarted(view, url, favicon) | ||
} | ||
|
||
override fun shouldInterceptRequest( | ||
view: WebView, | ||
request: WebResourceRequest | ||
): WebResourceResponse? { | ||
return handleRequest(request) | ||
} | ||
|
||
companion object { | ||
init { | ||
System.loadLibrary("{{app-name-snake-case}}") | ||
} | ||
} | ||
|
||
private external fun handleRequest(request: WebResourceRequest): WebResourceResponse? | ||
|
||
{{class-extension}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package {{app-domain-reversed}}.{{app-name-snake-case}} | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
abstract class TauriActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
create(this) | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
start() | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
resume() | ||
} | ||
|
||
override fun onPause() { | ||
super.onPause() | ||
pause() | ||
} | ||
|
||
override fun onStop() { | ||
super.onStop() | ||
stop() | ||
} | ||
|
||
override fun onWindowFocusChanged(hasFocus: Boolean) { | ||
super.onWindowFocusChanged(hasFocus) | ||
focus(hasFocus) | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
save() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
destroy() | ||
} | ||
|
||
override fun onLowMemory() { | ||
super.onLowMemory() | ||
memory() | ||
} | ||
|
||
fun getAppClass(name: String): Class<*> { | ||
return Class.forName(name) | ||
} | ||
|
||
companion object { | ||
init { | ||
System.loadLibrary("{{app-name-snake-case}}") | ||
} | ||
} | ||
|
||
private external fun create(activity: TauriActivity) | ||
private external fun start() | ||
private external fun resume() | ||
private external fun pause() | ||
private external fun stop() | ||
private external fun save() | ||
private external fun destroy() | ||
private external fun memory() | ||
private external fun focus(focus: Boolean) | ||
|
||
{{class-extension}} | ||
} |