morristech / common-mobile-services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Common Mobile Services

It is a library that provides a common interface for mobile services for Android developers. Its aim is removing 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 has 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, Account, Auth, Safety, Site, Crash, Push, Scene and Identity.

How to install

Step 1. Add the JitPack repository, Huawei repo and classpaths to your build file

buildscript {
    repositories {
    	...
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
    	...
        classpath 'com.huawei.agconnect:agcp:1.6.2.300'
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
	...
	maven { url 'https://jitpack.io' }
	maven { url "https://developer.huawei.com/repo/" }
    }
}

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 for module(s):

com.github.Explore-In-HMS.common-mobile-services

latest version 2.2.0

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>'

Account

implementation 'com.github.Explore-In-HMS.common-mobile-services:account:<versionName>'

Auth

implementation 'com.github.Explore-In-HMS.common-mobile-services:auth:<versionName>'

Scene

implementation 'com.github.Explore-In-HMS.common-mobile-services:scene:<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>'

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:

locationClient?.getLastKnownLocation{
    when(it.state){
        LocationResultState.SUCCESS->{
            Toast.makeText(
                context,
                "Current Location Latitude: ${it.location?.latitude} ,Current Location Longitude: ${it.location?.longitude}",
                Toast.LENGTH_LONG
            ).show()
        }
        LocationResultState.FAIL->{
            Toast.makeText(
                context,
                "Failed to get current location",
                Toast.LENGTH_LONG
            ).show()
        }
    }
 }

Getting location continuously:

locationClient?.requestLocationUpdates(priority = Priority.PRIORITY_HIGH_ACCURACY,interval = 1000){
   var message:String?=null
   when(it.state){
       LocationResultState.SUCCESS->{
           message="Current Latitude: ${it.location?.latitude} \nCurrent Longitude: ${it.location?.longitude}"
       }
       LocationResultState.FAIL->{
           message="Failed to get current location"
       }
       LocationResultState.GPS_DISABLED->{
           message="User disabled gps"
       }
       LocationResultState.LOCATION_UNAVAILABLE->{
           message="Location unavailable due to the environment"
       }
   }
   Log.d(TAG,message)
   locationUpdatesLiveData.value=message
}

When you don't need to location updates you can remove listener like this:

locationClient?.removeLocationUpdates()
Using the Mock location feature

To use the mock location function, go to Settings > System & updates > Developer options > Select mock location app and select the desired app. (If Developer options is unavailable, go to Settings > About phone and tap Build number for seven consecutive times. Then, Developer options will be displayed on System & updates.) You need to add necessary permission to use Mock location feature to AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
fun setMockMode(isMockMode : Boolean) : Work<Unit>
commonLocationClient?.setMockMode(true).addOnSuccessListener {  }.addOnFailureListener {  }
fun setMockLocation(location: Location): Work<Unit>
val mockLocation = Location(LocationManager.GPS_PROVIDER)
     mockLocation.latitude = latitude
     mockLocation.longitude = longitude
     commonLocationClient?.setMockLocation(mockLocation).addOnSuccessListener {  }.addOnFailureListener {  }

Geofence

The usage of the Geofence feature is as follows. To use the geofence service APIs of Location Kit, declare the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the AndroidManifest.xml file.

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

First you have to initialize the CommonGeofenceService object.

   private var geofenceService: CommonGeofenceService? = null
   private var geofenceList: ArrayList<Geofence>? = null
   private var pendingIntent: PendingIntent?=null

   geofenceService = CommonGeofenceService()
   pendingIntent = getPendingIntent()
   geofenceList = ArrayList()

Creating and Adding a Geofence

geofenceList!!.add(Geofence()
  .also { it.uniqueId= "testGeofence" }.also { it.conversions = Geofence.ENTER_GEOFENCE_CONVERSION }
  .also { it.validDuration = Geofence.GEOFENCE_NEVER_EXPIRE }.also { it.latitude = latitude }.also { it.longitude = longitude }
  .also { it.radius = 20F })

Create a request for adding a geofence

fun createGeofenceRequest(): GeofenceRequestRes{
      return GeofenceRequestRes().also { it.geofenceList = geofenceList }.also { it.initConversion = GeofenceRequestRes.INITIAL_TRIGGER_ENTER }
  }

You should initialize the pendingIntent object that we have created.

  private fun getPendingIntent(): PendingIntent? {
      val intent = Intent(this, GeofenceBroadcastReceiver::class.java)
      intent.action = "com.hms.commonmobileservices.GEOFENCE"
      return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
  }

After all this process, we need to send the request to add a geofence via createGeofenceList() method.

fun createGeofenceList(context: Context, geofenceReq: GeofenceRequestRes,pendingIntent: PendingIntent): Work<Unit>
geofenceService.createGeofenceList(applicationContext,createGeofenceRequest(),pendingIntent)
  .addOnSuccessListener {

  }.addOnFailureListener {

  }

After adding Geofence, we need to create a Broadcast receiver and define it in AndroidManifest file so that Geofence can be triggered.

 <receiver android:name=".GeofenceBroadcastReceiver"
    android:exported="true">
      <intent-filter>
    <action android:name="com.hms.commonmobileservices.GEOFENCE" />
      </intent-filter>
 </receiver>

We will be able to listen to the Geofence triggering process thanks to the Brodcast receiver.Thanks to our CommonGeofenceData class, you can get all information about the triggered event.

