[Huawei Developers] muraliameakula:
Introduction

In this article, we will learn how to implement Huawei Awareness kit features, so we can easily integrate these features in to our Flutter application. In this article we are going to take a look at the Awareness kit Capture API features such as Dark mode awareness and App status awareness.

​

What is Huawei Awareness kit Service?

Huawei Awareness kit supports to get the app insight into a users’ current situation more efficiently, making it possible to deliver a smarter, more considerate user experience and it provides the users’ current time, location, behavior, audio device status, ambient light, weather, and nearby beacons, application status, and mobile theme mode.

Restrictions

1. Dark mode: It supports EMUI 10.0 or later for Huawei devices and non-Huawei devices required Android 10.0 or later (API level 29 is required).

2. App status: It supports EMUI 5.0 or later for Huawei devices and non-Huawei devices currently it is not supporting

Requirements

1. Any operating system(i.e. MacOS, Linux and Windows)

2. Any IDE with Flutter SDK installed (i.e. IntelliJ, Android Studio and VsCode etc.)

3. Minimum API Level 29 is required.

4. Required EMUI 10.0 For Dark-mode and EMUI 5.0 for App status.

How to integrate HMS Dependencies.

1. First of all, we need to create an app on AppGallery Connect and add related details about HMS Core to our project. For more information check this link

2. Enable the Awareness Kit in the Manage API section and add the plugin.

3. Add the required dependencies to the build.gradle file under root folder.

maven {url 'http://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

4. Now we can implement Awareness Kit plugin. To implement Awareness Kit to our app, we need to download the plugin. Follow the URL for cross-platform plugins.

5. After completing all the above steps, you need to add the required kits’ Flutter plugins as dependencies to pubspec.yaml file. You can find all the plugins in pub.dev with the latest versions.

huawei_awareness:
path: ../huawei_awareness/

After adding them, run flutter pub get command. Now all the plugins are ready to use.

Note: Set multiDexEnabled to true in the android/app directory, so the app will not crash.

Use Awareness to get the dark mode status

With Dark-mode Status Awareness, we can detect the dark mode status of the device. We can get the status using capture API.

void loadAppTheme() async {
DarkModeResponse response = await AwarenessCaptureClient.getDarkModeStatus();
bool isDarkMode = response.isDarkModeOn;
setState(() {
if (isDarkMode) {
Provider.of<ThemeChanger>(context).setTheme(darkTheme);
} else {
Provider.of<ThemeChanger>(context).setTheme(lightTheme);
}
});
}

Use Awareness to get the Application status

With Application status Awareness, we can detect whether application is in which mode like silent, running using package name.

void checkAppStatus() async {
String packName = "******************";
ApplicationResponse response =
await AwarenessCaptureClient.getApplicationStatus(
packageName: packName);
int appState = response.applicationStatus;
setState(() {
switch (appState) {
case ApplicationStatus.Unknown:
_showDialog(context, "Demo Application Not found");
print("log1" + "Application Not found");
break;
case ApplicationStatus.Silent:
_showDialog(context, "Demo Application Currently in silent mode");
print("log1" + "Application silent");
break;
case ApplicationStatus.Running:
_showDialog(context, "Demo Application Currently in Running mode");
print("log1" + "Application Running");
break;
}
});

}

Final code here

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ThemeChanger>(
create: (context) => ThemeChanger(ThemeData.light()),
child: new App(),
);
}
}

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appState = Provider.of<ThemeChanger>(context);
return MaterialApp(
title: "WellFit",
theme: appState.getTheme(),
home: Scaffold(
body: Tabs(),
),
);
}
}

ThemeNotifier class

class ThemeChanger with ChangeNotifier {
ThemeData _themeData;

ThemeChanger(this._themeData);

getTheme() => _themeData;

setTheme(ThemeData themeData) {
_themeData = themeData;
notifyListeners();
}
}

Demo

​

Tips and Tricks

1. Download latest HMS Flutter plugin.

2. Set minSDK version to 29 or later.

3. Do not forget to click pug get after adding dependencies.

4. Do not forget to set data processing location.

5. Refer this URL for supported Devices list.

Conclusion

In this article, I have covered two services Dark-mode awareness and App status Awareness.

Using Dark-mode awareness we can easily identify which theme currently we activated in settings page.

Using App Status awareness we can monitor the application in which state like silent or running these two we covered in this article.

Thanks for reading! If you enjoyed this story, please click the Like button and Follow. Feel free to leave a Comment 💬 below.

Reference

Awareness Kit URL

Original Source