Skip to content

Commit

Permalink
Merge pull request #15 from StudyProject-NLI/Bene
Browse files Browse the repository at this point in the history
Added location functionalities.
  • Loading branch information
Bene0202 authored Jun 28, 2024
2 parents 1265542 + ad4f006 commit 78a948e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
24 changes: 7 additions & 17 deletions app/src/main/java/com/nlinterface/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class MainActivity : AppCompatActivity() {

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

val barcodeService = Intent(this, ConstantScanning()::class.java)
Expand All @@ -115,7 +117,7 @@ class MainActivity : AppCompatActivity() {
}
} else {
stopService(barcodeService)
Log.println(Log.INFO, "Scanner", "Stopping the Barcode Scanning Service")
Log.i("Scanner", "Stopping the Barcode Scanning Service")
}


Expand Down Expand Up @@ -358,7 +360,9 @@ class MainActivity : AppCompatActivity() {
/**
* Called when the permissions request is answered by the user and processes the result. If
* record audio permissions are granted, confirm that it was granted to the user. If not
* granted, request that it be granted for the full functionality to work.
* granted, request that it be granted for the full functionality to work. If audio permissions
* are granted further ask for location permissions.
*
*
* @param requestCode
* @param permissions
Expand Down Expand Up @@ -397,20 +401,6 @@ class MainActivity : AppCompatActivity() {
).show()
}
}
else if(requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (requestCode == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this, R.string.camera_permission_granted, Toast.LENGTH_LONG
).show()
val barcodeService = Intent(this, ConstantScanning()::class.java)
startService(barcodeService)
} else {
Toast.makeText(
this, R.string.camera_permission_denied,
Toast.LENGTH_LONG
).show()
}
}
}

/**
Expand Down
35 changes: 34 additions & 1 deletion app/src/main/java/com/nlinterface/utility/LocationGetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@ import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority


/**
* Location Getter is a service that allows to access the users location to use it for further
* context data for the AI. It helps the AI with choosing the appropriate functions with those
* additional context information.
*/
class LocationGetter: Service() {

private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var locationCallback: LocationCallback
private val globalParameters = GlobalParameters.instance!!
private var globalParameters = GlobalParameters.instance!!

/**
* Function that manages the Service, once initialized.
* It maps a value to location Client and
* starts the setupLocationCallback and startLocationUpdates
* Returns Start-Sticky to ensure it will try to start again,
* if it is destroyed for whatever reason.
*
* @param intent
* @param flags
* @param startId
*/
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
Expand All @@ -28,6 +45,10 @@ class LocationGetter: Service() {
return START_STICKY
}

/**
* Function that gets the location and saves it in a global variable to be accessed from
* everywhere.
*/
private fun setupLocationCallback() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
Expand All @@ -41,6 +62,9 @@ class LocationGetter: Service() {
}
}

/**
* Function that updates the location prioritizing low power drainage.
*/
private fun startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
Expand All @@ -54,10 +78,19 @@ class LocationGetter: Service() {
}
}

/**
* This method is required by the Service architecture,
* but not needed because the scanning should be done constantly.
* Therefore it just return null
*/

override fun onBind(intent: Intent?): IBinder? {
return null
}

/**
* When service is terminated stop updating the location.
*/
override fun onDestroy() {
fusedLocationClient.removeLocationUpdates(locationCallback)
Log.i("Location", "Accessing the location was stopped")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class ConstantScanning: Service() {
}

/**
*
* When service is terminated camera binds are removed and resources are cleared.
*/

override fun onDestroy() {
Expand Down

0 comments on commit 78a948e

Please sign in to comment.