-
Notifications
You must be signed in to change notification settings - Fork 18
Clean up formatting and naming #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,5 @@ | |
| package com.example.chatapp | ||
|
|
||
| import android.app.Application | ||
|
|
||
| abstract class BaseChatApplication : Application() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,6 @@ import android.content.pm.PackageManager | |||||||||||||
| import android.location.Address | ||||||||||||||
| import android.location.Geocoder | ||||||||||||||
| import android.location.LocationManager | ||||||||||||||
| import androidx.annotation.RequiresApi | ||||||||||||||
| import androidx.appfunctions.AppFunction | ||||||||||||||
| import androidx.appfunctions.AppFunctionSerializable | ||||||||||||||
| import androidx.appfunctions.AppFunctionService | ||||||||||||||
|
|
@@ -35,29 +34,27 @@ import kotlin.coroutines.resume | |||||||||||||
| import kotlin.coroutines.suspendCoroutine | ||||||||||||||
|
|
||||||||||||||
| /** Built-in AppFunctions for location and geocoding services. */ | ||||||||||||||
| @RequiresApi(36) | ||||||||||||||
| @AndroidEntryPoint | ||||||||||||||
| @AppFunctionServiceEntryPoint( | ||||||||||||||
| serviceName = "BuiltInAppFunctionService", | ||||||||||||||
| appFunctionXmlFileName = "builtin_app_function_service", | ||||||||||||||
| ) | ||||||||||||||
| abstract class BaseBuiltInAppFunctionService : AppFunctionService() { | ||||||||||||||
| abstract class AbstractBuiltInAppFunctions : AppFunctionService() { | ||||||||||||||
| /** | ||||||||||||||
| * Geocode a physical address string into its latitude and longitude coordinates. | ||||||||||||||
| * Geocodes a physical address string into its latitude and longitude coordinates. | ||||||||||||||
| * | ||||||||||||||
| * @param address The physical address to geocode (e.g., "1600 Amphitheatre Pkwy, Mountain View, | ||||||||||||||
| * CA"). | ||||||||||||||
| * @return The latitude and longitude coordinates of the address, or null if geocoding fails. | ||||||||||||||
| */ | ||||||||||||||
| @AppFunction(isDescribedByKDoc = true) | ||||||||||||||
| suspend fun geocodeAddress( | ||||||||||||||
| address: String, | ||||||||||||||
| ): LatLng? { | ||||||||||||||
| suspend fun geocodeAddress(address: String): LatLng? { | ||||||||||||||
| val context = this | ||||||||||||||
| if (!Geocoder.isPresent()) { | ||||||||||||||
| return null | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| val geocoder = Geocoder(this) | ||||||||||||||
| val geocoder = Geocoder(context) | ||||||||||||||
|
|
||||||||||||||
| return withContext(Dispatchers.IO) { | ||||||||||||||
| try { | ||||||||||||||
|
|
@@ -90,7 +87,7 @@ abstract class BaseBuiltInAppFunctionService : AppFunctionService() { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Retrieve the current latitude and longitude coordinates of the device. | ||||||||||||||
| * Retrieves the current latitude and longitude coordinates of the device. | ||||||||||||||
| * | ||||||||||||||
| * @return The current location coordinates of the device, or null if location is unavailable or | ||||||||||||||
| * permission is denied. | ||||||||||||||
|
|
@@ -99,25 +96,26 @@ abstract class BaseBuiltInAppFunctionService : AppFunctionService() { | |||||||||||||
| @AppFunction(isDescribedByKDoc = true) | ||||||||||||||
| suspend fun getCurrentLocation(): LatLng? = | ||||||||||||||
| withContext(Dispatchers.Default) { | ||||||||||||||
| val context = this@AbstractBuiltInAppFunctions | ||||||||||||||
|
|
||||||||||||||
| // Check permissions | ||||||||||||||
| val hasFineLocation = | ||||||||||||||
| ContextCompat.checkSelfPermission( | ||||||||||||||
| this@BaseBuiltInAppFunctionService, | ||||||||||||||
| Manifest.permission.ACCESS_FINE_LOCATION, | ||||||||||||||
| ) == PackageManager.PERMISSION_GRANTED | ||||||||||||||
| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == | ||||||||||||||
| PackageManager.PERMISSION_GRANTED | ||||||||||||||
|
|
||||||||||||||
| val hasCoarseLocation = | ||||||||||||||
| ContextCompat.checkSelfPermission( | ||||||||||||||
| this@BaseBuiltInAppFunctionService, | ||||||||||||||
| context, | ||||||||||||||
| Manifest.permission.ACCESS_COARSE_LOCATION, | ||||||||||||||
| ) == PackageManager.PERMISSION_GRANTED | ||||||||||||||
| ) == | ||||||||||||||
| PackageManager.PERMISSION_GRANTED | ||||||||||||||
|
|
||||||||||||||
| if (!hasFineLocation && !hasCoarseLocation) { | ||||||||||||||
| throw IllegalStateException("Location permission is not granted") | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
113
to
115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing a generic Instead, you should throw
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| val locationManager = | ||||||||||||||
| this@BaseBuiltInAppFunctionService.getSystemService(Context.LOCATION_SERVICE) as LocationManager | ||||||||||||||
| context.getSystemService(Context.LOCATION_SERVICE) as LocationManager | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| // Try GPS Provider first | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The local variable
contextis redundant here. SinceAbstractBuiltInAppFunctionsextendsAppFunctionService(which is aContext),thiscan be passed directly to theGeocoderconstructor. Removing the intermediatecontextvariable simplifies the code and reduces boilerplate.