[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

Online food ordering is process that delivers food from local restaurants. Mobile apps make our world better and easier customer to prefer comfort and quality instead of quantity.

Sign In Module

User can login with mobile number to access food order application. Using auth service we can integrate third party sign in options. Huawei Auth service provides a cloud based auth service and SDK.

In this article, following Kits are covered:

1. AGC Auth Service

2. Ads Kit

3. Site kit

Steps

1. Create App in Android.

2. Configure App in AGC.

3. Integrate the SDK in our new Android project.

4. Integrate the dependencies.

5. Sync project.

Screens

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904081913.41464793819728726584897334287762:50510915095709:2800:3CD38FE00A3FAD48AC4D29F84C855A99239200F2B284E0C8E758AD1B011B88E8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904081913.41464793819728726584897334287762:50510915095709:2800:3CD38FE00A3FAD48AC4D29F84C855A99239200F2B284E0C8E758AD1B011B88E8.png[/IMG]

Configuration

1. Login into AppGallery Connect, select FoodApp in My Project list.

2. Enable required APIs in manage APIs tab.

Choose Project Settings > ManageAPIs

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904085356.80050566566404676132036333799581:50510915095709:2800:C9C636D6DC08D527FDF585573F294303F215B26640C886156205B8699F41EDC5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904085356.80050566566404676132036333799581:50510915095709:2800:C9C636D6DC08D527FDF585573F294303F215B26640C886156205B8699F41EDC5.png[/IMG]

3. Enable auth service before enabling Authentication modes as we need to enable Auth Service.

Choose Build > Auth Service and click Enable now top right-corner

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904085557.04705179378145364182164319358504:50510915095709:2800:A0BBA832107FA832F9E251B2614FB366967AFB6E8BA5FB487E07844A84F51E62.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904085557.04705179378145364182164319358504:50510915095709:2800:A0BBA832107FA832F9E251B2614FB366967AFB6E8BA5FB487E07844A84F51E62.png[/IMG]

4. Enable sign in modes required for application

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904090031.80919303574321921558327768746498:50510915095709:2800:88CCE3D4C1A987315D00D0717498C9EA0C10CB7A9D7E86C3B0597359D95B27CF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904090031.80919303574321921558327768746498:50510915095709:2800:88CCE3D4C1A987315D00D0717498C9EA0C10CB7A9D7E86C3B0597359D95B27CF.png[/IMG]

Integration

Create Application in Android Studio.

App level gradle dependencies.

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

Gradle dependencies

implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation 'com.huawei.agconnect:agconnect-core:1.4.0.300'
implementation 'com.huawei.agconnect:agconnect-auth:1.4.0.300'
implementation 'com.huawei.hms:hwid:4.0.4.300'
implementation 'com.huawei.hms:site:4.0.3.300'
implementation 'com.huawei.hms:ads-lite:13.4.30.307'

Root level gradle dependencies

maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.3.1.300'

Add the below permissions in Android Manifest file

<manifest xlmns:android...>
...

<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" />

<application ...

</manifest>

Ads Kit: Huawei ads SDK to quickly integrate ads into your app, ads can be a powerful assistant to attract users.

Code Snippet

<com.huawei.hms.ads.banner.BannerView
android:id="@+id/huawei_banner_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
hwads:adId="testw6vs28auh3"
hwads:bannerSize="BANNER_SIZE_360_57" />

BannerView hwBannerView = findViewById(R.id.huawei_banner_view);
AdParam adParam = new AdParam.Builder()
.build();
hwBannerView.loadAd(adParam);

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084053.99929093411573092490424509186272:50510915095709:2800:1E41251E779D3361CA0B555D5A4BDDED91CE4780DC41CE1E7884ECB6E887CBF3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084053.99929093411573092490424509186272:50510915095709:2800:1E41251E779D3361CA0B555D5A4BDDED91CE4780DC41CE1E7884ECB6E887CBF3.png[/IMG]

Auth Service:

1. Create Object for VerifyCodeSettings. Apply for verification code by mobile number based login.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084119.42853646238034434344720927775734:50510915095709:2800:0A08AFBF2D07307004FBAFD96C64BC26B3D4751726CAE4DAD09E5F23C9E3257F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084119.42853646238034434344720927775734:50510915095709:2800:0A08AFBF2D07307004FBAFD96C64BC26B3D4751726CAE4DAD09E5F23C9E3257F.png[/IMG]

VerifyCodeSettings mVerifyCodeSettings = VerifyCodeSettings.newBuilder()
.action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)
.sendInterval(30)
.locale(Locale.getDefault())
.build();

