Skip to content

Commit

Permalink
global ua string
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Aug 19, 2019
1 parent 50478ac commit cfd7696
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 21 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/soko/ekibun/bangumi/App.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,7 +99,7 @@ class SearchPresenter(private val context: SearchActivity) {
private var monoCall : Call<List<MonoInfo>>? = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,4 @@ public List<Cookie> 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);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/appbar_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</com.google.android.material.appbar.AppBarLayout>

0 comments on commit cfd7696

Please sign in to comment.