Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan09811 authored Jul 2, 2024
1 parent 95d174b commit c8bc27d
Showing 1 changed file with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.github.troppical.network

import android.content.Context
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.ResponseBody
import com.google.android.gms.net.CronetProviderInstaller
import org.chromium.net.CronetEngine
import com.google.net.cronet.okhttp.CronetCallFactory
import org.chromium.net.UrlRequest
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.concurrent.Executor
import java.util.concurrent.Executors

class APKDownloader(private val context: Context, private val url: String, private val outputFile: File) {

Expand All @@ -18,22 +18,16 @@ class APKDownloader(private val context: Context, private val url: String, priva
useCronet = enable
}

private fun getClient(): OkHttpClient {
return if (useCronet) {
val cronetEngine = CronetEngine.Builder(context)
.build()
val callFactory = CronetCallFactory.newBuilder(cronetEngine).build()

OkHttpClient.Builder()
.callFactory(callFactory)
.build()
fun download(onProgress: (Int) -> Unit, onComplete: (Boolean) -> Unit) {
if (useCronet) {
downloadWithCronet(onProgress, onComplete)
} else {
OkHttpClient()
downloadWithOkHttp(onProgress, onComplete)
}
}

fun download(onProgress: (Int) -> Unit, onComplete: (Boolean) -> Unit) {
val client = getClient()
private fun downloadWithOkHttp(onProgress: (Int) -> Unit, onComplete: (Boolean) -> Unit) {
val client = OkHttpClient()
val request = Request.Builder().url(url).build()

client.newCall(request).enqueue(object : okhttp3.Callback {
Expand Down Expand Up @@ -76,4 +70,62 @@ class APKDownloader(private val context: Context, private val url: String, priva
}
})
}

private fun downloadWithCronet(onProgress: (Int) -> Unit, onComplete: (Boolean) -> Unit) {
val cronetEngine = CronetEngine.Builder(context)
.build()
val executor: Executor = Executors.newSingleThreadExecutor()

val requestBuilder = cronetEngine.newUrlRequestBuilder(
url,
object : UrlRequest.Callback() {
override fun onRedirectReceived(
request: UrlRequest?,
info: UrlRequest.RedirectInfo?
) {
// Handle redirect if necessary
}

override fun onResponseStarted(request: UrlRequest?, info: UrlRequest.ResponseInfo?) {
// Start receiving response
}

override fun onReadCompleted(
request: UrlRequest?,
info: UrlRequest.ResponseInfo?,
byteBuffer: java.nio.ByteBuffer?
) {
// Read and save the response
byteBuffer?.array()?.let { byteArray ->
try {
val outputStream = FileOutputStream(outputFile, true)
outputStream.write(byteArray)
outputStream.flush()
outputStream.close()

// Calculate progress
val totalBytesRead = info?.receivedByteCount ?: 0L
val progress = (totalBytesRead * 100 / info?.contentLength ?: 1L).toInt()
onProgress(progress)
} catch (e: IOException) {
e.printStackTrace()
onComplete(false)
}
}
}

override fun onSucceeded(request: UrlRequest?, info: UrlRequest.ResponseInfo?) {
onComplete(true)
}

override fun onFailed(request: UrlRequest?, info: UrlRequest.ResponseInfo?, error: java.io.IOException?) {
error?.printStackTrace()
onComplete(false)
}
},
executor
)

requestBuilder.build().start()
}
}

0 comments on commit c8bc27d

Please sign in to comment.