Common Mobile Services
It is a library that provides a common interface for mobile services for Android developers. Its aim is to remove special mobile service dependencies for your app code.
This has mainly two benefits:
1- It removes creation and lifecycle control code from your app code. So your classes get rid of one extra responsibility. (Separation of Concerns)
2- Makes it possible to use different mobile services. For example, not all Android devices have Google Mobile Services(GMS). By doing this library you can use
different services without modifying your app code.
This library contains 2 services for now: Google Mobile Services(GMS) and Huawei Mobile Services(HMS). This library will grow with the added services.
If you want to contribute don't hesitate to create PR's :)
Currently added services: MapKit, Location, Analytics, CreditCardScanner, Awareness, Scan, Translate, Speech To Text, Text To Speech, Object Detection, Text Recognition, Face Detection, Language Detection, Image Classification, Account, Auth, Safety, Crash, Push, Site, Ads, Identity and Remote Config.
How to install
Step 1. Add the dependency of Huawei AGC and Google Play Services to your build file
buildscript {
ext {
toolsBuildGradleVersion = '7.1.1'
}
dependencies {
classpath "com.android.tools.build:gradle:$toolsBuildGradleVersion"
classpath 'com.huawei.agconnect:agcp:1.9.1.301'
classpath "io.realm:realm-gradle-plugin:10.13.3-transformer-api"
classpath 'com.google.gms:google-services:4.3.15'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'
}
}
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
id 'com.google.gms.google-services' version '4.4.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Step 2. Get the agconnect-services.json file from the AGC Console and google-services.json file from Firebase Console. Then, place it under the app module. And, add plugins to app level gradle file header.
apply plugin: 'com.huawei.agconnect'
apply plugin: 'com.google.gms.google-services'
Step 3. Add the dependency to app level gradle file:
dependencies {
implementation(platform("com.google.firebase:firebase-bom:32.5.0"))
implementation 'com.huawei.agconnect:agconnect-core:1.9.1.300'
implementation 'com.huawei.hms:hmscoreinstaller:6.11.0.301'
}
Step 4. Add the JitPack and Huawei developer repository to settings.gradle:
pluginManagement {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
dependencyResolutionManagement {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
maven { url "https://jitpack.io" }
}
}
Step 5. Add the this configuration setting that disables the instrumentation of the application performance management system:
apmsInstrumentationEnabled=false
Step 6. Add the dependency for module(s):
com.github.Explore-In-HMS.common-mobile-services
Please write the latest version where the versionName is written below:

