11package com.example.deeplviewer.activity
22
3- import android.annotation.SuppressLint
43import android.content.Context
54import android.content.Intent
65import android.net.Uri
76import android.os.Bundle
7+ import android.util.Log
88import android.view.animation.AlphaAnimation
9- import android.webkit.WebSettings
109import android.webkit.WebView
1110import android.widget.ImageButton
1211import androidx.appcompat.app.AppCompatActivity
1312import com.example.deeplviewer.helper.CookieManagerHelper
1413import com.example.deeplviewer.webview.MyWebViewClient
1514import com.example.deeplviewer.R
15+ import com.example.deeplviewer.config.WebViewConfig
16+ import com.example.deeplviewer.helper.WebViewUrlHelper
1617import com.example.deeplviewer.webview.WebAppInterface
1718
1819class MainActivity : AppCompatActivity () {
20+ private lateinit var webView: WebView
1921 private lateinit var webViewClient: MyWebViewClient
22+
2023 private val startUrl by lazy {
21- return @lazy ORIGIN_URL + getSharedPreferences(" config" , Context .MODE_PRIVATE ).getString(
24+ ORIGIN_URL + getSharedPreferences(" config" , Context .MODE_PRIVATE ).getString(
2225 " urlParam" ,
2326 DEFAULT_PARAM
2427 )
@@ -32,56 +35,109 @@ class MainActivity : AppCompatActivity() {
3235 override fun onCreate (savedInstanceState : Bundle ? ) {
3336 super .onCreate(savedInstanceState)
3437 setContentView(R .layout.activity_main)
35- createWebView(intent, savedInstanceState)
3638
37- findViewById<ImageButton >(R .id.settingButton).setOnClickListener {
38- val intent = Intent (this , SettingsActivity ::class .java)
39- startActivity(intent)
40- }
39+ initializeWebView()
40+ createWebView(intent, savedInstanceState)
41+ setupSettingsButton()
4142 }
4243
4344 override fun onNewIntent (intent : Intent ? ) {
4445 super .onNewIntent(intent)
4546 createWebView(intent)
4647 }
4748
48- @SuppressLint(" SetJavaScriptEnabled" , " AddJavascriptInterface" )
49+ override fun onSaveInstanceState (outState : Bundle ) {
50+ super .onSaveInstanceState(outState)
51+ saveWebViewState(outState)
52+ }
53+
54+ override fun onDestroy () {
55+ if (::webView.isInitialized) {
56+ // Remove the WebView from its parent view and destroy it
57+ (webView.parent as ? android.view.ViewGroup )?.removeView(webView)
58+ webView.destroy()
59+ }
60+ super .onDestroy()
61+ }
62+
63+ /* *
64+ * Creates and configures the WebView with the provided intent or saved instance state
65+ */
4966 private fun createWebView (intent : Intent ? , savedInstanceState : Bundle ? = null) {
50- val floatingText = intent?.getStringExtra(" FLOATING_TEXT" )
51- val shareText = intent?.getStringExtra(Intent .EXTRA_TEXT )
52- val savedText = savedInstanceState?.getString(" SavedText" )
53- val receivedText = savedText ? : (floatingText ? : (shareText ? : " " ))
67+ val receivedText = extractReceivedText(intent, savedInstanceState)
5468
55- val webView: WebView = findViewById(R .id.webview)
69+ setupWebViewClient()
70+ setupCookies()
71+
72+ // Add JavaScript interface for communication
73+ webView.addJavascriptInterface(WebAppInterface (this ), " Android" )
74+
75+ // Load the initial URL
76+ val targetUrl = WebViewUrlHelper .buildUrl(startUrl, receivedText)
77+ webView.loadUrl(targetUrl)
78+ }
79+
80+ /* *
81+ * Initializes the WebView
82+ */
83+ private fun initializeWebView () {
84+ webView = findViewById(R .id.webview)
85+ webView.alpha = 0.0F
86+ WebViewConfig .applyBasicSettings(webView)
87+ WebViewConfig .applyOptimizedSettings(webView)
88+ }
89+
90+ /* *
91+ * Extracts the text from the intent or saved instance state
92+ */
93+ private fun extractReceivedText (intent : Intent ? , savedInstanceState : Bundle ? ): String {
94+ return savedInstanceState?.getString(" SavedText" )
95+ ? : intent?.getStringExtra(" FLOATING_TEXT" )
96+ ? : intent?.getStringExtra(Intent .EXTRA_TEXT )
97+ ? : " "
98+ }
99+
100+ /* *
101+ * Sets up the WebViewClient to handle page loading and animations
102+ */
103+ private fun setupWebViewClient () {
56104 webViewClient = MyWebViewClient (this )
57105 webViewClient.loadFinishedListener = {
58- val animation = AlphaAnimation (0.0F , 1.0F )
59- animation.duration = 100
106+ val animation = AlphaAnimation (0.0F , 1.0F ).apply { duration = 100 }
60107 webView.startAnimation(animation)
61108 webView.alpha = 1.0F
62109 }
110+ webView.webViewClient = webViewClient
111+ }
63112
64- CookieManagerHelper ().migrateCookie(this )
65- CookieManagerHelper ().addPrivacyCookie()
66-
67- webView.settings.javaScriptEnabled = true
68- webView.settings.domStorageEnabled = true
69- webView.settings.cacheMode = WebSettings .LOAD_CACHE_ELSE_NETWORK
113+ /* *
114+ * Sets up cookies for the WebView
115+ */
116+ private fun setupCookies () {
117+ try {
118+ CookieManagerHelper ().apply {
119+ migrateCookie(this @MainActivity)
120+ addPrivacyCookie()
121+ }
122+ } catch (e: Exception ) {
123+ Log .w(" MainActivity" , " Cookie setup failed" , e)
124+ }
125+ }
70126
71- webView.webViewClient = webViewClient
72- webView.addJavascriptInterface(WebAppInterface (this ), " Android" )
73- webView.loadUrl(
74- startUrl + Uri .encode(
75- receivedText.replace(
76- " /" ,
77- " \\ /"
78- )
79- )
80- )
127+ /* *
128+ * Sets up the settings button to open the SettingsActivity
129+ */
130+ private fun setupSettingsButton () {
131+ findViewById<ImageButton >(R .id.settingButton).setOnClickListener {
132+ val intent = Intent (this , SettingsActivity ::class .java)
133+ startActivity(intent)
134+ }
81135 }
82136
83- override fun onSaveInstanceState (outState : Bundle ) {
84- super .onSaveInstanceState(outState)
137+ /* *
138+ * Saves the current state of the WebView to the outState bundle
139+ */
140+ private fun saveWebViewState (outState : Bundle ) {
85141 val webView: WebView = findViewById(R .id.webview)
86142 val url = webView.url ? : " "
87143
@@ -95,6 +151,10 @@ class MainActivity : AppCompatActivity() {
95151 outState.putString(" SavedText" , inputText)
96152 }
97153
98- CookieManagerHelper ().saveCookies(this , webView)
154+ try {
155+ CookieManagerHelper ().saveCookies(this , webView)
156+ } catch (e: Exception ) {
157+ Log .w(" MainActivity" , " Cookie save failed" , e)
158+ }
99159 }
100- }
160+ }
0 commit comments