class GeofenceBroadcastReceiver : BroadcastReceiver() {
   override fun onReceive(context: Context?, intent: Intent) {
       val geofenceData = CommonGeofenceData().fetchDataFromIntent(context,intent)
       val errorCode = geofenceData.errorCode
       val conversion = geofenceData.conversion
       val convertingGeofenceList = geofenceData.convertingGeofenceList
       val convertingLocation = geofenceData.convertingLocation
       val isFailure = geofenceData.isFailure
   }
}

Remove a geofence

With the deleteGeofenceList() method, you can delete the previously created geofence list according to the geofence id list or according to the pending intent value.

fun deleteGeofenceList(context: Context,geofenceList:List<String>):Work<Unit>

fun deleteGeofenceList(context: Context,pendingIntent: PendingIntent):Work<Unit>
val geofenceIdList : ArrayList<String> = ArrayList()
geofenceIdList.add("testGeofence")
geofenceService.deleteGeofenceList(applicationContext,geofenceIdList)
   .addOnSuccessListener { }
   .addOnFailureListener { }
geofenceService.deleteGeofenceList(applicationContext,pendingIntent)
   .addOnSuccessListener { }
   .addOnFailureListener { }

Activity Recognition

Thanks to the Activity Recognition feature, you can follow the user's activity instantly. To use the activity recognition service in versions earlier than Android 10, add the following permission in the AndroidManifest.xml file.

 <uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION"/>
 <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

To use the activity recognition service in Android 10 and later versions, add the following permission in the AndroidManifest.xml file;

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

After adding the required permissions, you must create and initialize the CommonActivityIdentificationService object.

private var activityIdentificationService : CommonActivityIdentificationService?=null
private var pendingIntent: PendingIntent? = null

activityIdentificationService = CommonActivityIdentificationService()
pendingIntentEdit = getPendingIntent()

private fun getPendingIntent(): PendingIntent?{
      val intent = Intent(this, ActivityRecognitionReceiver::class.java)
      intent.action = "com.hms.commonmobileservices.ACTIVITY_RECOGNITION"
      return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
  }

With the createActivityIdentificationUpdates() method, the user's activities are controlled according to the specified time.

fun createActivityIdentificationUpdates(context: Context,intervalMillis:Long, pendingIntent: PendingIntent):Work<Unit>
activityIdentificationService.createActivityIdentificationUpdates(applicationContext,5000,pendingIntent)
   .addOnSuccessListener { }
   .addOnFailureListener { }

You can follow to the user activities with the Broadcast Receiver we will create.

  <receiver android:name=".ActivityRecognitionReceiver"
     android:exported="true">
        <intent-filter>
      <action android:name="com.hms.commonmobileservices.ACTIVITY_RECOGNITION" />
        </intent-filter>
  </receiver>

With fetchDataFromIntent() method you can get detected activities status list.

fun fetchDataFromIntent(context:Context, intent:Intent): CommonActivityIdentificationResponse
class ActivityRecognitionReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        val activityIdentificationResponse = CommonActivityIdentificationResponse().fetchDataFromIntent(context,intent)
        val detectedActivityList = activityIdentificationResponse.activityIdentificationDataList
    }
 }

To Stop requesting activity identification updates, you can use deleteActivityIdentificationUpdates() method.

fun deleteActivityIdentificationUpdates(context: Context,pendingIntent: PendingIntent):Work<Unit>
activityIdentificationService.deleteActivityIdentificationUpdates(applicationContext,pendingIntent)
        .addOnSuccessListener { }
        .addOnFailureListener { }

Activity Transition

Activity transition is a process of detecting user activity converting from one to another. You can call the createActivityConversionUpdates() method in your app to request user activity conversion updates.

fun createActivityConversionUpdates(context: Context,activityConversionReq: CommonActivityConversionReq,pendingIntent: PendingIntent):Work<Unit>
val activityConversionEnter = CommonActivityConversionInfo()
        .also { it.activityType = CommonActivityIdentificationData().activityType(applicationContext,CommonActivityIdentificationData.STILL)}
        .also { it.conversionType = CommonActivityConversionInfo.ENTER_ACTIVITY_CONVERSION }
val activityConversionExit = CommonActivityConversionInfo()
        .also { it.activityType = CommonActivityIdentificationData().activityType(applicationContext,CommonActivityIdentificationData.STILL) }
        .also { it.conversionType = CommonActivityConversionInfo.EXIT_ACTIVITY_CONVERSION }

val activityConversionList: MutableList<CommonActivityConversionInfo> = ArrayList()
      activityConversionList.add(activityConversionEnter)
      activityConversionList.add(activityConversionExit)

val activityConRequest = CommonActivityConversionReq()
      activityConRequest.activityConversions = activityConversionList

activityIdentificationService.createActivityConversionUpdates(applicationContext,activityConRequest,pendingIntent)
      .addOnSuccessListener { }
      .addOnFailureListener { }

You can receive the broadcast activity transition result with broadcast receiver. With Common fetchDataFromIntent() method you can get activity transition status.

fun fetchDataFromIntent(context: Context, intent: Intent): CommonActivityConversionResponse
class ActivityRecognitionReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        val activityTransitionResponse = CommonActivityConversionResponse().fetchDataFromIntent(context,intent)
        val activityTransitionList = activityTransitionResponse.getActivityConversionDataList
    }
 }

Stop activity transition update request

If you want to stop getting activity transition information, you can use deleteActivityConversionUpdates() method.

fun deleteActivityConversionUpdates(context: Context,pendingIntent: PendingIntent):Work<Unit>
activityIdentificationService.deleteActivityConversionUpdates(applicationContext,pendingIntent)
       .addOnSuccessListener { }
       .addOnFailureListener { }

