Skip to content

Commit 54b0ba9

Browse files
authored
Using OperationQueue (#385)
1 parent 5ff6470 commit 54b0ba9

File tree

16 files changed

+230
-348
lines changed

16 files changed

+230
-348
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 165 additions & 181 deletions
Large diffs are not rendered by default.

AndroidSDKCore/src/main/java/com/leanplum/LeanplumActivityHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import android.app.Application.ActivityLifecycleCallbacks;
2727
import android.content.Context;
2828
import android.content.res.Resources;
29-
import android.os.Build;
3029
import android.os.Bundle;
3130

3231
import com.leanplum.annotations.Parser;
3332
import com.leanplum.callbacks.PostponableAction;
3433
import com.leanplum.internal.ActionManager;
3534
import com.leanplum.internal.LeanplumInternal;
3635
import com.leanplum.internal.LeanplumUIEditorWrapper;
37-
import com.leanplum.internal.OsHandler;
36+
import com.leanplum.internal.OperationQueue;
3837
import com.leanplum.internal.Util;
3938

4039
import java.util.Collections;
@@ -128,7 +127,7 @@ public void onActivityResumed(final Activity activity) {
128127
if (Leanplum.isInterfaceEditingEnabled()) {
129128
// Execute runnable in next frame to ensure that all system stuff is setup, before
130129
// applying UI edits.
131-
OsHandler.getInstance().post(new Runnable() {
130+
OperationQueue.sharedInstance().addUiOperation(new Runnable() {
132131
@Override
133132
public void run() {
134133
LeanplumUIEditorWrapper.getInstance().applyInterfaceEdits(activity);

AndroidSDKCore/src/main/java/com/leanplum/LeanplumInbox.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.leanplum.internal.Constants;
3333
import com.leanplum.internal.JsonConverter;
3434
import com.leanplum.internal.Log;
35-
import com.leanplum.internal.OsHandler;
35+
import com.leanplum.internal.OperationQueue;
3636
import com.leanplum.internal.RequestOld;
3737
import com.leanplum.internal.Util;
3838
import com.leanplum.utils.SharedPreferencesUtil;
@@ -262,7 +262,7 @@ void removeMessage(String messageId) {
262262
void triggerChanged() {
263263
synchronized (changedCallbacks) {
264264
for (InboxChangedCallback callback : changedCallbacks) {
265-
OsHandler.getInstance().post(callback);
265+
OperationQueue.sharedInstance().addUiOperation(callback);
266266
}
267267
}
268268
}
@@ -275,7 +275,7 @@ void triggerInboxSyncedWithStatus(boolean success) {
275275
synchronized (changedCallbacks) {
276276
for (InboxSyncedCallback callback : syncedCallbacks) {
277277
callback.setSuccess(success);
278-
OsHandler.getInstance().post(callback);
278+
OperationQueue.sharedInstance().addUiOperation(callback);
279279
}
280280
}
281281
}

AndroidSDKCore/src/main/java/com/leanplum/Var.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import com.leanplum.internal.FileManager.DownloadFileResult;
3030
import com.leanplum.internal.LeanplumInternal;
3131
import com.leanplum.internal.Log;
32-
import com.leanplum.internal.OsHandler;
32+
import com.leanplum.internal.OperationQueue;
3333
import com.leanplum.internal.Util;
3434
import com.leanplum.internal.VarCache;
3535

@@ -409,7 +409,7 @@ private void triggerValueChanged() {
409409
synchronized (valueChangedHandlers) {
410410
for (VariableCallback<T> callback : valueChangedHandlers) {
411411
callback.setVariable(this);
412-
OsHandler.getInstance().post(callback);
412+
OperationQueue.sharedInstance().addUiOperation(callback);
413413
}
414414
}
415415
}
@@ -449,7 +449,7 @@ private void triggerFileIsReady() {
449449
fileIsPending = false;
450450
for (VariableCallback<T> callback : fileReadyHandlers) {
451451
callback.setVariable(this);
452-
OsHandler.getInstance().post(callback);
452+
OperationQueue.sharedInstance().addUiOperation(callback);
453453
}
454454
}
455455
}

AndroidSDKCore/src/main/java/com/leanplum/internal/FileManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.content.Context;
2626
import android.content.res.Resources;
2727
import android.net.Uri;
28-
import android.os.AsyncTask;
2928
import android.text.TextUtils;
3029

3130
import com.leanplum.Leanplum;
@@ -374,15 +373,14 @@ public static void enableResourceSyncing(final List<String> patternsToInclude,
374373
final List<Pattern> compiledExcludePatterns = compilePatterns(patternsToExclude);
375374

376375
if (isAsync) {
377-
Util.executeAsyncTask(false, new AsyncTask<Void, Void, Void>() {
376+
OperationQueue.sharedInstance().addParallelOperation(new Runnable() {
378377
@Override
379-
protected Void doInBackground(Void... params) {
378+
public void run() {
380379
try {
381380
enableResourceSyncing(compiledIncludePatterns, compiledExcludePatterns);
382381
} catch (Throwable t) {
383382
Util.handleException(t);
384383
}
385-
return null;
386384
}
387385
});
388386
} else {

AndroidSDKCore/src/main/java/com/leanplum/internal/LeanplumInternal.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.location.Address;
2525
import android.location.Geocoder;
2626
import android.location.Location;
27-
import android.os.AsyncTask;
2827

2928
import com.leanplum.ActionContext;
3029
import com.leanplum.Leanplum;
@@ -124,7 +123,7 @@ public static void triggerAction(final ActionContext context,
124123
}
125124
final AtomicBoolean handled = new AtomicBoolean(false);
126125
for (final ActionCallback callback : callbacks) {
127-
OsHandler.getInstance().post(new Runnable() {
126+
OperationQueue.sharedInstance().addUiOperation(new Runnable() {
128127
@Override
129128
public void run() {
130129
if (callback.onResponse(context) && handledCallback != null) {
@@ -438,16 +437,15 @@ public static void setUserLocationAttribute(final Location location,
438437
Leanplum.addStartResponseHandler(new StartCallback() {
439438
@Override
440439
public void onResponse(final boolean success) {
441-
// Geocoder query must be executed on background thread.
442-
Util.executeAsyncTask(false, new AsyncTask<Void, Void, Void>() {
440+
OperationQueue.sharedInstance().addParallelOperation(new Runnable() {
443441
@Override
444-
protected Void doInBackground(Void... voids) {
442+
public void run() {
445443
if (!success) {
446-
return null;
444+
return;
447445
}
448446
if (location == null) {
449447
Log.e("Location can't be null in setUserLocationAttribute.");
450-
return null;
448+
return;
451449
}
452450
String latLongLocation = String.format(Locale.US, "%.6f,%.6f", location.getLatitude(),
453451
location.getLongitude());
@@ -489,7 +487,6 @@ public void error(Exception e) {
489487
}
490488
});
491489
req.send();
492-
return null;
493490
}
494491
});
495492
}
@@ -567,7 +564,7 @@ public static void triggerStartIssued() {
567564
synchronized (startIssuedHandlers) {
568565
LeanplumInternal.setIssuedStart(true);
569566
for (Runnable callback : startIssuedHandlers) {
570-
OsHandler.getInstance().post(callback);
567+
OperationQueue.sharedInstance().addUiOperation(callback);
571568
}
572569
startIssuedHandlers.clear();
573570
}

AndroidSDKCore/src/main/java/com/leanplum/internal/Operation.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@
2525
import android.os.Looper;
2626

2727

28-
public class Operation extends Thread {
28+
public abstract class Operation implements Runnable {
2929

3030
private static final Handler handler = new Handler(Looper.getMainLooper());
3131

32-
private Runnable runnable;
33-
34-
protected Operation(Runnable runnable) {
35-
this.runnable = runnable;
36-
}
37-
3832
/**
3933
* Helper methods to execute runnable on main thread
4034
*
@@ -61,11 +55,4 @@ public static void runOnUiThreadAfterDelay(Runnable runnable, long delayTimeMill
6155
public static void removeOperationOnUiThread(Runnable runnable) {
6256
handler.removeCallbacks(runnable);
6357
}
64-
65-
@Override
66-
public void run() {
67-
if (runnable != null) {
68-
runnable.run();
69-
}
70-
}
7158
}

AndroidSDKCore/src/main/java/com/leanplum/internal/OperationQueue.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import android.os.Handler;
2525
import android.os.HandlerThread;
26+
import android.os.Looper;
2627
import android.os.Process;
2728

2829
import java.util.concurrent.Executor;
@@ -37,6 +38,7 @@ public class OperationQueue {
3738

3839
private HandlerThread handlerThread;
3940
private Handler handler;
41+
private Handler uiHandler = new Handler(Looper.getMainLooper());
4042

4143
private Executor executor = Executors.newCachedThreadPool();
4244

@@ -83,14 +85,24 @@ public void addParallelOperation(Runnable operation) {
8385
}
8486
}
8587

88+
/**
89+
* Add operation to UI Handler to be run on main thread
90+
* @param operation The operation that will be executed.
91+
*/
92+
public void addUiOperation(Runnable operation) {
93+
if (operation != null && uiHandler != null) {
94+
uiHandler.post(operation);
95+
}
96+
}
97+
8698
/**
8799
* Add operation to OperationQueue at the end
88100
* @param operation The operation that will be executed.
89101
* @return return true if the operation was successfully placed in to the operation queue. Returns false on failure.
90102
*/
91103
public boolean addOperation(Runnable operation) {
92104
if (operation != null && handler != null) {
93-
return handler.post(new Operation(operation));
105+
return handler.post(operation);
94106
}
95107
return false;
96108
}
@@ -102,7 +114,7 @@ public boolean addOperation(Runnable operation) {
102114
*/
103115
public boolean addOperationAtFront(Runnable operation) {
104116
if (operation != null && handler != null) {
105-
return handler.postAtFrontOfQueue(new Operation(operation));
117+
return handler.postAtFrontOfQueue(operation);
106118
}
107119
return false;
108120
}
@@ -114,7 +126,7 @@ public boolean addOperationAtFront(Runnable operation) {
114126
*/
115127
public boolean addOperationAtTime(Runnable operation, long millis) {
116128
if (operation != null && handler != null) {
117-
return handler.postAtTime(new Operation(operation), millis);
129+
return handler.postAtTime(operation, millis);
118130
}
119131
return false;
120132
}
@@ -127,7 +139,7 @@ public boolean addOperationAtTime(Runnable operation, long millis) {
127139
*/
128140
public boolean addOperationAfterDelay(Runnable operation, long delayMillis) {
129141
if (operation != null && handler != null) {
130-
return handler.postDelayed(new Operation(operation), delayMillis);
142+
return handler.postDelayed(operation, delayMillis);
131143
}
132144
return false;
133145
}

AndroidSDKCore/src/main/java/com/leanplum/internal/OsHandler.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

AndroidSDKCore/src/main/java/com/leanplum/internal/Registration.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,31 @@ public static void registerDevice(String email, final StartCallback callback) {
3434
Map<String, Object> params = new HashMap<>();
3535
params.put(Constants.Params.EMAIL, email);
3636
RequestOld request = RequestOld.post(Constants.Methods.REGISTER_FOR_DEVELOPMENT, params);
37+
3738
request.onResponse(new RequestOld.ResponseCallback() {
3839
@Override
39-
public void response(final JSONObject response) {
40-
OsHandler.getInstance().post(new Runnable() {
41-
@Override
42-
public void run() {
43-
try {
44-
boolean isSuccess = RequestOld.isResponseSuccess(response);
45-
if (isSuccess) {
46-
if (callback != null) {
47-
callback.onResponse(true);
48-
}
49-
} else {
50-
Log.e(RequestOld.getResponseError(response));
51-
if (callback != null) {
52-
callback.onResponse(false);
53-
}
54-
}
55-
} catch (Throwable t) {
56-
Util.handleException(t);
57-
}
40+
public void response(JSONObject response) {
41+
try {
42+
boolean success = RequestOld.isResponseSuccess(response);
43+
callback.setSuccess(success);
44+
45+
if (!success) {
46+
String error = RequestOld.getResponseError(response);
47+
Log.e(error);
5848
}
59-
});
49+
50+
OperationQueue.sharedInstance().addUiOperation(callback);
51+
} catch (Throwable t) {
52+
Util.handleException(t);
53+
}
6054
}
6155
});
56+
6257
request.onError(new RequestOld.ErrorCallback() {
6358
@Override
6459
public void error(final Exception e) {
65-
OsHandler.getInstance().post(new Runnable() {
66-
@Override
67-
public void run() {
68-
if (callback != null) {
69-
callback.onResponse(false);
70-
}
71-
}
72-
});
60+
callback.setSuccess(false);
61+
OperationQueue.sharedInstance().addUiOperation(callback);
7362
}
7463
});
7564
request.sendIfConnected();

0 commit comments

Comments
 (0)