Skip to content

Commit 78a948e

Browse files
authored
Merge pull request #15 from StudyProject-NLI/Bene
Added location functionalities.
2 parents 1265542 + ad4f006 commit 78a948e

3 files changed

Lines changed: 42 additions & 19 deletions

File tree

app/src/main/java/com/nlinterface/activities/MainActivity.kt

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class MainActivity : AppCompatActivity() {
8181

8282
/**
8383
* Called when the activity is started. It reads out the name of the activity and processes
84-
* the theme and keep screen on settings.
84+
* the theme and keep screen on settings. It also starts the camera and location tracking if
85+
* the permissions are granted.
8586
*/
8687
override fun onStart() {
8788
super.onStart()
@@ -103,6 +104,7 @@ class MainActivity : AppCompatActivity() {
103104
PackageManager.PERMISSION_GRANTED) {
104105
val locationService = Intent(this, LocationGetter()::class.java)
105106
startService(locationService)
107+
Log.i("Location", "Service for location tracking started.")
106108
}
107109

108110
val barcodeService = Intent(this, ConstantScanning()::class.java)
@@ -115,7 +117,7 @@ class MainActivity : AppCompatActivity() {
115117
}
116118
} else {
117119
stopService(barcodeService)
118-
Log.println(Log.INFO, "Scanner", "Stopping the Barcode Scanning Service")
120+
Log.i("Scanner", "Stopping the Barcode Scanning Service")
119121
}
120122

121123

@@ -358,7 +360,9 @@ class MainActivity : AppCompatActivity() {
358360
/**
359361
* Called when the permissions request is answered by the user and processes the result. If
360362
* record audio permissions are granted, confirm that it was granted to the user. If not
361-
* granted, request that it be granted for the full functionality to work.
363+
* granted, request that it be granted for the full functionality to work. If audio permissions
364+
* are granted further ask for location permissions.
365+
*
362366
*
363367
* @param requestCode
364368
* @param permissions
@@ -397,20 +401,6 @@ class MainActivity : AppCompatActivity() {
397401
).show()
398402
}
399403
}
400-
else if(requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
401-
if (requestCode == PackageManager.PERMISSION_GRANTED) {
402-
Toast.makeText(
403-
this, R.string.camera_permission_granted, Toast.LENGTH_LONG
404-
).show()
405-
val barcodeService = Intent(this, ConstantScanning()::class.java)
406-
startService(barcodeService)
407-
} else {
408-
Toast.makeText(
409-
this, R.string.camera_permission_denied,
410-
Toast.LENGTH_LONG
411-
).show()
412-
}
413-
}
414404
}
415405

416406
/**

app/src/main/java/com/nlinterface/utility/LocationGetter.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@ import com.google.android.gms.location.LocationResult
1313
import com.google.android.gms.location.LocationServices
1414
import 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+
*/
1622
class 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")

app/src/main/java/com/nlinterface/viewmodels/BarcodeProductinfoViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class ConstantScanning: Service() {
278278
}
279279

280280
/**
281-
*
281+
* When service is terminated camera binds are removed and resources are cleared.
282282
*/
283283

284284
override fun onDestroy() {

0 commit comments

Comments
 (0)