diff --git a/perf/app/build.gradle b/perf/app/build.gradle index bdc4e3991..62cdf69a3 100644 --- a/perf/app/build.gradle +++ b/perf/app/build.gradle @@ -49,6 +49,7 @@ dependencies { implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0' implementation 'com.github.bumptech.glide:glide:3.7.0' diff --git a/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/java/MainActivity.java b/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/java/MainActivity.java index a4572a049..5310e98e8 100644 --- a/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/java/MainActivity.java +++ b/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/java/MainActivity.java @@ -1,7 +1,6 @@ package com.google.firebase.quickstart.perfmon.java; import android.graphics.drawable.ColorDrawable; -import android.os.AsyncTask; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; @@ -16,7 +15,7 @@ import com.bumptech.glide.request.target.Target; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; -import com.google.android.gms.tasks.Tasks; +import com.google.android.gms.tasks.TaskCompletionSource; import com.google.firebase.perf.FirebasePerformance; import com.google.firebase.perf.metrics.Trace; import com.google.firebase.quickstart.perfmon.R; @@ -25,10 +24,11 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Random; -import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @@ -130,45 +130,55 @@ public boolean onResourceReady( } private Task writeStringToFile(final String filename, final String content) { - return Tasks.call( - AsyncTask.THREAD_POOL_EXECUTOR, - new Callable() { - @Override - public Void call() throws Exception { - FileOutputStream fos = new FileOutputStream(filename, true); - fos.write(content.getBytes()); - fos.close(); - return null; - } - }); + final TaskCompletionSource taskCompletionSource = new TaskCompletionSource<>(); + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + try { + FileOutputStream fos = new FileOutputStream(filename, true); + fos.write(content.getBytes()); + fos.close(); + taskCompletionSource.setResult(null); + } catch (IOException e) { + e.printStackTrace(); + } + + } + }); + return taskCompletionSource.getTask(); } private Task loadStringFromFile() { - return Tasks.call( - AsyncTask.THREAD_POOL_EXECUTOR, - new Callable() { - @Override - public String call() throws Exception { - File contentFile = new File(getFilesDir(), CONTENT_FILE); - if (contentFile.createNewFile()) { - // Content file exist did not exist in internal storage and new file was created. - // Copy in the default content. - InputStream is; - is = getAssets().open(DEFAULT_CONTENT_FILE); - int size = is.available(); - byte[] buffer = new byte[size]; - is.read(buffer); - is.close(); - FileOutputStream fos = new FileOutputStream(contentFile); - fos.write(buffer); - fos.close(); - } - FileInputStream fis = new FileInputStream(contentFile); - byte[] content = new byte[(int) contentFile.length()]; - fis.read(content); - return new String(content); + final TaskCompletionSource taskCompletionSource = new TaskCompletionSource<>(); + Executors.newSingleThreadExecutor().execute(new Runnable() { + @Override + public void run() { + try { + File contentFile = new File(getFilesDir(), CONTENT_FILE); + if (contentFile.createNewFile()) { + // Content file exist did not exist in internal storage and new file was created. + // Copy in the default content. + InputStream is; + is = getAssets().open(DEFAULT_CONTENT_FILE); + int size = is.available(); + byte[] buffer = new byte[size]; + is.read(buffer); + is.close(); + FileOutputStream fos = new FileOutputStream(contentFile); + fos.write(buffer); + fos.close(); } - }); + FileInputStream fis = new FileInputStream(contentFile); + byte[] content = new byte[(int) contentFile.length()]; + fis.read(content); + taskCompletionSource.setResult(new String(content)); + } catch (IOException e) { + e.printStackTrace(); + } + + } + }); + return taskCompletionSource.getTask(); } private void loadFileFromDisk() { diff --git a/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/kotlin/MainActivity.kt b/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/kotlin/MainActivity.kt index 9bba09765..ee1c7a841 100644 --- a/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/kotlin/MainActivity.kt +++ b/perf/app/src/main/java/com/google/firebase/quickstart/perfmon/kotlin/MainActivity.kt @@ -1,30 +1,32 @@ package com.google.firebase.quickstart.perfmon.kotlin import android.graphics.drawable.ColorDrawable -import android.os.AsyncTask import android.os.Bundle import androidx.core.content.ContextCompat import androidx.appcompat.app.AppCompatActivity import android.util.Log import android.widget.Toast +import androidx.lifecycle.lifecycleScope import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.drawable.GlideDrawable import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.google.android.gms.tasks.OnCompleteListener import com.google.android.gms.tasks.Task -import com.google.android.gms.tasks.Tasks +import com.google.android.gms.tasks.TaskCompletionSource import com.google.firebase.ktx.Firebase import com.google.firebase.perf.ktx.performance import com.google.firebase.perf.metrics.Trace import com.google.firebase.quickstart.perfmon.R import com.google.firebase.quickstart.perfmon.databinding.ActivityMainBinding +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.InputStream import java.util.Random -import java.util.concurrent.Callable import java.util.concurrent.CountDownLatch class MainActivity : AppCompatActivity() { @@ -109,38 +111,42 @@ class MainActivity : AppCompatActivity() { } private fun writeStringToFile(filename: String, content: String): Task { - return Tasks.call( - AsyncTask.THREAD_POOL_EXECUTOR, - Callable { - val fos = FileOutputStream(filename, true) - fos.write(content.toByteArray()) - fos.close() - null - }) + val taskCompletionSource = TaskCompletionSource() + lifecycleScope.launch { + withContext(Dispatchers.IO) { + val fos = FileOutputStream(filename, true) + fos.write(content.toByteArray()) + fos.close() + taskCompletionSource.setResult(null) + } + } + return taskCompletionSource.task } private fun loadStringFromFile(): Task { - return Tasks.call( - AsyncTask.THREAD_POOL_EXECUTOR, - Callable { - val contentFile = File(filesDir, CONTENT_FILE) - if (contentFile.createNewFile()) { - // Content file exist did not exist in internal storage and new file was created. - // Copy in the default content. - val `is`: InputStream = assets.open(DEFAULT_CONTENT_FILE) - val size = `is`.available() - val buffer = ByteArray(size) - `is`.read(buffer) - `is`.close() - val fos = FileOutputStream(contentFile) - fos.write(buffer) - fos.close() - } - val fis = FileInputStream(contentFile) - val content = ByteArray(contentFile.length().toInt()) - fis.read(content) - return@Callable String(content) - }) + val taskCompletionSource = TaskCompletionSource() + lifecycleScope.launch { + withContext(Dispatchers.IO) { + val contentFile = File(filesDir, CONTENT_FILE) + if (contentFile.createNewFile()) { + // Content file exist did not exist in internal storage and new file was created. + // Copy in the default content. + val `is`: InputStream = assets.open(DEFAULT_CONTENT_FILE) + val size = `is`.available() + val buffer = ByteArray(size) + `is`.read(buffer) + `is`.close() + val fos = FileOutputStream(contentFile) + fos.write(buffer) + fos.close() + } + val fis = FileInputStream(contentFile) + val content = ByteArray(contentFile.length().toInt()) + fis.read(content) + taskCompletionSource.setResult(String(content)) + } + } + return taskCompletionSource.task } private fun loadFileFromDisk() {