Skip to content

Commit

Permalink
Merge pull request #231 from prebid/async_replacement_bg_executor
Browse files Browse the repository at this point in the history
Removed AsyncTasks, replaced with executor implementation
  • Loading branch information
avohraa committed Feb 25, 2021
2 parents 3f53ddb + 60dd5ad commit ba1d1a7
Show file tree
Hide file tree
Showing 17 changed files with 879 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.prebid.mobile.tasksmanager.TasksManager;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -79,8 +81,13 @@ public void fetchDemand(@NonNull final OnCompleteListener2 listener) {

fetchDemand(keywordsMap, new OnCompleteListener() {
@Override
public void onComplete(ResultCode resultCode) {
listener.onComplete(resultCode, keywordsMap.size() != 0 ? Collections.unmodifiableMap(keywordsMap) : null);
public void onComplete(final ResultCode resultCode) {
TasksManager.getInstance().executeOnMainThread(new Runnable() {
@Override
public void run() {
listener.onComplete(resultCode, keywordsMap.size() != 0 ? Collections.unmodifiableMap(keywordsMap) : null);
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Looper;
import android.text.TextUtils;

import org.prebid.mobile.tasksmanager.TasksManager;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -56,7 +57,7 @@ private enum STATE {
private static STATE state = STATE.NOT_FETCHED;

/**
* Starts an AsyncTask to retrieve and set the AAID.
* Fetch a background executor to retrieve and set the AAID.
* Does nothing if PrebidServerSettings.aaid is already set for the SDK.
*
* @param context context to retrieve the AAID on.
Expand All @@ -68,19 +69,15 @@ static void retrieveAndSetAAID(Context context) {
}

AAIDTask getAAIDTask = new AAIDTask(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getAAIDTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
getAAIDTask.execute();
}
getAAIDTask.execute();
}

/**
* Retrieves AAID from GooglePlayServices via reflection
* Sets the SDK's aaid value to the result if successful,
* or null if failed.
*/
private static class AAIDTask extends AsyncTask<Void, Void, Void> {
private static class AAIDTask {
private static final String cAdvertisingIdClientName
= "com.google.android.gms.ads.identifier.AdvertisingIdClient";
private static final String cAdvertisingIdClientInfoName
Expand All @@ -92,8 +89,20 @@ private AAIDTask(Context context) {
this.context = new WeakReference<Context>(context);
}

@Override
protected Void doInBackground(Void... params) {
protected void execute() {
if (Looper.myLooper() == Looper.getMainLooper()) {
TasksManager.getInstance().executeOnBackgroundThread(new Runnable() {
@Override
public void run() {
fetchAAID();
}
});
} else {
fetchAAID();
}
}

private void fetchAAID() {
state = STATE.FETCHING;
// attempt to retrieve AAID from GooglePlayServices via reflection
// Setting aaid in the backend thread
Expand Down Expand Up @@ -129,7 +138,6 @@ protected Void doInBackground(Void... params) {
} else {
state = STATE.FETCHED;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import org.prebid.mobile.tasksmanager.TasksManager;

import java.util.HashMap;
import java.util.UUID;

Expand Down Expand Up @@ -131,12 +133,19 @@ private void notifyListener(final ResultCode resultCode) {
LogUtil.d("notifyListener:" + resultCode);

if (listener != null) {
listener.onComplete(resultCode);
}
// for single request, if done, finish current fetcher,
// let ad unit create a new fetcher for next request
if (periodMillis <= 0) {
destroy();
TasksManager.getInstance().executeOnMainThread(new Runnable() {
@Override
public void run() {
if (listener != null) {
listener.onComplete(resultCode);
}
// for single request, if done, finish current fetcher,
// let ad unit create a new fetcher for next request
if (periodMillis <= 0) {
destroy();
}
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,55 @@

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Looper;
import android.widget.ImageView;

import org.prebid.mobile.tasksmanager.TasksManager;

import java.io.InputStream;
import java.lang.ref.WeakReference;

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
public class DownloadImageTask {

WeakReference<ImageView> imageRef;

public DownloadImageTask(ImageView image) {
protected DownloadImageTask(ImageView image) {
this.imageRef = new WeakReference<>(image);
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
protected void execute(final String url) {
if (Looper.myLooper() == Looper.getMainLooper()) {
TasksManager.getInstance().executeOnBackgroundThread(new Runnable() {
@Override
public void run() {
fetchAndProcessImage(url);
}
});
} else {
fetchAndProcessImage(url);
}
}

private void fetchAndProcessImage(String url) {
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
InputStream in = new java.net.URL(url).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
LogUtil.e("Error", e.getMessage());
}
return mIcon11;
processImage(bitmap);
}

protected void onPostExecute(Bitmap result) {
ImageView image = this.imageRef.get();
if (image != null) {
image.setImageBitmap(result);
}
private void processImage(final Bitmap result) {
TasksManager.getInstance().executeOnMainThread(new Runnable() {
@Override
public void run() {
ImageView image = imageRef.get();
if (image != null) {
image.setImageBitmap(result);
}
}
});
}
}
Loading

0 comments on commit ba1d1a7

Please sign in to comment.