@@ -13,12 +13,29 @@ import com.google.android.gms.location.LocationResult
1313import com.google.android.gms.location.LocationServices
1414import com.google.android.gms.location.Priority
1515
16+
17+ /* *
18+ * Location Getter is a service that allows to access the users location to use it for further
19+ * context data for the AI. It helps the AI with choosing the appropriate functions with those
20+ * additional context information.
21+ */
1622class LocationGetter : Service () {
1723
1824 private lateinit var fusedLocationClient: FusedLocationProviderClient
1925 private lateinit var locationCallback: LocationCallback
20- private val globalParameters = GlobalParameters .instance!!
26+ private var globalParameters = GlobalParameters .instance!!
2127
28+ /* *
29+ * Function that manages the Service, once initialized.
30+ * It maps a value to location Client and
31+ * starts the setupLocationCallback and startLocationUpdates
32+ * Returns Start-Sticky to ensure it will try to start again,
33+ * if it is destroyed for whatever reason.
34+ *
35+ * @param intent
36+ * @param flags
37+ * @param startId
38+ */
2239 override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
2340
2441 fusedLocationClient = LocationServices .getFusedLocationProviderClient(this )
@@ -28,6 +45,10 @@ class LocationGetter: Service() {
2845 return START_STICKY
2946 }
3047
48+ /* *
49+ * Function that gets the location and saves it in a global variable to be accessed from
50+ * everywhere.
51+ */
3152 private fun setupLocationCallback () {
3253 locationCallback = object : LocationCallback () {
3354 override fun onLocationResult (locationResult : LocationResult ) {
@@ -41,6 +62,9 @@ class LocationGetter: Service() {
4162 }
4263 }
4364
65+ /* *
66+ * Function that updates the location prioritizing low power drainage.
67+ */
4468 private fun startLocationUpdates () {
4569 if (ActivityCompat .checkSelfPermission(this ,
4670 android.Manifest .permission.ACCESS_FINE_LOCATION )
@@ -54,10 +78,19 @@ class LocationGetter: Service() {
5478 }
5579 }
5680
81+ /* *
82+ * This method is required by the Service architecture,
83+ * but not needed because the scanning should be done constantly.
84+ * Therefore it just return null
85+ */
86+
5787 override fun onBind (intent : Intent ? ): IBinder ? {
5888 return null
5989 }
6090
91+ /* *
92+ * When service is terminated stop updating the location.
93+ */
6194 override fun onDestroy () {
6295 fusedLocationClient.removeLocationUpdates(locationCallback)
6396 Log .i(" Location" , " Accessing the location was stopped" )
0 commit comments