# Getting started with Sinch In-app Calling for Android SDK This guide shows you how to get started integrating your Android application with the In-app Calling SDK. Because this is just a getting started guide, we only covers basic steps how to sign in to the In-app Calling SDK and how to make and receive audio calls. Notes: This guide is meant to be used side by side with our samples available in the SDK Code snippets shown in the guide are part of 'sinch-rtc-sample-push' sample available on our [download page](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/) inside the SDK archive. Please follow along with the samples open in your IDE. For more complex examples and documentation, check inside the SDK package file or look at our [tutorials](https://developers.sinch.com/docs/in-app-calling/). ## Prerequisites - Android Studio and Android SDK tools available [here](https://developer.android.com/studio). - In-app Calling [SDK for Android](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/). - For convenience 2 physical Android devices (however 1 physical and 1 emulator is enough for testing purposes) ## Create Android Studio project Create a new Android Studio project using the 'Empty activity' template. ![as1](/assets/as1.7e79306efe2e767e0be3200c136d0e642c0f19363aee396610d1448f5e2aa849.4503b5b1.png) Notes: - You can use Kotlin as your development language in both Java and Kotlin versions of the SDK. - The name of your application doesn't need to correspond to the one provided in the Sinch Dashboard. ## Add Sinch Voice and Video SDK to your Android application 1. In your web browser, go to [Sinch SDKs Download page](https://developers.sinch.com/docs/in-app-calling/sdk-downloads/). 2. Find the Android SDK for Java, download the zip file and extract it. 3. Along with the library itself, the package also contains documentation and sample applications. For the purpose of this tutorial only the aar file is needed. Locate it inside the 'libs' folder and copy it to your clipboard. 4. Paste it to `/app/libs` directory. ![as2](/assets/as2.8b6ad95b988bd84dfb2729944c71915462339dff8a9de8792f20ca72e9aefc0d.4503b5b1.png) 5. Edit your **app's** build.gradle file located under `app/build.gradle` to include the following: *app/build.gradle* ```shell repositories { flatDir { dirs 'libs' } } dependencies { implementation(name:'sinch-android-rtc', version:'+', ext:'aar') /// } ``` You should see a message that gradle files have changed. Click **Sync now** and wait for Android Studio to adopt your changes. ## Interacting with Voice and Video SDK The `SinchClient` object is the main SDK entry point. Once created it's used to provide various SDKs features such as video, audio or PTSN calls. We recommend to initiate `SinchClient` once and retain its instance during the whole lifetime of the running application. That's why instead of placing it inside Activity or Fragment classes we recommend to put it inside a [Service](https://developer.android.com/guide/components/services) component. 1. Create a new Kotlin class that extends Service component (don't forget to define the service in your AndroidManifest.xml file). 2. Add `SinchClient` instance as a member variable. 3. Create a function that builds the client. The result of these steps is demonstrated below: ```kotlin class SinchService : Service() { companion object { private const val APP_KEY = "enter-application-key" private const val APP_SECRET = "enter-application-secret" private const val ENVIRONMENT = "ocra.api.sinch.com" } private var sinchClient: SinchClient? = null private fun createClient(username: String) { sinchClient = SinchClient.builder().context(applicationContext) .userId(username) .applicationKey(APP_KEY) .environmentHost(ENVIRONMENT) .pushConfiguration( PushConfiguration.fcmPushConfigurationBuilder() .senderID(APP_FCM_SENDER_ID) .registrationToken(getFcmRegistrationToken(this).orEmpty()).build() ) .pushNotificationDisplayName("User $username") .build() sinchClient?.addSinchClientListener(MySinchClientListener()) sinchClient?.callController?.addCallControllerListener(SinchCallControllerListener()) } /// } ``` To make this example work for you, you need to update some values: | Parameter | Your value | | --- | --- | | APP_KEY | You can find your key on your [dashboard](https://dashboard.sinch.com/settings/access-keys). | | APP_SECRET | You can find your secret on your [dashboard](https://dashboard.sinch.com/settings/access-keys). Your secret is only available to view right after creating your key, so make sure you copy it somewhere safe. | | pushConfiguration | FCM or HMS push configuration needed by the SDK to send push messages notifying about incoming calls. See [push notifications](https://developers.sinch.com/docs/in-app-calling/android/push-notifications/) section for more information. | There are a few other elements to notice: - The `environmentHost` parameter is the endpoint of the REST API the SDK is targeting. - The `UserId` parameter is the user identifier to register within your application (the specific value will be provided after clicking **Login** button in the Android application). When starting the Sinch client, SinchClientListener's `onCredentialsRequired` method executes. Inside this callback you must provide a signed (with your application secret) JWT token. In a production application the token should be generated on your backend infrastructure. Most importantly you shouldn't put your app secret value into your Android application source code. More information on that topic can be found in [Authentication & Authorization](https://developers.sinch.com/docs/in-app-calling/android/application-authentication/) and [Authorizing the Client](https://developers.sinch.com/docs/in-app-calling/android/sinch-client/#authorizing-the-client--user) sections. Just for this step-by-step guide purpose we will mimic a backend authentication server behaviour with a helper `JWT` class that creates the token based on userId and your application credentials locally and then passes it back to Sinch SDK: ```kotlin override fun onCredentialsRequired(clientRegistration: ClientRegistration) { clientRegistration.register(create(APP_KEY, APP_SECRET, settings.username)) } ``` Implementation of the JWT class can be found in any of samples provided with the SDK package. (For example, check out `samples/sinch-rtc-sample-push/src/com/sinch/android/rtc/sample/push/JWT.kt`) ## Communication between views and service To communicate between your application view layer (activities and fragments) and the Sinch client that lives inside the Service instance, we implement a bound service pattern. Information about what's a bound service and how it works can be found on the official Android SDK documentation [website](https://developer.android.com/guide/components/bound-services). 1. Inside `SinchService` create an inner class that extends `Binder`. 2. Add basic functionality that allows the application to start Sinch client for a given username and provide a way to be notified about the initialization result: ```kotlin private var listener: StartFailedListener? = null interface StartFailedListener { fun onFailed(error: SinchError) fun onStarted() } inner class SinchServiceInterface : Binder() { fun startClient() { // The username is fetched from settings. start() } fun setStartListener(listener: StartFailedListener?) { this@SinchService.listener = listener } } ``` 3. Override `onBind` method to return `SinchServiceBinder` ```kotlin private val sinchServiceInterface: SinchServiceInterface = SinchServiceInterface() override fun onBind(intent: Intent): IBinder { ... return sinchServiceInterface } ``` ## Logging into the application When the user starts the application, usually they must enter a username that will be passed as `userId` and used to create a Sinch client. The username they choose will then be used as a callee identifier for making the actual audio call. 1. Rename `MainActivity` and `activity_main.xml` files inside AndroidStudio (that were initially created after setting up the project) to `LoginActivity` and `login.xml`. Right click on the filename and choose `Refactor -> Rename`. 2. Create a simple layout containg EditText (for entering the username) and login button. ```xml ``` 3. Inside the `LoginActivity` file first bind to the Sinch client service by calling ```kotlin private fun bindService() { val serviceIntent = Intent(this, SinchService::class.java) applicationContext.bindService(serviceIntent, this, BIND_AUTO_CREATE) } ``` Note that as a second argument `LoginActivity` is passed, meaning it has to implement the [Service Connection](https://developer.android.com/reference/android/content/ServiceConnection) interface. 4. Inside `onServiceConnected` in BaseActivity callback assign provided binder to a local variable for later usage for communicating with the service and assign `LoginActivity` as a listener to get notifications about success or failure when starting Sinch client. `BaseActivity` ```kotlin protected var sinchServiceInterface: SinchService.SinchServiceInterface? = null private set override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) { if (SinchService::class.java.name == componentName.className) { sinchServiceInterface = iBinder as SinchService.SinchServiceInterface onServiceConnected() } } protected open fun onServiceConnected() { // for subclasses } ``` `LoginActivity` ```kotlin override fun onServiceConnected() { if (sinchServiceInterface?.isStarted == true) { openPlaceCallActivity() } else { sinchServiceInterface?.setStartListener(this) } } ``` 5. Finally assign the login button a click listener that initiates and starts the Sinch client: ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // loginButton.apply { setOnClickListener { loginClicked() } } } private fun loginClicked() { val username = loginNameEditText.text.toString() sinchServiceInterface?.username = username startClientAndOpenPlaceCallActivity() } ``` 6. Return to *SinchService* implementation. Inside `onClientStarted` and `onClientFailed` callbacks of `SinchClientListener` simply pass the result to the `sinchClientInitializationListener`. ```kotlin override fun onClientFailed(client: SinchClient, error: SinchError) { listener?.onFailed(error) ... } override fun onClientStarted(client: SinchClient) { listener?.onStarted() } ``` 7. Before launching the application declare 2 permissions inside your `AndroidManifest.xml` file that are required to start the SinchClient: ```xml ``` 8. Run the application, enter a username of your choice and verify your logcat output. You should see that the client was started successfully. ![as3](/assets/as3.ff34585691851fef1d79925c2782a5f40c70ae14cd1e309549a1a74667b005d1.4503b5b1.png) ## Next steps Now that your application is created, you can configure that application to make a call. Make a call