diff --git a/app/src/main/java/soko/ekibun/bangumi/App.kt b/app/src/main/java/soko/ekibun/bangumi/App.kt index d04a942..9e17885 100644 --- a/app/src/main/java/soko/ekibun/bangumi/App.kt +++ b/app/src/main/java/soko/ekibun/bangumi/App.kt @@ -1,15 +1,24 @@ package soko.ekibun.bangumi import android.app.Application +import android.content.Context import android.content.pm.ApplicationInfo +import android.webkit.WebView import soko.ekibun.bangumi.model.ThemeModel import soko.ekibun.bangumi.util.CrashHandler class App: Application(){ + val ua by lazy { WebView(this).settings.userAgentString } override fun onCreate() { super.onCreate() ThemeModel.setTheme(this, ThemeModel(this).getTheme()) if(applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE == 0) Thread.setDefaultUncaughtExceptionHandler(CrashHandler(this)) } + + companion object { + fun getUserAgent(context: Context): String { + return (context.applicationContext as App).ua + } + } } \ No newline at end of file diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/main/MainActivity.kt b/app/src/main/java/soko/ekibun/bangumi/ui/main/MainActivity.kt index 16968b4..9d1b299 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/main/MainActivity.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/main/MainActivity.kt @@ -15,6 +15,7 @@ import android.view.WindowManager import android.webkit.WebView import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* +import soko.ekibun.bangumi.App import soko.ekibun.bangumi.R import soko.ekibun.bangumi.api.ApiHelper import soko.ekibun.bangumi.api.bangumi.Bangumi @@ -66,7 +67,7 @@ class MainActivity : BaseActivity() { downloadCacheProvider.unbindService() } - val ua by lazy { WebView(this).settings.userAgentString } + val ua by lazy { App.getUserAgent(this) } override fun onStart() { super.onStart() mainPresenter.refreshUser() diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/search/SearchPresenter.kt b/app/src/main/java/soko/ekibun/bangumi/ui/search/SearchPresenter.kt index da81718..d1b8bb8 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/search/SearchPresenter.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/search/SearchPresenter.kt @@ -11,6 +11,7 @@ import android.webkit.WebView import android.widget.TextView import kotlinx.android.synthetic.main.activity_search.* import retrofit2.Call +import soko.ekibun.bangumi.App import soko.ekibun.bangumi.R import soko.ekibun.bangumi.api.ApiHelper import soko.ekibun.bangumi.api.bangumi.Bangumi @@ -98,7 +99,7 @@ class SearchPresenter(private val context: SearchActivity) { private var monoCall : Call>? = null private var lastKey = "" private var loadCount = 0 - private val ua by lazy { WebView(context).settings.userAgentString } + private val ua by lazy { App.getUserAgent(context) } fun search(key: String = lastKey, refresh: Boolean = false){ if(refresh || lastKey != key){ lastKey = key diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/subject/SubjectActivity.kt b/app/src/main/java/soko/ekibun/bangumi/ui/subject/SubjectActivity.kt index d21874a..6faa316 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/subject/SubjectActivity.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/subject/SubjectActivity.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.view.* import android.webkit.WebView import kotlinx.android.synthetic.main.activity_subject.* +import soko.ekibun.bangumi.App import soko.ekibun.bangumi.R import soko.ekibun.bangumi.api.bangumi.Bangumi import soko.ekibun.bangumi.api.bangumi.bean.Subject @@ -44,7 +45,7 @@ class SubjectActivity : SwipeBackActivity() { } } - val ua by lazy { WebView(this).settings.userAgentString } + val ua by lazy { App.getUserAgent(this) } val formhash get() = subjectPresenter.subject.formhash?:"" override fun onStart() { super.onStart() diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/topic/TopicPresenter.kt b/app/src/main/java/soko/ekibun/bangumi/ui/topic/TopicPresenter.kt index 8d9e7fc..5b36c52 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/topic/TopicPresenter.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/topic/TopicPresenter.kt @@ -10,6 +10,7 @@ import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element import org.jsoup.safety.Whitelist +import soko.ekibun.bangumi.App import soko.ekibun.bangumi.R import soko.ekibun.bangumi.api.ApiHelper import soko.ekibun.bangumi.api.bangumi.Bangumi @@ -28,7 +29,7 @@ class TopicPresenter(private val context: TopicActivity) { } } - private val ua by lazy { WebView(context).settings.userAgentString } + private val ua by lazy { App.getUserAgent(context) } fun getTopic(scrollPost: String = ""){ context.item_swipe.isRefreshing = true Bangumi.getTopic(context.openUrl, ua).enqueue(ApiHelper.buildCallback({topic-> diff --git a/app/src/main/java/soko/ekibun/bangumi/ui/view/NestedWebView.kt b/app/src/main/java/soko/ekibun/bangumi/ui/view/NestedWebView.kt index 4976a05..93e6c3a 100644 --- a/app/src/main/java/soko/ekibun/bangumi/ui/view/NestedWebView.kt +++ b/app/src/main/java/soko/ekibun/bangumi/ui/view/NestedWebView.kt @@ -75,12 +75,12 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute newView.parentWebView = webview webview.childWebView = newView newView.shouldOverrideUrlLoading = { v: WebView, request: WebResourceRequest -> + newView.shouldOverrideUrlLoading = webview.shouldOverrideUrlLoading val ret = newView.shouldOverrideUrlLoading(v, request) if (ret) { newView.close() } else { newView.loadUrl(request.url.toString()) - newView.shouldOverrideUrlLoading = webview.shouldOverrideUrlLoading } ret } diff --git a/app/src/main/java/soko/ekibun/bangumi/util/HtmlHttpImageGetter.kt b/app/src/main/java/soko/ekibun/bangumi/util/HtmlHttpImageGetter.kt index 5ac8ddb..c3356d5 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/HtmlHttpImageGetter.kt +++ b/app/src/main/java/soko/ekibun/bangumi/util/HtmlHttpImageGetter.kt @@ -14,6 +14,7 @@ import androidx.swiperefreshlayout.widget.CircularProgressDrawable import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.Headers import com.bumptech.glide.request.RequestOptions +import soko.ekibun.bangumi.App import java.lang.ref.WeakReference import soko.ekibun.bangumi.R @@ -74,7 +75,9 @@ class HtmlHttpImageGetter(container: TextView, private val baseUri: URI?, privat }) GlideUtil.with(view) ?.asDrawable()?.load(GlideUrl(url, Headers { - mapOf("referer" to baseUri.toString()) + mapOf("referer" to baseUri.toString(), + "user-agent" to App.getUserAgent(view.context), + "host" to try { URI.create(url).host } catch(e: Exception) { url }) })) ?.apply(RequestOptions().transform(SizeTransformation {width, _ -> val maxWidth = container.get()?.width?.toFloat()?:return@SizeTransformation 1f diff --git a/app/src/main/java/soko/ekibun/bangumi/util/WebViewCookieHandler.java b/app/src/main/java/soko/ekibun/bangumi/util/WebViewCookieHandler.java index fa0f572..8c4fa14 100644 --- a/app/src/main/java/soko/ekibun/bangumi/util/WebViewCookieHandler.java +++ b/app/src/main/java/soko/ekibun/bangumi/util/WebViewCookieHandler.java @@ -36,19 +36,4 @@ public List loadForRequest(HttpUrl url) { return Collections.emptyList(); } - - @Keep - public static String getCookie(String url){ - return CookieManager.getInstance().getCookie(url); - } - - @Keep - public static String getUserAgent(Context context){ - return (new WebView(context)).getSettings().getUserAgentString(); - } - - @Keep - public static void setCookie(String url, String cookie){ - CookieManager.getInstance().setCookie(url, cookie); - } } \ No newline at end of file diff --git a/app/src/main/res/layout/appbar_layout.xml b/app/src/main/res/layout/appbar_layout.xml index 43ea894..4f9c375 100644 --- a/app/src/main/res/layout/appbar_layout.xml +++ b/app/src/main/res/layout/appbar_layout.xml @@ -11,6 +11,7 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" + app:popupTheme="@style/AppTheme.PopupOverlay" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp" /> \ No newline at end of file