[Huawei Developers] HMS Community:
Overview

In this article, I will create a Directory android application using Kotlin in which I will integrate HMS Core kits such as HMS Account, AuthService and Identity 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.

Hms Core Identity Service Introduction

Hms Core Identity provides an easy interface to add or edit or delete user details and enables the users to authorize apps to access their addresses through a single tap on the screen. That is, app can obtain user addresses in a more convenient way.

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).

apply plugin: class=hljs-string>'com.android.application'

apply plugin: class=hljs-string>'kotlin-android'

apply plugin: class=hljs-string>'kotlin-android-extensions'

apply plugin: class=hljs-string>'com.huawei.agconnect'

android {

compileSdkVersion class=hljs-number>30

buildToolsVersion class=hljs-string>"29.0.3"

buildFeatures {

dataBinding = class=hljs-literal>true

viewBinding = class=hljs-literal>true

}

Add following dependency for HMS Kits

implementation class=hljs-string>'com.huawei.agconnect:agconnect-core:1.5.0.300'
implementation class=hljs-string>'com.huawei.hms:hwid:5.3.0.302'
implementation class=hljs-string>'com.huawei.hms:identity:5.3.0.300'

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

repositories {
google()
jcenter()
maven {url class=hljs-string>'https://developer.huawei.com/repo/'}
}
dependencies {
classpath class=hljs-string>"com.android.tools.build:gradle:4.0.1"
classpath class=hljs-string>'com.huawei.agconnect:agcp:1.4.2.300'

Code Implementation

Created following package model, event, viewmodel.

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 along with their FactoryViewModelProviders.

MainActivity.kt:

package com.hms.directory

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.R
import com.hms.corrierapp.databinding.ActivityMainBinding
import com.hms.directory.event.ActivityNavigation
import com.hms.directory.viewmodel.LoginViewModel
import com.hms.directory.viewmodel.LoginViewModelFactory

class=hljs-keyword>class MainActivity : AppCompatActivity(), ActivityNavigation {

private lateinit class=hljs-keyword>var viewModel: LoginViewModel
private lateinit class=hljs-keyword>var dataBinding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataBinding = DataBindingUtil.setContentView(class=hljs-keyword>this, R.layout.activity_main)

val viewModel: LoginViewModel by lazy {
val activity = requireNotNull(class=hljs-keyword>this) {}
ViewModelProviders.of(class=hljs-keyword>this, LoginViewModelFactory(activity.application))
.get(LoginViewModel::class=hljs-keyword>class.java)
}

dataBinding.loginViewModel = viewModel
dataBinding.lifecycleOwner = class=hljs-keyword>this
viewModel.startActivityForResultEvent.setEventReceiver(class=hljs-keyword>this, this)
}

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

}

LoginViewModel.kt:

package com.hms.directory.viewmodel

@SuppressLint(class=hljs-string>"StaticFieldLeak")
class=hljs-keyword>class LoginViewModel(application: Application) : AndroidViewModel(application), Observable {

private val context = getApplication<Application>().applicationContext
private class=hljs-keyword>var mAuthManager: AccountAuthService? = class=hljs-literal>null
private class=hljs-keyword>var mAuthParam: AccountAuthParams? = class=hljs-literal>null

val startActivityForResultEvent = LiveMessageEvent<ActivityNavigation>()

fun login() {
val intent = Intent(context, OrderActivity::class=hljs-keyword>class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)

class=hljs-comment>/* mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
class=hljs-comment> .setIdToken()
class=hljs-comment> .setAccessToken()
class=hljs-comment> .createParams()
class=hljs-comment> mAuthManager = AccountAuthManager.getService(Activity(), mAuthParam)
class=hljs-comment> startActivityForResultEvent.sendEvent {
class=hljs-comment> startActivityForResult(
class=hljs-comment> mAuthManager?.signInIntent,
class=hljs-comment> HMS_SIGN_IN
class=hljs-comment> )
class=hljs-comment> }*/
}

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

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

} class=hljs-keyword>else {
Log.d(class=hljs-string>"LoginViewModel", class=hljs-string>"SigIn Error")
}
}

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

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

}

ContactActivity.kt:

public class=hljs-keyword>class ContactListActivity extends AppCompatActivity {

@Override
protected class=hljs-keyword>void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);

class=hljs-comment>// Load contacts from file
Contacts.loadData(class=hljs-keyword>this);

class=hljs-comment>// Set up recycler view and fill it with all the contacts
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.contact_list);
recyclerView.setAdapter(class=hljs-keyword>new ContactListAdapter(class=hljs-keyword>this, Contacts.LIST));

}

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.

App Build Result

Tips and TricksView attachment 5624001

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 Identity Kit in Android application. After completely read this article user can easily implement Huawei ID in the Directory App 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