|
| 1 | +package com.datadog.cronet.sample |
| 2 | + |
| 3 | +import android.graphics.BitmapFactory |
| 4 | +import android.os.Bundle |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.activity.compose.setContent |
| 7 | +import androidx.activity.enableEdgeToEdge |
| 8 | +import androidx.compose.foundation.Image |
| 9 | +import androidx.compose.foundation.layout.Arrangement |
| 10 | +import androidx.compose.foundation.layout.Column |
| 11 | +import androidx.compose.foundation.layout.Spacer |
| 12 | +import androidx.compose.foundation.layout.fillMaxSize |
| 13 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 14 | +import androidx.compose.foundation.layout.height |
| 15 | +import androidx.compose.foundation.layout.padding |
| 16 | +import androidx.compose.material3.Button |
| 17 | +import androidx.compose.material3.CircularProgressIndicator |
| 18 | +import androidx.compose.material3.MaterialTheme |
| 19 | +import androidx.compose.material3.Scaffold |
| 20 | +import androidx.compose.material3.Text |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.runtime.getValue |
| 23 | +import androidx.compose.runtime.mutableStateOf |
| 24 | +import androidx.compose.runtime.remember |
| 25 | +import androidx.compose.runtime.setValue |
| 26 | +import androidx.compose.ui.Alignment |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.graphics.ImageBitmap |
| 29 | +import androidx.compose.ui.graphics.asImageBitmap |
| 30 | +import androidx.compose.ui.unit.dp |
| 31 | +import org.chromium.net.CronetEngine |
| 32 | +import org.chromium.net.CronetException |
| 33 | +import org.chromium.net.UrlRequest |
| 34 | +import org.chromium.net.UrlResponseInfo |
| 35 | +import java.nio.ByteBuffer |
| 36 | +import java.util.concurrent.Executors |
| 37 | + |
| 38 | +class MainActivity : ComponentActivity() { |
| 39 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 40 | + super.onCreate(savedInstanceState) |
| 41 | + enableEdgeToEdge() |
| 42 | + setContent { |
| 43 | + MaterialTheme { |
| 44 | + Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> |
| 45 | + ImageDownloaderScreen( |
| 46 | + cronetEngine = (application as SampleApplication).cronetEngine, |
| 47 | + modifier = Modifier.padding(innerPadding) |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +@Composable |
| 56 | +fun ImageDownloaderScreen( |
| 57 | + cronetEngine: CronetEngine, |
| 58 | + modifier: Modifier = Modifier |
| 59 | +) { |
| 60 | + var imageBitmap by remember { mutableStateOf<ImageBitmap?>(null) } |
| 61 | + var isLoading by remember { mutableStateOf(false) } |
| 62 | + var errorMessage by remember { mutableStateOf<String?>(null) } |
| 63 | + |
| 64 | + Column( |
| 65 | + modifier = modifier |
| 66 | + .fillMaxSize() |
| 67 | + .padding(16.dp), |
| 68 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 69 | + verticalArrangement = Arrangement.Center |
| 70 | + ) { |
| 71 | + Button( |
| 72 | + onClick = { |
| 73 | + isLoading = true |
| 74 | + errorMessage = null |
| 75 | + downloadImage( |
| 76 | + cronetEngine = cronetEngine, |
| 77 | + url = "https://storage.googleapis.com/cronet/sun.jpg", |
| 78 | + onSuccess = { bitmap -> |
| 79 | + imageBitmap = bitmap |
| 80 | + isLoading = false |
| 81 | + }, |
| 82 | + onError = { error -> |
| 83 | + errorMessage = error |
| 84 | + isLoading = false |
| 85 | + } |
| 86 | + ) |
| 87 | + }, |
| 88 | + enabled = !isLoading |
| 89 | + ) { |
| 90 | + Text("Download Image") |
| 91 | + } |
| 92 | + |
| 93 | + Spacer(modifier = Modifier.height(16.dp)) |
| 94 | + |
| 95 | + when { |
| 96 | + isLoading -> { |
| 97 | + CircularProgressIndicator() |
| 98 | + } |
| 99 | + errorMessage != null -> { |
| 100 | + Text("Error: $errorMessage") |
| 101 | + } |
| 102 | + imageBitmap != null -> { |
| 103 | + Image( |
| 104 | + bitmap = imageBitmap!!, |
| 105 | + contentDescription = "Downloaded image", |
| 106 | + modifier = Modifier.fillMaxWidth() |
| 107 | + ) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +private fun downloadImage( |
| 114 | + cronetEngine: CronetEngine, |
| 115 | + url: String, |
| 116 | + onSuccess: (ImageBitmap) -> Unit, |
| 117 | + onError: (String) -> Unit |
| 118 | +) { |
| 119 | + val executor = Executors.newSingleThreadExecutor() |
| 120 | + val byteArrayOutputStream = mutableListOf<ByteArray>() |
| 121 | + |
| 122 | + val callback = object : UrlRequest.Callback() { |
| 123 | + override fun onRedirectReceived( |
| 124 | + request: UrlRequest, |
| 125 | + info: UrlResponseInfo, |
| 126 | + newLocationUrl: String |
| 127 | + ) { |
| 128 | + request.followRedirect() |
| 129 | + } |
| 130 | + |
| 131 | + override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) { |
| 132 | + request.read(ByteBuffer.allocateDirect(32 * 1024)) |
| 133 | + } |
| 134 | + |
| 135 | + override fun onReadCompleted( |
| 136 | + request: UrlRequest, |
| 137 | + info: UrlResponseInfo, |
| 138 | + byteBuffer: ByteBuffer |
| 139 | + ) { |
| 140 | + byteBuffer.flip() |
| 141 | + val bytes = ByteArray(byteBuffer.remaining()) |
| 142 | + byteBuffer.get(bytes) |
| 143 | + byteArrayOutputStream.add(bytes) |
| 144 | + byteBuffer.clear() |
| 145 | + request.read(byteBuffer) |
| 146 | + } |
| 147 | + |
| 148 | + override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { |
| 149 | + val totalSize = byteArrayOutputStream.sumOf { it.size } |
| 150 | + val imageBytes = ByteArray(totalSize) |
| 151 | + var offset = 0 |
| 152 | + for (chunk in byteArrayOutputStream) { |
| 153 | + System.arraycopy(chunk, 0, imageBytes, offset, chunk.size) |
| 154 | + offset += chunk.size |
| 155 | + } |
| 156 | + |
| 157 | + val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size) |
| 158 | + if (bitmap != null) { |
| 159 | + onSuccess(bitmap.asImageBitmap()) |
| 160 | + } else { |
| 161 | + onError("Failed to decode image") |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { |
| 166 | + onError(error.message ?: "Unknown error") |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + val requestBuilder = cronetEngine.newUrlRequestBuilder(url, callback, executor) |
| 171 | + requestBuilder.build().start() |
| 172 | +} |
0 commit comments