Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2485] Remove cached events #1226

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions Branch-SDK/src/main/java/io/branch/referral/ServerRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,7 @@ protected ServerRequest(Defines.RequestPath requestPath, JSONObject post, Contex
public boolean shouldRetryOnFail() {
return false;
}

/**
* Specifies whether this request should be persisted to memory in order to re send in the next session
*
* @return {@code true} by default. Should be override for request that need not to be persisted
*/
boolean isPersistable() {
return true;
}


/**
* Specifies whether this request should add the limit app tracking value
*
Expand Down Expand Up @@ -348,7 +339,8 @@ public JSONObject toJSON() {
}
return json;
}


// TODO: Replace with in-memory only ServerRequest objects.
/**
* <p>Converts a {@link JSONObject} object containing keys stored as key-value pairs into
* a {@link ServerRequest}.</p>
Expand Down Expand Up @@ -388,13 +380,14 @@ public static ServerRequest fromJSON(JSONObject json, Context context) {
} catch (JSONException e) {
BranchLogger.w("Caught JSONException " + e.getMessage());
}

if (!TextUtils.isEmpty(requestPath)) {
return getExtendedServerRequest(requestPath, post, context, initiatedByClient);
}
return null;
}


// TODO: Replace with in-memory only ServerRequest objects.
/**
* <p>Factory method for creating the specific server requests objects. Creates requests according
* to the request path.</p>
Expand All @@ -406,18 +399,17 @@ public static ServerRequest fromJSON(JSONObject json, Context context) {
*/
private static ServerRequest getExtendedServerRequest(String requestPath, JSONObject post, Context context, boolean initiatedByClient) {
ServerRequest extendedReq = null;

if (requestPath.equalsIgnoreCase(Defines.RequestPath.GetURL.getPath())) {
extendedReq = new ServerRequestCreateUrl(Defines.RequestPath.GetURL, post, context);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.RegisterInstall.getPath())) {
extendedReq = new ServerRequestRegisterInstall(Defines.RequestPath.RegisterInstall, post, context, initiatedByClient);
} else if (requestPath.equalsIgnoreCase(Defines.RequestPath.RegisterOpen.getPath())) {
extendedReq = new ServerRequestRegisterOpen(Defines.RequestPath.RegisterOpen, post, context, initiatedByClient);
}

return extendedReq;
}

/**
* Updates the google ads parameters. This should be called only from a background thread since it involves GADS method invocation using reflection
* Ensure that when there is a valid GAID/AID, remove the SSAID if it's being used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,6 @@ private String generateLongUrlWithParams(String baseUrl) {
return longUrl;
}

@Override
boolean isPersistable() {
return false; // No need to retrieve create url request from previous session
}

@Override
protected boolean prepareExecuteWithoutTracking() {
// SDK-271 -- Allow creation of short links when tracking is disabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,50 +83,7 @@ static void shutDown() {
private ServerRequestQueue(Context c) {
sharedPref = c.getSharedPreferences("BNC_Server_Request_Queue", Context.MODE_PRIVATE);
editor = sharedPref.edit();
queue = retrieve(c);
}

private void persist() {
try {
JSONArray jsonArr = new JSONArray();
synchronized (reqQueueLockObject) {
for (ServerRequest aQueue : queue) {
if (aQueue.isPersistable()) {
JSONObject json = aQueue.toJSON();
if (json != null) {
jsonArr.put(json);
}
}
}
}

editor.putString(PREF_KEY, jsonArr.toString()).apply();
} catch (Exception ex) {
String msg = ex.getMessage();
BranchLogger.e("Failed to persist queue" + (msg == null ? "" : msg));
}
}

private List<ServerRequest> retrieve(Context context) {
String jsonStr = sharedPref.getString(PREF_KEY, null);
List<ServerRequest> result = Collections.synchronizedList(new LinkedList<ServerRequest>());
synchronized (reqQueueLockObject) {
if (jsonStr != null) {
try {
JSONArray jsonArr = new JSONArray(jsonStr);
for (int i = 0, size = Math.min(jsonArr.length(), MAX_ITEMS); i < size; i++) {
JSONObject json = jsonArr.getJSONObject(i);
ServerRequest req = ServerRequest.fromJSON(json, context);
if (req != null) {
result.add(req);
}
}
} catch (JSONException e) {
BranchLogger.w("Caught JSONException " + e.getMessage());
}
}
}
return result;
queue = Collections.synchronizedList(new LinkedList<ServerRequest>());
}

/**
Expand Down Expand Up @@ -154,7 +111,6 @@ void enqueue(ServerRequest request) {
if (getSize() >= MAX_ITEMS) {
queue.remove(1);
}
persist();
}
}
}
Expand Down Expand Up @@ -228,7 +184,6 @@ void insert(ServerRequest request, int index) {
index = queue.size();
}
queue.add(index, request);
persist();
} catch (IndexOutOfBoundsException e) {
BranchLogger.e("Caught IndexOutOfBoundsException " + e.getMessage());
}
Expand All @@ -250,7 +205,6 @@ public ServerRequest removeAt(int index) {
synchronized (reqQueueLockObject) {
try {
req = queue.remove(index);
persist();
} catch (IndexOutOfBoundsException e) {
BranchLogger.e("Caught IndexOutOfBoundsException " + e.getMessage());
}
Expand All @@ -270,7 +224,6 @@ public boolean remove(ServerRequest request) {
synchronized (reqQueueLockObject) {
try {
isRemoved = queue.remove(request);
persist();
} catch (UnsupportedOperationException e) {
BranchLogger.e("Caught UnsupportedOperationException " + e.getMessage());
}
Expand All @@ -285,7 +238,6 @@ void clear() {
synchronized (reqQueueLockObject) {
try {
queue.clear();
persist();
} catch (UnsupportedOperationException e) {
BranchLogger.e("Caught UnsupportedOperationException " + e.getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.gradle.api.tasks.testing.logging.*

plugins {
id("com.android.library") version "8.1.2" apply false
id("com.android.application") version "8.1.2" apply false
id("com.android.library") version "8.3.2" apply false
id("com.android.application") version "8.3.2" apply false
id("org.jetbrains.kotlin.android") version "1.6.21" apply false
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Sun Apr 07 13:06:19 PDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading