Skip to content

Instantly share code, notes, and snippets.

View barbeau's full-sized avatar

Sean Barbeau barbeau

View GitHub Profile
@barbeau
barbeau / build.gradle
Last active August 13, 2021 21:45
Flow - lightweight architecture article - app build.gradle
..
// New plugins for Hilt
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
...
dependencies {
...
// To observe the flow within the Service based on the lifecycle
@barbeau
barbeau / MainActivity.kt
Last active August 16, 2021 16:37
Flow - lightweight architecture article -
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
...
// Data store from which to receive location updates via Flow, injected via Hilt
@Inject
lateinit var repository: LocationRepository
// Get a reference to the Job from the Flow so we can stop it from UI events
private var locationFlow: Job? = null
@barbeau
barbeau / ForegroundOnlyLocationService.kt
Last active August 16, 2021 16:22
Flow - lighweight architecture article - Service
@AndroidEntryPoint
class ForegroundOnlyLocationService : LifecycleService() {
...
// Data store (in this case, the SharedLocationManager) that the service will observe, injected via Hilt
@Inject
lateinit var repository: LocationRepository
// Get a reference to the Job from the Flow so we can stop it from UI events
private var locationFlow: Job? = null
@barbeau
barbeau / LocationRepository.kt
Created August 12, 2021 15:25
Flow - lightweight architecture article - LocationRepository
class LocationRepository @Inject constructor(
private val sharedLocationManager: SharedLocationManager
) {
/**
* Observable flow for location updates
*/
fun getLocations() = sharedLocationManager.locationFlow()
}
@barbeau
barbeau / DataModule.kt
Created August 12, 2021 15:08
Flow - lighweight architecture article - Data Module
/**
* Configuration for DI on the repository and shared location manager
*/
@Module
@InstallIn(SingletonComponent::class)
object DataModule {
@Provides
@Singleton
fun provideSharedLocationManager(
@barbeau
barbeau / LocationApplication.kt
Created August 12, 2021 15:01
Flow - lightweight architecture article - LocationApplication
// Required for Hilt dependency injection
@HiltAndroidApp
class LocationApplication : Application() {
// No need to cancel this scope as it'll be torn down with the process - see https://medium.com/androiddevelopers/coroutines-patterns-for-work-that-shouldnt-be-cancelled-e26c40f142ad
val applicationScope = GlobalScope
}
@barbeau
barbeau / SharedLocationManager.kt
Last active August 16, 2021 16:04
Flow - lightweight architecture article - SharedLocationManager
/**
* Wraps the LocationServices and fused location provider in callbackFlow
*
* Derived in part from https://github.com/android/location-samples/blob/main/LocationUpdatesBackgroundKotlin/app/src/main/java/com/google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
* and https://github.com/googlecodelabs/kotlin-coroutines/blob/master/ktx-library-codelab/step-06/myktxlibrary/src/main/java/com/example/android/myktxlibrary/LocationUtils.kt
*/
class SharedLocationManager constructor(
private val context: Context,
externalScope: CoroutineScope
) {
/**
* Handles a fresh Navigation Message. The message is in its raw format.
*/
public void onNavMessageReported(byte prn, byte type, short id, byte[] rawData) {
Preconditions.checkArgument(type == 1, "Unsupported NavigationMessage Type: " + type);
Preconditions.checkArgument(
rawData != null && rawData.length == L1_CA_MESSAGE_LENGTH_BYTES,
"Invalid length of rawData for L1 C/A");
synchronized (fullyDecodedIntermediateEphemerides) {
switch (id) {
/**
* Parses a string array containing an updates to the navigation message and return the most
* recent {@link GpsNavMessageProto}.
*/
public void parseHwNavigationMessageUpdates(GnssNavigationMessage navigationMessage) {
byte messagePrn = (byte) navigationMessage.getSvid();
byte messageType = (byte) (navigationMessage.getType() >> 8);
int subMessageId = navigationMessage.getSubmessageId();
byte[] messageRawData = navigationMessage.getData();
@barbeau
barbeau / navigation-message-log.md
Last active July 6, 2021 22:33
Android Navigation Message example data
#   Nav Svid Type Status MessageId Sub-messageId Data(Bytes)
Nav 22 769 1 5 1 8 43 109 105 -31 -32 75 89 -100 35 -112
Nav 13 769 1 5 1 8 43 105 64 -45 -24 -119 -18 126 -97 72
Nav 12 769 1 5 1 8 43 104 -58 -72 -80 -109 -31 106 -65 -64
Nav 22 769 1 5 2 16 -85 0 -124 -112 8 5 -61 9 -67 -56
Nav 5 257 1 25 4 34 -64 116 36 37 121 -84 120 31 -18 111