Weather Demo Application With Awareness Kit

Erdal Kaymak
5 min readJul 6, 2021

Integrating Applications to HMS Core

To start developing an app with Huawei mobile services, you need to integrate your application to the HMS core. Check the link below to integrate your application, also don’t forget to enable the Awareness Kit from AppGallery Connect.

Demo Application Introduction

In this demo application, we get and show the hourly and weekly weather with details using the Awareness Kit. This demo application consists of two sections. In the hourly weather section; I show you information about an hour, rain probability, weather temperature in °C and °F. In the weekly weather section; I show you information about wind speed, wind direction, day of the week, moon state, max and min temperature of the day in °C.

Awareness Kit Introduction

Awareness Kit provides your app with the ability to obtain contextual information including users’ current time, location, behavior, audio device status, ambient light, weather, and nearby beacons. For this demo application, we use the weather feature of the Awareness Kit to getting weather information. Check this link if you want to learn more about this service.

Assigning Permissions in the Manifest File

Before calling the weather awareness capability, assign required permissions in the manifest file.

AndroidManifest.xml

Preparations for the Code

After adding permission to AndroidManifest.xml we need to add dependency of Awareness Kit to app/build gradle.

build.gradle

Before diving into the code, for this demo application, I created layouts. I am sharing it with you.

view_pager_weather_activity.xml

This is the first layout that contains ViewPager2 and TabLayout. TabLayout for change position of the fragment and ViewPager2 for the show changed fragments.

fragment_hourly_weather_detail.xml

This for showing hourly weather. That contains RecylerView to showing an hourly list.

item_hourly_weather.xml

This is a one-row style of the hourly weather detail. We connect them using a Fast Adapter.

fragment_weekly_weather_detail.xml

This for showing weekly weather. That contains RecylerView to showing a weekly list.

item_weekly_weather.xml

This is a one-row style of the weekly weather detail. We connect them using a Fast Adapter. Now we finish the layouts we can continue with Kotlin codes.

ViewPager Activity

ViewPagerWeatherActivity.kt

In this initialize method we create a fragment list with HourlyWeatherDetailFragment and WeeklyWeatherDetailFragment(lines 2 and 3). We create a ViewPagerWeatherAdapter instance and set the viewPager2 adapter (lines 9 and 15). After that, we create TabLayoutMediator to link a TabLayout with a ViewPager2 (lines 16 and 18 ). Finally, we call this method in the onCreate method after setContentView.

ViewPager Weather Adapter

ViewPagerWeatherAdapter.kt

This adapter class create with specific parameters and extended with FragmentStateAdapter we use this adapter on ViewPagerActivityClass.

Awareness Weather Info Data Class

AwarenessWeatherInfo.kt

In this data class, we hold the weather information with getting Awareness Kit. That way we don’t need to write values again and again only call this data class and all values are passes.

Awareness Kit Helper Class

AwarenessKitHelper.kt

In this method, we call the getCaptureClient(context).weather by device method for getting weather info and set it to AwarenessWeatherInfo data class. We should add permission explicitly (lines 6 and 8). We get the all information from WeatherStatusResponse class from Awareness Kit Weather Awareness Capture API and pass the data to the data class in the addOnSuccessListener method.

Hourly Weather Detail Adapter

HourlyWeatherDetailAdapter.kt

In this class, we create an adapter of the hourly weather detail using a Fast Adapter. Fast Adapter is an easy way of creating an adapter. For more info about Fast Adapter click this link. Also, we use view binding for getting XML objects with the layout(item_hourly_weather.xml).

Weekly Weather Detail Adapter

WeeklyWeatherDetailAdapter.kt

In this class, we create an adapter of the weekly weather detail using a Fast Adapter. Also, we use view binding for getting XML objects with the layout(item_weekly_weather.xml).

Hourly Weather Detail View Model

We use MVVM architecture with live data to updated UI when data change automatically.

HourlyWeatherDetailViewModel.kt

We have two live data for every variable. Mutable Live Data is used for posting or setting value while Live Data is not setting or posting value only for getting value. With the getAwarenessWeatherInfo method, we get the AwarenessWeatherInfo data class and post the value to Mutable Live Data of city and hourly weather (lines 17 and 18).

Hourly Weather Detail Fragment

HourlyWeatherDetailFragment.kt

In this fragment, we inject ViewModel with Dagger Hilt. Also, we create an instance of the AwarenessKitHelper class with Hourly Weather Detail Adapter. We use the Awareness Kit Helper instance for calling getAwarenessWeather Information method. In this method, we can get weather information and pass it to View Model with that way we posting our value to Mutable Live Data. After that in the observeModel method, we observe our Live Data and setting the value to UI elements (lines 36 and 41). In the setAdapter method, we setting our Fast Adapter. In the setList method, we set our adapter Items and add them to the adapter list. In lines 57 and 58 I get the weather Id and use the helper class to getting Id and give us the drawable item. I use a simple mapping for this problem. The service only gets us a value of the weather Id. In the documentation, all Ids correspond to weather. For more info, you can click this link.

Weekly Weather Detail View Model

WeeklyWeatherDetailViewModel.kt

We have two live data for every variable. With the getAwarenessWeatherInfo method, we get the AwarenessWeatherInfo data class and post the value to Mutable Live Data of city and daily weather (lines 17 and 18).

Weekly Weather Detail Fragment

WeeklyWeatherDetailFragment

In this fragment, we inject ViewModel with Dagger Hilt. Also, we create an instance of the AwarenessKitHelper class with a Weekly Weather Detail Adapter. It is the same as the Hourly Weather Detail Fragment except for the setList method in this setList method we get DailyWeather List and create a Weekly Weather Detail Adapter. Finally setting adapter items with new value and add to our Adapter List.

Conclusion

Now we learned how to implement and use the Awareness Kit Weather Awareness Feature. We call Capture API of the Weather Awareness to get users location weather and show with Hourly and Weekly in this demo application. If you want to learn more about the Awareness kit and its services, you can check this link.

Hourly Weather
Weekly Weather

I am looking forward to meeting you again until that time takes care of yourself!

References:

--

--