[Huawei Developers] Freemind R:
These days mobile devices are part of our life. We do many operations from our mobile phones such as making payment, logging in to social media accounts, checking our bank accounts.

These are the operations which need high security level. If our device will have malicious apps or something like that, our accounts will have trouble and we may suffer many financial and moral damages.

In this article, I will talk about how to improve app security by using HMS Safety Detect Kit.

To do that, I have developed a simple secure web browser app. Because in web browsers, we can use bank websites, we can login to our social media, we can make some payment and use our credit/bank card information. We wouldn’t like to our information to be stolen.

def koinVersion = "2.2.0-rc-4"

dependencies {
....
// Koin for Android
implementation "org.koin:koin-android:$koinVersion"
// Koin Android Scope feature
implementation "org.koin:koin-android-scope:$koinVersion"
// Koin Android ViewModel feature
implementation "org.koin:koin-android-viewmodel:$koinVersion"
}

After we have implemented the Koin dependencies, we need to create our modules which we will add in our application class.

We will get necessary objects with the help of these modules. I prefer to define different module files for different works.

val applicationModule = module {
single(named("appContext")){ androidApplication().applicationContext }

factory { HmsHelper() }
factory { SystemHelper() }
}

val dataModule = module {
factory<ErrorItem>(named("HmsNotAvailable")) { ErrorItem(
icon = ContextCompat.getDrawable(get(named("appContext")), R.drawable.huawei)!!,
title = androidContext().getString(R.string.hms_not_available),
message = androidContext().getString(R.string.download_hms_core)) }

factory<ErrorItem>(named("DeviceNotSecure")) { ErrorItem(
icon = ContextCompat.getDrawable(get(named("appContext")), R.drawable.ic_device_not_secure)!!,
title = androidContext().getString(R.string.device_not_secure),
message = androidContext().getString(R.string.device_not_secure_message)) }

factory<ErrorItem>(named("MaliciousApps")) { ErrorItem(
icon = ContextCompat.getDrawable(get(named("appContext")), R.drawable.ic_malicious_apps)!!,
title = androidContext().getString(R.string.device_not_secure),
message = androidContext().getString(R.string.malicious_apps_message)) }
}

App Preparations

I use Koin framework for dependency injection in my application.

To use Koin Framework in our application, we should add 3 dependencies to our app. In the above, you can find dependencies which you need to add in app-level build.gradle file.

val viewModelModule = module {
viewModel { SplashViewModel() }
}

After we have defined our modules, we need to setup Koin in our application class.

While starting Koin, we should add our modules which we have defined above, and if we want to use app context, we should androidContext value.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.berkberber.hms_securewebbrowser">
....
<application
android:name=".SecureWebBrowserApp"
....
>
....
</application>

</manifest>

class SecureWebBrowserApp: Application(){
override fun onCreate() {
super.onCreate()

setup()
}

private fun setupKoin(){
startKoin {
androidContext(this@SecureWebBrowserApp)
modules(
applicationModule,
viewModelModule,
dataModule
)
}
}

private fun setup(){
setupKoin()
}
}

To get more information about app and to see how I used other things such as Navigation Component, MVVM, and etc. you can visit my GitHub repository.

HMS Safety Detect

Safety Detect Kit helps us to improve the security level of our apps. There are 5 different APIs we can use with HMS Safety Detect Kit.

SysIntegrity API: Helps us to check device security. We can determine that device has been rooted or has not.

AppsCheck API: Helps us to determine and list malicious apps which have installed to device.

URLCheck API: Helps us check whether websites are safe.

UserDetect API: Helps us to determine that user is fake or is not.

WifiDetect API: Helps us to check whether Wi-Fi which the device has connected is secure.

Note: UserDetect API is available outside of Chinese mainland. WifiDetect API is available only in the Chinese mainland.

In this article, I have been focused on app security. So, I used SysIntegrity API and AppsCheck API and I will give you informations about these APIs.

Checking is HMS available on device (optional)

We will use Safety Detect Kit in our application. Safety Detect Kit requires HMS Core to be installed on the device.

We don’t have to make this control, but if device doesn’t have HMS, we can’t use HMS Safety Detect Kit. That’s why I recommend you to check HMS Core availability on device and if device doesn’t have HMS, it is better to show an error screen to user.

To check HMS availability we need to add base HMS dependency to our app-level build.gradle file.

To check that device has HMS support or has not, we can write very basic function called as isHmsAvailable().

More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0204429014247900019