Skip to content

Commit

Permalink
Revert "fix(ANR): Moves saveRequestForLater from main thread. Adds sy…
Browse files Browse the repository at this point in the history
…nchroniz… (#162)"

This reverts commit 1a3e3b9.
  • Loading branch information
Alexis Oyama committed Jun 7, 2018
1 parent 71cdd02 commit 5ee0d76
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;

import com.leanplum.Leanplum;
import com.leanplum.utils.SharedPreferencesUtil;
Expand All @@ -42,8 +41,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
* LeanplumEventDataManager class to work with SQLite.
Expand All @@ -60,7 +57,6 @@ public class LeanplumEventDataManager {
private static SQLiteDatabase database;
private static LeanplumDataBaseManager databaseManager;
private static ContentValues contentValues = new ContentValues();
private static final Executor sqlLiteThreadExecutor = Executors.newSingleThreadExecutor();

static boolean willSendErrorLog = false;

Expand All @@ -69,7 +65,7 @@ public class LeanplumEventDataManager {
*
* @param context Current context.
*/
public static synchronized void init(Context context) {
public static void init(Context context) {
if (database != null) {
Log.e("Database is already initialized.");
return;
Expand Down Expand Up @@ -185,17 +181,6 @@ private static void handleSQLiteError(String log, Throwable t) {
}
}

/**
* Execute async task on single thread Executer.
*
* @param task Async task to execute.
* @param params Params.
*/
static <T> void executeAsyncTask(AsyncTask<T, ?, ?> task,
T... params) {
task.executeOnExecutor(sqlLiteThreadExecutor, params);
}

private static class LeanplumDataBaseManager extends SQLiteOpenHelper {
LeanplumDataBaseManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Expand Down
57 changes: 23 additions & 34 deletions AndroidSDKCore/src/main/java/com/leanplum/internal/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,41 +230,30 @@ private Map<String, Object> createArgsDictionary() {
return args;
}

private void saveRequestForLater(final Map<String, Object> args) {
final Request currentRequest = this;
LeanplumEventDataManager.executeAsyncTask(new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
synchronized (Request.class) {
Context context = Leanplum.getContext();
SharedPreferences preferences = context.getSharedPreferences(
LEANPLUM, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
long count = LeanplumEventDataManager.getEventsCount();
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
uuid = UUID.randomUUID().toString();
editor.putString(Constants.Defaults.UUID_KEY, uuid);
SharedPreferencesUtil.commitChanges(editor);
}
args.put(UUID_KEY, uuid);
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));

dataBaseIndex = count;
// Checks if here response and/or error callback for this request. We need to add callbacks to
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
// will handle error callback for this event.
if (response != null || error != null && !Util.isConnected()) {
eventCallbackManager.addCallbacks(currentRequest, response, error);
}
}
} catch (Throwable t) {
Util.handleException(t);
}
return null;
private void saveRequestForLater(Map<String, Object> args) {
synchronized (Request.class) {
Context context = Leanplum.getContext();
SharedPreferences preferences = context.getSharedPreferences(
LEANPLUM, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
long count = LeanplumEventDataManager.getEventsCount();
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
uuid = UUID.randomUUID().toString();
editor.putString(Constants.Defaults.UUID_KEY, uuid);
SharedPreferencesUtil.commitChanges(editor);
}
});
args.put(UUID_KEY, uuid);
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));

dataBaseIndex = count;
// Checks if here response and/or error callback for this request. We need to add callbacks to
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
// will handle error callback for this event.
if (response != null || error != null && !Util.isConnected()) {
eventCallbackManager.addCallbacks(this, response, error);
}
}
}

public void send() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ public void before() throws Exception {
// Mock with our executor which will run on main thread.
ReflectionHelpers.setStaticField(Util.class, "asyncExecutor", new SynchronousExecutor());
ReflectionHelpers.setStaticField(Util.class, "singleThreadExecutor", new SynchronousExecutor());
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "sqlLiteThreadExecutor",
new SynchronousExecutor());

ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "databaseManager", null);
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "database", null);
// Get and set application context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.leanplum.Leanplum;
import com.leanplum.__setup.LeanplumTestApp;
import com.leanplum._whitebox.utilities.SynchronousExecutor;

import junit.framework.TestCase;

Expand All @@ -35,7 +34,6 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -67,8 +65,6 @@ public void setUp() {
Application context = RuntimeEnvironment.application;
assertNotNull(context);
Leanplum.setApplicationContext(context);
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "sqlLiteThreadExecutor", new SynchronousExecutor());
LeanplumEventDataManagerTest.setDatabaseToNull();
}

/**
Expand Down

0 comments on commit 5ee0d76

Please sign in to comment.