diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/ECID.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/ECID.java index 401a5376..3c35b1f0 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/ECID.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/ECID.java @@ -11,8 +11,9 @@ package com.adobe.marketing.mobile.edge.identity; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; + +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.StringUtils; import java.util.Locale; import java.util.Objects; @@ -23,6 +24,8 @@ */ final class ECID { + private static final String LOG_SOURCE = "ECID"; + private final String ecidString; /** @@ -43,9 +46,9 @@ final class ECID { */ ECID(final String ecidString) { if (StringUtils.isNullOrEmpty(ecidString)) { - MobileCore.log( - LoggingMode.DEBUG, - IdentityConstants.LOG_TAG, + Log.debug( + LOG_TAG, + LOG_SOURCE, "Creating an ECID with null or empty ecidString is not allowed, generating a new ECID." ); this.ecidString = new ECID().toString(); diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Identity.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Identity.java index 63967f02..d22a4d3b 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Identity.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Identity.java @@ -20,8 +20,8 @@ import com.adobe.marketing.mobile.Extension; import com.adobe.marketing.mobile.ExtensionError; import com.adobe.marketing.mobile.ExtensionErrorCallback; -import com.adobe.marketing.mobile.LoggingMode; import com.adobe.marketing.mobile.MobileCore; +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.DataReader; import com.adobe.marketing.mobile.util.StringUtils; import java.util.HashMap; @@ -36,6 +36,8 @@ public class Identity { public static final Class EXTENSION = IdentityExtension.class; private static final long CALLBACK_TIMEOUT_MILLIS = 500L; + private static final String LOG_SOURCE = "Identity"; + private Identity() {} /** @@ -59,11 +61,10 @@ public static void registerExtension() { new ExtensionErrorCallback() { @Override public void error(ExtensionError extensionError) { - MobileCore.log( - LoggingMode.ERROR, + Log.error( LOG_TAG, - "Identity - There was an error registering the Edge Identity extension: " + - extensionError.getErrorName() + LOG_SOURCE, + "There was an error registering the Edge Identity extension: " + extensionError.getErrorName() ); } } @@ -79,11 +80,7 @@ public void error(ExtensionError extensionError) { */ public static void getExperienceCloudId(final AdobeCallback callback) { if (callback == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "Identity - Unexpected null callback, provide a callback to retrieve current ECID." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Unexpected null callback, provide a callback to retrieve current ECID."); return; } @@ -105,10 +102,10 @@ public void call(Event responseEvent) { final IdentityMap identityMap = IdentityMap.fromXDMMap(responseEvent.getEventData()); if (identityMap == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "Identity - Failed to read IdentityMap from response event, invoking error callback with AdobeError.UNEXPECTED_ERROR" + LOG_SOURCE, + "Failed to read IdentityMap from response event, invoking error callback with AdobeError.UNEXPECTED_ERROR" ); returnError(callback, AdobeError.UNEXPECTED_ERROR); return; @@ -128,11 +125,11 @@ public void call(Event responseEvent) { @Override public void fail(AdobeError adobeError) { returnError(callback, adobeError); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, + LOG_SOURCE, String.format( - "Identity - Failed to dispatch %s event: Error : %s.", + "Failed to dispatch %s event: Error : %s.", IdentityConstants.EventNames.IDENTITY_REQUEST_IDENTITY_ECID, adobeError.getErrorName() ) @@ -162,10 +159,10 @@ public void fail(AdobeError adobeError) { */ public static void getUrlVariables(final AdobeCallback callback) { if (callback == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "Identity - Unexpected null callback, provide a callback to retrieve current visitor identifiers (URLVariables) query string." + LOG_SOURCE, + "Unexpected null callback, provide a callback to retrieve current visitor identifiers (URLVariables) query string." ); return; } @@ -209,11 +206,11 @@ public void call(final Event responseEvent) { @Override public void fail(final AdobeError adobeError) { returnError(callback, adobeError); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, + LOG_SOURCE, String.format( - "Identity - Failed to dispatch %s event: Error : %s.", + "Failed to dispatch %s event: Error : %s.", IdentityConstants.EventNames.IDENTITY_REQUEST_URL_VARIABLES, adobeError.getErrorName() ) @@ -234,11 +231,7 @@ public void fail(final AdobeError adobeError) { */ public static void updateIdentities(final IdentityMap identityMap) { if (identityMap == null || identityMap.isEmpty()) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "Identity - Unable to updateIdentities, IdentityMap is null or empty" - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Unable to updateIdentities, IdentityMap is null or empty"); return; } @@ -262,16 +255,12 @@ public static void updateIdentities(final IdentityMap identityMap) { */ public static void removeIdentity(final IdentityItem item, final String namespace) { if (StringUtils.isNullOrEmpty(namespace)) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "Identity - Unable to removeIdentity, namespace is null or empty" - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Unable to removeIdentity, namespace is null or empty"); return; } if (item == null) { - MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "Identity - Unable to removeIdentity, IdentityItem is null"); + Log.debug(LOG_TAG, LOG_SOURCE, "Unable to removeIdentity, IdentityItem is null"); return; } @@ -297,10 +286,10 @@ public static void removeIdentity(final IdentityItem item, final String namespac */ public static void getIdentities(final AdobeCallback callback) { if (callback == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "Identity - Unexpected null callback, provide a callback to retrieve current IdentityMap." + LOG_SOURCE, + "Unexpected null callback, provide a callback to retrieve current IdentityMap." ); return; } @@ -323,10 +312,10 @@ public void call(final Event responseEvent) { final IdentityMap identityMap = IdentityMap.fromXDMMap(responseEvent.getEventData()); if (identityMap == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "Identity - Failed to read IdentityMap from response event, invoking error callback with AdobeError.UNEXPECTED_ERROR" + LOG_SOURCE, + "Failed to read IdentityMap from response event, invoking error callback with AdobeError.UNEXPECTED_ERROR" ); returnError(callback, AdobeError.UNEXPECTED_ERROR); return; @@ -338,11 +327,11 @@ public void call(final Event responseEvent) { @Override public void fail(final AdobeError adobeError) { returnError(callback, adobeError); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, + LOG_SOURCE, String.format( - "Identity - Failed to dispatch %s event: Error : %s.", + "Failed to dispatch %s event: Error : %s.", IdentityConstants.EventNames.REQUEST_IDENTITIES, adobeError.getErrorName() ) diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityExtension.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityExtension.java index 385dc9c7..ac595451 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityExtension.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityExtension.java @@ -18,11 +18,11 @@ import com.adobe.marketing.mobile.Event; import com.adobe.marketing.mobile.Extension; import com.adobe.marketing.mobile.ExtensionApi; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; import com.adobe.marketing.mobile.SharedStateResolution; import com.adobe.marketing.mobile.SharedStateResult; import com.adobe.marketing.mobile.SharedStateStatus; +import com.adobe.marketing.mobile.services.Log; +import com.adobe.marketing.mobile.services.ServiceProvider; import com.adobe.marketing.mobile.util.StringUtils; import com.adobe.marketing.mobile.util.TimeUtils; import java.util.HashMap; @@ -30,6 +30,8 @@ class IdentityExtension extends Extension { + private static final String LOG_SOURCE = "IdentityExtension"; + /** * A {@code SharedStateCallback} to retrieve the last set state of an extension and to * create an XDM state at the event provided. @@ -190,7 +192,7 @@ private void handleUrlVariablesRequest(@NonNull final Event event) { handleUrlVariableResponse( event, null, - "IdentityExtension - Cannot process getUrlVariables request Identity event, Experience Cloud Org ID not found in configuration." + "Cannot process getUrlVariables request Identity event, Experience Cloud Org ID not found in configuration." ); return; } @@ -202,7 +204,7 @@ private void handleUrlVariablesRequest(@NonNull final Event event) { handleUrlVariableResponse( event, null, - "IdentityExtension - Cannot process getUrlVariables request Identity event, ECID not found." + "Cannot process getUrlVariables request Identity event, ECID not found." ); return; } @@ -250,7 +252,7 @@ void handleUrlVariableResponse(@NonNull final Event event, final String urlVaria .build(); if (StringUtils.isNullOrEmpty(urlVariables) && !StringUtils.isNullOrEmpty(errorMsg)) { - MobileCore.log(LoggingMode.WARNING, LOG_TAG, errorMsg); + Log.warning(LOG_TAG, LOG_SOURCE, errorMsg); } getApi().dispatch(responseEvent); @@ -264,15 +266,18 @@ void handleUrlVariableResponse(@NonNull final Event event, final String urlVaria void handleUpdateIdentities(@NonNull final Event event) { final Map eventData = event.getEventData(); - if (eventData == null) return; // TODO: Add log message when logging changes are made + if (eventData == null) { + Log.trace(LOG_TAG, LOG_SOURCE, "Cannot update identifiers, event data is null."); + return; + } final IdentityMap map = IdentityMap.fromXDMMap(eventData); if (map == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityExtension - Failed to update identifiers as no identifiers were found in the event data." + LOG_SOURCE, + "Failed to update identifiers as no identifiers were found in the event data." ); return; } @@ -289,15 +294,18 @@ void handleUpdateIdentities(@NonNull final Event event) { void handleRemoveIdentity(@NonNull final Event event) { final Map eventData = event.getEventData(); - if (eventData == null) return; // TODO: Add log message when logging changes are made + if (eventData == null) { + Log.trace(LOG_TAG, LOG_SOURCE, "Cannot remove identifiers, event data is null."); + return; + } final IdentityMap map = IdentityMap.fromXDMMap(eventData); if (map == null) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityExtension - Failed to remove identifiers as no identifiers were found in the event data." + LOG_SOURCE, + "Failed to remove identifiers as no identifiers were found in the event data." ); return; } diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityItem.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityItem.java index 0a400b0b..70efedb0 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityItem.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityItem.java @@ -13,8 +13,7 @@ import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.DataReader; import com.adobe.marketing.mobile.util.DataReaderException; import java.util.HashMap; @@ -29,6 +28,8 @@ */ public final class IdentityItem { + private static final String LOG_SOURCE = "IdentityItem"; + private final String id; private final AuthenticatedState authenticatedState; private final boolean primary; @@ -182,7 +183,7 @@ static IdentityItem fromData(final Map data) { return new IdentityItem(id, authenticatedState, primary); } catch (final DataReaderException e) { - MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "IdentityItem - Failed to create IdentityItem from data."); + Log.debug(LOG_TAG, LOG_SOURCE, "Failed to create IdentityItem from data."); return null; } } diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityMap.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityMap.java index c82f3469..4db3ee9b 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityMap.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityMap.java @@ -13,8 +13,7 @@ import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.DataReader; import com.adobe.marketing.mobile.util.StringUtils; import java.util.ArrayList; @@ -33,6 +32,8 @@ @SuppressWarnings("unused") public class IdentityMap { + private static final String LOG_SOURCE = "IdentityMap"; + private final Map> identityItems = new HashMap<>(); /** @@ -90,20 +91,12 @@ public void addItem(final IdentityItem item, final String namespace) { */ public void removeItem(final IdentityItem item, final String namespace) { if (item == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityMap remove item ignored as must contain a non-null IdentityItem." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Remove item ignored as must contain a non-null IdentityItem."); return; } if (StringUtils.isNullOrEmpty(namespace)) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityMap remove item ignored as must contain a non-null/non-empty namespace." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Remove item ignored as must contain a non-null/non-empty namespace."); return; } @@ -161,20 +154,12 @@ public String toString() { */ void addItem(final IdentityItem item, final String namespace, final boolean isFirstItem) { if (item == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityMap - add item ignored as must contain a non-null IdentityItem." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Add item ignored as must contain a non-null IdentityItem."); return; } if (StringUtils.isNullOrEmpty(namespace)) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityMap - add item ignored as must contain a non-null/non-empty namespace." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Add item ignored as must contain a non-null/non-empty namespace."); return; } diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityProperties.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityProperties.java index 110b313c..36c79dec 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityProperties.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityProperties.java @@ -13,10 +13,9 @@ import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.StringUtils; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -25,13 +24,12 @@ */ class IdentityProperties { - private static final List reservedNamespaces = new ArrayList() { - { - add(IdentityConstants.Namespaces.ECID); - add(IdentityConstants.Namespaces.GAID); - add(IdentityConstants.Namespaces.IDFA); - } - }; + private static final String LOG_SOURCE = "IdentityProperties"; + private static final List reservedNamespaces = Arrays.asList( + IdentityConstants.Namespaces.ECID, + IdentityConstants.Namespaces.GAID, + IdentityConstants.Namespaces.IDFA + ); private final IdentityMap identityMap; @@ -155,7 +153,7 @@ void setECIDSecondary(final ECID newSecondaryEcid) { // do not set secondary ECID if primary ECID is not set if (getECID() == null) { - MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "Cannot set secondary ECID value as no primary ECID exists."); + Log.debug(LOG_TAG, LOG_SOURCE, "Cannot set secondary ECID value as no primary ECID exists."); return; } @@ -248,20 +246,20 @@ private void removeIdentitiesWithReservedNamespaces(final IdentityMap identityMa reservedNamespace.equalsIgnoreCase(IdentityConstants.Namespaces.GAID) || reservedNamespace.equalsIgnoreCase(IdentityConstants.Namespaces.IDFA) ) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, + LOG_SOURCE, String.format( - "IdentityProperties - Operation not allowed for namespace %s; use MobileCore.setAdvertisingIdentifier instead.", + "Operation not allowed for namespace %s; use MobileCore.setAdvertisingIdentifier instead.", reservedNamespace ) ); } else { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, + LOG_SOURCE, String.format( - "IdentityProperties - Updating/Removing identifiers in namespace %s is not allowed.", + "Updating/Removing identifiers in namespace %s is not allowed.", reservedNamespace ) ); diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityState.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityState.java index 40a6f291..2abf6c67 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityState.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityState.java @@ -15,10 +15,12 @@ import androidx.annotation.VisibleForTesting; import com.adobe.marketing.mobile.Event; -import com.adobe.marketing.mobile.LoggingMode; import com.adobe.marketing.mobile.MobileCore; import com.adobe.marketing.mobile.SharedStateResult; import com.adobe.marketing.mobile.SharedStateStatus; +import com.adobe.marketing.mobile.services.DataStoring; +import com.adobe.marketing.mobile.services.Log; +import com.adobe.marketing.mobile.services.ServiceProvider; import com.adobe.marketing.mobile.util.DataReader; import java.util.HashMap; import java.util.Map; @@ -28,24 +30,25 @@ */ class IdentityState { + private static final String LOG_SOURCE = "IdentityState"; + + private final IdentityStorageManager identityStorageManager; private IdentityProperties identityProperties; private boolean hasBooted; - /** - * Loads the persisted identities (if any) into {@link #identityProperties} - */ IdentityState() { - this(IdentityStorageService.loadPropertiesFromPersistence()); + this(new IdentityStorageManager(ServiceProvider.getInstance().getDataStoreService())); } /** - * Creates a new {@link IdentityState} with the given {@link IdentityProperties} - * - * @param identityProperties identity properties + * Loads the persisted identities (if any) into {@link #identityProperties} */ @VisibleForTesting - IdentityState(final IdentityProperties identityProperties) { - this.identityProperties = (identityProperties != null) ? identityProperties : new IdentityProperties(); + IdentityState(final IdentityStorageManager identityStorageManager) { + this.identityStorageManager = identityStorageManager; + + final IdentityProperties persistedProperties = identityStorageManager.loadPropertiesFromPersistence(); + this.identityProperties = (persistedProperties != null) ? persistedProperties : new IdentityProperties(); } /** @@ -93,16 +96,14 @@ boolean bootupIfReady(final SharedStateCallback callback) { } // Attempt to get ECID from direct Identity persistence to migrate an existing ECID - final ECID directIdentityEcid = IdentityStorageService.loadEcidFromDirectIdentityPersistence(); + final ECID directIdentityEcid = identityStorageManager.loadEcidFromDirectIdentityPersistence(); if (directIdentityEcid != null) { identityProperties.setECID(directIdentityEcid); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityState - On bootup Loading ECID from direct Identity extension '" + - directIdentityEcid + - "'" + LOG_SOURCE, + "On bootup Loading ECID from direct Identity extension '" + directIdentityEcid + "'" ); } // If direct Identity has no persisted ECID, check if direct Identity is registered with the SDK @@ -115,10 +116,10 @@ else if (isIdentityDirectRegistered(eventHubStateResult.getValue())) { // If there is no direct Identity shared state, abort boot-up and try again when direct Identity shares its state if (sharedStateResult == null || sharedStateResult.getStatus() != SharedStateStatus.SET) { - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityState - On bootup direct Identity extension is registered, waiting for its state change." + LOG_SOURCE, + "On bootup direct Identity extension is registered, waiting for its state change." ); return false; } @@ -129,18 +130,18 @@ else if (isIdentityDirectRegistered(eventHubStateResult.getValue())) { // Generate a new ECID as the direct Identity extension is not registered with the SDK and there was no direct Identity persisted ECID else { identityProperties.setECID(new ECID()); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityState - Generating new ECID on bootup '" + identityProperties.getECID().toString() + "'" + LOG_SOURCE, + "Generating new ECID on bootup '" + identityProperties.getECID().toString() + "'" ); } - IdentityStorageService.savePropertiesToPersistence(identityProperties); + identityStorageManager.savePropertiesToPersistence(identityProperties); } hasBooted = true; - MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "IdentityState - Edge Identity has successfully booted up"); + Log.debug(LOG_TAG, LOG_SOURCE, "Edge Identity has successfully booted up"); callback.createXDMSharedState(identityProperties.toXDMData(false), null); return hasBooted; @@ -153,7 +154,7 @@ void resetIdentifiers() { identityProperties = new IdentityProperties(); identityProperties.setECID(new ECID()); identityProperties.setECIDSecondary(null); - IdentityStorageService.savePropertiesToPersistence(identityProperties); + identityStorageManager.savePropertiesToPersistence(identityProperties); } /** @@ -163,7 +164,7 @@ void resetIdentifiers() { */ void updateCustomerIdentifiers(final IdentityMap map) { identityProperties.updateCustomerIdentifiers(map); - IdentityStorageService.savePropertiesToPersistence(identityProperties); + identityStorageManager.savePropertiesToPersistence(identityProperties); } /** @@ -173,7 +174,7 @@ void updateCustomerIdentifiers(final IdentityMap map) { */ void removeCustomerIdentifiers(final IdentityMap map) { identityProperties.removeCustomerIdentifiers(map); - IdentityStorageService.savePropertiesToPersistence(identityProperties); + identityStorageManager.savePropertiesToPersistence(identityProperties); } /** @@ -212,7 +213,7 @@ void updateAdvertisingIdentifier(final Event event, final SharedStateCallback ca } // Save to persistence - IdentityStorageService.savePropertiesToPersistence(identityProperties); + identityStorageManager.savePropertiesToPersistence(identityProperties); callback.createXDMSharedState(identityProperties.toXDMData(false), event); } @@ -237,11 +238,11 @@ boolean updateLegacyExperienceCloudId(final ECID legacyEcid) { } identityProperties.setECIDSecondary(legacyEcid); - IdentityStorageService.savePropertiesToPersistence(identityProperties); - MobileCore.log( - LoggingMode.DEBUG, + identityStorageManager.savePropertiesToPersistence(identityProperties); + Log.debug( LOG_TAG, - "IdentityState - Identity direct ECID updated to '" + legacyEcid + "', updating the IdentityMap" + LOG_SOURCE, + "Identity direct ECID updated to '" + legacyEcid + "', updating the IdentityMap" ); return true; } @@ -256,19 +257,17 @@ boolean updateLegacyExperienceCloudId(final ECID legacyEcid) { private void handleECIDFromIdentityDirect(final ECID legacyEcid) { if (legacyEcid != null) { identityProperties.setECID(legacyEcid); // migrate legacy ECID - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityState - Identity direct ECID '" + - legacyEcid + - "' was migrated to Edge Identity, updating the IdentityMap" + LOG_SOURCE, + "Identity direct ECID '" + legacyEcid + "' " + "was migrated to Edge Identity, updating the IdentityMap" ); } else { // opt-out scenario or an unexpected state for Identity direct, generate new ECID identityProperties.setECID(new ECID()); - MobileCore.log( - LoggingMode.DEBUG, + Log.debug( LOG_TAG, - "IdentityState - Identity direct ECID is null, generating new ECID '" + + LOG_SOURCE, + "Identity direct ECID is null, generating new ECID '" + identityProperties.getECID() + "', updating the IdentityMap" ); @@ -296,7 +295,7 @@ private boolean isIdentityDirectRegistered(final Map eventHubSha final Map identityDirectInfo = DataReader.optTypedMap( Object.class, extensions, - IdentityConstants.SharedState.Hub.EXTENSIONS, + IdentityConstants.SharedState.IdentityDirect.NAME, null ); diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageManager.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageManager.java new file mode 100644 index 00000000..62092e10 --- /dev/null +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageManager.java @@ -0,0 +1,129 @@ +/* + Copyright 2022 Adobe. All rights reserved. + This file is licensed to you under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. You may obtain a copy + of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under + the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + OF ANY KIND, either express or implied. See the License for the specific language + governing permissions and limitations under the License. +*/ + +package com.adobe.marketing.mobile.edge.identity; + +import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; + +import com.adobe.marketing.mobile.services.DataStoring; +import com.adobe.marketing.mobile.services.Log; +import com.adobe.marketing.mobile.services.NamedCollection; +import com.adobe.marketing.mobile.util.JSONUtils; +import com.adobe.marketing.mobile.util.StringUtils; +import java.util.Map; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Manages persistence for this Identity extension + */ +class IdentityStorageManager { + + private static final String LOG_SOURCE = "IdentityStorageManager"; + private final NamedCollection edgeIdentityStore; + private final NamedCollection directIdentityStore; + + IdentityStorageManager(final DataStoring dataStoreService) { + this.edgeIdentityStore = dataStoreService.getNamedCollection(IdentityConstants.DataStoreKey.DATASTORE_NAME); + this.directIdentityStore = + dataStoreService.getNamedCollection(IdentityConstants.DataStoreKey.IDENTITY_DIRECT_DATASTORE_NAME); + } + + /** + * Loads identity properties from local storage, returns null if not found. + * + * @return {@code IdentityProperties} stored in local storage if present; + * null - if the content cannot be loaded from persistence or, if the content cannot be + * serialized to a {@code JSONObject} + */ + IdentityProperties loadPropertiesFromPersistence() { + if (edgeIdentityStore == null) { + Log.warning( + LOG_TAG, + LOG_SOURCE, + "EdgeIdentity named collection is null. Unable to load saved identity properties from persistence." + ); + return null; + } + final String jsonString = edgeIdentityStore.getString(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, null); + + if (jsonString == null) { + Log.debug( + LOG_TAG, + LOG_SOURCE, + "No previous properties were stored in persistence. Current identity properties are null" + ); + return null; + } + + try { + final JSONObject jsonObject = new JSONObject(jsonString); + final Map propertyMap = JSONUtils.toMap(jsonObject); + return new IdentityProperties(propertyMap); + } catch (JSONException exception) { + Log.debug( + LOG_TAG, + LOG_SOURCE, + "Serialization error while reading properties jsonString from persistence. Unable to load saved identity properties from persistence." + ); + return null; + } + } + + /** + * Saves identity properties to local storage. + * + * @param properties properties to be stored + */ + void savePropertiesToPersistence(final IdentityProperties properties) { + if (edgeIdentityStore == null) { + Log.warning( + LOG_TAG, + LOG_SOURCE, + "EdgeIdentity named collection is null. Unable to write identity properties to persistence." + ); + return; + } + + if (properties == null) { + Log.debug(LOG_TAG, LOG_SOURCE, "Identity Properties are null, removing them from persistence."); + edgeIdentityStore.remove(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES); + return; + } + + final JSONObject jsonObject = new JSONObject(properties.toXDMData(false)); + final String jsonString = jsonObject.toString(); + edgeIdentityStore.setString(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, jsonString); + } + + /** + * Retrieves the direct Identity extension ECID value stored in persistence. + * + * @return {@link ECID} stored in direct Identity extension's persistence, or null if no ECID value is stored. + */ + ECID loadEcidFromDirectIdentityPersistence() { + if (directIdentityStore == null) { + Log.debug( + LOG_TAG, + LOG_SOURCE, + "Identity direct named collection is null. Unable to load ECID from Identity Direct persistence." + ); + return null; + } + + final String ecidString = directIdentityStore.getString( + IdentityConstants.DataStoreKey.IDENTITY_DIRECT_ECID_KEY, + null + ); + + return StringUtils.isNullOrEmpty(ecidString) ? null : new ECID(ecidString); + } +} diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageService.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageService.java deleted file mode 100644 index fb6d2f51..00000000 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/IdentityStorageService.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - Copyright 2021 Adobe. All rights reserved. - This file is licensed to you under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. You may obtain a copy - of the License at http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under - the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS - OF ANY KIND, either express or implied. See the License for the specific language - governing permissions and limitations under the License. -*/ - -package com.adobe.marketing.mobile.edge.identity; - -import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; - -import android.app.Application; -import android.content.Context; -import android.content.SharedPreferences; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; -import com.adobe.marketing.mobile.util.JSONUtils; -import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; - -/** - * Manages persistence for this Identity extension - */ -class IdentityStorageService { - - private IdentityStorageService() {} - - /** - * Loads identity properties from local storage, returns null if not found. - * - * @return properties stored in local storage if present, otherwise null. - */ - static IdentityProperties loadPropertiesFromPersistence() { - final SharedPreferences sharedPreferences = getSharedPreference(IdentityConstants.DataStoreKey.DATASTORE_NAME); - - if (sharedPreferences == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Shared Preference value is null. Unable to load saved identity properties from persistence." - ); - return null; - } - - final String jsonString = sharedPreferences.getString(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, null); - - if (jsonString == null) { - MobileCore.log( - LoggingMode.VERBOSE, - LOG_TAG, - "IdentityStorageService - No previous properties were stored in persistence. Current identity properties are null" - ); - return null; - } - - try { - final JSONObject jsonObject = new JSONObject(jsonString); - final Map propertyMap = JSONUtils.toMap(jsonObject); - return new IdentityProperties(propertyMap); - } catch (JSONException exception) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Serialization error while reading properties jsonString from persistence. Unable to load saved identity properties from persistence." - ); - return null; - } - } - - /** - * Saves the properties to local storage - * - * @param properties properties to be stored - */ - static void savePropertiesToPersistence(final IdentityProperties properties) { - final SharedPreferences sharedPreferences = getSharedPreference(IdentityConstants.DataStoreKey.DATASTORE_NAME); - - if (sharedPreferences == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Shared Preference value is null. Unable to write identity properties to persistence." - ); - return; - } - - final SharedPreferences.Editor editor = sharedPreferences.edit(); - - if (editor == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Shared Preference Editor is null. Unable to write identity properties to persistence." - ); - return; - } - - if (properties == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Identity Properties are null, removing them from persistence." - ); - editor.remove(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES); - editor.apply(); - return; - } - - final JSONObject jsonObject = new JSONObject(properties.toXDMData(false)); - final String jsonString = jsonObject.toString(); - editor.putString(IdentityConstants.DataStoreKey.IDENTITY_PROPERTIES, jsonString); - editor.apply(); - } - - /** - * Retrieves the direct Identity extension ECID value stored in persistence. - * - * @return {@link ECID} stored in direct Identity extension's persistence, or null if no ECID value is stored. - */ - static ECID loadEcidFromDirectIdentityPersistence() { - final SharedPreferences sharedPreferences = getSharedPreference( - IdentityConstants.DataStoreKey.IDENTITY_DIRECT_DATASTORE_NAME - ); - - if (sharedPreferences == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Shared Preference value is null. Unable to load saved direct identity ECID from persistence." - ); - return null; - } - - final String ecidString = sharedPreferences.getString( - IdentityConstants.DataStoreKey.IDENTITY_DIRECT_ECID_KEY, - null - ); - - if (ecidString == null || ecidString.isEmpty()) { - return null; - } - - return new ECID(ecidString); - } - - /** - * Getter for the applications {@link SharedPreferences} - *

- * Returns null if the app or app context is not available - * - * @param datastoreName the name of the data store to get - * @return a {@code SharedPreferences} instance - */ - private static SharedPreferences getSharedPreference(final String datastoreName) { - final Application application = MobileCore.getApplication(); - - if (application == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Application value is null. Unable to read/write data from persistence." - ); - return null; - } - - final Context context = application.getApplicationContext(); - - if (context == null) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "IdentityStorageService - Context value is null. Unable to read/write data from persistence." - ); - return null; - } - - return context.getSharedPreferences(datastoreName, Context.MODE_PRIVATE); - } -} diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/URLUtils.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/URLUtils.java index 8b5e81e9..2970d61c 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/URLUtils.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/URLUtils.java @@ -11,8 +11,9 @@ package com.adobe.marketing.mobile.edge.identity; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; + +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.StringUtils; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -20,7 +21,7 @@ class URLUtils { - static final String LOG_TAG = "URLUtils"; + private static final String LOG_SOURCE = "URLUtils"; /** * Helper function to generate url variables in format acceptable by the AEP web SDKs @@ -56,7 +57,7 @@ static String generateURLVariablesPayload(final String ts, final String ecid, fi urlFragment.append(URLEncoder.encode(theIdString, StandardCharsets.UTF_8.toString())); } } catch (UnsupportedEncodingException e) { - MobileCore.log(LoggingMode.DEBUG, LOG_TAG, String.format("Failed to encode urlVariable string: %s", e)); + Log.debug(LOG_TAG, LOG_SOURCE, String.format("Failed to encode urlVariable string: %s", e)); } return urlFragment.toString(); } diff --git a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Utils.java b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Utils.java index 2ad211ea..9d93020d 100644 --- a/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Utils.java +++ b/code/edgeidentity/src/main/java/com/adobe/marketing/mobile/edge/identity/Utils.java @@ -13,8 +13,7 @@ import static com.adobe.marketing.mobile.edge.identity.IdentityConstants.LOG_TAG; -import com.adobe.marketing.mobile.LoggingMode; -import com.adobe.marketing.mobile.MobileCore; +import com.adobe.marketing.mobile.services.Log; import com.adobe.marketing.mobile.util.JSONUtils; import java.util.ArrayList; import java.util.List; @@ -24,6 +23,8 @@ class Utils { + private static final String LOG_SOURCE = "Utils"; + private Utils() {} static boolean isNullOrEmpty(final Map map) { @@ -69,11 +70,7 @@ static Map deepCopy(final Map map) { // TODO: Add/verify tests to check side effects of retaining nulls in the resulting Map return JSONUtils.toMap(new JSONObject(map)); } catch (final JSONException | NullPointerException e) { - MobileCore.log( - LoggingMode.DEBUG, - LOG_TAG, - "Utils(deepCopy) - Unable to deep copy map, json string invalid." - ); + Log.debug(LOG_TAG, LOG_SOURCE, "Unable to deep copy map, json string is invalid."); } return null;