Merge remote-tracking branch 'origin/master'

pull/1333/head
gedoor 3 years ago
commit a5210a3d11
  1. BIN
      app/cronetlib/cronet_api.jar
  2. BIN
      app/cronetlib/cronet_impl_common_java.jar
  3. 2
      app/src/main/assets/cronet.json
  4. 1
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  5. 52
      app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt
  6. 4
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  7. 25
      app/src/main/java/io/legado/app/help/http/cronet/CronetLoader.kt
  8. 34
      app/src/main/java/io/legado/app/help/http/cronet/CronetRequestCallback.kt
  9. 2
      app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt
  10. 2
      gradle.properties

Binary file not shown.

@ -1 +1 @@
{"arm64-v8a":"18441011d4a2e3751b8152ad05ab24da","armeabi-v7a":"701930e54c2cfa3d6d0df37168ecc4a6","x86":"3d4ebb7f23b910a8b50a2966eb39bbe7","x86_64":"9be68a8e03867c8f844e8eec88fe09ed","version":"93.0.4577.62"} {"arm64-v8a":"9717593e18b283ac23a0be548c5aeea6","armeabi-v7a":"44c83d10c4a32ff20466bd7ef363c264","x86":"ce4ad637ead52aa3761e4ce3083f6453","x86_64":"a9924d9d2a31510d7335f6c6af32abc0","version":"93.0.4577.82"}

@ -29,6 +29,7 @@ val okHttpClient: OkHttpClient by lazy {
.connectTimeout(15, TimeUnit.SECONDS) .connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS)
.callTimeout(60,TimeUnit.SECONDS)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory, SSLHelper.unsafeTrustManager) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory, SSLHelper.unsafeTrustManager)
.retryOnConnectionFailure(true) .retryOnConnectionFailure(true)
.hostnameVerifier(SSLHelper.unsafeHostnameVerifier) .hostnameVerifier(SSLHelper.unsafeHostnameVerifier)

