Skip to content

Commit

Permalink
refactor(perf): replace AsyncTask with Executor in java (firebase#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatfiredev authored Nov 24, 2020
1 parent 9074fe0 commit c67035b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 70 deletions.
1 change: 1 addition & 0 deletions perf/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -130,45 +130,55 @@ public boolean onResourceReady(
}

private Task<Void> writeStringToFile(final String filename, final String content) {
return Tasks.call(
AsyncTask.THREAD_POOL_EXECUTOR,
new Callable<Void>() {
@Override
public Void call() throws Exception {
FileOutputStream fos = new FileOutputStream(filename, true);
fos.write(content.getBytes());
fos.close();
return null;
}
});
final TaskCompletionSource<Void> 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<String> loadStringFromFile() {
return Tasks.call(
AsyncTask.THREAD_POOL_EXECUTOR,
new Callable<String>() {
@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<String> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -109,38 +111,42 @@ class MainActivity : AppCompatActivity() {
}

private fun writeStringToFile(filename: String, content: String): Task<Void> {
return Tasks.call(
AsyncTask.THREAD_POOL_EXECUTOR,
Callable<Void> {
val fos = FileOutputStream(filename, true)
fos.write(content.toByteArray())
fos.close()
null
})
val taskCompletionSource = TaskCompletionSource<Void>()
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<String> {
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<String>()
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() {
Expand Down

0 comments on commit c67035b

Please sign in to comment.