Analytics

It is made to ease logging for your project. You can log your event with a one line of code.

How to use

First, get the instance of common logger by calling CommonAnalytics.instance(this). The function takes a context instance as a parameter. Then returns an implementation of CommonAnalytics. It can be the class that uses Firebase or HiAnalyticsTools for logging. If you want to implement your own logger you can just implement the CommonAnalytics interface and use it instead.

You can log your events with classic Bundle.

val myEvent = Bundle().apply {
    putString("productName", "socks")
    putInt("quantity", 5)
}
CommonAnalytics.instance(this)?.saveEvent("cartEvent", myEvent)

You can also delete previously collected data with the "clearCachedData()" method. Data configured through the following APIs will be cleared:

onEvent setUserId setUserProfile addDefaultEventParams

CommonAnalytics.instance(this)?.clearCachedData()

You can set whether to enable event tracking. If event tracking is disabled, no data is recorded or analyzed. The default value is true

CommonAnalytics.instance(this)?.setAnalyticsEnabled(false)

User id can be assigned. When this method is called, a new session will be generated if the old value of id is not empty and is different from the new value. If you do not want to use id to identify a user (for example, when a user signs out), you must set id to null.

CommonAnalytics.instance(this)?.setUserId("id")

You can set user attributes. The values of user attributes remain unchanged throughout the app lifecycle and during each session. A maximum of 25 user attributes are supported. If the name of an attribute set later is the same as that of an existing attribute, the value of the existing attribute is updated.

CommonAnalytics.instance(this)?.setUserProfile("name", "value")

This method sets the session timeout interval. A new session will be generated when an app is running in the foreground but the interval between two adjacent events exceeds the specified timeout interval. The minimum value is 5 seconds, and the maximum value is 5 hours. If the specified value is beyond the value range, the boundary value is used. By default, the timeout interval is 1,800,000 milliseconds (that is, 30 minutes).

val milliseconds = 10000
CommonAnalytics.instance(this)?.setSessionDuration(milliseconds)

You can also add default event parameters. These parameters will be added to all events except the automatically collected events. If the name of a default event parameter is the same as that of an event parameter, the event parameter will be used.

val params = Bundle().apply {
    putString("productName", "keyboard")
    putInt("quantity", 3)
}
CommonAnalytics.instance(this)?.addDefaultEventParams(params)

If you want to obtain AAID then you can use getAAID() method. This method returns to you AAID as a string.

CommonAnalytics.instance(this)?.getAAID()

Credit Card Scanner

This library reads a credit card with device camera. It uses Huawei ML-Card-Bcr library to scan image. But it is inherited from CreditCardScanner common interface, so you can use your own implementation of credit card reader. HMS library does not require a Huawei device or HMS Core, so this library works well in all devices.

How to use

Get CreditCardScanner instance and call scan() function like this:

CreditCardScanner.instance(this)?.scan {
    when(it){
        is ResultData.Success ->{
            // handle success data
        }
        is ResultData.Failed->{
            // show error to user
        }
        is ResultData.Loading->{
            // show loading indicator
        }
    }

The scan() function takes a callback lambda function as a parameter. The lambda function gives us a ResultData sealed class object.

fun scan(callback: (ResultData<CommonCreditCardResult>) -> Unit)

This library has a ScanError class which is inherited from ErrorModel of ResultData. ScanError as error states to manage scan error situations.

data class ScanError(
    val errorString: String,
    val scanErrorType: ScanErrorType = ScanErrorType.ERROR
) : ErrorModel(errorString) {

    enum class ScanErrorType {
        USER_CANCELED, ERROR, DENIED
    }
}

Awareness

Awareness SDK provides your app with the ability to obtain contextual information including users' behavior, audio device status, weather, and current time. Depending on the situation, this information can be obtained in certain instantly.

How to use

To use Google Awareness Api Key, you need to get Google Awareness Api Key. With this Api Key, add the code block to your Manifest.xml file as follows.

<meta-data
    android:name="com.google.android.awareness.API_KEY"
    android:value="GOOGLE_AWARENESS_API_KEY"/>

By using the code block below, you instantly get the data from the class you want to use the Awareness SDK for.

HuaweiGoogleAwarenessManager(this).getWeather {dataArray ->
            dataArray.handleSuccess {
                it.data?.forEach {value ->
                    Log.d("AWARENESS_WEATHER",WeatherDataValue.valueOf(value)!!.name)
                }
            }
        }

The getWeather() or getBehavior() or getHeadset() or getTime() functions take a callback lambda function as a parameter. The lambda function gives us a ResultData sealed class object.

fun getWeather(callback: (weatherVal: ResultData<IntArray>) -> Unit)

Scan

Scan SDK scans and parses all major 1D and 2D barcodes, helping you quickly barcode scanning functions into your apps.

How to use

The scan process is started with the following line of code. scanBarcodeResultCode is the variable that is the result of OnActivityResult.

HuaweiGoogleScanManager(this).performScan(this,scanBarcodeResultCode)

After scanning is done, The result of the scanning process gets from onActivityResult. Using the parseScanToTextData function, the data is converted to string format.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null) {
            if (requestCode == scanBarcodeResultCode) {
                HuaweiGoogleScanManager(this).parseScanToTextData({resultData->
                    Toast.makeText(this,"The result is -> ${resultData.handleSuccess { 
                        it.data
                    }}",Toast.LENGTH_SHORT).show()
                },this,data)
            }
        }
    }

The parseScanToTextData() function takes a callback lambda function as a parameter. The lambda function gives us a ResultData sealed class object.

