-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
androidApp-kt/addedFolder/MainActivity files/MainActivity-1.kt
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,33 @@ | ||
package com.hadithmv.hmv | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.activity.ComponentActivity | ||
|
||
class MainActivity : ComponentActivity() { | ||
private val applicationUrl = "file:///android_asset/index.html" | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
|
||
val webView = WebView(this).apply { | ||
webViewClient = WebViewClient() | ||
settings.apply { | ||
javaScriptEnabled = true | ||
allowFileAccess = true | ||
domStorageEnabled = true | ||
// added | ||
allowFileAccessFromFileURLs = true | ||
// https://developer.android.com/reference/android/webkit/WebSettings | ||
// https://chromium.googlesource.com/chromium/src/+/HEAD/android_webview/docs/cors-and-webview-api.md | ||
// https://docs.jumio.com/production/Content/Integration/Integration%20Channels/Android%20WebView.htm | ||
} | ||
loadUrl(applicationUrl) | ||
} | ||
setContentView(webView) | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
androidApp-kt/addedFolder/MainActivity files/MainActivity-2.kt
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,165 @@ | ||
package com.hadithmv.hmv | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.activity.ComponentActivity | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebSettings | ||
import androidx.activity.OnBackPressedCallback | ||
import android.content.SharedPreferences | ||
|
||
/** | ||
* MainActivity serves as the primary activity for the WebView-based application. | ||
* It handles URL loading, state persistence, and back button navigation. | ||
*/ | ||
class MainActivity : ComponentActivity() { | ||
// Base URL for the application's local HTML content in the assets folder | ||
private val applicationUrl = "file:///android_asset/books/index.html" | ||
|
||
// WebView instance that will be used throughout the activity's lifecycle | ||
private lateinit var webView: WebView | ||
|
||
// SharedPreferences instance for persisting the last visited URL | ||
private lateinit var prefs: SharedPreferences | ||
|
||
// Key used to store and retrieve the last visited URL in SharedPreferences | ||
private val LAST_URL_KEY = "last_url" | ||
|
||
/** | ||
* Initialize the activity and set up the WebView with all necessary configurations. | ||
* @param savedInstanceState Bundle containing the activity's previously saved state | ||
*/ | ||
@SuppressLint("SetJavaScriptEnabled") // Suppress JavaScript warning as it's required for our app | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
// Initialize SharedPreferences for storing the last visited URL | ||
prefs = getSharedPreferences("WebViewPrefs", MODE_PRIVATE) | ||
|
||
// Create and configure the WebView | ||
webView = WebView(this).apply { | ||
// Configure the WebViewClient to handle URL loading | ||
webViewClient = object : WebViewClient() { | ||
/** | ||
* Handle URL loading requests. External URLs are opened in the browser, | ||
* while local files are handled within the WebView. | ||
*/ | ||
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { | ||
val url = request?.url?.toString() | ||
return when { | ||
url == null -> false // No URL provided, let WebView handle it | ||
url.startsWith("file://") -> false // Local file, let WebView handle it | ||
else -> { | ||
try { | ||
// External URL: open in browser | ||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) | ||
true | ||
} catch (e: Exception) { | ||
// If no app can handle the URL, let WebView try to handle it | ||
false | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Called when a page finishes loading. Save the URL to preferences | ||
* for persistence across app restarts. | ||
*/ | ||
override fun onPageFinished(view: WebView?, url: String?) { | ||
super.onPageFinished(view, url) | ||
url?.let { saveLastUrl(it) } | ||
} | ||
} | ||
|
||
// Configure WebView settings | ||
settings.apply { | ||
javaScriptEnabled = true // Enable JavaScript execution | ||
allowFileAccess = true // Allow access to local files | ||
domStorageEnabled = true // Enable DOM storage API | ||
allowFileAccessFromFileURLs = true // Allow local files to access other local files | ||
allowContentAccess = true // Allow access to content providers | ||
|
||
// Prevent mixed content (HTTP content in HTTPS pages) | ||
mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW | ||
|
||
// Enable caching | ||
cacheMode = WebSettings.LOAD_DEFAULT | ||
|
||
// Enable database storage API | ||
databaseEnabled = true | ||
} | ||
|
||
// Load either the last visited URL or the default URL | ||
loadUrl(getLastUrl()) | ||
} | ||
|
||
// Set the WebView as the content view | ||
setContentView(webView) | ||
|
||
// Configure back button handling | ||
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { | ||
/** | ||
* Handle back button press: | ||
* - If WebView can go back, navigate to previous page | ||
* - Otherwise, exit the app | ||
*/ | ||
override fun handleOnBackPressed() { | ||
when { | ||
webView.canGoBack() -> webView.goBack() | ||
else -> { | ||
isEnabled = false | ||
onBackPressedDispatcher.onBackPressed() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Save the last visited URL to SharedPreferences. | ||
* @param url The URL to save | ||
*/ | ||
private fun saveLastUrl(url: String) { | ||
prefs.edit().apply { | ||
putString(LAST_URL_KEY, url) | ||
apply() // Asynchronously save changes | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve the last visited URL from SharedPreferences. | ||
* @return The last visited URL or the default application URL if none exists | ||
*/ | ||
private fun getLastUrl(): String { | ||
return prefs.getString(LAST_URL_KEY, applicationUrl) ?: applicationUrl | ||
} | ||
|
||
/** | ||
* Save WebView state when the activity is paused. | ||
*/ | ||
override fun onPause() { | ||
super.onPause() | ||
webView.saveState(Bundle()) | ||
} | ||
|
||
/** | ||
* Save WebView state before the activity might be destroyed. | ||
*/ | ||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
webView.saveState(outState) | ||
} | ||
|
||
/** | ||
* Restore WebView state when the activity is recreated. | ||
*/ | ||
override fun onRestoreInstanceState(savedInstanceState: Bundle) { | ||
super.onRestoreInstanceState(savedInstanceState) | ||
webView.restoreState(savedInstanceState) | ||
} | ||
} |
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 @@ | ||
https://github.com/hadithmv/hadithmv.github.io/tree/master/androidApp-kt/app/src/main/java/com/hadithmv/hmv |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.