[Huawei Developers] muraliameakula:
Overview

In this article, I will create a Doctor Consult Demo App along with the integration of Huawei Id and HMS Core Identity. Which provides an easy interface to Book an Appointment with doctor. Users can choose specific doctors and get the doctor details using Huawei User Address.

By Reading this article, you'll get an overview of HMS Core Identity, including its functions, open capabilities, and business value.

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)

Android Studio

AppGallery Account

App Gallery Integration process

Sign In and Create or Choosea 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

1. Create A New Project.

2. Configure Project Gradle.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath 'com.huawei.agconnect:agcp:1.4.2.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

3. Configure App Gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

android {
compileSdkVersion 30
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "com.hms.doctorconsultdemo"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//noinspection GradleCompatible
implementation 'com.android.support:recyclerview-v7:27.0.2'

implementation 'com.huawei.hms:identity:5.3.0.300'
implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300'
implementation 'com.huawei.hms:hwid:5.3.0.302'
}

4. Configure AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hms.doctorconsultdemo">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".BookAppointmentActivity"></activity>
<activity android:name=".DoctorDetails" />
<activity android:name=".HomeActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

5. Create Activity class with XML UI.

MainActivity:

This activity performs login with Huawei ID operation.

package com.hms.doctorconsultdemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.huawei.hmf.tasks.Task;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.hwid.HuaweiIdAuthManager;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParams;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParamsHelper;
import com.huawei.hms.support.hwid.result.AuthHuaweiId;
import com.huawei.hms.support.hwid.service.HuaweiIdAuthService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final int REQUEST_SIGN_IN_LOGIN = 1002;
private static String TAG = MainActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button view = findViewById(R.id.btn_sign);
view.setOnClickListener(this);

}

private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), REQUEST_SIGN_IN_LOGIN);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sign:
signIn();
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());

Intent intent = new Intent(this, HomeActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();

} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}

}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Doctor Consult"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:textSize="34sp"
android:textStyle="bold" />

<Button
android:id="@+id/btn_sign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@color/colorPrimary"
android:text="Login With Huawei Id"
android:textColor="@color/colorAccent"
android:textStyle="bold" />

</LinearLayout>

</ScrollView>

</RelativeLayout>

HomeActivity:

This activity displays list of type of treatments so that user can choose and book an appointment.

package com.hms.doctorconsultdemo;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

public static final String TAG = "Home";
private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

//init RecyclerView
initRecyclerViewsForPatient();

//Toolbar initialization
mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Home");

// add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}

public void initRecyclerViewsForPatient() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(layoutManager);

ArrayList<PatientHomeData> list = new ArrayList();
list.add(new PatientHomeData("Cardiologist", R.drawable.ekg_2069872_640));
list.add(new PatientHomeData("Neurologist", R.drawable.brain_1710293_640));

list.add(new PatientHomeData("Oncologist", R.drawable.cancer));
list.add(new PatientHomeData("Pathologist", R.drawable.boy_1299626_640));
list.add(new PatientHomeData("Hematologist", R.drawable.virus_1812092_640));
list.add(new PatientHomeData("Dermatologist", R.drawable.skin));

PatientHomeViewAdapter adapter = new PatientHomeViewAdapter(getApplicationContext(), list);
recyclerView.setAdapter(adapter);

}

}

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/main_page_toolbar"
layout="@layout/app_bar_layout" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/main_page_toolbar"
android:layout_alignParentLeft="true" />

</RelativeLayout>

BookAppointmentActivity:

This activity performs Huawei User Address so user can get an details.

package com.hms.doctorconsultdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.identity.Address;
import com.huawei.hms.identity.entity.GetUserAddressResult;
import com.huawei.hms.identity.entity.UserAddress;
import com.huawei.hms.identity.entity.UserAddressRequest;
import com.huawei.hms.support.api.client.Status;

public class BookAppointmentActivity extends AppCompatActivity {

private static final String TAG = "BookAppoinmentActivity";
private static final int GET_ADDRESS = 1000;

private TextView txtUser;
private TextView txtDesc;

private Button queryAddrButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_appointment);

txtUser = findViewById(R.id.txt_title);
txtDesc = findViewById(R.id.txt_desc);
queryAddrButton = findViewById(R.id.btn_get_address);
queryAddrButton.setOnClickListener(v -> {
getUserAddress();
});
}