fun parseScanToTextData(callback: (scanToTextResult: ResultData<String>) -> Unit, activity: Activity, data:Intent)

Translate

It provides a very simple use of the translate feature. Currently 39 different languages ​​are supported. Click here to view instantly supported languages.

How to use

Parameters that should be used to use the translate feature; context, text to be translated, huawei api key and language code to be translated to whatever language is used.

HuaweiGoogleTranslateManager(this).performTranslate({
            it.handleSuccess {
                Log.d("Translate result: ", it.data.toString())
            }
        },"How are you today?","fr",this,huaweiAPIKey)

The performTranslate() function takes a callback lambda function as a parameter. The lambda function gives us a ResultData sealed class object.

fun performTranslate(callback: (translateValue: ResultData<String>) -> Unit, translatingText:String, targetLanguageCode:String, activity: Activity, apiKey:String)

Speech To Text

Speech to Text can recognize speech not longer than 60s and convert the input speech into text in real time. This service uses industry-leading deep learning technologies to achieve a recognition accuracy of over 95%. Currently, Mandarin Chinese (including Chinese-English bilingual speech), English, French, German, Spanish, and Italian can be recognized. Click here to view instantly supported speech to text languages and language codes.

How to use

The speech to text process is started with the following line of code. speechToTextResultCode is the variable that is the result of OnActivityResult. The language to be spoken in must be chosen.

HuaweiGoogleSpeechToTextManager(this).performSpeechToText(this,speechToTextResultCode,"en-US","API_KEY")

After speaking is done, The result of the speech to text process gets from onActivityResult. Using the parseSpeechToTextData function, the data is converted to string format.

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == speechToTextResultCode) {
            if (data != null) {
                HuaweiGoogleSpeechToTextManager(this).parseSpeechToTextData(
                    {
                        it.handleSuccess {
                            Log.d("Speech to text result: ",it.data.toString())
                        }
                   }, this, data, resultCode
                )
            }
        }
    }

The parseSpeechToTextData() function takes a callback lambda function as a parameter. The lambda function gives us a ResultData sealed class object.

fun parseSpeechToTextData(callback: (speechToTextResult: ResultData<String>) -> Unit, activity:Activity, data:Intent, resultCode:Int)

Scene

This library wraps Scene Kit views to use it in your application easily. It has IArView, IAugmentedFaceView and ISceneView interfaces which can be GoogleArView or HuaweiArView etc. It is related to your used service. Custom views created to hold these views: CommonSceneView, CommonAugmentedFaceView, CommonArView. These views also manages lifecycle events of its child views.


Note: In this version we implemented only Huawei views. But you can implement your own view according to your service.


How to use

First you need to add ".gltf" type model under the assets folder. Then, you need to add these common views to your layout file according to your usecase. After that you need to pass the params to the init method of this common view. For example, for CommonAugmentedFaceView, you need "FaceViewParams" object to pass your init() method of your view. And you can hide or show the render object by using the load(), clear() functions of common view.

buttonLoad.setOnClickListener {
    (renderView as CommonView).load()
}

Also you can use all views in one layout file according to your usecase.

when (viewType) {
    ViewType.AR_VIEW -> {
        tempRenderView = CommonArView(requireContext())
        tempRenderView.init(commonData = renderParams.params)
        makeToastMessage("Move the camera around yourself until the plane dots appear")
    }
    ViewType.FACE_VIEW -> {
        tempRenderView = CommonAugmentedFaceView(requireContext())
        tempRenderView.init(commonData = renderParams.params)
    }
    ViewType.SCENE_VIEW -> {
        tempRenderView = CommonSceneView(requireContext())
        tempRenderView.init(commonData = renderParams.params)
    }
}

If you want to listen OpenGL functions of these views, you can set the callback for these views.

commonArView.onArViewCallback(object : ArViewCallback, () -> Unit {
    override fun onSurfaceChanged(gL10: GL10?, width: Int, height: Int) {}

    override fun onDrawFrame(gL10: GL10?) {}

        .
        .
        .
})

Safety

This library includes both Huawei and Google services, allowing you to make your application more secure. This library, it performs necessary checks about whether users are fake users and device security. Click here for view service introduction and more description about Safety Detect Kit features.

How To Use

You need to initialize SafetyService interface as follows;

User Detection

