Skip to content

Commit 3596d44

Browse files
committed
implement fullscreen setting and view
1 parent 009886d commit 3596d44

7 files changed

Lines changed: 135 additions & 5 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
tools:targetApi="31">
2525
<activity
2626
android:name=".MainActivity"
27-
android:exported="true"
28-
android:label="@string/app_name">
27+
android:exported="true">
2928
<intent-filter>
3029
<action android:name="android.intent.action.MAIN" />
3130
<category android:name="android.intent.category.LAUNCHER" />
3231
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
3332
</intent-filter>
3433
</activity>
34+
<activity
35+
android:name=".ui.fullscreen.WebViewActivity"
36+
android:exported="true"
37+
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
38+
</activity>
3539
</application>
3640

3741
</manifest>

app/src/main/java/com/caniwebview/android/ui/config/ConfigFragment.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class ConfigFragment : Fragment() {
8686
}
8787

8888
// Settings
89+
binding.fullscreenWebViewCheckBox.setOnCheckedChangeListener { _, isChecked ->
90+
saveSetting("fullscreenWebView", isChecked)
91+
}
8992
binding.javaScriptEnabledCheckBox.setOnCheckedChangeListener { _, isChecked ->
9093
saveSetting("javaScriptEnabled", isChecked)
9194
}
@@ -102,11 +105,12 @@ class ConfigFragment : Fragment() {
102105
}
103106

104107
private fun loadSettings() {
108+
binding.fullscreenWebViewCheckBox.isChecked = sharedPreferences.getBoolean("fullscreenWebView", false)
105109
binding.javaScriptEnabledCheckBox.isChecked = sharedPreferences.getBoolean("javaScriptEnabled", true)
106110
binding.javaScriptCanOpenWindowsCheckBox.isChecked = sharedPreferences.getBoolean("javaScriptCanOpenWindows", true)
107111
binding.domStorageEnabledCheckBox.isChecked = sharedPreferences.getBoolean("domStorageEnabled", true)
108112
binding.geolocationEnabledCheckBox.isChecked = sharedPreferences.getBoolean("geolocationEnabled", true)
109-
// Load other settings here...
113+
// Load other settings here..
110114
}
111115

