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

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202333898987840045&fid=0101187876626530001

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821103209.04859358073771652644824559681128:50510901090322:2800:789D38C0DB7B2FC644BE58DDF048B554BE3942916B78848EC25EA2305400ED2E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821103209.04859358073771652644824559681128:50510901090322:2800:789D38C0DB7B2FC644BE58DDF048B554BE3942916B78848EC25EA2305400ED2E.png[/IMG]​

Huawei Awareness Kit provides our application to obtain information such as current time, location, behavior, audio device status, ambient light, weather, and nearby beacons. Using this information we can get an advantage over user's current situation more efficiently and can manipulate data for better user experience.

In this article, we will use weather information using HMS Awareness Kit for manipulating warm up exercise for user.

Demo

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821103451.73295479609594447871624476128549:50510901090322:2800:8DB0A494FF6F4C8A9377A0C20123B4776A8E00276A35A3086792BBF172F762B0.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821103451.73295479609594447871624476128549:50510901090322:2800:8DB0A494FF6F4C8A9377A0C20123B4776A8E00276A35A3086792BBF172F762B0.gif[/IMG]

In the above demo, the warm up exercise for user will get change according to the weather condition. Here the weather condition is divided into two parts:

1) Outdoor warm up exercise: When the weather condition is normal such as sunny or not raining, then the warm up exercise will get change to running.

2) Inside warm up exercise: When the weather condition is abnormal such as raining or snow fall, then the warm up exercise will get change to invisible jumping rope, burpee and squats.

By this way we are providing user a better experience without avoiding any exercise due to certain condition. It’s a win-win situation for developers to not to lose any user due to bad user experience.

Prerequisite

1) Must have a Huawei Developer Account.

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 installed.

Things Need To Be Done

1) Create a project in android studio.

2) Get the SHA Key. For getting the SHA key we can refer to this article.

3) Create an app in the Huawei AppGallery connect.

4) Enable awareness kit setting in Manage APIs section.

5) Provide the SHA Key in App Information Section.

6) Provide storage location.

7) After completing all the above points we need to download the agconnect-services.json from App Information Section. Copy and paste the Json file in the app folder of the android project.

8) Enter the below maven url inside the repositories of buildscript and allprojects (project build.gradle file):

maven { url ‘http://developer.huawei.com/repo/’ }

9) Enter the below plugin in the app build.gradle file:

apply plugin: ‘com.huawei.agconnect’

10) Enter the below HMS Push kit dependencies in the dependencies section:

implementation ‘com.huawei.hms:awareness:1.0.6.301’

11) Enter the below permission in android manifest file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

12) Now Sync the gradle.

Weather Awareness

It is used for providing weather information such as current day or next seven days weather information of a specific device location. It does not support barrier information or function.

public void sendWeatherId(){
Awareness.getCaptureClient(this).getWeatherByDevice()
.addOnSuccessListener(new OnSuccessListener<WeatherStatusResponse>() {
@Override
public void onSuccess(WeatherStatusResponse weatherStatusResponse) {
WeatherStatus weatherStatus = weatherStatusResponse.getWeatherStatus();
List<HourlyWeather> hourlyWeather = weatherStatus.getHourlyWeather();
Utility.setWeatherId(getApplicationContext(),hourlyWeather.get(1).getWeatherId());
if(Utility.getWeatherId(getApplicationContext())!=0){
goToValidPage();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e("TAG", "get Hourly weather failed");

}
});
}

The above code is used to get the weather information such as sunny, rain, cloudy etc. Using this information we will manipulate the warm up exercise and show user the list of exercise.

getWeatherByDevice

This method is used to obtain the weather information using the current location of a device. There are some restriction using this method:

1) The total number of capture API calls every 5 seconds cannot exceed 20

2) The total number of capture API calls every hour cannot exceed 1000

3) This function is available only on device with API level 24 or later

4) ACCESS_FINE_LOCATION permission is needed in android manifest file.

WeatherStatus

This is the main API where we will fetch the weather id. Using this id we will manipulate the exercise. This API contain five methods:

1) getAqi()

2) getWeatherSituation()

3) getDailyWeather()

4) getHourlyWeather()

5) getLiveInfo()

Here we will use getHourlyWeather to obtain weather information in the next 24 hours. The information will fetch weather ID, time, temperature and rainfall probability.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821112622.93860368777230364506414597516189:50510901090322:2800:097A2AE7FAAA58EBD15296BF05B91CCCA2EDCF9730DCDFA2FB7BCA69AF331EA7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821112622.93860368777230364506414597516189:50510901090322:2800:097A2AE7FAAA58EBD15296BF05B91CCCA2EDCF9730DCDFA2FB7BCA69AF331EA7.png[/IMG]

Opening YouTube Video in HMS devices

public static void watchYoutubeVideo(Context context, String id){
try {
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + id));
context.startActivity(webIntent);
} catch (ActivityNotFoundException ex) {

}
}

The Id here is YouTube Id.

CountDownTime

As the name implies to counts down the time specified in milliseconds.

mCountDownTimer = new CountDownTimer(TIMER_DURATION, TIMER_INTERVAL) {

@Override
public void onTick(long millisUntilFinished) {
txtTimer.setText("00:"+(millisUntilFinished / 1000 < 10 ? "0"+ millisUntilFinished / 1000 : String.valueOf(millisUntilFinished / 1000)));
mTimeRemaining = millisUntilFinished; // Saving timeRemaining in Activity for pause/resume of CountDownTimer.
}

@Override
public void onFinish() {
mCountDownTimer.cancel();
Intent intent = new Intent(ReadyActivity.this, CurrentWorkoutActivity.class);
startActivity(intent);
finish();
}
}.start();

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821104550.51394664608516416076118407411818:50510901090322:2800:C3CAE1D5A10DCE1E156972511BC8499A5797376DE2A48D085DA1AF6AC4AD7F02.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821104550.51394664608516416076118407411818:50510901090322:2800:C3CAE1D5A10DCE1E156972511BC8499A5797376DE2A48D085DA1AF6AC4AD7F02.jpg[/IMG]

Timer

In case user go for run we need to have a timer to capture the duration of its completion.

private void timer (){
Timer timer = new Timer();
countTimer = 0;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
String s_time = String.format("%02d:%02d:%02d",
countTimer / 3600,
(countTimer % 3600) / 60,
countTimer % 60);
System.out.println("TIMER >>>" + s_time);
txtTimer.setText(s_time);
if (!pauseTimer) countTimer++;

}
});
}
}, 1000, 1000);
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821104620.50514354286293289698566549513534:50510901090322:2800:A84015B93264BF4820B23E693BC6A83C5E1587707F386AFBA25CF5DFE2667288.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200821104620.50514354286293289698566549513534:50510901090322:2800:A84015B93264BF4820B23E693BC6A83C5E1587707F386AFBA25CF5DFE2667288.jpg[/IMG]

What we learn?

We learn how to manipulate data using HMS Weather Awareness Kit and also situation where we need the most.

GitHub

Very soon I am going to post the entire code on GitHub. So come back again to check it out.

For more reference

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

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/weather-awareness-0000001050121038-V5