To check if the user is a fake user, you need to call the userDetect method; Appkey value is app id value in Huawei services. In Google services the app key value is SITE_API_KEY. You can create SITE_API_KEY value from reCAPTCHA Admin Console. Click here

 private var safetyService : SafetyService ?= null
 override fun onCreate(savedInstanceState: Bundle?) {
    safetyService  = SafetyService.Factory.create(applicationContext)
    safetyService?.userDetect(appKey, object: ResultCallback<SafetyServiceResponse>{
            override fun onSuccess(result: SafetyServiceResponse?) {
                  if(result!= null){
                     Log.d("CMS", result.responseToken)
                  }
                }
                override fun onFailure(error: Exception) {
                     Log.e("CMS", error.toString())
                }
                override fun onCancelled() {
                    TODO("Not yet implemented")
                }})

Root Detection

In this library, you need to call the rootDetection method to check whether the device is safe or not. Appkey value is app id value in Huawei services. In Google services the app key value is API_KEY. You can create API_KEY value from Google APIs Console. Click here

 override fun onCreate(savedInstanceState: Bundle?){
    safetyService  = SafetyService.Factory.create(applicationContext)
    safetyService?.rootDetection(appKey, object: ResultCallback<RootDetectionResponse> {
                override fun onSuccess(result: RootDetectionResponse?) {
                     if(result!=null){
                        if(result.basicIntegrity){
                           Log.d("CMS", result.toString())
                     }
                    }else{
                        Log.d( "CMS","You need to install Google or Huawei Mobile Services to run application.")
                    }
                }
                override fun onFailure(error: Exception) {
                    Log.e("CMS", error.toString())
                }
                override fun onCancelled() {
                    TODO("Not yet implemented")
                }})

AppChecks

With the AppChecks feature, you can easily and quickly detect harmful applications on your device. You can check whether the AppChecks feature is active on your device with the isAppChecksEnabled() method.

fun isAppChecksEnabled(callback: ResultCallback<CommonVerifyAppChecksEnabledRes>)
safetyService.isAppChecksEnabled(object: ResultCallback<CommonVerifyAppChecksEnabledRes>{
            override fun onSuccess(appsCheckResp: CommonVerifyAppChecksEnabledRes?) {
                if(appsCheckResp!=null){
                    val result = appsCheckResp.result
                    if(result){
                        Log.d("CMS", "App Checks is enabled")
                    }else{
                        Log.d("CMS", "App Checks is disabled")
                    }
                }
            }
            override fun onFailure(e: Exception) {
                Log.e("CMS", "App Checks Fail: ${e.message}")
            }
        })

If the App Checks feature is disabled, you can enable it with the enableAppsCheck() method.

fun enableAppsCheck(callback: ResultCallback<CommonVerifyAppChecksEnabledRes>)
 safetyService.enableAppsCheck(object: ResultCallback<CommonVerifyAppChecksEnabledRes>{
      override fun onSuccess(appsCheckResp: CommonVerifyAppChecksEnabledRes?) {
          if(appsCheckResp!=null){
              val result = appsCheckResp.result
              if(result){
                  Log.d("CMS", "App Checks enabled")
              }else{
                  Log.d("CMS", "App Checks not enabled")
              }
          }
      }
      override fun onFailure(e: Exception) {
          Log.e("CMS", "App Checks Fail: ${e.message}")
      }
  })

You can detect malicious applications on your device with the getMaliciousAppsList() method.

fun getMaliciousAppsList(callback: ResultCallback<CommonMaliciousAppResponse>)
safetyService.getMaliciousAppsList(object: ResultCallback<CommonMaliciousAppResponse>{
    override fun onSuccess(maliciousAppResponse: CommonMaliciousAppResponse?){
	 if (maliciousAppResponse != null) {
	    val appList = maliciousAppResponse.getMaliciousAppsList
	    if (appList?.isNotEmpty() == true){
		Log.e("CMS", "Potentially harmful apps are installed!")
		for (harmfulApp in appList){
		    Log.e("CMS", "Information about a harmful app:")
		    Log.e("CMS", "  APK: ${harmfulApp.apkPackageName}")
		    Log.e("CMS", "  SHA-256: ${harmfulApp.apkSha256}")
		    Log.e("CMS", "  Category: ${harmfulApp.apkCategory}")
		 }
	    }else{
		  Log.d("CMS", "There are no known harmful apps installed.")
	    }
	  }
    }
    override fun onFailure(e: Exception){
	      Log.e("CMS", "Error code: ${e.localizedMessage} -- Message: ${e.message}")
    }
})

URLCheck

You can check whether URL addresses are safe with the URLCheck feature.

To activate the URLCheck feature, you must first call the initURLCheck() method.

fun initURLCheck():Work<Unit>
safetyService.initURLCheck().addOnSuccessListener{
    Log.d("CMS", "Url checks activated")
}.addOnFailureListener{
    Log.e("CMS", "Url check fail: ${it.message}")
}

You can check whether the URL you will specify is safe with the urlCheck() method.

fun urlCheck(url:String,appKey: String,threatType:Int,callback:ResultCallback<CommonUrlCheckRes>)

Appkey value is app id value in Huawei services. In Google services the app key value is API_KEY. You can create API_KEY value from Google APIs Console. Click here You need to activate the SafeBrowsing API feature. You can create new API_KEY from the Credentials tab.

val url = "https://github.com/Explore-In-HMS/common-mobile-services"
safetyService.urlCheck(url,appKey,CommonUrlCheckThreat().urlThreatType(this,CommonUrlCheckThreat.MALWARE_APPLICATIONS),object:ResultCallback<CommonUrlCheckRes>{
	override fun onSuccess(appsCheckResp: CommonUrlCheckRes?) {
		if(appsCheckResp!=null){
		    val result = appsCheckResp.urlCheckThreats
		    if(result!!.isNotEmpty()){
			for(urlLists in result){
			    Log.d("CMS", "URL Check result: ${urlLists.urlCheckResult}")
			}
		    }else{
			Log.d("CMS", "No threads found")
		    }
		}
	    }
	override fun onFailure(e: Exception) {
	    Log.e("CMS", "URLCheck fail : ${e.message}")
	}
})

You can use the shutDownUrlCheck() method to disable the URLCheck feature.

fun shutDownUrlCheck(): Work<Unit>
safetyService.shutDownUrlCheck().addOnSuccessListener{
    Log.d("CMS", "Url check is disabled")
}.addOnFailureListener {
    Log.e("CMS", "URLCheck fail: ${it.message}")
}

Crash

This library includes both Huawei and Google services, allowing you to make your application more secure. This library, it performs Crash Service. Click here for view service introduction and more description about Crash Kit features.

How To Use

First of all, you have to add root gradle 'classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'' & app gradle 'apply plugin: 'com.google.firebase.crashlytics'';

val crashService = CrashService.Factory.create(context)

It needs Context to check mobile services availability and provide proper mobile service type.

Functions

    //You can reach just write with "crashService." all of functions:

    testIt(context: Context)

    log(var1: String)

    log(var1: Int, var2: String)

    setCustomKey(var1: String, var2: String)

    setCustomKey(var1: String, var2: Boolean)

    setCustomKey(var1: String, var2: Double)

    setCustomKey(var1: String, var2: Float)

    setCustomKey(var1: String, var2: Int)

    setCustomKey(var1: String, var2: Long)

    setCustomKeys(var1: CustomKeysAndValues)

    setUserId(var1: String)

    recordException(var1: Throwable)

    enableCrashCollection(enable: Boolean)

Push

HMS Push Kit is messaging service. Push Kit helps you quickly and efficiently reach users. By integrating Push Kit, you can send messages to your apps on users' devices in real time. Click here for view service introduction and more description about Push Kit features.

How To Use

First of all, you have to create Services extends by HMS and GMS Messaging;

class HMSPushService : HmsMessageService() {

    val TAG = "HMS_PUSH"

    override fun onNewToken(token: String?) {
        super.onNewToken(token)
        Log.i(TAG, "Token: $token")
    }

    override fun onMessageReceived(message: RemoteMessage?) {
        super.onMessageReceived(message)
        Log.i(TAG, "Remote Message: ${message?.data}")
    }
}
class GMSPushService : FirebaseMessagingService() {

    val TAG = "GMS_PUSH"

    override fun onNewToken(token: String?) {
        super.onNewToken(token)
        Log.i(TAG, "Token: $token")
    }

    override fun onMessageReceived(message: RemoteMessage?) {
        super.onMessageReceived(message)
        Log.i(TAG, "Remote Message: ${message?.data}")
    }
}

After that you have to add your services into AndroidManifest.xml file and don't forget to add meta-data lines for auto init enabled;

<!-- HMS Push Service -->
<service android:name="com.hms.lib.commonmobileservices.push.basic.services.HMSPushService" android:exported="false">
    <intent-filter>
        <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
    </intent-filter>
</service>
<meta-data
    android:name="push_kit_auto_init_enabled"
    android:value="true" />

<!-- Firebase Push Service -->
<service android:name="com.hms.lib.commonmobileservices.push.basic.services.GMSPushService" android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<meta-data
    android:name="firebase_messaging_auto_init_enabled"
    android:value="true" />

<!-- Common Push Service -->
<receiver android:name="com.hms.lib.commonmobileservices.app.services.CommonPushService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.commonmobileservices.action.MESSAGING_EVENT" />
    </intent-filter>
</receiver>

Common Mobile Services provide a slider push notification in Push service. If you want to use that feature you should send data-message like this;

Slider Push Notification - Sample

{
    "validate_only": false,
    "message":
    {
        "data": "{'slider':'true','header':'This is a header','callToAction':'This is a sentence for call to action','items':[{'title':'Title of Slider 1','image':'https://yuklio.com/f/ph1d1-sample1.jpg','url':'https://url_of_slider_1.com'},{'title':'Title of Slider 2','image':'https://yuklio.com/f/aPmbR-sample2.jpg','url':'https://url_of_slider_2.com'},{'title':'Title of Slider 3','image':'https://yuklio.com/f/SKWB0-sample3.jpg','url':'https://url_of_slider_3.com'}]}",
        "token": ["DEVICE_TOKEN"]
    }
}

You can use getToken method to obtain token.

        val token = HuaweiPushServiceImpl(this).getToken()
        token.addOnSuccessListener {
            println("token-> ${it.token}")
        }
        val token = GooglePushServiceImpl(this).getToken()
        token.addOnSuccessListener {
            println("token-> ${it.token}")
        }

kt subscribeToTopic method is subscribes to topics in asynchronous mode. The topic messaging function provided by Push Kit allows you to send messages to multiple devices whose users have subscribed to a specific topic. You can write messages about the topic as required, and Push Kit determines target devices and then sends messages to the devices in a reliable manner. The name of the topic to subscribe. Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}". You need to add a listener to listen to the operation result.

        HuaweiPushServiceImpl(this).subscribeToTopic("test")
            .addOnSuccessListener { result: Unit ->
                Log.i(TAG, "OK")
            }
            .addOnFailureListener { error: Exception ->
                error.printStackTrace()
            }
            .addOnCanceledListener {
                Log.i(TAG, "Cancelled")
            }
        GooglePushServiceImpl(this).subscribeToTopic("test")
            .addOnSuccessListener { result: Unit ->
                Log.i(TAG, "OK")
            }
            .addOnFailureListener { error: Exception ->
                error.printStackTrace()
            }
            .addOnCanceledListener {
                Log.i(TAG, "Cancelled")
            }

kt unsubscribeFromTopic method is unsubscribes in asynchronous mode from topics that are subscribed to through the subscribe method. You need to add a listener to listen to the operation result.

        HuaweiPushServiceImpl(this).unsubscribeFromTopic("test")
            .addOnSuccessListener { result: Unit ->
                Log.i(TAG, "OK")
            }
            .addOnFailureListener { error: Exception ->
                error.printStackTrace()
            }
            .addOnCanceledListener {
                Log.i(TAG, "Cancelled")
            }
        GooglePushServiceImpl(this).unsubscribeFromTopic("test")
            .addOnSuccessListener { result: Unit ->
                Log.i(TAG, "OK")
            }
            .addOnFailureListener { error: Exception ->
                error.printStackTrace()
            }
            .addOnCanceledListener {
                Log.i(TAG, "Cancelled")
            }

kt setAutoInitEnabled method is sets whether to enable automatic initialization. If the enable parameter is set to true, the SDK automatically generates an AAID and obtains a token. The token is returned through the kt onNewToken() callback method.

HuaweiPushServiceImpl(this).autoInitEnabled(true)
GooglePushServiceImpl(this).autoInitEnabled(true)

kt isAutoInitEnabled method is checks whether automatic initialization is enabled. The default value is false.

HuaweiPushServiceImpl(this).isAutoInitEnabled()
GooglePushServiceImpl(this).isAutoInitEnabled()

Account

This library provides AccountService interface to handle Google Account Service and Huawei Account Kit with single code base.

How to use

First, initialize AccountService:

val accountService = AccountService.Factory.create(
        context,
        SignInParams.Builder()
            .requestEmail()
            .create()
)

It needs Context to check mobile services availability and provide proper mobile service type. Needs SignInParams to get permission from user to get extra information like email.

Then call silentSignIn to get last signed account. If result is success and there is no signed account it returns null.

accountService.silentSignIn(object : ResultCallback<SignInUser>{
    override fun onSuccess(result: SignInUser?) {}
    override fun onFailure(error: Exception) {}
    override fun onCancelled() {}
})

Call getSignInIntent to start the Activity of the relevant service for the use login.

accountService.getSignInIntent { intent ->
    startActivityForResult(intent, REQUEST_CODE)
}

Then get result from signInIntent by calling onSignInActivityResult. Call this function in the onActivityResult.

accountService.onSignInActivityResult(intent, 
    object: ResultCallback<SignInUser> {
        override fun onSuccess(result: SignInUser?) {}
        override fun onFailure(error: Exception) {}
        override fun onCancelled() {}
    }
)

Call signOut to sign out of the account.

accountService.signOut()
    .addOnSuccessListener {}
    .addOnFailureListener {}
    .addOnCanceledListener {}

Auth

This library provides AuthService interface to handle Firebase Auth Service and AGC Auth Service with single code base.

How to use

First, initialize AuthService:

val authService = AuthService.Factory.create(context)

It needs Context to check mobile services availability and provide proper mobile service type.

Then get last signed user by calling getUser(). If there is no signed user it will return null

Facebook Sign in

Call signInWithFacebook to sign in with facebook account. It needs token which you get from facebook sdk. If it is success, it returns AuthUser.

authService.signInWithFacebook(token)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Huawei or Google Sign in

Call signInWithGoogleOrHuawei to sign in with google or huawei account. It needs token which you get from account sdk. If it is success, it returns AuthUser.

authService.signInWithGoogleOrHuawei(token)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Email Sign in

Call signInWithEmail to sign in with email. It needs email and password. If it is success, it returns AuthUser.

authService.signInWithEmail(email, password)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Phone Sign in

Call signInWithPhone to sign in with phone. It needs countryCode, phoneNumber, password and verifyCode. If it is success, it returns AuthUser.

authService.signInWithPhone(countryCode,phoneNumber,password,verifyCode)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Twitter Sign in

Call signInWithTwitter to sign in with twitter. It needs token and secret. If it is success, it returns AuthUser.

authService.signInWithTwitter(token, secret)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Anonymous Sign in

Call anonymousSignIn to sign in with an anonymous account that generated by Auth Service Server. It doesn't need any information. If it is success, it returns AuthUser.

authService.anonymousSignIn()
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Email Sign up

Call signUp to sign up with email. It needs email and password. If it is success, it returns VerificationType.

authService.signUp(email, password)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

If VerificationType is CODE, it means that an e-mail containing verificaton code has been sent to the user. Call verifyCode to save user with verification code.

authService.verifyCode(email, password, verificationCode)
    .addOnSuccessListener {}
    .addOnFailureListener {}

Reset password

Call resetPassword to reset user' s password. It needs email. If it is success, it returns VerificationType.

authService.resetPassword(email)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

If verification type is LINK, it means that an e-mail containing link to reset password has been sent to the user. If VerificationType is CODE, it means that an e-mail containing verificaton code has been sent to the user. Call verifyCodeToResetPassword to reset user' s password.

authService.verifyCodeToResetPassword(email, newPassword, verificationCode)
    .addOnSuccessListener {}
    .addOnFailureListener {}

Update Username

Call updateUsername to update user' s username. It needs user login. If it is success, it returns VerificationType.

authService.updateUsername(email)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Update Photo

Call updatePhoto to update user' s photo. It needs user login. If it is success, it returns VerificationType.

authService.updatePhoto(photo)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Get Code

Call getCode to get verification code. It needs email or phone number. If it is success, it returns VerificationType.

authService.getCode(email)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}
authService.getCodePassword(email)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}
authService.getPhoneCode(country_code,phone,activity) //country_code ex: like Turkey: +90 then phone: 532xxxxxx
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Update Email

