Skip to content

Commit

Permalink
feat: new way of consent removal
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Oct 16, 2024
1 parent 7142d50 commit 7918bbc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,35 @@ public void recordView_previousViewName() throws JSONException {
validateView("test2", 0.0, 1, 2, false, true, TestUtils.map(), "_CLY_", "_CLY_", "test");
}

/**
* "startView" with consent removal
* Validate that all running views are stopped when the view consent is removed
*
* @throws JSONException if the JSON is not valid
*/
@Test
public void startView_consentRemoval() throws JSONException {
CountlyConfig countlyConfig = TestUtils.createBaseConfig();
countlyConfig.setRequiresConsent(true);
countlyConfig.setLoggingEnabled(true);
countlyConfig.giveAllConsents();
countlyConfig.setEventQueueSizeToSend(1);

Countly countly = new Countly().init(countlyConfig);

countly.views().startView("test");
ModuleConsentTests.validateAllConsentRequest(TestUtils.commonDeviceId, 0);
validateView("test", 0.0, 1, 2, true, true, TestUtils.map(), "_CLY_", "_CLY_", null);

countly.views().startView("test2");
validateView("test2", 0.0, 2, 3, false, true, TestUtils.map(), "_CLY_", "_CLY_", null);

countly.consent().removeConsent(new String[] { Countly.CountlyFeatureNames.views });
validateView("test", 0.0, 3, 6, false, false, TestUtils.map(), "_CLY_", "_CLY_", null);
validateView("test2", 0.0, 4, 6, false, false, TestUtils.map(), "_CLY_", "_CLY_", null);
ModuleConsentTests.validateConsentRequest(TestUtils.commonDeviceId, 5, new boolean[] { true, true, true, true, true, true, true, true, true, true, true, true, false, true, true });
}

static void validateView(String viewName, Double viewDuration, int idx, int size, boolean start, boolean visit, Map<String, Object> customSegmentation, String id, String pvid) throws JSONException {
validateView(viewName, viewDuration, idx, size, start, visit, customSegmentation, id, pvid, null);
}
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/main/java/ly/count/android/sdk/ModuleBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ void deviceIdChanged(boolean withoutMerge) {
void onConsentChanged(@NonNull final List<String> consentChangeDelta, final boolean newConsent, @NonNull final ModuleConsent.ConsentChangeSource changeSource) {
}

void consentWillChange(@NonNull List<String> consentThatWillChange, final boolean isConsentGiven) {
}

//notify the SDK modules that internal configuration was updated
void sdkConfigurationChanged() {

Expand Down
13 changes: 10 additions & 3 deletions sdk/src/main/java/ly/count/android/sdk/ModuleConsent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ModuleConsent extends ModuleBase implements ConsentProvider {
Consent consentInterface = null;
Expand Down Expand Up @@ -213,6 +214,7 @@ void setConsentInternal(@Nullable final String[] featureNames, final boolean isC
}

List<String> consentThatWillChange = new ArrayList<>(featureNames.length);
Map<String, Boolean> consentUpdateMap = new ConcurrentHashMap<>();

for (String featureName : featureNames) {
if (!isValidFeatureName(featureName)) {
Expand All @@ -223,12 +225,17 @@ void setConsentInternal(@Nullable final String[] featureNames, final boolean isC
if (getConsentTrue(featureName) != isConsentGiven) {
//if the current consent does not match the one give, add it to the list
consentThatWillChange.add(featureName);

//set new consent value
featureConsentValues.put(featureName, isConsentGiven);
//set new consent values later because some modules need to do operation before changing consent
consentUpdateMap.put(featureName, isConsentGiven);
}
}

for (ModuleBase module : _cly.modules) {
module.consentWillChange(consentThatWillChange, isConsentGiven);
}

featureConsentValues.putAll(consentUpdateMap);

for (ModuleBase module : _cly.modules) {
module.onConsentChanged(consentThatWillChange, isConsentGiven, changeSource);
}
Expand Down
22 changes: 9 additions & 13 deletions sdk/src/main/java/ly/count/android/sdk/ModuleViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ void stopViewWithIDInternal(@Nullable String viewID, @Nullable Map<String, Objec
L.d("[ModuleViews] View [" + vd.viewName + "], id:[" + vd.viewID + "] is getting closed, reporting duration: [" + (UtilsTime.currentTimestampSeconds() - vd.viewStartTimeSeconds) + "] s, current timestamp: [" + UtilsTime.currentTimestampSeconds() + "]");

if (!consentProvider.getConsent(Countly.CountlyFeatureNames.views)) {
L.w("[ModuleViews] stopViewWithIDInternal, no consent given for views, ignoring call");
return;
}

Expand All @@ -295,7 +296,7 @@ void recordViewEndEvent(ViewData vd, @Nullable Map<String, Object> customViewSeg

//only record view if the view name is not null
if (vd.viewName == null) {
L.e("[ModuleViews] stopViewWithIDInternal, view has no internal name, ignoring it");
L.e("[ModuleViews] recordViewEndEvent, view has no internal name, ignoring it");
return;
}

Expand Down Expand Up @@ -506,6 +507,13 @@ void onConfigurationChanged(Configuration newConfig) {
}
}

@Override
void consentWillChange(@NonNull List<String> consentThatWillChange, final boolean isConsentGiven) {
if (consentThatWillChange.contains(Countly.CountlyFeatureNames.views) && !isConsentGiven) {
stopAllViewsInternal(null);
}
}

@Override
void onActivityStopped(int updatedActivityCount) {
if (autoViewTracker) {
Expand Down Expand Up @@ -558,18 +566,6 @@ void onActivityStarted(Activity activity, int updatedActivityCount) {
}
}

@Override
void onConsentChanged(@NonNull final List<String> consentChangeDelta, final boolean newConsent, @NonNull final ModuleConsent.ConsentChangeSource changeSource) {
L.d("[ModuleViews] onConsentChanged, consentChangeDelta:[" + consentChangeDelta + "], newConsent:[" + newConsent + "], changeSource:[" + changeSource + "]");
if (consentChangeDelta.contains(Countly.CountlyFeatureNames.views)) {
if (!newConsent) {
L.d("[ModuleViews] onConsentChanged, stopping all views because consent was removed");

stopAllViewsInternal(null);
}
}
}

/**
* Needed for mocking test result
*
Expand Down

0 comments on commit 7918bbc

Please sign in to comment.