MapKit
implementation 'com.github.Explore-In-HMS.common-mobile-services:mapkit:<versionName>'
Location
implementation 'com.github.Explore-In-HMS.common-mobile-services:location:<versionName>'
Analytics
implementation 'com.github.Explore-In-HMS.common-mobile-services:analytics:<versionName>'
Credit Card Scanner
implementation 'com.github.Explore-In-HMS.common-mobile-services:creditcardscanner:<versionName>'
Awareness
implementation 'com.github.Explore-In-HMS.common-mobile-services:awareness:<versionName>'
Scan
implementation 'com.github.Explore-In-HMS.common-mobile-services:scan:<versionName>'
Translate
implementation 'com.github.Explore-In-HMS.common-mobile-services:translate:<versionName>'
Speech To Text
implementation 'com.github.Explore-In-HMS.common-mobile-services:speechtotext:<versionName>'
Text To Speech
implementation 'com.github.Explore-In-HMS.common-mobile-services:texttospeech:<versionName>'
Object Detection
implementation 'com.github.Explore-In-HMS.common-mobile-services:objectdetection:<versionName>'
Text Recognition
implementation 'com.github.Explore-In-HMS.common-mobile-services:textrecognition:<versionName>'
Ads
implementation 'com.github.Explore-In-HMS.common-mobile-services:ads:<versionName>'
Face Detection
implementation 'com.github.Explore-In-HMS.common-mobile-services:facedetection:<versionName>'
Language Detection
implementation 'com.github.Explore-In-HMS.common-mobile-services:languagedetection:<versionName>'
Image Classification
implementation 'com.github.Explore-In-HMS.common-mobile-services:imageclassification:<versionName>'
Account
implementation 'com.github.Explore-In-HMS.common-mobile-services:account:<versionName>'
implementation 'com.github.Explore-In-HMS.common-mobile-services::<versionName>'
Safety
implementation 'com.github.Explore-In-HMS.common-mobile-services:safety:<versionName>'
Crash
implementation 'com.github.Explore-In-HMS.common-mobile-services:crash:<versionName>'
Push
implementation 'com.github.Explore-In-HMS.common-mobile-services:push:<versionName>'
Site
implementation 'com.github.Explore-In-HMS.common-mobile-services:site:<versionName>'
Identity
implementation 'com.github.Explore-In-HMS.common-mobile-services:identity:<versionName>'
Remote Config
implementation 'com.github.Explore-In-HMS.common-mobile-services:remoteconfig:<versionName>'
Result Data
Libraries usually return data in a common data wrapper class: ResultData. It has three states: Success, Failed and Loading. This wrapper class helps us to manage the data status.
sealed class ResultData<out T>{
data class Loading(val nothing: Nothing? = null): ResultData<Nothing>()
data class Success<out T>(val data: T? = null): ResultData<T>()
data class Failed(val error: String? = null,val errorModel: ErrorModel?=null): ResultData<Nothing>()
}
MapKit
This library wraps a mapview to use it in application code. It has CommonMap interface which can be GoogleCommonMapImpl or HMSCommonMapImpl. A custom view created to hold these map views: CommonMapView. This view also manages lifecycle events of its map.
How to use
First add Google Map api key to Manifest.xml.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="xxx-xxx"/>
Add CommonMapView to your layout file
<com.hms.commonmobileservices.mapkit.CommonMapView
android:id="@+id/mapView"
app:hms_api_key="..."
android:layout_width="match_parent"
android:layout_height="match_parent" />
It's advised to put the api_key that taken from AppGallery console
Then in your Activity or Fragment
val commonMap=mapView.onCreate(savedInstanceState,lifecycle).apply {
getMapAsync {
it.addMarker("Marker", "Snippet", 41.0540255, 29.0129607)
it.animateCamera(41.0540255, 29.0129607, 10f)
}
}
CommonMapView.onCreate() function needs savedInstanceState and lifecycle. It uses savedInstanceState to keep the map state when Activity/Fragment is destroyed and recreated. Uses lifecycle to redirect lifecycle events to the map view via LifecycleObserver. So you don't need to override lifecycle callback like onDestroy() and call commonMap.onDestroy()
getMapAsync() function takes a callback that informs you when the map is ready. After this callback is called you can use commonMap functions.
Here is all functions of CommonMap
interface CommonMap : UISettings{
fun getMapView(): View
fun onCreate(bundle: Bundle?)
fun getMapAsync(onMapReadyListener: (map: CommonMap) -> Unit)
fun addMarker(title: String?=null,
snippet: String?=null,
latitude: Double,
longitude: Double,
iconBitmap: Bitmap?=null,
anchor : Pair<Float,Float>?=null
) : CommonMarker
fun addPolygon(commonPolygonOptions: CommonPolygonOptions) : CommonPolygon
fun setOnInfoWindowClickListener(markerClickCallback : (markerTitle: String?,
markerSnippet: String?) -> Unit)
fun setOnMapClickListener(onClick : (commonLatLng: CommonLatLng) -> Unit)
fun moveCamera(latitude: Double, longitude: Double, zoomRatio: Float)
fun animateCamera(latitude: Double, longitude: Double, zoomRatio: Float)
fun setMyLocationEnabled(myLocationEnabled: Boolean?,context: Context) : Boolean
fun clear()
fun onSaveInstanceState(bundle: Bundle)
fun onStart()
fun onResume()
fun onPause()
fun onStop()
fun onDestroy()
fun onLowMemory()
}
You can also use UISettings functions
interface UISettings {
fun isCompassEnabled(): Boolean
fun setCompassEnabled(compassEnabled: Boolean?)
fun isIndoorLevelPickerEnabled(): Boolean
fun setIndoorLevelPickerEnabled(indoorLevelPickerEnabled: Boolean?)
fun isMapToolbarEnabled(): Boolean
fun setMapToolbarEnabled(mapToolbarEnabled: Boolean?)
fun isMyLocationButtonEnabled(): Boolean
fun setMyLocationButtonEnabled(myLocationButtonEnabled: Boolean?)
fun isRotateGesturesEnabled(): Boolean
fun setRotateGesturesEnabled(rotateGesturesEnabled: Boolean?)
fun isScrollGesturesEnabled(): Boolean
fun setScrollGesturesEnabled(scrollGesturesEnabled: Boolean?)
fun isScrollGesturesEnabledDuringRotateOrZoom(): Boolean
fun setScrollGesturesEnabledDuringRotateOrZoom(scrollGesturesEnabledDuringRotateOrZoom: Boolean?)
fun isTiltGesturesEnabled(): Boolean
fun setTiltGesturesEnabled(tiltGesturesEnabled: Boolean?)
fun isZoomControlsEnabled(): Boolean
fun setZoomControlsEnabled(zoomControlsEnabled: Boolean?)
fun isZoomGesturesEnabled(): Boolean
fun setZoomGesturesEnabled(zoomGesturesEnabled: Boolean?)
fun setAllGesturesEnabled(allGestureEnable: Boolean?)
}
Location
This library provides a CommonLocationClient. It is a base class for GMS FusedLocationProviderClient and HMS FusedLocationProviderClient. This library handles enabling GPS and getting location permissions at runtime.
How to use
First, initialize CommonLocationClient:
val locationClient: CommonLocationClient? = LocationFactory.getLocationClient(this,lifecycle)
It needs Activity to handle runtime permissions, create fused client and check GPS setting is on or off. Needs lifecycle to manage the lifecycle of its element according to Activity/Fragment lifecycles. Finally needs to permission callbacks implemented by the application code. It covers denying permissions one time and permanently cases.
Then you must check if the GPS setting is on or off. If it is enabled you are ready to use CommonLocationClient.
locationClient.enableGps{enableGPSFinalResult, error ->
when(enableGPSFinalResult){
EnableGPSFinalResult.ENABLED -> showToast("GPS Enabled")
EnableGPSFinalResult.FAILED -> showToast("GPS Enabling Failed")
EnableGPSFinalResult.USER_CANCELLED -> showToast("GPS Enabling Cancelled")
}
}
If enableGPSFinalResult is EnableGPSFinalResult.ENABLED you can use clients functions.
Getting last known location: