From ded23ea06be43b6a936aa72265223f1c17f873bf Mon Sep 17 00:00:00 2001 From: deeppp15 Date: Fri, 6 Dec 2024 23:20:57 -0500 Subject: [PATCH] added abs vs rel path, permissions and more --- app/src/main/AndroidManifest.xml | 2 + .../myplanet/base/BaseContainerFragment.kt | 188 ++++++++++-------- .../myplanet/ui/viewer/WebViewActivity.kt | 145 +++++++++----- 3 files changed, 193 insertions(+), 142 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1e8da18fcb..62c212ea0a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,8 @@ + + diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseContainerFragment.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseContainerFragment.kt index 9514ddd210..7930520238 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseContainerFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseContainerFragment.kt @@ -152,88 +152,88 @@ abstract class BaseContainerFragment : BaseResourceFragment() { } } - private fun checkFileExtension(items: RealmMyLibrary) { - val mimetype = Utilities.getMimeType(items.resourceLocalAddress) - val userId = "${model?.id}" - Log.d("FileExtension", "Mimetype: ${items.resourceLocalAddress}") - - val extension = items.resourceLocalAddress - ?.substringAfterLast('.', "") - ?.lowercase() - - val existingAction = mRealm.where(RealmUserChallengeActions::class.java) - .equalTo("userId", userId) - .equalTo("resourceId", items.resourceId) - .findFirst() - - if (mimetype != null) { - if (mimetype.contains("image")) { - openIntent(items, ImageViewerActivity::class.java) - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - } else if (mimetype.contains("pdf")) { - openPdf(items) - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - } else if (mimetype.contains("audio")) { - openIntent(items, AudioPlayerActivity::class.java) - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - } else { - checkMoreFileExtensions(extension, items) - } - } - } - - private fun checkMoreFileExtensions(extension: String?, items: RealmMyLibrary) { - val userId = "${model?.id}" - val existingAction = mRealm.where(RealmUserChallengeActions::class.java) - .equalTo("userId", userId) - .equalTo("resourceId", items.resourceId) - .findFirst() - - when (extension) { - "txt" -> { - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - openIntent(items, TextFileViewerActivity::class.java) - } - "html", "htm" -> { - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - openHtmlResource(items) - } - "md" -> { - if (items.resourceLocalAddress?.contains("README.md", ignoreCase = true) == true) { - Log.d("FileExtension", "Detected README.md file") - return - } - - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - openIntent(items, MarkdownViewerActivity::class.java) - } - "csv" -> { - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - openIntent(items, CSVViewerActivity::class.java) - } - "apk" -> { - if (existingAction == null) { - createAction(mRealm, userId, items.resourceId, "resourceOpen") - } - installApk(items) - } - else -> Toast.makeText(activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG).show() - } - } +// private fun checkFileExtension(items: RealmMyLibrary) { +// val mimetype = Utilities.getMimeType(items.resourceLocalAddress) +// val userId = "${model?.id}" +// Log.d("FileExtension", "Mimetype: ${items.resourceLocalAddress}") +// +// val extension = items.resourceLocalAddress +// ?.substringAfterLast('.', "") +// ?.lowercase() +// +// val existingAction = mRealm.where(RealmUserChallengeActions::class.java) +// .equalTo("userId", userId) +// .equalTo("resourceId", items.resourceId) +// .findFirst() +// +// if (mimetype != null) { +// if (mimetype.contains("image")) { +// openIntent(items, ImageViewerActivity::class.java) +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// } else if (mimetype.contains("pdf")) { +// openPdf(items) +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// } else if (mimetype.contains("audio")) { +// openIntent(items, AudioPlayerActivity::class.java) +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// } else { +// checkMoreFileExtensions(extension, items) +// } +// } +// } +// +// private fun checkMoreFileExtensions(extension: String?, items: RealmMyLibrary) { +// val userId = "${model?.id}" +// val existingAction = mRealm.where(RealmUserChallengeActions::class.java) +// .equalTo("userId", userId) +// .equalTo("resourceId", items.resourceId) +// .findFirst() +// +// when (extension) { +// "txt" -> { +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// openIntent(items, TextFileViewerActivity::class.java) +// } +// "html", "htm" -> { +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// openHtmlResource(items) +// } +// "md" -> { +// if (items.resourceLocalAddress?.contains("README.md", ignoreCase = true) == true) { +// Log.d("FileExtension", "Detected README.md file") +// return +// } +// +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// openIntent(items, MarkdownViewerActivity::class.java) +// } +// "csv" -> { +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// openIntent(items, CSVViewerActivity::class.java) +// } +// "apk" -> { +// if (existingAction == null) { +// createAction(mRealm, userId, items.resourceId, "resourceOpen") +// } +// installApk(items) +// } +// else -> Toast.makeText(activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG).show() +// } +// } private fun installApk(items: RealmMyLibrary) { currentLibrary = items @@ -285,7 +285,9 @@ abstract class BaseContainerFragment : BaseResourceFragment() { if (mimetype.startsWith("video")) { playVideo(videoType, items) } else { - checkFileExtension(items) +// createAction(mRealm, , items.resourceId, "resourceOpen") + openHtmlResource(items) + //checkFileExtension(items) } } @@ -368,14 +370,24 @@ abstract class BaseContainerFragment : BaseResourceFragment() { } private fun openHtmlResource(items: RealmMyLibrary) { - val intent = Intent(activity, WebViewActivity::class.java).apply { - putExtra("TOUCHED_FILE", "${items.id}/${items.resourceLocalAddress}") - putExtra("title", items.title) - putExtra("isLocalFile", true) + //correct path found in emulator is : /storage/self/primary/Android/data/org.ole.planet.myplanet/files/ole/folder/index.html + val resourcePath = File(MainApplication.context.getExternalFilesDir(null), "ole/${items.id}/${items.resourceLocalAddress}") + if (resourcePath.exists()) { + Log.d("webview","THE FILE PATH IS:"+resourcePath.toURI().toString()) + val intent = Intent(activity, WebViewActivity::class.java).apply { + putExtra("TOUCHED_FILE", resourcePath.toURI().toString()) // Use corrected path + putExtra("title", items.title) + putExtra("isLocalFile", true) + } + startActivity(intent) + } else { + Log.e("BaseContainerFragment", "File not found: $resourcePath") + Toast.makeText(requireContext(), "NO QUERO FILE", Toast.LENGTH_LONG).show() } - startActivity(intent) } + + open fun handleBackPressed() { val fragmentManager = parentFragmentManager fragmentManager.popBackStack() diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/viewer/WebViewActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/viewer/WebViewActivity.kt index a847dda0de..ea6deb513a 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/viewer/WebViewActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/viewer/WebViewActivity.kt @@ -6,11 +6,7 @@ import android.os.Bundle import android.text.TextUtils import android.util.Log import android.view.View -import android.webkit.CookieManager -import android.webkit.WebChromeClient -import android.webkit.WebSettings -import android.webkit.WebView -import android.webkit.WebViewClient +import android.webkit.* import androidx.appcompat.app.AppCompatActivity import org.ole.planet.myplanet.MainApplication import org.ole.planet.myplanet.databinding.ActivityWebViewBinding @@ -18,99 +14,140 @@ import org.ole.planet.myplanet.utilities.Utilities import java.io.File class WebViewActivity : AppCompatActivity() { - private lateinit var activityWebViewBinding: ActivityWebViewBinding + + private lateinit var binding: ActivityWebViewBinding private var fromDeepLink = false private lateinit var link: String private var isLocalFile = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - activityWebViewBinding = ActivityWebViewBinding.inflate(layoutInflater) - setContentView(activityWebViewBinding.root) - val dataFromDeepLink = intent.dataString - fromDeepLink = !TextUtils.isEmpty(dataFromDeepLink) - val title: String? = intent.getStringExtra("title") + binding = ActivityWebViewBinding.inflate(layoutInflater) + setContentView(binding.root) + + extractIntentData() + clearCookies() + + setupWebViewSettings() + setupListeners() + + if (isLocalFile) { + loadLocalFile() + } else { + loadRemoteUrl() + } + setupWebClient() + } + + private fun extractIntentData() { + fromDeepLink = !TextUtils.isEmpty(intent.dataString) link = intent.getStringExtra("link") ?: "" isLocalFile = intent.getBooleanExtra("isLocalFile", false) - Log.d("WebViewActivity", "onCreate: $link, isLocalFile: $isLocalFile") - clearCookie() + Log.d("webview", "Intent Data - link: $link, isLocalFile: $isLocalFile, fromDeepLink: $fromDeepLink") - if (!TextUtils.isEmpty(title)) { - activityWebViewBinding.contentWebView.webTitle.text = title + intent.getStringExtra("title")?.let { + binding.contentWebView.webTitle.text = it } + } - activityWebViewBinding.contentWebView.pBar.max = 100 - activityWebViewBinding.contentWebView.pBar.progress = 0 - setListeners() - activityWebViewBinding.contentWebView.wv.settings.apply { + private fun clearCookies() { + CookieManager.getInstance().apply { + removeAllCookies(null) + flush() + } + Log.d("webview", "Cookies cleared.") + } + + private fun setupWebViewSettings() { + with(binding.contentWebView.wv.settings) { javaScriptEnabled = true - domStorageEnabled = true javaScriptCanOpenWindowsAutomatically = true + + domStorageEnabled = true + mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW + + cacheMode = WebSettings.LOAD_DEFAULT + databaseEnabled = true + + useWideViewPort = true + loadWithOverviewMode = true + setSupportZoom(true) builtInZoomControls = true + displayZoomControls = false + + allowFileAccess = true + allowContentAccess = true } + Log.d("webview", "WebView settings configured.") + } -// activityWebViewBinding.contentWebView.wv.settings.javaScriptEnabled = true -// activityWebViewBinding.contentWebView.wv.settings.javaScriptCanOpenWindowsAutomatically = true - if (isLocalFile) { - val touchedFile = intent.getStringExtra("TOUCHED_FILE") - if (!touchedFile.isNullOrEmpty()) { - val localFilePath = File(MainApplication.context.getExternalFilesDir(null), touchedFile).absolutePath - activityWebViewBinding.contentWebView.wv.loadUrl("file://$localFilePath") + private fun loadLocalFile() { + val touchedFile = intent.getStringExtra("TOUCHED_FILE") + + if (!touchedFile.isNullOrEmpty()) { + val localFilePath = if (touchedFile.startsWith("file://")) { + touchedFile + } else { + File(MainApplication.context.getExternalFilesDir(null), touchedFile).absolutePath } + + binding.contentWebView.wv.loadUrl(localFilePath) + Log.d("webview", "Loaded local file: $localFilePath") } else { - // Existing remote URL loading logic - val headers = mapOf("Authorization" to Utilities.header) - activityWebViewBinding.contentWebView.wv.loadUrl(link, headers) + Log.w("webview", "TOUCHED_FILE is null or empty.") } - activityWebViewBinding.contentWebView.finish.setOnClickListener { finish() } - setWebClient() } + private fun loadRemoteUrl() { + val headers = mapOf("Authorization" to Utilities.header) + binding.contentWebView.wv.loadUrl(link, headers) + Log.d("webview", "Loaded remote URL: $link with headers.") + } - private fun setWebClient() { - activityWebViewBinding.contentWebView.wv.webViewClient = object : WebViewClient() { + private fun setupWebClient() { + binding.contentWebView.wv.webViewClient = object : WebViewClient() { override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) { super.onPageStarted(view, url, favicon) + Log.d("webview", "Page started loading: $url") + if (url.endsWith("/eng/")) { + Log.d("webview", "Finishing activity due to /eng/ URL.") finish() } - val i = Uri.parse(url) - activityWebViewBinding.contentWebView.webSource.text = i.host + + val host = Uri.parse(url).host.orEmpty() + binding.contentWebView.webSource.text = host } override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(view, url) + Log.d("webview", "Page finished loading: $url") } } - } - - private fun clearCookie() { - val cookieManager = CookieManager.getInstance() - cookieManager.removeAllCookies(null) - cookieManager.flush() - } - private fun setListeners() { - activityWebViewBinding.contentWebView.wv.webChromeClient = object : WebChromeClient() { + binding.contentWebView.wv.webChromeClient = object : WebChromeClient() { override fun onProgressChanged(view: WebView, newProgress: Int) { - activityWebViewBinding.contentWebView.pBar.progress = newProgress - if (view.url?.endsWith("/eng/") == true) { - finish() - } - activityWebViewBinding.contentWebView.pBar.incrementProgressBy(newProgress) - if (newProgress == 100 && activityWebViewBinding.contentWebView.pBar.isShown) { - activityWebViewBinding.contentWebView.pBar.visibility = View.GONE + binding.contentWebView.pBar.progress = newProgress + if (newProgress == 100) { + binding.contentWebView.pBar.visibility = View.GONE } + Log.d("webview", "Page loading progress: $newProgress%") } override fun onReceivedTitle(view: WebView, title: String) { - activityWebViewBinding.contentWebView.webTitle.text = title - super.onReceivedTitle(view, title) + binding.contentWebView.webTitle.text = title + Log.d("webview", "Received page title: $title") } } } + private fun setupListeners() { + binding.contentWebView.finish.setOnClickListener { + Log.d("webview", "Finish button clicked.") + finish() + } + } }