Skip to content

Commit

Permalink
Merge pull request #399 from Countly/content_checks
Browse files Browse the repository at this point in the history
[Android] fix content and test
  • Loading branch information
turtledreams authored Oct 4, 2024
2 parents a507dd9 + 1e72480 commit 1910cfd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* Disabled caching for webviews.
* Expanded the flag (enablePreviousNameRecording) to add current view name as segmentation to custom events. (Experimental!)

* Mitigated an issue where a session could have started while the app was in the background when the device ID was changed (non-merge).
* Mitigated an issue where content fetching was enabled after initialization of the SDK.

* Fixed an issue where the validation of the parameters during content retrieval was improper.
* Mitigated an issue where a session could have started while the app was in the background when the device ID was changed (non-merge).
* Mitigated an issue that density calculation was missing while resizing content.
* Mitigated an issue where content fetching was enabled after initialization of the SDK.

## 24.7.3
* Automatic view pause/resumes are changed with stop/start for better data consistency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ public String prepareFetchContents(int portraitWidth, int portraitHeight, int la
L.e("Error while preparing fetch contents request");
}

return prepareCommonRequestData() + "&method=queue" + "&category=" + Arrays.asList(categories) + "&res=" + UtilsNetworking.urlEncodeString(json.toString());
return prepareCommonRequestData() + "&method=queue" + "&category=" + Arrays.asList(categories) + "&resolution=" + UtilsNetworking.urlEncodeString(json.toString());
}

@Override
Expand Down
19 changes: 15 additions & 4 deletions sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ly.count.android.sdk;

import android.util.Log;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
Expand Down Expand Up @@ -29,13 +30,23 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
}

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return null;
}
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { return null ; }

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return null;
String url = request.getUrl().toString();
Log.d(Countly.TAG, "[WebClient] Intercepted request URL: [" + url + "]");

// Call listeners for specific actions
for (WebViewUrlListener listener : listeners) {
boolean handled = listener.onUrl(url, view);
if (handled) {
Log.d(Countly.TAG, "Request handled by listener: " + url);
break;
}
}

return super.shouldInterceptRequest(view, request);
}

public void registerWebViewUrlListeners(List<WebViewUrlListener> listener) {
Expand Down
23 changes: 12 additions & 11 deletions sdk/src/main/java/ly/count/android/sdk/ModuleContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,27 @@ private String prepareContentFetchRequest(@NonNull DisplayMetrics displayMetrics
}

boolean validateResponse(@NonNull JSONObject response) {
boolean success = response.optString("result", "error").equals("success");
JSONArray content = response.optJSONArray("content");
return success && content != null && content.length() > 0;
return response.has("geo");
//boolean success = response.optString("result", "error").equals("success");
//JSONArray content = response.optJSONArray("content");
//return success && content != null && content.length() > 0;
}

@NonNull
Map<Integer, TransparentActivityConfig> parseContent(@NonNull JSONObject response, @NonNull DisplayMetrics displayMetrics) {
Map<Integer, TransparentActivityConfig> placementCoordinates = new ConcurrentHashMap<>();
JSONArray contents = response.optJSONArray("content");
assert contents != null;
//assert contents != null; TODO enable later

JSONObject contentObj = contents.optJSONObject(0);
JSONObject contentObj = response; //contents.optJSONObject(0); TODO this will be changed
assert contentObj != null;

String content = contentObj.optString("pathToHtml");
JSONObject coordinates = contentObj.optJSONObject("placementCoordinates");
String content = contentObj.optString("html");
JSONObject coordinates = contentObj.optJSONObject("geo");

assert coordinates != null;
placementCoordinates.put(Configuration.ORIENTATION_PORTRAIT, extractOrientationPlacements(coordinates, displayMetrics.density, "portrait", content));
placementCoordinates.put(Configuration.ORIENTATION_LANDSCAPE, extractOrientationPlacements(coordinates, displayMetrics.density, "landscape", content));
placementCoordinates.put(Configuration.ORIENTATION_PORTRAIT, extractOrientationPlacements(coordinates, displayMetrics.density, "p", content));
placementCoordinates.put(Configuration.ORIENTATION_LANDSCAPE, extractOrientationPlacements(coordinates, displayMetrics.density, "l", content));

return placementCoordinates;
}
Expand All @@ -155,8 +156,8 @@ private TransparentActivityConfig extractOrientationPlacements(@NonNull JSONObje
assert orientationPlacements != null;
int x = orientationPlacements.optInt("x");
int y = orientationPlacements.optInt("y");
int w = orientationPlacements.optInt("width");
int h = orientationPlacements.optInt("height");
int w = orientationPlacements.optInt("w");
int h = orientationPlacements.optInt("h");
L.d("[ModuleContent] extractOrientationPlacements, orientation: [" + orientation + "], x: [" + x + "], y: [" + y + "], w: [" + w + "], h: [" + h + "]");

TransparentActivityConfig config = new TransparentActivityConfig((int) Math.ceil(x * density), (int) Math.ceil(y * density), (int) Math.ceil(w * density), (int) Math.ceil(h * density));
Expand Down
49 changes: 25 additions & 24 deletions sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,39 +181,40 @@ private boolean contentUrlAction(String url, TransparentActivityConfig config, W
Map<String, Object> query = splitQuery(url);
Log.v(Countly.TAG, "[TransparentActivity] contentUrlAction, query: [" + query + "]");

Object clyEvent = query.get("cly_x_action_event");
Object clyEvent = query.get("?cly_x_action_event");

if (clyEvent == null || !clyEvent.equals("1")) {
Log.w(Countly.TAG, "[TransparentActivity] contentUrlAction, this is not a countly action event url");
return false;
}
Object clyAction = query.get("action");
if (!(clyAction instanceof String)) {
Log.w(Countly.TAG, "[TransparentActivity] contentUrlAction, action is not a string");
Log.w(Countly.TAG, "[TransparentActivity] contentUrlAction, event:[" + clyEvent + "] this is not a countly action event url");
return false;
}

String action = (String) clyAction;
Object clyAction = query.get("action");
boolean result = false;

switch (action) {
case "event":
eventAction(query);
break;
case "link":
result = linkAction(query, view);
break;
case "resize_me":
resizeMeAction(query);
break;
default:
break;
if (clyAction instanceof String) {
Log.d(Countly.TAG, "[TransparentActivity] contentUrlAction, action string:[" + clyAction + "]");
String action = (String) clyAction;

switch (action) {
case "event":
eventAction(query);
break;
case "link":
linkAction(query, view);
break;
case "resize_me":
resizeMeAction(query);
break;
default:
break;
}
}

if (query.containsKey("close") && Objects.equals(query.get("close"), "1")) {
finish();
config.globalContentCallback.onContentCallback(ContentStatus.CLOSED, query);
if (config.globalContentCallback != null) { // TODO: verify this later
config.globalContentCallback.onContentCallback(ContentStatus.CLOSED, query);
}
ModuleContent.waitForDelay = 2; // this is indicating that we will wait 1 min after closing the content and before fetching the next one
finish();
return true;
}

Expand Down Expand Up @@ -315,7 +316,7 @@ private void eventAction(Map<String, Object> query) {

private Map<String, Object> splitQuery(String url) {
Map<String, Object> query_pairs = new ConcurrentHashMap<>();
String[] pairs = url.split("https://countly_action_event?");
String[] pairs = url.split("https://countly_action_event/?");
if (pairs.length != 2) {
return query_pairs;
}
Expand Down

0 comments on commit 1910cfd

Please sign in to comment.