forked from Scottish-Tech-Army/Soundscape-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial integration of MapCompose map
This code is the initial integration of the MapCompose open street map UI. The soundscape service connection has been moved out into a separate class and then a MapCompose based UI has been added to the Home screen. The service now uses lastLocation to give a much quicker 'lock' on the current position. An HTTP cache has also been initiated - this likely affects all HTTP and not just the Tiles for open street map. As a result it may be required to mark Tile HTTP requests with a cache-control header if we don't want them to go through the cache? The API key for thunderforest is stored in local.properties as tileProviderApiKey so must be configured for each developer i.e. tileProviderApiKey=xxXXxxXXxxXXxxXXxxXXxxXXxxXXxxXX with an appropriate key. For builds, there's a new GitHub secret TILE_PROVIDER_API_KEY that needs setup.
- Loading branch information
Showing
11 changed files
with
325 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/src/main/java/org/scottishtecharmy/soundscape/SoundscapeServiceConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.scottishtecharmy.soundscape | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.os.IBinder | ||
import android.util.Log | ||
|
||
import org.scottishtecharmy.soundscape.services.SoundscapeService | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class SoundscapeServiceConnection @Inject constructor(@ApplicationContext context: Context) { | ||
|
||
var soundscapeService: SoundscapeService? = null | ||
private val appContext = context | ||
|
||
private var _serviceBoundState = MutableStateFlow(false) | ||
val serviceBoundState = _serviceBoundState.asStateFlow() | ||
|
||
// needed to communicate with the service. | ||
private val connection = object : ServiceConnection { | ||
|
||
override fun onServiceConnected(className: ComponentName, service: IBinder) { | ||
// we've bound to ExampleLocationForegroundService, cast the IBinder and get ExampleLocationForegroundService instance. | ||
Log.d(TAG, "onServiceConnected") | ||
|
||
val binder = service as SoundscapeService.LocalBinder | ||
soundscapeService = binder.getService() | ||
_serviceBoundState.value = true | ||
} | ||
|
||
override fun onServiceDisconnected(arg0: ComponentName) { | ||
// This is called when the connection with the service has been disconnected. Clean up. | ||
Log.d(TAG, "onServiceDisconnected") | ||
|
||
_serviceBoundState.value = false | ||
} | ||
} | ||
|
||
fun create() { | ||
tryToBindToServiceIfRunning() | ||
} | ||
|
||
private fun destroy() { | ||
|
||
// If this was the first launch | ||
if(serviceBoundState.value) { | ||
appContext.unbindService(connection) | ||
_serviceBoundState.value = false | ||
} | ||
} | ||
|
||
fun stopServiceAndExit() { | ||
// service is already running, stop it | ||
soundscapeService?.stopForegroundService() | ||
|
||
destroy() | ||
} | ||
|
||
fun tryToBindToServiceIfRunning() { | ||
if(!serviceBoundState.value) { | ||
Intent(appContext, SoundscapeService::class.java).also { intent -> | ||
appContext.bindService(intent, connection, 0) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "MainActivity" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.