Call updateEmail to update user's mail. It needs email. If it is success, it returns VerificationType.

authService.updateEmail(email, verificationCode)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Update Phone

Call updatePhone to update user's phone. It needs phone. If it is success, it returns VerificationType.

authService.updatePhone(country_code, phone, verificationCode) //country_code ex: like Turkey: +90 then phone: 532xxxxxx
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Update Password

Call updatePassword to update user's password. It needs password. If it is success, it returns VerificationType.

authService.updatePassword(password, verificationCode)
    .addOnSuccessListener {verificationType -> }
    .addOnFailureListener {}

Link With Twitter

Call linkWithTwitter to link account with twitter. It needs token and secret. If it is success, it returns AuthUser.

authService.linkWithTwitter(token, secret)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Link With Facebook

Call linkWithFacebook to link account with facebook. It needs token. If it is success, it returns AuthUser.

authService.linkWithFacebook(token)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Link With Email

Call linkWithEmail to link account with email. It needs email, password and verifyCode. If it is success, it returns AuthUser.

authService.linkWithEmail(email,password,verifyCode)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Link With Phone

Call linkWithPhone to link account with phone. It needs countryCode, phoneNumber, password and verifyCode. If it is success, it returns AuthUser.

authService.linkWithPhone(countryCode,phoneNumber,password,verifyCode)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