112116
private fun saveSetting(key: String, value: Boolean) {
@@ -125,12 +129,14 @@ class ConfigFragment : Fragment() {
125129
binding.geolocationEnabledCheckBox.isChecked = true
126130
}
127131
"none" -> {
132+
binding.fullscreenWebViewCheckBox.isChecked = false
128133
binding.javaScriptEnabledCheckBox.isChecked = false
129134
binding.javaScriptCanOpenWindowsCheckBox.isChecked = false
130135
binding.domStorageEnabledCheckBox.isChecked = false
131136
binding.geolocationEnabledCheckBox.isChecked = false
132137
}
133138
"cordova" -> {
139+
binding.fullscreenWebViewCheckBox.isChecked = true
134140
binding.javaScriptEnabledCheckBox.isChecked = true
135141
binding.javaScriptCanOpenWindowsCheckBox.isChecked = true
136142
binding.domStorageEnabledCheckBox.isChecked = true
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.caniwebview.android.ui.fullscreen
2+
3+
import android.annotation.SuppressLint
4+
import android.content.SharedPreferences
5+
import android.os.Bundle
6+
import android.webkit.WebResourceRequest
7+
import android.webkit.WebResourceResponse
8+
import android.webkit.WebSettings
9+
import android.webkit.WebView
10+
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.webkit.WebViewAssetLoader
12+
import androidx.webkit.WebViewClientCompat
13+
import com.caniwebview.android.databinding.FullscreenWebviewBinding
14+
15+
class WebViewActivity : AppCompatActivity() {
16+
17+
private var _binding: FullscreenWebviewBinding? = null
18+
private val binding get() = _binding!!
19+
private lateinit var sharedPreferences: SharedPreferences
20+
private lateinit var webView: WebView
21+
22+
@SuppressLint("SetJavaScriptEnabled")
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
_binding = FullscreenWebviewBinding.inflate(layoutInflater)
26+
setContentView(binding.root)
27+
28+
sharedPreferences = getSharedPreferences("WebViewSettings", MODE_PRIVATE)
29+
30+
webView = binding.webView
31+
applyWebViewSettings(webView.settings)
32+
33+
// Create the asset loader
34+
// Create the asset loader with a custom domain
35+
val assetLoader = WebViewAssetLoader.Builder()
36+
.setDomain("caniwebview.local") // Set your custom domain here
37+
.setHttpAllowed(false) // Enforce HTTPS (recommended)
38+
.addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
39+
.build()
40+
41+
42+
// Set the WebViewClient
43+
webView.webViewClient = object : WebViewClientCompat() {
44+
override fun shouldInterceptRequest(
45+
view: WebView,
46+
request: WebResourceRequest
47+
): WebResourceResponse? {
48+
return assetLoader.shouldInterceptRequest(request.url)
49+
}
50+
}
51+
52+
// Load the URL passed from the intent
53+
val url = intent.getStringExtra("url")
54+
if (url != null) {
55+
webView.loadUrl(url)
56+
}
57+
}
58+
59+
override fun onResume() {
60+
super.onResume()
61+
applyWebViewSettings(webView.settings)
62+
}
63+
64+
private fun applyWebViewSettings(settings: WebSettings) {
65+
settings.javaScriptEnabled = sharedPreferences.getBoolean("javaScriptEnabled", true)
66+
settings.javaScriptCanOpenWindowsAutomatically =
67+
sharedPreferences.getBoolean("javaScriptCanOpenWindows", true)
68+
settings.domStorageEnabled = sharedPreferences.getBoolean("domStorageEnabled", true)
69+
settings.setGeolocationEnabled(sharedPreferences.getBoolean("geolocationEnabled", true))
70+
// Apply other settings here...
71+
settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
72+
settings.saveFormData = false
73+
settings.mediaPlaybackRequiresUserGesture = false
74+
}
75+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.caniwebview.android.ui.webview
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5+
import android.content.Intent
56
import android.content.SharedPreferences
67
import android.os.Bundle
78
import android.view.LayoutInflater
@@ -13,10 +14,11 @@ import android.webkit.WebSettings
1314
import android.webkit.WebView
1415
import android.widget.Button
1516
import android.widget.EditText
16-
import androidx.core.view.isVisible
1717
import androidx.fragment.app.Fragment
1818
import androidx.webkit.WebViewAssetLoader
1919
import androidx.webkit.WebViewClientCompat
20+
import com.caniwebview.android.R
21+
import com.caniwebview.android.ui.fullscreen.WebViewActivity
2022
import com.caniwebview.android.databinding.FragmentWebviewBinding
2123

2224
class WebViewFragment : Fragment() {
@@ -84,7 +86,15 @@ class WebViewFragment : Fragment() {
8486
putString("url", url)
8587
apply()
8688
}
87-
webView.loadUrl(url)
89+
90+
if (sharedPreferences.getBoolean("fullscreenWebView", false)) {
91+
val intent = Intent(requireContext(), WebViewActivity::class.java)
92+
intent.putExtra("url", url)
93+
startActivity(intent)
94+
} else {
95+
webView.loadUrl(url)
96+
urlBar.text = url
97+
}
8898
}
8999
}
90100

@@ -111,6 +121,13 @@ class WebViewFragment : Fragment() {
111121
settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
112122
settings.saveFormData = false
113123
settings.mediaPlaybackRequiresUserGesture = false
124+
125+
// Change label of load button if fullscreen is enabled
126+
if (sharedPreferences.getBoolean("fullscreenWebView", false)) {
127+
binding.loadButton.text = getString(R.string.fullscreen)
128+
} else {
129+
binding.loadButton.text = getString(R.string.load)
130+
}
114131
}
115132

116133
override fun onDestroyView() {

app/src/main/res/layout/fragment_config.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949

5050
</LinearLayout>
5151

52+
<CheckBox
53+
android:id="@+id/fullscreenWebViewCheckBox"
54+
android:layout_width="match_parent"
55+
android:layout_height="wrap_content"
56+
android:text="@string/fullscreen_webview" />
57+
5258
<CheckBox
5359
android:id="@+id/javaScriptEnabledCheckBox"
5460
android:layout_width="match_parent"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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">
8+
9+
<!-- Define your new layout here -->
10+
11+
<WebView
12+
android:id="@+id/webView"
13+
android:layout_width="0dp"
14+
android:layout_height="0dp"
15+
app:layout_constraintTop_toTopOf="parent"
16+
app:layout_constraintBottom_toBottomOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintEnd_toEndOf="parent" />
19+
20+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
<string name="none">None</string>
1616
<string name="all">All</string>
1717
<string name="presets">Presets</string>
18+
<string name="fullscreen_webview">Fullscreen WebView</string>
19+
<string name="fullscreen">Fullscreen</string>
1820
</resources>

0 commit comments

Comments
 (0)