[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, AuthService, Push and Cloud DB Kit.

We have integrated HMS Account and AuthService Kit in part-1 and Client Push Notification Using HMS Push Kit in Part-2 of this series. Kindly go through the link below-

Part-1 https://forums.developer.huawei.com/forumPortal/en/topic/0202841957497640128

Part-2 https://forums.developer.huawei.com/forumPortal/en/topic/0201847982965230092

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 Cloud DB Kit Introduction

Huawei Cloud DB is a device-cloud synergy database product that enables seamless data synchronization between the device and cloud and between devices, and supports offline application operations, helping you quickly develop device-cloud and multi-device synergy applications.

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.

Navigate to My Projects > Project Settings > Build > Cloud DB, and click Enable now.

Click Add

Click Export, select Java, android and Package Name, and click OK.

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) and Add following dependency for HMS Cloud DB Kits.

implementation 'com.huawei.agconnect:agconnect-cloud-database:1.5.0.300'
implementation "com.huawei.agconnect:agconnect-auth-huawei:1.6.0.300"
implementation 'com.huawei.agconnect:agconnect-auth:1.5.0.300'

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

ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
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'

Code Implementation

Created following package model, clouddb, viewmodel.

Model: In your primary folder, create a new package and name it model.

CloudInfo.java:

package com.hms.corrierapp.clouddb;import com.huawei.agconnect.cloud.database.CloudDBZoneObject;
import com.huawei.agconnect.cloud.database.annotations.DefaultValue;
import com.huawei.agconnect.cloud.database.annotations.EntireEncrypted;
import com.huawei.agconnect.cloud.database.annotations.Indexes;
import com.huawei.agconnect.cloud.database.annotations.NotNull;
import com.huawei.agconnect.cloud.database.annotations.PrimaryKeys;@PrimaryKeys({"CourierID"})
@Indexes({"ID:CourierID"})
public final class CourierInfo extends CloudDBZoneObject {
private Integer CourierID;@NotNull
@DefaultValue(stringValue = "Courier Name")
private String CourierName;@DefaultValue(stringValue = "Desc")
private String Desc;@EntireEncrypted(isEncrypted = true)
private String FromAdress;@EntireEncrypted(isEncrypted = true)
private String ToAddress;public CourierInfo() {
super(CourierInfo.class);
this.CourierName = "Courier Name";
this.Desc = "Desc";
}public Integer getCourierID() {
return CourierID;
}public void setCourierID(Integer CourierID) {
this.CourierID = CourierID;
}public String getCourierName() {
return CourierName;
}public void setCourierName(String CourierName) {
this.CourierName = CourierName;
}public String getDesc() {
return Desc;
}public void setDesc(String Desc) {
this.Desc = Desc;
}public String getFromAdress() {
return FromAdress;
}public void setFromAdress(String FromAdress) {
this.FromAdress = FromAdress;
}public String getToAddress() {
return ToAddress;
}public void setToAddress(String ToAddress) {
this.ToAddress = ToAddress;
}}

ObjectTypeInfoHelper.java:

package com.hms.corrierapp.clouddb;import com.huawei.agconnect.cloud.database.CloudDBZoneObject;
import com.huawei.agconnect.cloud.database.ObjectTypeInfo;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public final class ObjectTypeInfoHelper {
private static final int FORMAT_VERSION = 2;
private static final int OBJECT_TYPE_VERSION = 1;public static ObjectTypeInfo getObjectTypeInfo() {
ObjectTypeInfo objectTypeInfo = new ObjectTypeInfo();
objectTypeInfo.setFormatVersion(FORMAT_VERSION);
objectTypeInfo.setObjectTypeVersion(OBJECT_TYPE_VERSION);
List<Class<? extends CloudDBZoneObject>> objectTypeList = new ArrayList<>();
Collections.addAll(objectTypeList, CourierInfo.class);
objectTypeInfo.setObjectTypes(objectTypeList);
return objectTypeInfo;
}
}

CloudDBViewModel.kt:

package com.hms.corrierapp.viewmodelimport android.app.Application
import com.hms.corrierapp.push.NotificationMessageBody
import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.hms.corrierapp.clouddb.ObjectTypeInfoHelper
import com.hms.corrierapp.model.Email
import com.hms.corrierapp.model.SMS
import com.hms.corrierapp.push.*
import com.huawei.agconnect.cloud.database.AGConnectCloudDB
import com.huawei.agconnect.cloud.database.CloudDBZone
import com.huawei.agconnect.cloud.database.CloudDBZoneConfig
import com.huawei.agconnect.cloud.database.exceptions.AGConnectCloudDBException
import com.huawei.agconnect.config.AGConnectServicesConfig
import com.huawei.hms.aaid.HmsInstanceId
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Responseclass CloudDBViewModel(application: Application) : AndroidViewModel(application), Observable {var mCloudDBZone: CloudDBZone? = nullprivate fun setAGC_DB() {
// val user = AGConnectAuth.getInstance().currentUser
AGConnectCloudDB.initialize(getApplication())
val mCloudDB = AGConnectCloudDB.getInstance()
try {
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo())
} catch (e: AGConnectCloudDBException) {
e.printStackTrace()
}
val mConfig = CloudDBZoneConfig("Zone1",
CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC)
mConfig.persistenceEnabled = true
val openDBZoneTask = mCloudDB.openCloudDBZone2(mConfig, true)
openDBZoneTask.addOnSuccessListener { cloudDBZone ->
Toast.makeText(getApplication(),"Cloud DB successfully opened", Toast.LENGTH_SHORT).show()
if (mCloudDBZone == null) {
mCloudDBZone = cloudDBZone
}
}.addOnFailureListener { e -> e.printStackTrace() }
}override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
}

CloudDBViewModelFactory.kt:

package com.hms.corrierapp.viewmodelimport androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviderclass CloudDBViewModelFactory : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(CloudDBViewModel::class.java)) {
return CloudDBViewModel() as T
}
throw IllegalArgumentException("UnknownViewModel")
}
}

App Build Result

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.

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

Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.

Conclusion

In this article, we have learned how to integrate Huawei ID, Push and Cloud DB Kit in Android application. After completely read this article user can easily implement Huawei ID and Client Side Push Notification and submit courier information on Cloud DB 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

https://developer.huawei.com/consum...-Guides/service-introduction-0000001050040060

https://developer.huawei.com/consum...des/agc-clouddb-introduction-0000001054212760

Cloud DB Kit Training Video:

https://developer.huawei.com/consumer/en/training/course/video/101628259491513909