Unlink

Call unlink to unlink account from the linked account. It needs provider. If it is success, it returns AuthUser.

authService.unlink(provider)
    .addOnSuccessListener {authUser -> }
    .addOnFailureListener {}

ReAuthenticate

Call reAuthenticate to re-authenticate users. It needs email and password.

authService.reAuthenticate(email,password)
    .addOnSuccessListener {}
    .addOnFailureListener {}

Delete User

Call deleteUser to delete users. It needs email and password.

authService.deleteUser()
    .addOnSuccessListener {}
    .addOnFailureListener {}

Site Kit

This kit provides different functionalities such as nearby search, text search, place details and place autocomplete. You can use these functions for both Google Services using Places API and Huawei Services using Site Kit.

How to use

At first we initialize SiteService interface:

val siteservice = SiteService.Factory.create( Context, apikey)

Context serves to check the mobile services availability and decide what service to run and the apikey serves to use the services of Site Kit or make the Places API calls.

getNearbyPlaces is a function that takes parameters like latitude, longitude, query or the keyword to search, hwpoiType which is the poi type you want to search (in case of using Huawei services you should be careful to enter the poi type matching the HW Poi Types from Site Kit eg. “RESTAURANT” instead of “restaurant”), radius of the area you want the results to be focused, language you want the results in, page index and page size, strict bounds to determine whether we want the location bounds to be strict or not.

