Skip to content

Commit

Permalink
Merge pull request #164 from mslxl/master
Browse files Browse the repository at this point in the history
fix: OkHttpClient Instance didn't update in time when pref_enableNetworkBypass change.  #162
  • Loading branch information
yellowbluesky authored Jul 7, 2021
2 parents ea1878f + 3058946 commit 9fddaf6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PixivArtWorker(context: Context, params: WorkerParameters) : Worker(contex
private var clearArtwork = false

fun enqueueLoad(clear: Boolean, context: Context?) {
Log.d(LOG_TAG,"Enqueued load")
if (clear) {
clearArtwork = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.antony.muzei.pixiv.provider.network

import android.annotation.SuppressLint
import android.util.Log
import androidx.preference.PreferenceManager
import com.antony.muzei.pixiv.BuildConfig
import com.antony.muzei.pixiv.PixivMuzei
Expand All @@ -10,6 +11,7 @@ import java.security.cert.X509Certificate
import javax.net.ssl.X509TrustManager

object OkHttpSingleton {
private const val LOG_TAG = "OkHttpSingleton"
private val x509TrustManager: X509TrustManager = object : X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
override fun checkClientTrusted(x509Certificates: Array<X509Certificate>, s: String) {
Expand Down Expand Up @@ -40,7 +42,9 @@ object OkHttpSingleton {
.retryOnConnectionFailure(true)
.apply {
val prefs = PreferenceManager.getDefaultSharedPreferences(PixivMuzei.context?.applicationContext)
if (prefs.getBoolean("pref_enableNetworkBypass", false)) {
val enableNetworkBypass = prefs.getBoolean("pref_enableNetworkBypass", false)
Log.d(LOG_TAG,"network bypass was $enableNetworkBypass")
if (enableNetworkBypass) {
sslSocketFactory(RubySSLSocketFactory(), x509TrustManager)
dns(RubyHttpDns.getInstance())
}
Expand All @@ -51,4 +55,10 @@ object OkHttpSingleton {
}
return instance as OkHttpClient
}

fun refreshInstance(){
// Through set it to null, the OkHttpClient will be create again and apply with new preference when `getInstance` was invoked.
instance = null
Log.d(LOG_TAG,"set OkHttp instance to null")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

object RestClient {
private val okHttpClientAuthBuilder = OkHttpSingleton.getInstance().newBuilder()
private val okHttpClientAuthBuilder get() = OkHttpSingleton.getInstance().newBuilder()
.apply {
addNetworkInterceptor(PixivAuthHeaderInterceptor())
//addInterceptor(CustomClientHeaderInterceptor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import androidx.preference.*
import androidx.work.*
import com.antony.muzei.pixiv.R
import com.antony.muzei.pixiv.provider.ClearCacheWorker
import com.antony.muzei.pixiv.provider.PixivArtWorker
import com.antony.muzei.pixiv.provider.network.OkHttpSingleton
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -172,6 +174,15 @@ class AdvOptionsPreferenceFragment : PreferenceFragmentCompat() {
true
}

val enableNetworkPassbyCheckbox = findPreference<SwitchPreference>("pref_enableNetworkBypass")!!
enableNetworkPassbyCheckbox.setOnPreferenceChangeListener { _, _ ->

OkHttpSingleton.refreshInstance() // Renew a instance with sslSocketFactory by this

PixivArtWorker.enqueueLoad(false, context)

true
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
preferenceScreen = findPreference(resources.getString(R.string.preferenceScreen))
val prefCatPostProcess = findPreference<PreferenceCategory>("prefCat_postProcess")
Expand Down

0 comments on commit 9fddaf6

Please sign in to comment.