Initialize the Android SDK
If you used the sdk-configurator to generate your configuration model, as described in Configuring the client, you can use the OneginiClientBuilder#build()
method to initialize the Android SDK. If you created your own implementation of the OneginiClientConfigModel
, you can provide it explicitly using the OneginiClientBuilder#setConfigModel()
method.
The OneginiClientBuilder
constructor has three mandatory parameters:
applicationContext
OneginiCreatePinRequestHandler
OneginiPinAuthenticationRequestHandler
For more information about these parameters see authenticators.
public static OneginiClient getOneginiClient(final Context context) {
OneginiClient oneginiClient = OneginiClient.getInstance();
if (oneginiClient == null) {
oneginiClient = buildSDK(context);
}
return oneginiClient;
}
private static OneginiClient buildSDK(final Context context) {
final Context applicationContext = context.getApplicationContext();
final RegistrationRequestHandler registrationRequestHandler = new RegistrationRequestHandler(applicationContext);
final CreatePinRequestHandler createPinRequestHandler = new CreatePinRequestHandler(applicationContext);
final PinAuthenticationRequestHandler pinAuthenticationRequestHandler = new PinAuthenticationRequestHandler(applicationContext);
return new OneginiClientBuilder(applicationContext, createPinRequestHandler, pinAuthenticationRequestHandler).build();
}
fun getOneginiClient(context: Context): OneginiClient = OneginiClient.getInstance() ?: buildSDK(context)
private fun buildSDK(context: Context): OneginiClient {
val applicationContext: Context = context.applicationContext
val registrationRequestHandler = RegistrationRequestHandler(applicationContext)
val createPinRequestHandler = CreatePinRequestHandler(applicationContext)
val pinAuthenticationRequestHandler = PinAuthenticationRequestHandler(applicationContext)
return OneginiClientBuilder(applicationContext, createPinRequestHandler, pinAuthenticationRequestHandler)
.setBrowserRegistrationRequestHandler(registrationRequestHandler)
.build()
}
Starting the Android SDK
When the user starts the application, the first method that should be called on the Android SDK is the OneGiniClient#start
method. The method is responsible for asynchronous initialization of the Android SDK. The method requires a OneginiInitializationHandler
in order to return initialization results and errors.
Note
The OneginiClient#start
method must complete before calling subsequent Android SDK methods. Calling additional methods before OneginiClient#start
completes may cause operation errors.
If an error occurs during start, the Android SDK returns onError
with an OneginiInitializationError
object containing the error details.
Example: initialization
// method called during app's startup
private void setupOneginiSDK() {
final OneginiClient oneginiClient = OneginiSDK.getOneginiClient(this);
oneginiClient.start(new OneginiInitializationHandler() {
@Override
public void onSuccess(final Set<UserProfile> removedUserProfiles) {
// remove UserProfiles that were removed on the server side and proceed with you application flow
}
@Override
public void onError(final OneginiInitializationError error) {
handleInitializationErrors(error);
}
});
}
private void handleInitializationErrors(final OneginiInitializationError error) {
@OneginiInitializationError.InitializationErrorType int errorType = error.getErrorType();
switch (errorType) {
case OneginiInitializationError.NETWORK_CONNECTIVITY_PROBLEM:
case OneginiInitializationError.SERVER_NOT_REACHABLE:
showToast("No internet connection.");
break;
case OneginiInitializationError.OUTDATED_APP:
showToast("Please update this application in order to use it.");
break;
case OneginiInitializationError.OUTDATED_OS:
showToast("Please update your Android version to use this application.");
break;
case OneginiInitializationError.DEVICE_DEREGISTERED:
// in that case clear local storage from device and all user's related data
onDeviceDeregistered();
break;
case OneginiInitializationError.DEVICE_REGISTRATION_ERROR:
case OneginiInitializationError.GENERAL_ERROR:
default:
// Just display the error for other, less relevant errors
displayError(error);
break;
}
}
// method called during app's startup
private fun setupOneginiSDK() {
val oneginiClient: OneginiClient = OneginiSDK.getOneginiClient(this)
oneginiClient.start(object : OneginiInitializationHandler {
override fun onSuccess(removedUserProfiles: Set<UserProfile>) {
// remove UserProfiles that were removed on the server side and proceed with you application flow
}
override fun onError(error: OneginiInitializationError) {
handleInitializationErrors(error)
}
})
}
private fun handleInitializationErrors(error: OneginiInitializationError) {
@InitializationErrorType val errorType = error.errorType
when (errorType) {
OneginiInitializationError.NETWORK_CONNECTIVITY_PROBLEM,
OneginiInitializationError.SERVER_NOT_REACHABLE ->
showToast("No internet connection.")
OneginiInitializationError.OUTDATED_APP ->
showToast("Please update this application in order to use it.")
OneginiInitializationError.OUTDATED_OS ->
showToast("Please update your Android version to use this application.")
OneginiInitializationError.DEVICE_DEREGISTERED ->
// in that case clear local storage from device and all user's related data
onDeviceDeregistered()
OneginiInitializationError.DEVICE_REGISTRATION_ERROR,
OneginiInitializationError.GENERAL_ERROR ->
// Just display the error for other, less relevant errors
displayError(error)
else -> displayError(error)
}
}