fun getNearbyPlaces(siteLat: Double?,
                            siteLng: Double?,
                            query: String?,
                            hwpoiType: String?,
                            radius: Int?,
                            language: String?,
                            pageIndex: Int?,
                            pageSize: Int?,
                            strictBounds: Boolean?,
                    callback: (SiteToReturnResult: ResultData<List<SiteServiceReturn>>) -> Unit)

getTextSearchPlaces function uses almost the same parameters and performs search requests based on keyword.

fun getTextSearchPlaces(siteLat: Double?,
                                    siteLng: Double?,
                                    query: String?,
                                    hwpoiType: String?,
                                    radius: Int?,
                                    language: String?,
                                    pageIndex: Int?,
                                    pageSize: Int?,
                            callback: (SiteToReturnResult: ResultData<List<SiteServiceReturn>>) -> Unit)

placeSuggestion function returns a list of autocompleted places based on the keyword you have entered, the location you enter, the radius you want your results to be focused on and the language of them. childrenNode parameter is only used by the Huawei Service to return information on the children nodes.

 fun placeSuggestion(siteLat: Double?,
                                siteLng: Double?,
                                keyword: String?,
                                childrenNode: Boolean?,
                                areaRadius: Int?,
                                areaLanguage: String?,
                        callback: (SiteToReturnResult: ResultData<List<SiteServiceReturn>>) -> Unit)

getDetailSearch function is a function used to get the details of a place based on the place's ID.

fun getDetailSearch(siteID: String,
                                areaLanguage: String,
                                childrenNode: Boolean,
                        callback: (SiteToReturnResult: ResultData<SiteServiceReturn>) -> Unit)

childrenNode is also only used in case of the Huawei Services.

The results are returned as a data class of SiteServiceReturn

data class SiteServiceReturn (
    val id : String?,
    val name : String?,
    val locationLat : Double?,
    val locationLong : Double?,
    val phoneNumber : String?,
    val formatAddress : String?,
    val distance : Double?,
    val image : ArrayList<String>?,
    val averagePrice : Double?,
    val point : Double?,
)

You can reach the data from SiteServiceReturn as a result from the callback. Final example of how to call a function is given below.

SiteService.Factory.create( context, ApiKey).getNearbyPlaces(siteLat,siteLng,query,hwpoiType,radius,language,pageIndex,pageSize,strictBounds,
    {
        it.handleSuccess {
            it.data!![0].name //get name of first result of data
        }
    })

Identity Kit

HUAWEI Identity Kit provides unified address management services for users, including adding, editing, deleting, and querying addresses, and enables the users to authorize apps to access their addresses through a single tap on the screen.

Note: Identity Kit just working on HMS devices. GMS Identity service was deprecated.

How to use

At first we initialize IdentityServices interface as like this:

val identityService = IdentityService.Factory.create(context)

Than we can call getUserAddress function for obtain user's updated and detailed address information:

identityService!!.getUserAddress()

For catch the address details we have to listen onActivityResult override function with our response code:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        IdentityService.requestCode** -> when (resultCode) {
            Activity.RESULT_OK -> {
                val userAddress = UserAddressResponse().parseIntent(data)

                if (userAddress != null) {
                    // Do something with user address
                    println("$userAddress")
                } else {
                    println("userAddress is null")
                }

            }
            Activity.RESULT_CANCELED -> {
            }
            else -> {
            }
        }
        else -> {
        }
    }
}

About

License:Apache License 2.0


Languages

Language:Kotlin 100.0%