[Huawei Developers] HMS Community:
Overview

In this article, I will create a Courier android application using Kotlin in which I will integrate HMS Core kits such as HMS Account and AuthService Kit.

App will make use of android MVVM clean architecture using Jetpack components such as DataBinding, AndroidViewModel, Observer, LiveData and much more.

In this article we are going to implement DataBinding using Observable pattern.

Huawei ID Service Introduction

Huawei ID login provides you with simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.

Prerequisite

Huawei Phone EMUI 3.0 or later.

Non-Huawei phones Android 4.4 or later (API level 19 or higher).

HMS Core APK 4.0.0.300 or later

Android Studio

AppGallery Account

App Gallery Integration process

Sign In and Create or Choose a project on AppGallery Connect portal.

Navigate to Project settings and download the configuration file.

Navigate to General Information, and then provide Data Storage location.

App Development

Add Required Dependencies:

Launch Android studio and create a new project. Once the project is ready.

Navigate to the Gradle scripts folder and open build.gradle (module: app).

In the same build.gradle file, add the lifecycle library to your dependencies. This library helps connect the UI to a ViewModel and LiveData.

classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.huawei.agconnect:agcp:1.4.2.300'

Add following dependency for HMS Kits

//HMS Kits
implementation 'com.huawei.agconnect:agconnect-core:1.5.0.300'
implementation 'com.huawei.hms:hwid:5.3.0.302'

Code Implementation

Created following package model, event, viewmodel.

Model: In your primary folder, create a new package and name it model. Then create a User.kt file in this package.

ViewModel: The ViewModel makes it easy to update data changes on the UI.Create a package named viewmodel in your main folder.Then create a new file and name it LoginViewModel.kt,

LoginViewModel.kt:

package com.hms.corrierapp.viewmodel

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.databinding.Observable
import androidx.lifecycle.ViewModel
import com.hms.corrierapp.event.ActivityNavigation
import com.hms.corrierapp.event.LiveMessageEvent
import com.hms.corrierapp.OrderActivity
import com.huawei.hmf.tasks.Task
import com.huawei.hms.support.account.AccountAuthManager
import com.huawei.hms.support.account.request.AccountAuthParams
import com.huawei.hms.support.account.request.AccountAuthParamsHelper
import com.huawei.hms.support.account.result.AuthAccount
import com.huawei.hms.support.account.service.AccountAuthService

const val HMS_SIGN_IN: Int = 9001

class LoginViewModel : ViewModel(), Observable {

private var mAuthManager: AccountAuthService? = null
private var mAuthParam: AccountAuthParams? = null

val startActivityForResultEvent = LiveMessageEvent<ActivityNavigation>()

fun login() {
mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams()
mAuthManager = AccountAuthManager.getService(Activity(), mAuthParam)
startActivityForResultEvent.sendEvent {
startActivityForResult(
mAuthManager?.signInIntent,
HMS_SIGN_IN
)
}
}

fun onResultFromActivity(context: Context, requestCode: Int, data: Intent?) {
when (requestCode) {
HMS_SIGN_IN -> {
val authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data)
onCompleteLogin(context, authAccountTask)
}
}
}

private fun onCompleteLogin(context: Context, doneTask: Task<AuthAccount>) {
if (doneTask.isSuccessful) {
val authAccount = doneTask.result
Log.d("LoginViewModel", "SigIn Success")
context.startActivity(Intent(context, OrderActivity::class.java))

} else {
Log.d("LoginViewModel", "SigIn Error")
}
}

override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
}

override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
}

}

Xml layout DataBinding

To include data binding in the UI, enclose all content with <layout></layout>.

The ViewModel is introduced to the layout in the <data></data> section, as shown. Ensure that the type value points to the specific folder that has the required ViewModel.

activity_main.xml

MainActivity.kt:

package com.hms.corrierapp

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import com.hms.corrierapp.event.ActivityNavigation
import com.hms.corrierapp.databinding.ActivityMainBinding
import com.hms.corrierapp.viewmodel.LoginViewModel
import com.hms.corrierapp.viewmodel.LoginViewModelFactory

class MainActivity : AppCompatActivity(), ActivityNavigation {

private lateinit var viewModel: LoginViewModel
private lateinit var dataBinding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val factory = LoginViewModelFactory()
viewModel = ViewModelProviders.of(this, factory).get(LoginViewModel::class.java)
dataBinding.loginViewModel = viewModel
dataBinding.lifecycleOwner=this
viewModel.startActivityForResultEvent.setEventReceiver(this, this)
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
viewModel.onResultFromActivity(this,requestCode, data)
super.onActivityResult(requestCode, resultCode, data)
}

}

App Build Result

View attachment 5582519

Tips and Tricks

Identity Kit displays the HUAWEI ID registration or sign-in page first. The user can use the functions provided by Identity Kit only after signing in using a registered HUAWEI ID.

Conclusion

In this article, we have learned how to integrate Huawei ID in Android application. After completely read this article user can easily implement Huawei ID in the Courier android application using Kotlin.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

HMS Docs:

https://developer.huawei.com/consum.../HMSCore-Guides/introduction-0000001050048870