Skip to content

Commit

Permalink
releases/v3.3.3 (#283)
Browse files Browse the repository at this point in the history
## Improvements

* Update to MuxCore v7.8.0, adds `longBeaconDispatch` to `CustomOptions`. This feature should only be used in a small number of use cases, and your setting may be overridden by mux's backend servers
  • Loading branch information
daytime-em authored Mar 22, 2023
1 parent a868df5 commit 2c5f6f5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.net.Uri;
import android.os.AsyncTask;
import com.mux.stats.sdk.core.util.MuxLogger;
import com.mux.stats.sdk.muxstats.INetworkRequest;
import com.mux.stats.sdk.muxstats.MuxStats;
import com.mux.stats.sdk.muxstats.compat.AsyncTaskCompat;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -13,7 +11,10 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.json.JSONObject;
Expand Down Expand Up @@ -153,14 +154,23 @@ public Hashtable<String, String> getHeaders() {
}
}

private static class Response {
final boolean success;
final Map<String, List<String>> headers;

private Response(boolean success, Map<String, List<String>> headers) {
this.success = success;
this.headers = headers;
}
}

/**
* This is the asynchronous implementation of network dispatcher, runs on main Thread but does not
* block it. Expose methods for:
* <ul>
* <li>Get request: @link {@link NetworkRequest#get(URL)}</li>
* <li>Post request: @link {@link NetworkRequest#post(URL, JSONObject, Hashtable)}</li>
* <li>Post request with callback to be called on completion: @link {@link
* NetworkRequest#postWithCompletion(String, String, Hashtable, IMuxNetworkRequestsCompletion)}
* <li>Post request with callback to be called on completion
* </li>
* </ul>
*/
Expand All @@ -177,7 +187,7 @@ private static class NetworkTaskRunner extends AsyncTask<NetworkRequest, Void, V
*/
private static final int BASE_TIME_BETWEEN_BEACONS = 5000;
/** Callback to be executed after each successful request. */
private final IMuxNetworkRequestsCompletion callback;
private final IMuxNetworkRequestsCompletion2 callback;
/** Number of failed attempts on network request. */
private int failureCount = 0;

Expand All @@ -186,7 +196,7 @@ private static class NetworkTaskRunner extends AsyncTask<NetworkRequest, Void, V
*
* @param callback to be called when request is completed.
*/
public NetworkTaskRunner(IMuxNetworkRequestsCompletion callback) {
public NetworkTaskRunner(IMuxNetworkRequestsCompletion2 callback) {
this.callback = callback;
}

Expand Down Expand Up @@ -222,16 +232,19 @@ protected Void doInBackground(NetworkRequest... params) {

MuxLogger.d(TAG, "making " + method + " request to: " + url.toString());
boolean successful = false;
Map<String, List<String>> responseHeaders = null;
while (!successful && failureCount < MAXIMUM_RETRY) {
try {
Thread.sleep(getNextBeaconTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
successful = executeHttp(url, method, headers, body);
Response response = executeHttp(url, method, headers, body);
successful = response.success;
responseHeaders = response.headers;
}
if (callback != null) {
callback.onComplete(successful);
callback.onComplete(successful, responseHeaders);
}
return null;
}
Expand All @@ -244,11 +257,12 @@ protected Void doInBackground(NetworkRequest... params) {
* @param headers to send with request.
* @param body payload to send with the request.
*/
private boolean executeHttp(URL url, String method, Hashtable<String, String> headers,
private Response executeHttp(URL url, String method, Hashtable<String, String> headers,
String body) {
HttpURLConnection conn = null;
InputStream stream = null;
boolean successful = true;
Map<String, List<String>> responseHeaders = null;

try {
conn = (HttpURLConnection) url.openConnection();
Expand Down Expand Up @@ -285,6 +299,7 @@ private boolean executeHttp(URL url, String method, Hashtable<String, String> he

conn.connect();
stream = conn.getInputStream();
responseHeaders = conn.getHeaderFields();
MuxLogger.d(TAG, "got response: " + conn.getResponseCode());
} catch (Exception e) {
MuxLogger.d(TAG, e.getMessage());
Expand All @@ -304,7 +319,7 @@ private boolean executeHttp(URL url, String method, Hashtable<String, String> he
conn.disconnect();
}
}
return successful;
return new Response(successful, responseHeaders);
}

/**
Expand Down Expand Up @@ -387,7 +402,7 @@ public void postWithCompletion(String domain, String propertyKey, String body,
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.scheme("https").authority(this.getAuthority(propertyKey, domain)).path(
"android");
AsyncTaskCompat.executeParallel(new NetworkTaskRunner(callback),
AsyncTaskCompat.executeParallel(new NetworkTaskRunner((success, respHeaders) -> callback.onComplete(success)),
new PostRequest(new URL(uriBuilder.build().toString()), body, headers));
} else {
throw new Exception("propertyKey is null");
Expand All @@ -397,4 +412,24 @@ public void postWithCompletion(String domain, String propertyKey, String body,
callback.onComplete(true);
}
}

@Override
public void postWithCompletion(String domain, String propertyKey, String body,
Hashtable<String, String> headers,
INetworkRequest.IMuxNetworkRequestsCompletion2 callback) {
try {
if (propertyKey != null) {
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.scheme("https").authority(this.getAuthority(propertyKey, domain)).path(
"android");
AsyncTaskCompat.executeParallel(new NetworkTaskRunner(callback),
new PostRequest(new URL(uriBuilder.build().toString()), body, headers));
} else {
throw new Exception("propertyKey is null");
}
} catch (Exception e) {
MuxLogger.d(TAG, e.getMessage());
callback.onComplete(true, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class AdsPlaybackTests extends TestBase {

static final int PREROLL_AD_PERIOD = 10000;
static final int PREROLL_AD_PERIOD = 22000;
static final int BUMPER_AD_PERIOD = 5000;
static final int CAN_SKIP_AD_AFTER = 5000;

Expand Down Expand Up @@ -176,7 +176,7 @@ public void testPreRollAndBumperAds() {
int adPauseIndex = networkRequest.getIndexForFirstEvent(AdPauseEvent.TYPE);
long firstAdPlayPeriod = networkRequest.getCreationTimeForEvent(adPauseIndex) -
networkRequest.getCreationTimeForEvent(adPlayingIndex);
long expectedFirstAdPlayPeriod = PREROLL_AD_PERIOD / 2;
long expectedFirstAdPlayPeriod = 5 * 1000; // Ad is 10s long, roughly half is played
if (Math.abs(firstAdPlayPeriod - expectedFirstAdPlayPeriod) > 500) {
fail("First ad play period do not match expected play period, reported: " +
firstAdPlayPeriod + ", expected ad play period: " + expectedFirstAdPlayPeriod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testLateStatsInit() {
testActivity.runOnUiThread(() -> {
testActivity.initMuxSats();
});
Thread.sleep(INIT_MUX_STATS_AFTER * 2);
Thread.sleep(INIT_MUX_STATS_AFTER * 4);
// This is initialized with the MuxStats, it need to be called after
// testActivity.initMuxSats();
networkRequest = testActivity.getMockNetwork();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ allprojects {
compileSdkVersion = 31
targetSdkVersion = 31

muxCoreVersion = "7.7.2"
muxCoreVersion = "7.8.0"
kotlinxCoroutinesVersion = "1.6.1"
}
}
Expand Down

0 comments on commit 2c5f6f5

Please sign in to comment.