DEV Community

MuraliAkula1
MuraliAkula1

Posted on

Integration of Huawei Account Kit in Attendance Tracker Android app (Kotlin) - Part 1

Image description
Introduction
In this article, we can learn how to integrate the Huawei Account Kit of Obtaining Icon Resources feature in Attendance Tracker app. So, I will provide the series of articles on this Attendance Tracker App, in upcoming articles I will integrate other Huawei Kits.

Account Kit
Huawei Account Kit provides for developers with simple, secure, and quick sign-in and authorization functions. User is not required to enter accounts, passwords and waiting for authorization. User can click on Sign In with HUAWEI ID button to quickly and securely sign in to the app.

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 and above installed.
  4. Minimum API Level 24 is required.
  5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

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

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

  • Generate a SHA-256 certificate fingerprint.

  • 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.
    Image description

Note: Project Name depends on the user created name.

  • Create an App in AppGallery Connect.
  • Download the agconnect-services.json file from App information, copy and paste in android **Project **under **app **directory, as follows.
    Image description

  • Enter SHA-256 certificate fingerprint and click **Save **button, as follows.
    Image description

  • Click Manage APIs tab and enable Account Kit.
    Image description

  • 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.6.0.300'

  • Add the below plugin and dependencies in build.gradle(Module) file.
    apply plugin: id 'com.huawei.agconnect'
    // Huawei AGC
    implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
    // Huawei Account Kit
    implementation 'com.huawei.hms:hwid:6.3.0.301'

  • Now Sync the gradle.

  • Add the required permission to the AndroidManifest.xml file.
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Let us move to development
I have created a project on Android studio with empty activity let us start coding.
In the MainActivity.kt we can find the business logic for login icon.
`class MainActivity : AppCompatActivity() {

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

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

    btn_click.setOnClickListener(mOnClickListener)

}

private fun findResources() {
    mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
        .setIdToken()
        .setAccessToken()
        .setProfile()
        .createParams()
   mAuthManager = AccountAuthManager.getService(this@MainActivity, mAuthParam)
   startActivityForResult(mAuthManager?.signInIntent, 1002)
    // Obtain Icon steps
    val task : Task<AccountIcon> = mAuthManager.channel
    task.addOnSuccessListener { accountIcon ->
        // Obtain icon information
        Toast.makeText(this, "Display Name: " + accountIcon.description, Toast.LENGTH_LONG).show()
        // Log.i("TAG", "displayName:" + accountIcon.description)
    }
    task.addOnFailureListener { e ->
        // The information fails to be obtained.
        if (e is ApiException) {
            Log.i("TAG", "getChannelfailed status:" + e.statusCode)
        }
    }

}

private val mOnClickListener: View.OnClickListener = object : View.OnClickListener {
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_click -> findResources()
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 1002 ) {
        val authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data)
        if (authAccountTask.isSuccessful) {
            Toast.makeText(this, "SigIn success", Toast.LENGTH_LONG).show()
            val intent = Intent(this@MainActivity, Home::class.java)
            startActivity(intent)
        }
        else {
            Toast.makeText(this, "SignIn failed: " + (authAccountTask.exception as ApiException).statusCode, Toast.LENGTH_LONG).show()
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}`

In the activity_main.xml we can create the UI screen.
`<?xml version="1.0" encoding="utf-8"?>
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:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:id="@+id/main_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:text="Welcome back! Login"
    android:textSize="28sp"
    android:textColor="@color/design_default_color_primary"
    android:textStyle="bold" />

<TextView
    android:id="@+id/txt_username"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:text=" Username"
    android:textColor="@color/black"
    android:textSize="16sp" />
<EditText
    android:id="@+id/edt_main_name"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
    android:hint=" Enter your username"
    android:drawableLeft="@drawable/ic_baseline_person"
    android:drawablePadding="3dp"
    android:drawableTint="@color/design_default_color_primary"
    android:textSize="17sp" />

<TextView
    android:id="@+id/txt_password"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:text=" Password"
    android:textColor="@color/black"
    android:textSize="16sp" />
<EditText
    android:id="@+id/main_password"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
    android:hint=" Enter your password"
    android:drawableTint="@color/design_default_color_primary"
    android:drawableLeft="@android:drawable/ic_lock_idle_lock"
    android:textSize="17sp" />
<TextView
    android:id="@+id/forgot_password"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_marginEnd="35dp"
    android:textAlignment="textEnd"
    android:layout_marginTop="10dp"
    android:textColor="@color/black"
    android:text=" Forgot password?"
    android:textSize="16sp" />

<Button
    android:id="@+id/click_login"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:padding="5dp"
    android:textColor="@color/black"
    android:text="LOGIN" />

<TextView
    android:id="@+id/txt_signin"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:textAlignment="center"
    android:layout_marginBottom="10dp"
    android:text=" Or SigIn Using"
    android:textColor="@color/black"
    android:drawablePadding="3dp"
    android:textSize="17sp" />
<ImageView
    android:id="@+id/btn_click"
    android:layout_width="50dp"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal"
    app:srcCompat="@drawable/hwid_auth_button_normal" />
Enter fullscreen mode Exit fullscreen mode

`

Find the ic_baseline_person.xml for UI design.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
</vector>

Demo

Image description

Image description

Image description

Tips and Tricks

  1. Make sure you are already registered as Huawei developer.
  2. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
  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.

Conclusion
In this article, we have learned how to integrate the Huawei Account Kit of Obtaining Icon Resources feature in Attendance Tracker app. So, I will provide the series of articles on this Attendance Tracker App, in upcoming articles will integrate other Huawei Kits.

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

Reference
Account Kit – Documentation
Account Kit – Training Video

Top comments (0)