@ -12,11 +12,11 @@ import org.chromium.net.ExperimentalCronetEngine
import org.chromium.net.UploadDataProviders import org.chromium.net.UploadDataProviders
import org.chromium.net.UrlRequest import org.chromium.net.UrlRequest
import splitties.init.appCtx import splitties.init.appCtx
import java.util.concurrent.Executor import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors import java.util.concurrent.Executors
val executor: Executor by lazy { Executors.newCachedThreadPool() } val executor: ExecutorService by lazy { Executors.newCachedThreadPool() }
val cronetEngine: ExperimentalCronetEngine by lazy { val cronetEngine: ExperimentalCronetEngine by lazy {
if (AppConfig.isGooglePlay) { if (AppConfig.isGooglePlay) {
@ -27,16 +27,17 @@ val cronetEngine: ExperimentalCronetEngine by lazy {
val builder = ExperimentalCronetEngine.Builder(appCtx).apply { val builder = ExperimentalCronetEngine.Builder(appCtx).apply {
if (!AppConfig.isGooglePlay&&CronetLoader.install()) { if (!AppConfig.isGooglePlay && CronetLoader.install()) {
setLibraryLoader(CronetLoader)//设置自定义so库加载 setLibraryLoader(CronetLoader)//设置自定义so库加载
} }
setStoragePath(appCtx.externalCacheDir?.absolutePath)//设置缓存路径 setStoragePath(appCtx.externalCacheDir?.absolutePath)//设置缓存路径
enableHttpCache(HTTP_CACHE_DISK, (1024 * 1024 * 50).toLong())//设置缓存模式 enableHttpCache(HTTP_CACHE_DISK, (1024 * 1024 * 50).toLong())//设置50M的磁盘缓存
enableQuic(true)//设置支持http/3 enableQuic(true)//设置支持http/3
enableHttp2(true) //设置支持http/2 enableHttp2(true) //设置支持http/2
enablePublicKeyPinningBypassForLocalTrustAnchors(true) enablePublicKeyPinningBypassForLocalTrustAnchors(true)
enableBrotli(true)//Brotli压缩 enableBrotli(true)//Brotli压缩
} }
val engine = builder.build() val engine = builder.build()
Log.d("Cronet", "Cronet Version:" + engine.versionString) Log.d("Cronet", "Cronet Version:" + engine.versionString)
@ -49,31 +50,32 @@ val cronetEngine: ExperimentalCronetEngine by lazy {
fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest {
val url = request.url.toString() val url = request.url.toString()
val requestBuilder = cronetEngine.newUrlRequestBuilder(url, callback, executor)
requestBuilder.setHttpMethod(request.method)
val headers: Headers = request.headers val headers: Headers = request.headers
headers.forEachIndexed { index, _ ->
requestBuilder.addHeader(headers.name(index), headers.value(index))
}
val requestBody = request.body val requestBody = request.body
if (requestBody != null) { return cronetEngine.newUrlRequestBuilder(url, callback, executor).apply {
val contentType: MediaType? = requestBody.contentType() setHttpMethod(request.method)//设置
if (contentType != null) { allowDirectExecutor()
requestBuilder.addHeader("Content-Type", contentType.toString()) headers.forEachIndexed { index, _ ->
} else { addHeader(headers.name(index), headers.value(index))
requestBuilder.addHeader("Content-Type", "text/plain")
} }
val buffer = Buffer() if (requestBody != null) {
requestBody.writeTo(buffer) val contentType: MediaType? = requestBody.contentType()
requestBuilder.setUploadDataProvider( if (contentType != null) {
UploadDataProviders.create(buffer.readByteArray()), addHeader("Content-Type", contentType.toString())
executor } else {
) addHeader("Content-Type", "text/plain")
}
val buffer = Buffer()
requestBody.writeTo(buffer)
setUploadDataProvider(
UploadDataProviders.create(buffer.readByteArray()),
executor
)
}
}.build()
}
return requestBuilder.build()
} }

@ -42,7 +42,7 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
@Throws(IOException::class) @Throws(IOException::class)
private fun proceedWithCronet(request: Request, call: Call): Response { private fun proceedWithCronet(request: Request, call: Call): Response {
val callback = CronetUrlRequestCallback(request, call) val callback = CronetRequestCallback(request, call)
val urlRequest = buildRequest(request, callback) val urlRequest = buildRequest(request, callback)
urlRequest.start() urlRequest.start()
return callback.waitForDone(urlRequest) return callback.waitForDone(urlRequest)
@ -50,7 +50,7 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
private fun getCookie(url: HttpUrl): String { private fun getCookie(url: HttpUrl): String {
val sb = StringBuilder() val sb = StringBuilder()
//处理从 Cookjar 获取到的Cookies //处理从 Cookiejar 获取到的Cookies
if (cookieJar != null) { if (cookieJar != null) {
val cookies = cookieJar.loadForRequest(url) val cookies = cookieJar.loadForRequest(url)
for (cookie in cookies) { for (cookie in cookies) {

@ -24,8 +24,8 @@ import java.util.*
object CronetLoader : CronetEngine.Builder.LibraryLoader() { object CronetLoader : CronetEngine.Builder.LibraryLoader() {
//https://storage.googleapis.com/chromium-cronet/android/92.0.4515.159/Release/cronet/libs/arm64-v8a/libcronet.92.0.4515.159.so //https://storage.googleapis.com/chromium-cronet/android/92.0.4515.159/Release/cronet/libs/arm64-v8a/libcronet.92.0.4515.159.so
//https://cdn.jsdelivr.net/gh/ag2s20150909/cronet-repo@92.0.4515.159/cronet/92.0.4515.127/arm64-v8a/libcronet.92.0.4515.159.so.js
private const val TAG = "CronetLoader" private const val TAG = "CronetLoader"
private const val soVersion = BuildConfig.Cronet_Version private const val soVersion = BuildConfig.Cronet_Version
private const val soName = "libcronet.$soVersion.so" private const val soName = "libcronet.$soVersion.so"
private val soUrl: String private val soUrl: String
@ -34,6 +34,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() {
private var cpuAbi: String? = null private var cpuAbi: String? = null
private var md5: String private var md5: String
var download = false var download = false
private var cacheInstall = false
init { init {
soUrl = ("https://storage.googleapis.com/chromium-cronet/android/" soUrl = ("https://storage.googleapis.com/chromium-cronet/android/"
@ -49,18 +50,33 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() {
Log.e(TAG, "soUrl:$soUrl") Log.e(TAG, "soUrl:$soUrl")
} }
/**
* 判断Cronet是否安装完成
* @return
*/
fun install(): Boolean { fun install(): Boolean {
if (cacheInstall) {
return true
}
if (AppConfig.isGooglePlay) { if (AppConfig.isGooglePlay) {
//检查GMS的Cronet服务是否安装 //检查GMS的Cronet服务是否安装
return CronetProviderInstaller.isInstalled() cacheInstall = CronetProviderInstaller.isInstalled()
return cacheInstall
} }
if (md5.length != 32 || !soFile.exists() || md5 != getFileMD5(soFile)) { if (md5.length != 32 || !soFile.exists() || md5 != getFileMD5(soFile)) {
return false cacheInstall = false
return cacheInstall
} }
return soFile.exists() cacheInstall = soFile.exists()
return cacheInstall
} }
/**
* 预加载Cronet
*/
fun preDownload() { fun preDownload() {
if (AppConfig.isGooglePlay) { if (AppConfig.isGooglePlay) {
CronetProviderInstaller.installProvider(appCtx) CronetProviderInstaller.installProvider(appCtx)
@ -269,6 +285,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() {
Log.e(TAG, "download success, copy to $destSuccessFile") Log.e(TAG, "download success, copy to $destSuccessFile")
//下载成功拷贝文件 //下载成功拷贝文件
copyFile(downloadTempFile, destSuccessFile) copyFile(downloadTempFile, destSuccessFile)
cacheInstall=false
val parentFile = downloadTempFile.parentFile val parentFile = downloadTempFile.parentFile
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
deleteHistoryFile(parentFile!!, null) deleteHistoryFile(parentFile!!, null)

@ -15,7 +15,7 @@ import java.io.IOException
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.* import java.util.*
class CronetUrlRequestCallback @JvmOverloads internal constructor( class CronetRequestCallback @JvmOverloads internal constructor(
private val originalRequest: Request, private val originalRequest: Request,
private val mCall: Call, private val mCall: Call,
eventListener: EventListener? = null, eventListener: EventListener? = null,
@ -100,6 +100,8 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
info: UrlResponseInfo, info: UrlResponseInfo,
byteBuffer: ByteBuffer byteBuffer: ByteBuffer
) { ) {
byteBuffer.flip() byteBuffer.flip()
try { try {
@ -143,9 +145,8 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
responseCallback?.onFailure(mCall, e) responseCallback?.onFailure(mCall, e)
} }
override fun onCanceled(request: UrlRequest, info: UrlResponseInfo) { override fun onCanceled(request: UrlRequest, info: UrlResponseInfo?) {
mResponseCondition.open() mResponseCondition.open()
this.eventListener?.callEnd(mCall) this.eventListener?.callEnd(mCall)
@ -184,20 +185,23 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
private fun headersFromResponse(responseInfo: UrlResponseInfo): Headers { private fun headersFromResponse(responseInfo: UrlResponseInfo): Headers {
val headers = responseInfo.allHeadersAsList val headers = responseInfo.allHeadersAsList
val headerBuilder = Headers.Builder() return Headers.Builder().apply {
for ((key, value) in headers) { for ((key, value) in headers) {
try { try {
if (key.equals("content-encoding", ignoreCase = true)) {
// Strip all content encoding headers as decoding is done handled by cronet if (key.equals("content-encoding", ignoreCase = true)) {
continue // Strip all content encoding headers as decoding is done handled by cronet
continue
}
add(key, value)
} catch (e: Exception) {
Log.w(TAG, "Invalid HTTP header/value: $key$value")
// Ignore that header
} }
headerBuilder.add(key, value)
} catch (e: Exception) {
Log.w(TAG, "Invalid HTTP header/value: $key$value")
// Ignore that header
} }
}
return headerBuilder.build() }.build()
} }
private fun responseFromResponse( private fun responseFromResponse(

@ -118,6 +118,8 @@ class ReadAloudDialog : BaseDialogFragment() {
seekTtsSpeechRate.isEnabled = !isChecked seekTtsSpeechRate.isEnabled = !isChecked
upTtsSpeechRate() upTtsSpeechRate()
} }
//设置保存的默认值
seekTtsSpeechRate.progress=AppConfig.ttsSpeechRate
seekTtsSpeechRate.setOnSeekBarChangeListener(object : SeekBarChangeListener { seekTtsSpeechRate.setOnSeekBarChangeListener(object : SeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) { override fun onStopTrackingTouch(seekBar: SeekBar) {
AppConfig.ttsSpeechRate = seekBar.progress AppConfig.ttsSpeechRate = seekBar.progress

@ -22,4 +22,4 @@ kotlin.code.style=official
android.enableResourceOptimizations=true android.enableResourceOptimizations=true
CronetVersion=93.0.4577.62 CronetVersion=93.0.4577.82

Loading…
Cancel
Save