[Huawei Developers] muraliameakula:
Introduction

In this article, we can learn the integration of user address in apps by Huawei Identity Kit. The Identity Kit provides an easy interface to add or edit or delete user details and enables the users to grant permission for apps to access their addresses through a single click on the screen.

This kit is mainly used in e-commerce, food delivery and logistics apps to deliver the products in an easy and safe way to users.

Services

Address Management: Users can enter and edit address information.

Address Access: Increases productivity by allowing user to access address information with the required permission from users.

Address Selection: Users can select addresses quickly and reliably.

Advantages

Easy access: Only one interface to integrate address selection service.

Extensive coverage: More than 170 countries and regions are covered.

Privacy protection: Strict compliance with European General Data Protection Regulation (GDPR) regulations and compliant use of address data.

Requirements

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.0.300 or later.

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4. Minimum API Level 21 is required.

5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Note: Project Name depends on the user created name.

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

7. Enter SHA-256 certificate fingerprint and click tick icon, as follows.

Note: Above steps from Step 1 to 7 is common for all Huawei Kits.

8. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

9. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.5.0.300'
// Identity Kit.
implementation 'com.huawei.hms:identity:4.0.4.300'

10. Now Sync the gradle.

11. Add the required permission to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

Let us move to development

I have created a project on Android studio with empty activity let's start coding.

In the MainActivity.kt we can create the business logic.

class MainActivity : AppCompatActivity() {

private val GET_ADDRESS = 1000

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

query_user_address.setOnClickListener {
if(isNetworkAvailable(this@MainActivity)){
getUserAddress()
}
else {
Toast.makeText(this, "Please check your internet connection...", Toast.LENGTH_SHORT).show()
}
}
}

// To parse user address selection, returning the selected user address and displaying the selected user address in text view.
@SuppressLint("SetTextI18n")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Toast.makeText(this, "onActivityResult requestCode $requestCode resultCode $resultCode", Toast.LENGTH_SHORT).show()
when (requestCode) {
GET_ADDRESS -> when(resultCode) {
RESULT_OK -> {
val userAddress = UserAddress.parseIntent(data)
if(userAddress != null){
val sb = StringBuilder()
sb.apply {
append("name: ${userAddress.name} ,")
append("city: ${userAddress.administrativeArea} ,")
append("area: ${userAddress.locality} ,")
append("address: ${userAddress.addressLine1} ${userAddress.addressLine2} ,")
append("phone: ${userAddress.phoneNumber} ,")
}
Toast.makeText(this, "user address is $sb", Toast.LENGTH_SHORT).show()
user_address.text = sb.toString()
}else {
user_address.text = "Failed to get user address."
}
}
RESULT_CANCELED -> {
}
else -> Toast.makeText(this, "Result is wrong, result code is $resultCode", Toast.LENGTH_SHORT).show()
}
else -> {
}
}
}

// To query the user addresses and open the user address selection page.
private fun getUserAddress() {
val req = UserAddressRequest()
val task = com.huawei.hms.identity.Address.getAddressClient(this).getUserAddress(req)
task.addOnSuccessListener { result ->
Toast.makeText(this, "onSuccess result code: ${result.returnCode}", Toast.LENGTH_SHORT).show()
try{
startActivityForResult(result)
}
catch (e: IntentSender.SendIntentException){
e.printStackTrace()
}
}.addOnFailureListener { e ->
Toast.makeText(this, "on Failed result code: ${e.message}", Toast.LENGTH_SHORT).show()
}
}

private fun startActivityForResult(result: GetUserAddressResult){
val status = result.status
if(result.returnCode == 0 && status.hasResolution()){
Toast.makeText(this, "The result had resolution", Toast.LENGTH_SHORT).show()
status.startResolutionForResult(this, GET_ADDRESS)
}
else {
Toast.makeText(this, "the response is wrong, the return code is ${result.returnCode}", Toast.LENGTH_SHORT).show()
}
}

fun isNetworkAvailable(context: Context?): Boolean {
if(context != null){
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val aNetworkInfo = connectivityManager.activeNetworkInfo
aNetworkInfo?.let{
return aNetworkInfo.isAvailable
}
}
return false
}

}

In the activity_main.xml we can create the UI screen.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
android:orientation="vertical">
<TextView
android:id="@+id/user_address"
android:layout_width="match_parent"
android:layout_height="35dp"
android:hint="show shipping address"
android:textAllCaps="false"
android:textSize="15sp"
android:text="Show User Address"/>
<Button
android:id="@+id/query_user_address"
android:layout_width="match_parent"
android:layout_marginTop="10sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="15sp"
android:text="Get Huawei User Address"/>
<TextView
android:id="@+id/demo_introduce"
android:layout_width="match_parent"
android:textSize="15sp"
android:layout_height="320dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="50dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:text="@string/demo_introduction" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Demo

​

Tips and Tricks

1. Make sure you are already registered as Huawei developer.

2. Set minSDK version to 21 or later.

3. Make sure you have added the agconnect-services.json file to app folder.

4. Make sure you have added SHA-256 fingerprint without fail.

5. Make sure all the dependencies are added properly

6. The Identity Kit functions can be used only after signin with registered Huawei ID.

7. A maximum of 10 user addresses are allowed.

Conclusion

In this article, we have learnt integration of user address feature in apps by Huawei Identity Kit. It allows the user to login with Huawei ID and can access the easy interface to add or edit or delete user details. It helps to deliver the online booking products by e-commerce, food delivery and logistics apps in an easy and safe way to users.

I hope you have read this article. If you found it is helpful, please provide likes and comments.

Reference

Identity Kit

Original Source