Skip to content

Commit 142b980

Browse files
committed
implement WebViewAssetloader
1 parent 0a91c50 commit 142b980

4 files changed

Lines changed: 92 additions & 35 deletions

File tree

.idea/deploymentTargetSelector.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/assets/html/index.html

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,39 @@ <h1>CanIWebView Test App</h1>
1111
</a>
1212
</p>
1313

14+
<h2>Tools</h2>
1415

1516
<b>
16-
<a href="https://caniwebview.com/app">Check out caniwebview.com</a>
17+
<a href="https://caniwebview.com/app">Check out data on caniwebview.com</a>
1718
</b>
19+
20+
<p>
21+
<a href="https://mdn-bcd-collector.gooborg.com/">BCD Collector</a>
22+
</p>
23+
24+
<button onclick="corsTest()">
25+
CORS Test Request
26+
</button>
27+
28+
<script type="text/javascript">
29+
function corsTest() {
30+
fetch('https://caniwebview.merz.workers.dev/',
31+
{
32+
method: 'POST',
33+
mode: 'cors',
34+
headers: {
35+
'Cookie': 'test=android'
36+
}
37+
})
38+
.then(response => response.json())
39+
.then(data => {
40+
alert(data);
41+
console.log(data);
42+
})
43+
.catch(error => {
44+
alert('CORS request failed');
45+
console.error(error);
46+
});
47+
}
48+
</script>
1849
</body>

app/src/main/java/com/caniwebview/android/ui/webview/WebViewFragment.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.webkit.WebSettings
1313
import android.webkit.WebView
1414
import android.widget.Button
1515
import android.widget.EditText
16+
import androidx.core.view.isVisible
1617
import androidx.fragment.app.Fragment
1718
import androidx.webkit.WebViewAssetLoader
1819
import androidx.webkit.WebViewClientCompat
@@ -40,6 +41,8 @@ class WebViewFragment : Fragment() {
4041
webView = binding.webView
4142
val urlEditText: EditText = binding.urlEditText
4243
val loadButton: Button = binding.loadButton
44+
val urlBar = binding.urlBar
45+
4346

4447
// Create the asset loader
4548
// Create the asset loader with a custom domain
@@ -58,6 +61,12 @@ class WebViewFragment : Fragment() {
5861
): WebResourceResponse? {
5962
return assetLoader.shouldInterceptRequest(request.url)
6063
}
64+
65+
override fun onPageFinished(view: WebView?, url: String?) {
66+
super.onPageFinished(view, url)
67+
// Show the current URL in the URL bar
68+
urlBar.text = url
69+
}
6170
}
6271

6372
// Load url in field if saved
Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
1-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
android:layout_width="match_parent"
3-
android:layout_height="match_parent"
4-
android:orientation="vertical">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".ui.webview.WebViewFragment">
58

6-
<LinearLayout
7-
android:layout_width="match_parent"
8-
android:layout_height="wrap_content"
9-
android:orientation="horizontal">
9+
<EditText
10+
android:id="@+id/urlEditText"
11+
android:layout_width="0dp"
12+
android:layout_height="wrap_content"
13+
android:layout_marginStart="8dp"
14+
android:layout_marginTop="8dp"
15+
android:layout_marginEnd="8dp"
16+
android:ems="10"
17+
android:hint="@string/enter_url"
18+
android:inputType="textUri"
19+
app:layout_constraintEnd_toStartOf="@+id/loadButton"
20+
app:layout_constraintStart_toStartOf="parent"
21+
app:layout_constraintTop_toTopOf="parent" />
1022

11-
<EditText
12-
android:id="@+id/url_edit_text"
13-
android:layout_width="0dp"
14-
android:layout_height="wrap_content"
15-
android:layout_weight="1"
16-
android:autofillHints="url"
17-
android:hint="@string/enter_url"
18-
android:inputType="textUri"
19-
android:minHeight="48dp"
20-
android:padding="8dp"
21-
android:text="@string/default_url" />
23+
<Button
24+
android:id="@+id/loadButton"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginEnd="8dp"
28+
android:text="@string/load"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintTop_toTopOf="@+id/urlEditText" />
2231

23-
<Button
24-
android:id="@+id/load_button"
25-
android:layout_width="wrap_content"
26-
android:layout_height="wrap_content"
27-
android:text="@string/load"
28-
android:padding="8dp" />
29-
</LinearLayout>
32+
<WebView
33+
android:id="@+id/webView"
34+
android:layout_width="0dp"
35+
android:layout_height="0dp"
36+
android:layout_marginTop="8dp"
37+
app:layout_constraintBottom_toTopOf="@+id/urlBar"
38+
app:layout_constraintEnd_toEndOf="parent"
39+
app:layout_constraintStart_toStartOf="parent"
40+
app:layout_constraintTop_toBottomOf="@+id/urlEditText" />
3041

31-
<WebView
32-
android:id="@+id/web_view"
33-
android:layout_width="match_parent"
34-
android:layout_height="0dp"
35-
android:layout_weight="1" />
42+
<TextView
43+
android:id="@+id/urlBar"
44+
android:layout_width="0dp"
45+
android:layout_height="wrap_content"
46+
android:background="#EEEEEE"
47+
android:padding="8dp"
48+
android:textAlignment="center"
49+
android:visibility="gone"
50+
app:layout_constraintBottom_toBottomOf="parent"
51+
app:layout_constraintEnd_toEndOf="parent"
52+
app:layout_constraintStart_toStartOf="parent" />
3653

37-
</LinearLayout>
54+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)