| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This is a library that contains utilities which are useful when developing Android SDKs. They are intended for use internally by Rakuten's SDKs.
Add to your build.gradle:
repositories {
mavenCentral()
}
dependency {
implementation "io.github.rakutentech.sdkutils:sdk-utils:$version"
}⚠️ This SDK uses a ContentProvider in order to automatically initialize. It is not recommended to override the default value set for initOrder. However, you may need to set it if you are facing an Exception when trying to use features of this SDK at launch time. If you are also using a ContentProvider in your App or you are developing an SDK which uses a ContentProvider to automatically initialize, then you may need to manually set the initOrder for this SDK so that it is initialized first. This can be set in AndroidManifest.xml:
<application>
<!-- The `initOrder` for this SDK is set to `9999` by default. -->
<!-- ⚠️ It is not recommended to override the `initOrder`, however, if you need to manually set it, make sure that it is initialized before your own ContentProvider. -->
<provider
tools:replace="android:initOrder"
android:name="com.rakuten.tech.mobile.sdkutils.SdkUtilsInitProvider"
android:authorities="${applicationId}.SdkUtilsInitProvider"
android:exported="false"
android:initOrder="9999" />
</application>This library contains an extension function for OkHttpClient.Builder so you can more easily add headers to all network requests which are made using that client.
private val client = OkHttpClient.Builder()
.addHeaderInterceptor(
"header_name" to "header_value",
"another_header" to "another_value"
).build()The AppInfo class can be used to retrieve some properties of the App which normally require a Context to retrieve. This class is automatically initialized, so no Context needs to be provided.
val appName = AppInfo.instance.name
val appVersion = AppInfo.instance.versionThis library contains a simple logging facility.
Logging conventions are:
By default only info, warn and error are logged. Debug is only logged if loggerInstance.setDebug(true) is called.
The level of the stack trace to be logged is set by default to 5 (it's the trace level of the running thread when SDKUtils is implemented in an application). You may need to upgrade the level using loggerInstance.setDebugLevel(yourLevel) if SDKUtils is implemented in another SDK.
private val log = Logger(TAG)
log.setDebugLevel(6)All log calls come in 2 variants:
private val log = Logger(MainActivity::class.java.simpleName)
//enable debug logs (by default only info, warn and error are logged)
log.setDebug(true)
//Examples:
log.debug("simple debug log") // simple debug log
log.debug("simple debug log at %s", listOf(Date())) // simple debug log at [Mon Dec 20 16:51:12 GMT+09:00 2021]
Please check the following sample code to use the preferences utility feature for caching data.
// We can put int, string, float, long, string set and boolean to cache in preferences
PreferencesUtil.putString(appContext, preferencesFileName, key, value)
// We can get int, string, float, long, string set and boolean to retrieve from preferences
PreferencesUtil.getString(appContext, preferencesFileName, key, defaultValue)This utility can be used to convert a JSON string loaded from app resources to an equivalent object.
//deserializes a Json loaded from app resources into a generic type object.
val path = "my-file.json"
val devices: List<Device>? = gson.fromResources(path, object: TypeToken<List<Device>>(){}.type)
//deserializes a Json loaded from app resources into an object.
val path = "my-file.json"
val device: Device? = gson.fromResources(path, Device::class.java)Please check the following sample code to use the extension function (s).
// To check the device mode setting, i.e. dark/ light
Context.isDarkMode(context)
// To check the play service of device
Activity.checkPlayServices(activity)
// To get the Sha256 digest data
String.getSha256HashData("Test")
// To get the MD5 digest data
String.getMD5HashData("Test")Please see StringExtension and ContextExtension classes for available APIs.
See the following networking utility classes and extension functions for easier usage of networking framework.
NetworkUtil: a class that queries network connectivity state and capabilities.
// Init
// Wrap in try-catch block since exception can be thrown when network callback registration fails.
val networkUtil = NetworkUtil(context)
try {
networkUtil.initialize()
} catch (ex: Exception) {
// Call any error callback if needed.
}
// Check whether device has network connectivity
networkUtil.isOnline()
// Get network capabilities
networkUtil.networkCapabilities()HttpCallback: a Callback Class that communicate the remote server response.
// by using an HttpCallback object:
client.newCall(request).enqueue(HttpCallback(
{ response -> ... },{ exception -> ... }
))
//or by using a lambda function:
//1- create your function e.g.
fun get(success: (response: Response) -> Unit, failure: (exception: Exception) -> Unit) {
val request = Request.Builder().url(url).build()
...
client.newCall(request).enqueue(HttpCallback(success, failure))
}
// 2- get the response
get({ response = it ... }, { exception = it ... })Kotlin extension to get a reference of Retrofit
//Get a reference of Retrofit using the default parameters
val retrofit = Retrofit.Builder().build("your_baseUrl")
// Get a reference of Retrofit using your personalysed parameter(s)
val okHttpClient: OkHttpClient = your_httpClient
val gsonConverterFactory: GsonConverterFactory = your_converterFactory
val executor: ExecutorService = your_executor
val retrofit = Retrofit.Builder().build("your_baseUrl", okHttpClient gsonConverterFactory, executor)
A remote troubleshooting utility which helps to track or send important events to a remote API server. This is intended to be used internally by Rakuten's SDKs.
configure: Sets the server configuration and other initialization processing. Call this as early as possible in the application lifecycle, such as onCreate or other initialization methods. Calling other APIs without calling this API has no effect.
| Parameter | Description | Datatype | Required? |
|---|---|---|---|
| context | Application context | Context | Yes |
| apiUrl | Non-empty server API URL | String | Yes |
| apiKey | Non-empty server API Key | String | Yes |
override fun onCreate(savedInstanceState: Bundle?) {
// API: configure
EventLogger.configure(this, "my-api-url", "my-api-key")
}
send*Event: Logs an event and sends to server based on criticality.
| Parameter | Description | Datatype | Required? |
|---|---|---|---|
| sourceName | Non-empty source of the event, e.g. "sdkutils" | String | Yes |
| sourceVersion | Non-empty source' version, e.g. "2.0.0" | String | Yes |
| errorCode | Non-empty source' error code or HTTP backend response code e.g. "500" | String | Yes |
| errorMessage | Non-empty description of the error. Make it as descriptive as possible, for example, the stacktrace of an exception | String | Yes |
| info | Optional parameter to attach other useful key-value pair, such as filename, method, line number, etc. | Map | No |
// API: sendCriticalEvent
// Logs a critical event, wherein the unique event will be sent to the server immediately.
EventLogger.sendCriticalEvent(
sourceName = "sdkutils",
sourceVersion = "2.0.0",
errorCode = "XXX",
errorMessage = "java.lang.ArithmeticException: divide by zero
at com.rakuten.test.MainActivityFragment.onViewCreated(MainActivityFragment.kt:58)
at androidx.fragment.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManagerImpl.java:1144)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:851)"
info = mapOf("filename" to "MyFile.kt")
)
// API: sendWarningEvent
// Logs a warning event, which will be sent at a later time based on TTL.
EventLogger.sendWarningEvent(
sourceName = "sdkutils",
sourceVersion = "2.0.0",
errorCode = "YYY",
errorMessage = "Incorrect credentials",
info = mapOf("filename" to "MyFile.kt")
)
| Back | FazBrowse Home | New Git URL |