The AEP SDK contains a set of services. These services provide shared functionality throughout the SDK that can be shared by extensions. For example, services provide shared functionality for networking, logging, caching, and more.
A public interface
defines each service; this allows customers to override services if they see fit. For example, here is the definition of the Logging
service responsible for supplying shared logging functionality to all extensions.
public interface Logging {
void trace(String tag, String message);
void debug(String tag, String message);
void warning(String tag, String message);
void error(String tag, String message);
}
The Logging
service above defines a simple interface for logging messages.
The SDK provides a shared ServicesProvider
, responsible for maintaining the current set of provided services and any potential service overrides.
Some services provide wrapper classes. For example, the Log
class is a wrapper around the LoggingService
. However, in some cases, a wrapper class may not exist, and one might need to access a service directly from the ServiceProvider. For example, the below code shows how to access CacheService
.
CacheService cacheService =
ServiceProvider.getInstance().getCacheService();
val cacheService = ServiceProvider.getInstance().cacheService
This example will show how one would implement their own Logging
service throughout the SDK.
First, one must implement a type that conforms to the Logging
interface, as defined above. We will do this by defining a logging service that only prints out messages with a log level of Error
.
class ErrorLogger implements Logging {
@Override
public void trace(String tag, String message) {}
@Override
public void debug(String tag, String message) {}
@Override
public void warning(String tag, String message) {}
@Override
public void error(String tag, String message) {
Log.e("ErrorLogger", message);
}
}
internal class ErrorLogger : Logging {
override fun trace(tag: String, message: String) {}
override fun debug(tag: String, message: String) {}
override fun warning(tag: String, message: String) {}
override fun error(tag: String, message: String) {
Log.e("ErrorLogger", message)
}
}
In the code snippet above, we have a class that implements Logging
and provides simple implementation for the single required API.
As we saw above, implementing the Logging
interface was quite simple, but how do we get the entire SDK to take advantage of this new service in place of the default implementation?
We can do this by setting the loggingService
on the shared ServiceProvider
, used by the entire SDK.
For the Android SDK to use overridden services, Services overriding should be done before the SDK is initialized.
ServiceProvider.getInstance().setLoggingService(new ErrorLogger());
ServiceProvider.getInstance().loggingService = ErrorLogger()
If one wishes to revert to the loggingService
default implementation, you can set the loggingService
to nil.
ServiceProvider.getInstance().setLoggingService(null);
ServiceProvider.getInstance().loggingService = null
Note: Use caution when overriding services. Changes to behavior for a given service can have unintended consequences throughout the SDK.