Article Introduction
In this article we will show how to integrate Huawei ASR ML Kit and Site Kit to search and show all the nearby restaurants on the map using Huawei Map Kit.
1. Huawei ML Kit (ASR)
Automatic speech recognition (ASR) service can convert speech (no more than 60 seconds) into text in real time. Currently, Mandarin Chinese (including Chinese-English bilingual speech), English, French, German, Spanish, and Italian are supported. This service is widely used in social networking, conferencing, and shopping apps. For example, when it is inconvenient for users to manually enter text to search for information, they can speak a keyword for search, to further improve search efficiency.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
2. Huawei Site Kit
With Site Kit, your app can provide users with convenient and secure access to diverse, place-related services, and therefore acquire more users. Site Kit provides the following core capabilities for you to quickly build apps with which your users can explore the world around them:
Article Introduction
3. Huawei Map Kit
Map Kit is an SDK for map development. It covers map data of more than 200 countries and regions, and supports hundreds of languages. With this SDK, you can easily integrate map-based functions into your apps. Map Kit uses the WGS 84 GPS coordinate system, which meets most map development requirements outside China, including:
Map display: Displays buildings, roads, water systems, and Points of Interest (POIs).
Map interaction: Controls the interaction gestures and buttons on the map.
Map drawing: Adds location markers, map layers, overlays, and various shapes.
Pre-Requisites
Before we jump into the development, we have to integrate HMS Core in our project. Please follow the HMS Core Integration link mentioned in the References section of this article to integrate HMS Core. After integrating HMS Core, make sure you enabled Site Kit, Map Kit and ML Kit in the AGConnect Console and added the agconnet-service.json file in the app level directory.
Step 1: Add Dependencies & Permissions
1.1: Add the following dependencies in the app level build.gradle file:
Code:
dependencies {
// ... Below all the previously added dependencies
// Huawei Automatic speech recognition ML Kit
implementation 'com.huawei.hms:ml-computer-voice-asr:2.0.3.300'
implementation 'com.huawei.hms:ml-computer-voice-asr-plugin:2.0.3.300'
// Huawei Scan Kit
implementation 'com.huawei.hms:site:5.0.3.302'
// Huawei Map Kit
implementation 'com.huawei.hms:maps:5.0.3.302'
// Custom Runtime Permissions lib
implementation 'com.nabinbhandari.android:permissions:3.8'
}
1.2: Add the following permissions in the AndroidManifest.xml:
Code:
<!-- General Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required permission for ASR ML Kit -->
<!--Write permission-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--Read permission-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--Record permission-->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--Network status obtaining permission-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--Wi-Fi status obtaining permission-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<permission
android:name="com.huawei.mlkit.asr.permission"
android:label="ASR" />
Step 2: Add Layout Files
2.1: Add the following view_search.xml layout file in the layout folder of the res. This is the layout view of Custom Search bar which is used as Action Bar in the application, contains two views i-e searchCloseView and searchOpenView.
Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorAccent">
<RelativeLayout
android:id="@+id/searchClosedView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_toStartOf="@id/btnOpenSearch"
android:text="@string/app_name" />
<Button
android:id="@+id/btnOpenSearch"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_search" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/searchOpenedView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="@drawable/rounded_corner_background"
android:visibility="invisible">
<Button
android:id="@+id/btnCloseSearch"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="@drawable/ic_close" />
<EditText
android:id="@+id/editTextSearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textSize="13sp"
android:maxLines="1"
android:inputType="text"
android:imeOptions="actionDone"
android:hint="@string/search_hint"
android:layout_toStartOf="@id/btnVoiceSearch"
android:layout_toEndOf="@id/btnCloseSearch" />
<Button
android:id="@+id/btnVoiceSearch"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/btnSendSearch"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_microphone" />
<Button
android:id="@+id/btnSendSearch"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_send" />
</RelativeLayout>
</FrameLayout>
More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0203409993972490187
Well explained
Related
More information like this, you can visit HUAWEI Developer Forum
Take a photo and automatically get a infromations from the object in there, the scenes presented an behaiviors, this can be very usefull if you need to categorize your images and photos based on deep learning, this is something that we can do with HiAI - category label capability, in this post, you will see how to do it with a JAVA project.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
HIAI has serveral categories, base on deep learning you will be able to get a category that is identified in your images.
if you want to get the full information you will get it on the API reference in the next link: https://developer.huawei.com/consumer/en/doc/development/hiai-References/20201004
What do we need :
Java JDK installation package
Android Studio 3.1 or later
Android SDK package
HiAI SDK package
1) Install DevEco IDE Plugins
Into your Android istudio go to File>Settings>Plugins and search for Dev ECO ID and Enable it \m/
Once is enable, dont forget to Restart you IDE (Android Studio).
2) Create your APP
3) Add dependency library
· Download the Huawei-hiai-vision.aar package in the Huawei AI Engine SDKs from the Huawei developer community.
· Copy the downloaded vision-release.aar package to the app/libs directory of the project.
4) Lets code
I will create a project on android studio starting with an empty activity, for the visual side i will add an imageView to show the photo, a button to take the photo and a textview to show the different value sthat i can get for the category from HIAI, for the logic side i will use the HiAi methods to make the API call to get the Category Label of the photos, the steps that we need are the following:
-Define the layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.CategoryLabel">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintBottom_toTopOf="@+id/linearlayout">
<com.huawei.hiaicodedemo.widget.TitleBar
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleName="@string/as_catLabel"
app:titleTextColor="@color/black"
app:titleTextSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<ImageView
android:id="@+id/text_origin"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="30dp"
android:src="@color/color_6e"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.8"
app:layout_constraintTop_toBottomOf="@+id/titleBar"
app:layout_constraintBottom_toTopOf="@+id/after"/>
<LinearLayout
android:id="@+id/after"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="15dp"
android:text="@string/as_result"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_6e"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/color_6e"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintTop_toBottomOf="@+id/constraintlayout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintWidth_percent="0.8"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:background="@color/color_6e">
<EditText
android:id="@+id/text_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:textColor="@color/black" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/takePhoto"
android:layout_width="90dp"
android:layout_height="37dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="@drawable/submit_button"
android:text="@string/foto"
android:textAllCaps="false"
android:textColor="@drawable/submit_button_text_color"
android:textSize="15sp"
app:layout_constraintHorizontal_bias="0.031"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_album"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.37" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
-Prepare the intent to get the picture from the camera and save it into a Bitmap
Code:
public void tomaFotoCategory(CategoryLabel view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_FOTO);
}
Remember to add the permises to use the periferic in you manifest:
Code:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-Connect to HiAi Engine to use the Category Label using VisionBase Class
Code:
private void initHiAI() {
VisionBase.init(this, new ConnectionCallback() {
@Override
public void onServiceConnect() {
}
@Override
public void onServiceDisconnect() {
}
});
}
-Make the API CALL to get the Category Label, for this you need to declare a variable Frame & Label where you will get the response from the method
Code:
/** Define class detector, the context of this project is the input parameter*/
LabelDetector labelDetector = new LabelDetector(this);
/**Define the frame, put the bitmap that needs to detect the image into the frame*/
Frame frame = new Frame();
frame.setBitmap(bitmap);
/** Call the detect method to get the result of the label detection */
/** Note: This line of code must be placed in the worker thread instead of the main thread*/
JSONObject jsonLabel = labelDetector.detect(frame, null);
/** Call convertResult() method to convert the json to java class and get the label detection(you can parse the json by yourself, too) */
Label label1 = labelDetector.convertResult(jsonLabel);
this.label = label1.getCategory();
this.labelContents =label1.getLabelContent();
this.categoria=label1.getCategory();
this.prob=label1.getCategoryProbability();
handler.sendEmptyMessage(AESTHETICSCORE_RESULT);
-To print the result into the text i will use the getters of all the values that i can get form the object Label, LabelContents, Category anf probablillity:
Code:
textEdit.setText("Label: "+String.valueOf(label)
+" \n Categoria: "+categoria
+" \n Label_ID: "+labelContents.get(0).getLabelId()
+" \n Prob: "+ String.valueOf(prob)
+" \n Probabilidad: "+labelContents.get(0).getProbability());
If you need the complete code project, let me know, in there you will be able to review all the parameter definition .
1) Once you have your DevEco IDE plugin Eneable you will see an AMAZING KIT ASSITANT
go to DevECO>EMUI KIT>KIT ASSITANT
2) You will see a Menu like this:
3) Select HiAi and look Aesthetic
4) BOOM!!! Code Samples, Descriptions, Suggestions, DRAG & DROP CODE!!!!
Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201322052049010285&fid=0101187876626530001
More information like this, you can visit HUAWEI Developer Forum
Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201333475840980029&fid=0101187876626530001
You take a photo and you want to automatically identify which scene is by an intelligent recognize, or maybe you want to categorize you photo album by scenes, this is possible by a HIAI engine by a quick scene clasification.
On this post i will show you how you can use this feature on a Java-native app, since the front activities until the methos from HIAI you must call.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
HiAi Engine can recognize several scenes automatically bby using its methods, here some examples:
if you want to get the full information you will get it on the API reference in the next link: https://developer.huawei.com/consumer/en/doc/development/hiai-References/20201204
What do we need :
Java JDK installation package
Android Studio 3.1 or later
Android SDK package
HiAI SDK package
1) Install DevEco IDE Plugins
Into your Android istudio go to File>Settings>Plugins and search for Dev ECO ID and Enable it \m/
Once is enable, dont forget to Restart you IDE (Android Studio).
2) Create your APP
3) Add dependency library
· Download the Huawei-hiai-vision.aar package in the Huawei AI Engine SDKs from the Huawei developer community.
Copy the downloaded vision-release.aar package to the app/libs directory of the project.
4) Lets code
I will create a project on android studio starting with an empty activity, for the visual side i will add an imageView to show the photo, a button to take the photo and a textview to show the different values that i can get for the scene, for the logic side i will use the HiAi methods to make the API call to get the image scene of the photos.
-Define the layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.SceneDetection">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintBottom_toTopOf="@+id/linearlayout">
<com.huawei.hiaicodedemo.widget.TitleBar
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleName="@string/as_scene"
app:titleTextColor="@color/black"
app:titleTextSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<ImageView
android:id="@+id/text_origin"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="30dp"
android:src="@color/color_6e"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.8"
app:layout_constraintTop_toBottomOf="@+id/titleBar"
app:layout_constraintBottom_toTopOf="@+id/after"/>
<LinearLayout
android:id="@+id/after"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="15dp"
android:text="@string/as_scene"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_6e"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/color_6e"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintTop_toBottomOf="@+id/constraintlayout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintWidth_percent="0.8"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:background="@color/color_6e">
<EditText
android:id="@+id/text_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:textColor="@color/black" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/takePhoto"
android:layout_width="90dp"
android:layout_height="37dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="@drawable/submit_button"
android:text="@string/foto"
android:textAllCaps="false"
android:textColor="@drawable/submit_button_text_color"
android:textSize="15sp"
app:layout_constraintHorizontal_bias="0.031"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_album"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.37" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
-Prepare the intent to get the picture from the camera and save it into a Bitmap
Code:
public void tomaFotoCategory(CategoryLabel view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_FOTO);
}
Remember to add the permises to use the periferic in you manifest:
Code:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-Connect to HiAi Engine to use the scene detection by VisionBase Class
In the class where you are going to call the methos to use the HIAI, we have to init the HIAI capability, i have created a method to call this init as following:
Code:
private void initHiAI() {
VisionBase.init(this, new ConnectionCallback() {
@Override
public void onServiceConnect() {
}
@Override
public void onServiceDisconnect() {
}
});
}
-lets get the scene
Now, to get the scene category from your photo, you will have to make an instance from the class SceneDetectior and using its detect() method the value can be identified, in my case i create a method to do this job, every step is decribed in my code bellow:
Code:
private void setHiAi() {
/** Define class detector, the context of this project is the input parameter: */
SceneDetector sceneDetector = new SceneDetector(this);
/** define frame class, put the picture which need to be scene detected into the frame */
Frame frame = new Frame();
/** BitmapFactory.decodeFile input resource file path*/
frame.setBitmap(bitmap);
/** Call the detect method to get the result of the scene detection */
/** Note: This line of code must be placed in the worker thread instead of the main thread */
JSONObject jsonScene = sceneDetector.detect(frame, null);
/** Call convertResult() method to convert the json to java class and get the label detection (you can parse the json by yourself, too) */
Scene scene = sceneDetector.convertResult(jsonScene);
/** Get the identified scene type*/
int type = scene.getType();
this.resultado = type;
handler.sendEmptyMessage(TEXTDETECTOR_RESULT);
}
The result is the following (if you need the full project, let me know in the comments):
---------------------- < BONUS TRACK /> --------------------
1) Once you have your DevEco IDE plugin Eneable you will see an AMAZING KIT ASSITANT
go to DevECO>EMUI KIT>KIT ASSITANT
2) You will see a Menu like this:
3) Select HiAi and search for scene detection:
4) BOOM!!! Code Samples, Descriptions, Suggestions, DRAG & DROP CODE!!!!
How much working days it will take to integrate ?
This is very nice, it helps a lot.
If you have a big library is very useful. It will be very awesome if you could use it with movies.
Overview
Smart search application provides user to search capabilities through the device-side SDK and cloud-side APIs. Moreover this app results the selection search of Web Page Search, Image Search, Video Search and News Search. To achieve this the app is using HMS Search kit.
Huawei Search Kit
HUAWEI Search Kit fully opens Petal Search capabilities through the device-side SDK and cloud-side APIs, enabling ecosystem partners to quickly provide the optimal mobile app search experience.
Advantages of Using HMS Search kit
Rapid app development
Opens search capabilities through the device-side SDK and cloud-side APIs for different kinds of apps to build their own search capabilities within a short period of time.
Quick response
Allows you to use the global index library built by Petal Search and its massive computing and storage resources to quickly return the search results from the destination site that you specified.
Low cost
Frees you from having to consider such problems as technical implementation, operations, and maintenance, thus decreasing your development cost.
Integration Steps
1) Create an app in App Gallery Connect (AGC)
2) Enable Search kit from Manage Apis.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
3) Add SHA-256 certificate fingerprint , Set Data storage location.
4) Now download the agconnect-service.json file from app information and paste in your app folder in android studio.
5) Add the maven url in buildscript and all projects in project level gradle file.
Java:
maven {url 'https://developer.huawei.com/repo/'}
6) Add the below dependencies in app level gradle file.
Java:
implementation 'com.huawei.hms:searchkit:5.0.4.303'
7) Set minimum SDK version as 24.
Java:
android {
defaultConfig {
minSdkVersion 24
}
}
8) Create Application class and set the AppID.
Java:
class ApplicationClass: Application() {
override fun onCreate() {
super.onCreate()
SearchKitInstance.init(this, Constants.APP_ID)
}
}
9) Now sync the gradle.
Development
1. Web Page Search
This explains how to use web page search and in this you can set the restriction for the search such as Language,Region, Page number and the number of search results to be displayed.
Add this code in the MainActivity and call this by passing searchText whenever the user search using WebSearch.
Java:
fun webSearch(searchText: String, token: String) : ArrayList<WebItem>{
val webSearchRequest = WebSearchRequest()
webSearchRequest.setQ(searchText)
webSearchRequest.setLang(Language.ENGLISH)
webSearchRequest.setSregion(Region.UNITEDKINGDOM)
webSearchRequest.setPs(10)
webSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val webSearchResponse = SearchKitInstance.getInstance().webSearcher.search(webSearchRequest)
for(i in webSearchResponse.getData()){
webResults.clear()
webResults.add(i)
}
return webResults
}
2. Image Search
This explains how to use Image search and the expected result will be in Image. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.
Add this code in the MainActivity and call this by passing searchText whenever the user search using ImageSearch.
Java:
fun imageSearch(searchText: String, token: String) : ArrayList<ImageItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val imageSearchResponse = SearchKitInstance.getInstance().imageSearcher.search(commonSearchRequest)
for(i in imageSearchResponse.getData()) {
imageResults.clear()
imageResults.add(i)
}
return imageResults
}
3. Video Search
This explains how to use Video search and the expected result will be in the Video format. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.
Add this code in the MainActivity and call this by passing searchText whenever the user search using VideoSearch.
Java:
fun videoSearch(searchText: String, token: String) : ArrayList<VideoItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val videoSearchResponse = SearchKitInstance.getInstance().videoSearcher.search(commonSearchRequest)
for(i in videoSearchResponse.getData()) {
videoResults.clear()
videoResults.add(i)
}
return videoResults
}
4. News Search
This explains how to use News search in your application. Also here you can set the restrictions to the search like specify the region, the language for the search, the page number and the number of search results to be returned on the page.
Add this code in the MainActivity and call this by passing searchText whenever the user search using NewsSearch.
Java:
fun newsSearch(searchText: String, token: String) : ArrayList<NewsItem>{
val commonSearchRequest = CommonSearchRequest()
commonSearchRequest.setQ(searchText)
commonSearchRequest.setLang(Language.ENGLISH)
commonSearchRequest.setSregion(Region.UNITEDKINGDOM)
commonSearchRequest.setPs(10)
commonSearchRequest.setPn(1)
SearchKitInstance.getInstance().setInstanceCredential(token)
val newsSearchResponse = SearchKitInstance.getInstance().newsSearcher.search(commonSearchRequest)
for(i in newsSearchResponse.getData()) {
newsResults.clear()
newsResults.add(i)
}
return newsResults
}
5. activity_main.xml
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".activity.SearchActivity">
<RelativeLayout
android:id="@+id/searchview_layout"
android:layout_height="36dp"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="36dp"
android:background="@drawable/shape_search_text"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical|start"
android:hint="Search"
android:imeOptions="actionSearch"
android:paddingStart="42dp"
android:paddingEnd="40dp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="13"
android:textAlignment="viewStart"
android:textColor="#000000"
android:textColorHint="#61000000"
android:textCursorDrawable="@drawable/selectedittextshape"
android:textSize="16sp" />
<ImageView
android:id="@+id/search_src_icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="3dp"
android:clickable="false"
android:focusable="false"
android:padding="10dp"
android:src="@drawable/ic_public_search" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/suggest_list"
android:visibility="gone"
android:layout_below="@+id/searchview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:id="@+id/linear_spell_check"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/searchview_layout"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="spellCheck:"
android:textColor="#000000"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/tv_spell_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<View
android:id="@+id/v_line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:layout_below="@+id/linear_spell_check"
android:background="#e2e2e2"/>
<LinearLayout
android:id="@+id/linear_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/v_line"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
app:tabIndicatorColor="#0000ad"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabRippleColor="#0D000000"
app:tabSelectedTextColor="#0000ad">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/content_list"
android:visibility="gone"
android:layout_below="@+id/v_line"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Now the implementation is done. From the search bar edit text you can call any of the respective search method and obtain the result as expected.
Result
Tips and Tricks
1) Do not forget to add the AppID in Application class.
2) Maintain minSdkVersion 24 and above.
Conclusion
This application is very much useful for those who wants to create a search application using HMS Search kit. Moreover it helps to understand the usage of HMS Search kit and to get the appropriate search results.
Reference
https://developer.huawei.com/consum.../HMSCore-Guides/introduction-0000001055591730
Can we customize search parameters based on requirement?
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Introduction
In this article, we can learn how to integrate the Huawei Account Kit and Analytics Kit in Quiz Application. The quiz application is mainly intended to develop the knowledge for users and also to learn about particular topics. So, I will provide the series of articles on this Quiz 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.
Analytics Kit
HUAWEI Analytics Kit provides analysis models to understand user behaviour and gain in-depth insights into users, products and content. It helps you to gain insight about user behaviour on different platforms based on the user behaviour events and user attributes reported by through apps.
AppGallery Connect
Find the Analytics using AppGallery connect dashboard.
Choose My Projects > Huawei Analytics > Overview > Project overview.
Project overview displays the core indicators of current project, such as the number of new users, User activity, User acquisition, User revisit, New user retention, Active user retention, User characteristics and Popular pages etc. providing a quick overview of the users and how they are using your 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
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 Save button, as follows.
Note: Above steps from Step 1 to 7 is common for all Huawei Kits.
8. Click Manage APIs tab and enable Account Kit and HUAWEI Analytics.
9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
Java:
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
10. Add the below plugin and dependencies in build.gradle(Module) file.
Java:
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'
// Huawei Analytics Kit
implementation 'com.huawei.hms:hianalytics:6.4.0.300'
11. Now Sync the gradle.
12. Add the required permission to the AndroidManifest.xml file.
XML:
<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 Huawei login button.
Java:
class MainActivity : AppCompatActivity() {
// Account Kit variables
private var mAuthManager: AccountAuthService? = null
private var mAuthParam: AccountAuthParams? = null
// Analytics Kit - Initialize the Analytics
var mInstance: HiAnalyticsInstance? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Account kit button click Listener
btn_login.setOnClickListener(mOnClickListener)
// Initialize the Analytics function
initAna()
}
// Account kit, method to send an authorization request.
private fun signIn() {
mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.setProfile()
.createParams()
mAuthManager = AccountAuthManager.getService([email protected], mAuthParam)
startActivityForResult(mAuthManager?.signInIntent, 1002)
}
private val mOnClickListener: View.OnClickListener = object : View.OnClickListener {
override fun onClick(v: View?) {
when (v?.id) {
R.id.btn_login -> signIn()
}
}
}
// Process the authorization result
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) {
// Analytics data to send custom events
val bundle = Bundle()
bundle.putString("name", data!!.extras!!.getString("name" ))
bundle.putString("email", data!!.extras!!.getString("email" ))
mInstance!!.onEvent(HAEventType.SIGNIN, bundle)
Toast.makeText(this, "SigIn success", Toast.LENGTH_LONG).show()
val intent = Intent([email protected], Home::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "SignIn failed: " + (authAccountTask.exception as ApiException).statusCode, Toast.LENGTH_LONG).show()
}
}
}
private fun initAna() {
// Enable Analytics Kit Log
HiAnalyticsTools.enableLog()
// Generate the Analytics Instance
mInstance = HiAnalytics.getInstance(this)
// Enable collection capability
mInstance?.setAnalyticsEnabled(true)
}
}
In the activity_main.xml we can create the UI screen.
Java:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ExtraText,MissingConstraints">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="273dp"
android:background="@drawable/blue_bg">
<ImageView
android:layout_width="77dp"
android:layout_height="77dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="93dp"
android:src="@drawable/quiz_icon" />
</FrameLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="37dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/username_icon"
android:background="@android:color/transparent"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/username_icon"
android:layout_width="18dp"
android:layout_height="13dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/email" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="13dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pass_icon"
android:background="@android:color/transparent"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/pass_icon"
android:layout_width="18dp"
android:layout_height="13dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="19dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_fill__rounded_color"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="@color/white"
android:textSize="13sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="FORGOT PASSWORD ?"
android:textColor="#1566e0"
android:textSize="12sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="or"
android:textColor="#0E0E0E"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/btn_login"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/huawei_icon" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/facebook_icon" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:src="@drawable/instagram_icon" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Demo
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 to integrate the Huawei Account Kit and Analytics Kit in Quiz Application. The quiz application is mainly intended to develop the knowledge for users and also to learn about particular topics. So, I will provide a series of articles on this Quiz App, in upcoming articles I will integrate other Huawei Kits.
I hope you have read this article. If you found it helpful, please provide likes and comments.
Reference
Account Kit – Documentation
Account Kit – Training Video
Analytics Kit – Documentation
Analytics Kit – Training Video
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Introduction
In this article, we can learn how to integrate the Huawei Account Kit into the Patient Tracking app. So, I will provide a series of articles on this Patient Tracking 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. The user is not required to enter accounts, and passwords and waiting for authorization. Users 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
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 Save button, as follows.
Note: Above steps from Step 1 to 7 is common for all Huawei Kits.
8. Click Manage APIs tab and enable Account Kit.
9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
Java:
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
10. Add the below plugin and dependencies in build.gradle(Module) file.
Java:
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'
11. Now Sync the gradle.
12. Add the required permission to the AndroidManifest.xml file.
Java:
<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.
Java:
class MainActivity : AppCompatActivity() {
// Account Kit variables
private var mAuthManager: AccountAuthService? = null
private var mAuthParam: AccountAuthParams? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Account kit button click Listener
btn_login.setOnClickListener(mOnClickListener)
}
// Account kit, method to send an authorization request.
private fun signIn() {
mAuthParam = AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.setProfile()
.createParams()
mAuthManager = AccountAuthManager.getService([email protected], mAuthParam)
startActivityForResult(mAuthManager?.signInIntent, 1002)
}
private val mOnClickListener: View.OnClickListener = object : View.OnClickListener {
override fun onClick(v: View?) {
when (v?.id) {
R.id.btn_login -> signIn()
}
}
}
// Process the authorization result
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([email protected], Home::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "SignIn failed: " + (authAccountTask.exception as ApiException).statusCode, Toast.LENGTH_LONG).show()
}
}
}
}
In the activity_main.xml we can create the UI screen.
Java:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ExtraText,MissingConstraints">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="35dp"
android:layout_marginRight="62dp"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold">
</TextView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="32dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/username_icon"
android:background="@android:color/transparent"
android:hint="Enter Username "
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/username_icon"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/username" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginTop="13dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_border_rounded_cornwe">
<EditText
android:id="@+id/edt_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pass_icon"
android:background="@android:color/transparent"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10"
android:maxLines="1"
android:paddingLeft="17dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="13sp">
</EditText>
<ImageView
android:id="@+id/pass_icon"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:src="@drawable/password" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="63dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Forgot Password?"
android:textAllCaps="false"
android:textColor="#0A0A0B"
android:textSize="14sp">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="62dp"
android:layout_marginTop="19dp"
android:layout_marginRight="62dp"
android:background="@drawable/blue_fill__rounded_color"
android:gravity="center"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Login"
android:textColor="@color/white"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="45dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sign in with Social Networks"
android:textColor="#0E0E0E"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="55dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/btn_login"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/huawei_icon" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="@drawable/google" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:src="@drawable/instagram_icon" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Demo
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 into the Patient Tracking app. So, I will provide a series of articles on this Patient Tracking App, in upcoming articles I will integrate other Huawei Kits.
I hope you have read this article. If you found it helpful, please provide likes and comments.
Reference
Account Kit – Documentation
Account Kit – Training Video