aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-09-28 23:53:47 -0700
committerGitHub <noreply@github.com>2019-09-28 23:53:47 -0700
commitc9fec8cc993f159eb81803e8c0cd5855d2a9abd3 (patch)
treec3ae5a6890b0797110f397b99812362312c07ff6 /app/src/main/kotlin
parentf5ae18fc0d824dbdce5523128f9ded86bdfa6d08 (diff)
parent73e16427879908e8b46b7c9fe1e87b0df838fa78 (diff)
downloadfrost-c9fec8cc993f159eb81803e8c0cd5855d2a9abd3.tar.gz
frost-c9fec8cc993f159eb81803e8c0cd5855d2a9abd3.tar.bz2
frost-c9fec8cc993f159eb81803e8c0cd5855d2a9abd3.zip
Merge pull request #1559 from AllanWang/desktop-url-convert
Convert desktop urls to mobile ones
Diffstat (limited to 'app/src/main/kotlin')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt18
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt9
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt7
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt28
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt7
6 files changed, 62 insertions, 9 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt
index 312e7665..628ab5dc 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt
@@ -59,6 +59,9 @@ import com.pitchedapps.frost.enums.OverlayContext
import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
+import com.pitchedapps.frost.facebook.USER_AGENT
+import com.pitchedapps.frost.facebook.USER_AGENT_DESKTOP_CONST
+import com.pitchedapps.frost.facebook.USER_AGENT_MOBILE_CONST
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.kotlin.subscribeDuringJob
import com.pitchedapps.frost.utils.ARG_URL
@@ -140,12 +143,24 @@ class FrostWebActivity : WebOverlayActivityBase() {
}
/**
+ * Variant that forces a mobile user agent. This is largely internal,
+ * and is only necessary when we are launching from an existing [WebOverlayActivityBase]
+ */
+class WebOverlayMobileActivity : WebOverlayActivityBase(USER_AGENT_MOBILE_CONST)
+
+/**
+ * Variant that forces a desktop user agent. This is largely internal,
+ * and is only necessary when we are launching from an existing [WebOverlayActivityBase]
+ */
+class WebOverlayDesktopActivity : WebOverlayActivityBase(USER_AGENT_DESKTOP_CONST)
+
+/**
* Internal overlay for the app; this is tied with the main task and is singleTop as opposed to singleInstance
*/
class WebOverlayActivity : WebOverlayActivityBase()
@UseExperimental(ExperimentalCoroutinesApi::class)
-abstract class WebOverlayActivityBase : BaseActivity(),
+abstract class WebOverlayActivityBase(private val userAgent: String = USER_AGENT) : BaseActivity(),
ActivityContract, FrostContentContainer,
VideoViewHolder, FileChooserContract by FileChooserDelegate() {
@@ -207,6 +222,7 @@ abstract class WebOverlayActivityBase : BaseActivity(),
}
with(web) {
+ userAgentString = userAgent
Prefs.prevId = Prefs.userId
val authDefer = BiometricUtils.authenticate(this@WebOverlayActivityBase)
launch {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
index c01bce55..8139bebc 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbConst.kt
@@ -22,8 +22,9 @@ package com.pitchedapps.frost.facebook
const val FACEBOOK_COM = "facebook.com"
const val FBCDN_NET = "fbcdn.net"
-const val HTTPS_FACEBOOK_COM = "https://$FACEBOOK_COM/"
-const val FB_URL_BASE = "https://m.$FACEBOOK_COM/"
+const val WWW_FACEBOOK_COM = "www.$FACEBOOK_COM"
+const val FACEBOOK_BASE_COM = "m.$FACEBOOK_COM"
+const val FB_URL_BASE = "https://$FACEBOOK_BASE_COM/"
fun profilePictureUrl(id: Long) = "https://graph.facebook.com/$id/picture?type=large"
const val FB_LOGIN_URL = "${FB_URL_BASE}login"
const val FB_HOME_URL = "${FB_URL_BASE}home.php"
@@ -36,10 +37,10 @@ const val FB_HOME_URL = "${FB_URL_BASE}home.php"
*/
// Default user agent
-private const val USER_AGENT_MOBILE_CONST =
+const val USER_AGENT_MOBILE_CONST =
"Mozilla/5.0 (Linux; Android 8.0.0; ONEPLUS A3000) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.90 Mobile Safari/537.36"
// Desktop agent, for pages like messages
-private const val USER_AGENT_DESKTOP_CONST =
+const val USER_AGENT_DESKTOP_CONST =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.90 Safari/537.36"
const val USER_AGENT = USER_AGENT_DESKTOP_CONST
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt
index 6292f4f0..76e58465 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt
@@ -78,6 +78,8 @@ class FbUrlFormatter(url: String) {
cleanedUrl = cleanedUrl.substring(0, qm)
}
discardableQueries.forEach { queries.remove(it) }
+ // Convert desktop urls to mobile ones
+ cleanedUrl = cleanedUrl.replace(WWW_FACEBOOK_COM, FACEBOOK_BASE_COM)
if (cleanedUrl.startsWith("/")) cleanedUrl = FB_URL_BASE + cleanedUrl.substring(1)
cleanedUrl = cleanedUrl.replaceFirst(
".facebook.com//",
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
index d725acd9..8c5f6db6 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
@@ -57,6 +57,7 @@ import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.activities.TabCustomizerActivity
import com.pitchedapps.frost.activities.WebOverlayActivity
import com.pitchedapps.frost.activities.WebOverlayActivityBase
+import com.pitchedapps.frost.activities.WebOverlayMobileActivity
import com.pitchedapps.frost.db.CookieEntity
import com.pitchedapps.frost.facebook.FACEBOOK_COM
import com.pitchedapps.frost.facebook.FBCDN_NET
@@ -138,6 +139,12 @@ private inline fun <reified T : WebOverlayActivityBase> Context.launchWebOverlay
fun Context.launchWebOverlay(url: String) = launchWebOverlayImpl<WebOverlayActivity>(url)
+// TODO Currently, default is overlay. Switch this if default changes
+fun Context.launchWebOverlayDesktop(url: String) = launchWebOverlay(url)
+
+fun Context.launchWebOverlayMobile(url: String) =
+ launchWebOverlayImpl<WebOverlayMobileActivity>(url)
+
private fun Context.fadeBundle() = ActivityOptions.makeCustomAnimation(
this,
android.R.anim.fade_in, android.R.anim.fade_out
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt
index 24608a8b..3df3b2c2 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostUrlOverlayValidator.kt
@@ -22,15 +22,19 @@ import com.pitchedapps.frost.activities.WebOverlayActivityBase
import com.pitchedapps.frost.contracts.VideoViewHolder
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
+import com.pitchedapps.frost.facebook.USER_AGENT_DESKTOP_CONST
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
+import com.pitchedapps.frost.utils.isFacebookUrl
import com.pitchedapps.frost.utils.isImageUrl
import com.pitchedapps.frost.utils.isIndependent
import com.pitchedapps.frost.utils.isIndirectImageUrl
import com.pitchedapps.frost.utils.isVideoUrl
import com.pitchedapps.frost.utils.launchImageActivity
import com.pitchedapps.frost.utils.launchWebOverlay
+import com.pitchedapps.frost.utils.launchWebOverlayDesktop
+import com.pitchedapps.frost.utils.launchWebOverlayMobile
import com.pitchedapps.frost.views.FrostWebView
/**
@@ -50,21 +54,22 @@ import com.pitchedapps.frost.views.FrostWebView
* as we have no need of sending a new intent to the same activity
*/
fun FrostWebView.requestWebOverlay(url: String): Boolean {
+ @Suppress("NAME_SHADOWING") val url = url.formattedFbUrl
L.v { "Request web overlay: $url" }
val context = context // finalize reference
if (url.isVideoUrl && context is VideoViewHolder) {
L.d { "Found video through overlay" }
- context.runOnUiThread { context.showVideo(url.formattedFbUrl) }
+ context.runOnUiThread { context.showVideo(url) }
return true
}
if (url.isImageUrl) {
L.d { "Found fb image" }
- context.launchImageActivity(url.formattedFbUrl)
+ context.launchImageActivity(url)
return true
}
if (url.isIndirectImageUrl) {
L.d { "Found indirect fb image" }
- context.launchImageActivity(url.formattedFbUrl, cookie = FbCookie.webCookie)
+ context.launchImageActivity(url, cookie = FbCookie.webCookie)
return true
}
if (!url.isIndependent) {
@@ -72,7 +77,22 @@ fun FrostWebView.requestWebOverlay(url: String): Boolean {
return false
}
if (!Prefs.overlayEnabled) return false
- if (context is WebOverlayActivityBase) return false
+ if (context is WebOverlayActivityBase) {
+ val shouldUseDesktop = url.isFacebookUrl
+ //already overlay; manage user agent
+ if (userAgentString != USER_AGENT_DESKTOP_CONST && shouldUseDesktop) {
+ L._i { "Switch to desktop agent overlay" }
+ context.launchWebOverlayDesktop(url)
+ return true
+ }
+ if (userAgentString == USER_AGENT_DESKTOP_CONST && !shouldUseDesktop) {
+ L._i { "Switch from desktop agent" }
+ context.launchWebOverlayMobile(url)
+ return true
+ }
+ L._i { "return false switch" }
+ return false
+ }
L.v { "Request web overlay passed" }
context.launchWebOverlay(url)
return true
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
index 721b2c2d..a7e642b1 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewClients.kt
@@ -23,8 +23,10 @@ import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import ca.allanwang.kau.utils.withAlpha
+import com.pitchedapps.frost.facebook.FACEBOOK_BASE_COM
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
+import com.pitchedapps.frost.facebook.WWW_FACEBOOK_COM
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.JsActions
@@ -193,6 +195,11 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() {
if (Prefs.linksInDefaultApp && view.context.resolveActivityForUri(request.url)) {
return true
}
+ // Convert desktop urls to mobile ones
+ if (url.contains("https://www.facebook.com")) {
+ view.loadUrl(url.replace(WWW_FACEBOOK_COM, FACEBOOK_BASE_COM))
+ return true
+ }
return super.shouldOverrideUrlLoading(view, request)
}
}