mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
fix AsyncTask deprecation suppression in ImageStoreManager.kt (#44113)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44113 Changelog: [Internal] as title Reviewed By: cortinico Differential Revision: D56189597 fbshipit-source-id: 5e33f5db378c634a3ef34b1124bb25d3c97308ed
This commit is contained in:
committed by
Facebook GitHub Bot
parent
89eb794751
commit
94a58a8e42
+21
-35
@@ -5,26 +5,21 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// TODO T185922447 migrate from deprecated AsyncTask
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package com.facebook.react.modules.camera
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.util.Base64
|
||||
import android.util.Base64OutputStream
|
||||
import com.facebook.fbreact.specs.NativeImageStoreAndroidSpec
|
||||
import com.facebook.react.bridge.Callback
|
||||
import com.facebook.react.bridge.GuardedAsyncTask
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactContext
|
||||
import com.facebook.react.module.annotations.ReactModule
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.Closeable
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@ReactModule(name = NativeImageStoreAndroidSpec.NAME)
|
||||
public class ImageStoreManager(reactContext: ReactApplicationContext) :
|
||||
@@ -38,21 +33,12 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) :
|
||||
* @param error callback to be invoked on error (e.g. file not found, not readable etc.)
|
||||
*/
|
||||
override public fun getBase64ForTag(uri: String, success: Callback, error: Callback) {
|
||||
GetBase64Task(getReactApplicationContext(), uri, success, error)
|
||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
|
||||
}
|
||||
|
||||
private inner class GetBase64Task(
|
||||
reactContext: ReactContext,
|
||||
private val uri: String,
|
||||
private val success: Callback,
|
||||
private val error: Callback
|
||||
) : GuardedAsyncTask<Void?, Void?>(reactContext) {
|
||||
protected override fun doInBackgroundGuarded(vararg params: Void?) {
|
||||
val executor = Executors.newSingleThreadExecutor()
|
||||
executor.execute {
|
||||
try {
|
||||
val contentResolver = getReactApplicationContext().getContentResolver()
|
||||
val uri = Uri.parse(uri)
|
||||
val inputStream = contentResolver.openInputStream(uri) as InputStream
|
||||
val parsedUri = Uri.parse(uri)
|
||||
val inputStream = contentResolver.openInputStream(parsedUri) as InputStream
|
||||
try {
|
||||
success.invoke(convertInputStreamToBase64OutputStream(inputStream))
|
||||
} catch (e: IOException) {
|
||||
@@ -66,22 +52,6 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) :
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
internal fun convertInputStreamToBase64OutputStream(inputStream: InputStream): String {
|
||||
val baos = ByteArrayOutputStream()
|
||||
val b64os = Base64OutputStream(baos, Base64.NO_WRAP)
|
||||
val buffer = ByteArray(BUFFER_SIZE)
|
||||
var bytesRead: Int
|
||||
try {
|
||||
while (inputStream.read(buffer).also { bytesRead = it } > -1) {
|
||||
b64os.write(buffer, 0, bytesRead)
|
||||
}
|
||||
} finally {
|
||||
closeQuietly(b64os) // this also closes baos and flushes the final content to it
|
||||
}
|
||||
return baos.toString()
|
||||
}
|
||||
|
||||
public companion object {
|
||||
public const val NAME: String = NativeImageStoreAndroidSpec.NAME
|
||||
|
||||
@@ -94,5 +64,21 @@ public class ImageStoreManager(reactContext: ReactApplicationContext) :
|
||||
// shhh
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
internal fun convertInputStreamToBase64OutputStream(inputStream: InputStream): String {
|
||||
val baos = ByteArrayOutputStream()
|
||||
val b64os = Base64OutputStream(baos, Base64.NO_WRAP)
|
||||
val buffer = ByteArray(BUFFER_SIZE)
|
||||
var bytesRead: Int
|
||||
try {
|
||||
while (inputStream.read(buffer).also { bytesRead = it } > -1) {
|
||||
b64os.write(buffer, 0, bytesRead)
|
||||
}
|
||||
} finally {
|
||||
closeQuietly(b64os) // this also closes baos and flushes the final content to it
|
||||
}
|
||||
return baos.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -71,8 +71,7 @@ class ImageStoreManagerTest {
|
||||
}
|
||||
|
||||
private fun invokeConversion(inputStream: InputStream): String {
|
||||
return ImageStoreManager(reactApplicationContext)
|
||||
.convertInputStreamToBase64OutputStream(inputStream)
|
||||
return ImageStoreManager.convertInputStreamToBase64OutputStream(inputStream)
|
||||
}
|
||||
|
||||
private fun generateRandomByteString(length: Int): ByteArray {
|
||||
|
||||
Reference in New Issue
Block a user