Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send js messages #2

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
android:supportsRtl="true"
android:theme="@style/Theme.Material3.DayNight"
tools:ignore="DataExtractionRules">
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="false"
android:label="@string/title_activity_fullscreen"
android:theme="@style/Theme.Material3.DayNight.Fullscreen"/>
<activity
android:name=".WebViewActivity"
android:exported="false"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.g00fy2.quickiesample
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat

public class ResultActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val bundle = intent.extras
val postMsg = bundle?.getString("postMsg")

val myTextView: TextView = findViewById(R.id.textView)
myTextView.text = postMsg
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,84 @@
package io.github.g00fy2.quickiesample

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat

class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
public class WebViewActivity : AppCompatActivity() {

class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return false
}
}

@JavascriptInterface
public fun msg(): String {
return "From Android!"
}

@JavascriptInterface
public fun postMsg(msg: String) {
val resultActivityIntent = Intent(this, ResultActivity::class.java)
resultActivityIntent.putExtra("postMsg", msg);
startActivity(resultActivityIntent)
}

override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Hide the system bars.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
// Hide top app name bar
supportActionBar?.hide()

val bundle = intent.extras
val url = bundle?.getString("url")
val url = bundle!!.getString("url")

val myWebView: WebView = findViewById(R.id.webview)
myWebView.webViewClient = WebViewClient()

val wvSettings = myWebView.getSettings()
@SuppressLint("SetJavaScriptEnabled")
wvSettings.javaScriptEnabled = true
wvSettings.domStorageEnabled = true
//wvSettings.loadWithOverviewMode = true
//wvSettings.useWideViewPort = true
//wvSettings.builtInZoomControls = true
//wvSettings.displayZoomControls = true
//wvSettings.defaultTextEncodingName = "utf-8"

myWebView.addJavascriptInterface(this, "Android")

//myWebView.loadUrl("https://docusign.github.io/examples/androidTest.html")
myWebView.loadUrl(url!!)

// REMOVE Uses url to stop lint errors if url is not used above
val resultActivityIntent = Intent(this, ResultActivity::class.java)
resultActivityIntent.putExtra("foo", url);


//myWebView.loadUrl("https://xerox.com?$url")
}




}
}
17 changes: 17 additions & 0 deletions sample/src/main/res/layout/activity_result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"
>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="136dp"
android:gravity="center"
/>
</LinearLayout>
11 changes: 6 additions & 5 deletions sample/src/main/res/layout/activity_web_view.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/fullscreenBackgroundColor"
android:theme="@style/ThemeOverlay.Android_QR_Webview.FullscreenContainer"
android:id="@+id/main"
tools:context=".WebViewActivity">

<WebView
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
7 changes: 7 additions & 0 deletions sample/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<resources>

<style name="ThemeOverlay.Android_QR_Webview.FullscreenContainer" parent="">
<item name="fullscreenBackgroundColor">@color/light_blue_900</item>
<item name="fullscreenTextColor">@color/light_blue_A400</item>
</style>
</resources>
6 changes: 6 additions & 0 deletions sample/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<declare-styleable name="FullscreenAttrs">
<attr name="fullscreenBackgroundColor" format="color" />
<attr name="fullscreenTextColor" format="color" />
</declare-styleable>
</resources>
7 changes: 6 additions & 1 deletion sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#3DDC84</color>
<color name="ic_launcher_background">#3DDC84</color>
<color name="light_blue_600">#FF039BE5</color>
<color name="light_blue_900">#FF01579B</color>
<color name="light_blue_A200">#FF40C4FF</color>
<color name="light_blue_A400">#FF00B0FF</color>
<color name="black_overlay">#66000000</color>
</resources>
3 changes: 3 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<string name="scan_barcode" translatable="false">Scan barcode</string>
<string name="barcode_format" translatable="false">BarcodeFormat</string>
<string name="title" tools:ignore="MissingTranslation">This app opens the QR-Code URL in a Webview</string>
<string name="title_activity_fullscreen">FullscreenActivity</string>
<string name="dummy_button">Dummy Button</string>
<string name="dummy_content">DUMMY\nCONTENT</string>
</resources>
11 changes: 11 additions & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<style name="Widget.Theme.Material3.DayNight.ActionBar.Fullscreen" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>

<style name="Widget.Theme.Material3.DayNight.ButtonBar.Fullscreen" parent="">
<item name="android:background">@color/black_overlay</item>
<item name="android:buttonBarStyle">?android:attr/buttonBarStyle</item>
</style>
</resources>
15 changes: 15 additions & 0 deletions sample/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<resources>

<style name="Theme.Material3.DayNight.Fullscreen" parent="Theme.Material3.DayNight">
<item name="android:actionBarStyle">
@style/Widget.Theme.Material3.DayNight.ActionBar.Fullscreen
</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
</style>

<style name="ThemeOverlay.Android_QR_Webview.FullscreenContainer" parent="">
<item name="fullscreenBackgroundColor">@color/light_blue_600</item>
<item name="fullscreenTextColor">@color/light_blue_A200</item>
</style>
</resources>
Loading