Skip to content

Add manifest file Internet permission #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.codelabs.cronet">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand Down
1 change: 1 addition & 0 deletions solution/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.codelabs.cronet">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ internal class CronetImageDownloader (
request: UrlRequest,
info: UrlResponseInfo,
bodyBytes: ByteArray) {
engine.stopNetLog()

cont.resume(ImageDownloaderResult(
successful = true,
blob = bodyBytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ImagesViewModel : ViewModel() {
val images = mutableStateListOf<DownloadedImage>()

fun addImage(): Int {
images.add(DownloadedImage(URLS[Random.nextInt(URLS.size)]))
// images.add(DownloadedImage(URLS[Random.nextInt(URLS.size)]))
images.add(DownloadedImage(URLS[5]))

return images.size - 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.google.android.gms.net.CronetProviderInstaller
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.chromium.net.CronetEngine
import java.io.File
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Supplier

Expand All @@ -67,9 +68,15 @@ class MainActivity : ComponentActivity() {
CronetProviderInstaller.installProvider(ctx).addOnCompleteListener {
if (it.isSuccessful) {
Log.i(LOGGER_TAG, "Successfully installed Play Services provider: $it")
val logFile = File(ctx.cacheDir, "network_log.json")

val cronetEngine = CronetEngine.Builder(ctx)
.setStoragePath(ctx.cacheDir.absolutePath)
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 10 * 1024 * 1024)
.build()
// .enableQuic(true)
.build().apply {
startNetLogToFile(logFile.absolutePath, /* include_http2 */ true)
}
imageDownloader.set(CronetImageDownloader(cronetEngine))
} else {
Log.w(LOGGER_TAG, "Unable to load Cronet from Play Services", it.exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ internal abstract class ReadToMemoryCronetCallback : UrlRequest.Callback() {
final override fun onRedirectReceived(
request: UrlRequest, info: UrlResponseInfo?, newLocationUrl: String?
) {
Log.d("Cronet", "Response Redirect")

request.followRedirect()
}

final override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) {
Log.i(TAG, "****** Response Started ******")
Log.i(TAG, "*** Headers Are *** ${info.allHeaders}")
Log.d("Cronet", "Response started")
Log.d("Cronet", "URL: ${info}")

// One must use a *direct* byte buffer when calling the read method.
request.read(ByteBuffer.allocateDirect(BYTE_BUFFER_CAPACITY_BYTES))
Expand All @@ -44,6 +48,9 @@ internal abstract class ReadToMemoryCronetCallback : UrlRequest.Callback() {
final override fun onReadCompleted(
request: UrlRequest, info: UrlResponseInfo, byteBuffer: ByteBuffer
) {
Log.d("Cronet", "Read completed")
Log.d("Cronet", "Buffer remaining: ${byteBuffer.remaining()}")

// The byte buffer we're getting in the callback hasn't been flipped for reading,
// so flip it so we can read the content.
byteBuffer.flip()
Expand All @@ -58,6 +65,8 @@ internal abstract class ReadToMemoryCronetCallback : UrlRequest.Callback() {

final override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) {
val bodyBytes = bytesReceived.toByteArray()
Log.d("Cronet", "Request succeeded")
Log.d("Cronet", "URL: ${info.url}")

// We invoke the callback directly here for simplicity. Note that the executor running this
// callback might be shared with other Cronet requests, or even with other parts of your
Expand Down