private void getUserAddress() {
UserAddressRequest req = new UserAddressRequest();
Task<GetUserAddressResult> task = Address.getAddressClient(this).getUserAddress(req);
task.addOnSuccessListener(new OnSuccessListener<GetUserAddressResult>() {
@Override
public void onSuccess(GetUserAddressResult result) {
Log.i(TAG, "onSuccess result code:" + result.getReturnCode());
try {
startActivityForResult(result);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "on Failed result code:" + e.getMessage());
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
switch (apiException.getStatusCode()) {
case 60054:
Toast.makeText(getApplicationContext(), "Country not supported identity", Toast.LENGTH_SHORT).show();
break;
case 60055:
Toast.makeText(getApplicationContext(), "Child account not supported identity", Toast.LENGTH_SHORT).show();
break;
default: {
Toast.makeText(getApplicationContext(), "errorCode:" + apiException.getStatusCode() + ", errMsg:" + apiException.getMessage(), Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(getApplicationContext(), "unknown exception", Toast.LENGTH_SHORT).show();
}
}
});

}

private void startActivityForResult(GetUserAddressResult result) throws IntentSender.SendIntentException {
Status status = result.getStatus();
if (result.getReturnCode() == 0 && status.hasResolution()) {
Log.i(TAG, "the result had resolution.");
status.startResolutionForResult(this, GET_ADDRESS);
} else {
Log.i(TAG, "the response is wrong, the return code is " + result.getReturnCode());
Toast.makeText(getApplicationContext(), "errorCode:" + result.getReturnCode() + ", errMsg:" + result.getReturnDesc(), Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult requestCode " + requestCode + " resultCode " + resultCode);
switch (requestCode) {
case GET_ADDRESS:
switch (resultCode) {
case Activity.RESULT_OK:
UserAddress userAddress = UserAddress.parseIntent(data);
if (userAddress != null) {
StringBuilder sb = new StringBuilder();
sb.append(" " + userAddress.getPhoneNumber());
sb.append(" " + userAddress.getEmailAddress());
sb.append("\r\n" + userAddress.getCountryCode() + " ");
sb.append(userAddress.getAdministrativeArea());
if (userAddress.getLocality() != null) {
sb.append(userAddress.getLocality());
}
if (userAddress.getAddressLine1() != null) {
sb.append(userAddress.getAddressLine1());
}
sb.append(userAddress.getAddressLine2());
if (!"".equals(userAddress.getPostalNumber())) {
sb.append("\r\n" + userAddress.getPostalNumber());
}
Log.i(TAG, "user address is " + sb.toString());
txtUser.setText(userAddress.getName());
txtDesc.setText(sb.toString());
txtUser.setVisibility(View.VISIBLE);
txtDesc.setVisibility(View.VISIBLE);
} else {
txtUser.setText("Failed to get user Name");
txtDesc.setText("Failed to get user Address.");
Toast.makeText(getApplicationContext(), "the user address is null.", Toast.LENGTH_SHORT).show();
}
break;

default:
Log.i(TAG, "result is wrong, result code is " + resultCode);
Toast.makeText(getApplicationContext(), "the user address is null.", Toast.LENGTH_SHORT).show();
break;
}
default:
break;
}
}
}

activity_book_appointment.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">

<include
android:id="@+id/main_page_toolbar"
layout="@layout/app_bar_layout" />

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="30dp"
android:background="@color/colorAccent"
android:orientation="vertical"
android:padding="25dp">

<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mr Doctor"
android:textAlignment="center"
android:visibility="gone"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/txt_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Details"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="normal" />

<Button
android:id="@+id/btn_get_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@color/colorPrimary"
android:text="Get Huawei User Address"
android:textColor="@color/colorAccent"
android:textStyle="bold" />

</LinearLayout>

</RelativeLayout>

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.

A maximum of 10 user addresses are allowed.

If HMS Core (APK) is installed on a mobile phone, check the version. If the version is earlier than 4.0.0, upgrade it to 4.0.0 or later. If the version is 4.0.0 or later, you can call the HMS Core Identity SDK to use the capabilities.

Conclusion

In this article, we have learned how to integrate HMS Core Identity in Android application. After completely read this article user can easily implement Huawei User Address APIs by HMS Core Identity So that User can book appointment with Huawei User Address.

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 Identity Docs: https://developer.huawei.com/consumer/en/hms/huawei-identitykit/

Original Source