2. Send mobile number, country code and verify code settings object.

if (!mMobileNumber.isEmpty() && mMobileNumber.length() == 10) {
Task<VerifyCodeResult> resultTask = PhoneAuthProvider.requestVerifyCode("+91", mMobileNumber, mVerifyCodeSettings);
resultTask.addOnSuccessListener(verifyCodeResult -> {
Toast.makeText(SignIn.this, "verify code has been sent.", Toast.LENGTH_SHORT).show();
if (!isDialogShown) {
verfiyOtp();
}
}).addOnFailureListener(e -> Toast.makeText(SignIn.this, "Send verify code failed.", Toast.LENGTH_SHORT).show());
Toast.makeText(this, mEdtPhone.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid Phone Number!", Toast.LENGTH_SHORT).show();
}

3. Create object for Phone User Builder.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084318.19416294024173402163937665041988:50510915095709:2800:6464035B10D8FBE339FCDBFF252017A75B2852FA02363C046967A6AD936AD53D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084318.19416294024173402163937665041988:50510915095709:2800:6464035B10D8FBE339FCDBFF252017A75B2852FA02363C046967A6AD936AD53D.png[/IMG]

PhoneUser mPhoneUser = new PhoneUser.Builder()
.setCountryCode("+91")
.setPhoneNumber(mMobileNumber)
.setVerifyCode(otp)
.setPassword(null)
.build();

4. Check below code snippet to validate OTP.

AGConnectAuth.getInstance().createUser(mPhoneUser)
.addOnSuccessListener(signInResult -> {
Toast.makeText(SignIn.this, "Verfication success!", Toast.LENGTH_LONG).show();
SharedPrefenceHelper.setPreferencesBoolean(SignIn.this, IS_LOGGEDIN, true);
redirectActivity(MainActivity.class);
}).addOnFailureListener(e -> Toast.makeText(SignIn.this, "Verfication failed!", Toast.LENGTH_LONG).show());

Site kit: Using HMS Site kit we can provide easy access to hotels and places.

searchService.textSearch(textSearchRequest, new SearchResultListener<TextSearchResponse>() {
@Override
public void onSearchResult(TextSearchResponse response) {
for (Site site : response.getSites()) {
SearchResult searchResult = new SearchResult(site.getAddress().getLocality(), site.getName());
String result = site.getName() + "," + site.getAddress().getSubAdminArea() + "\n" +
site.getAddress().getAdminArea() + "," +
site.getAddress().getLocality() + "\n" +
site.getAddress().getCountry() + "\n";
list.add(result);
searchList.add(searchResult);
}
mAutoCompleteAdapter.clear();
mAutoCompleteAdapter.addAll(list);
mAutoCompleteAdapter.notifyDataSetChanged();
autoCompleteTextView.setAdapter(mAutoCompleteAdapter);
Toast.makeText(getActivity(), String.valueOf(response.getTotalCount()), Toast.LENGTH_SHORT).show();
}

@Override
public void onSearchError(SearchStatus searchStatus) {
Toast.makeText(getActivity(), searchStatus.getErrorCode(), Toast.LENGTH_SHORT).show();
}
});

Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084703.37958940673233412321744142617513:50510915095709:2800:7CE2270D0179046FB992F9A70DE28506B285E33581AEA62A1CFEE3CBFE565EEA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084703.37958940673233412321744142617513:50510915095709:2800:7CE2270D0179046FB992F9A70DE28506B285E33581AEA62A1CFEE3CBFE565EEA.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084708.86523030508699111927500400608704:50510915095709:2800:0BD456610766D0438F0FC84F2CD5BDF8712D88D7F07FD2A529D2485DCAF760DE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084708.86523030508699111927500400608704:50510915095709:2800:0BD456610766D0438F0FC84F2CD5BDF8712D88D7F07FD2A529D2485DCAF760DE.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084715.49240147269922787431161836215371:50510915095709:2800:8E6D7F8E772F801B10852DD708B8744CFFACEAA9F2AF26D16DB1AE292798CD03.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200904084715.49240147269922787431161836215371:50510915095709:2800:8E6D7F8E772F801B10852DD708B8744CFFACEAA9F2AF26D16DB1AE292798CD03.png[/IMG]

Conclusion:

In this article we have learnt Auth service,Ads Kit & site kit Integration in food application.

Reference:

Auth Service:

https://developer.huawei.com/consumer/en/doc/development/Tools-Guides/agc-conversion-auth-0000001050157270

Ads Kit :

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-introduction-0000001050064960

Site Kit :

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-sdk-introduction-0000001050158571