Skip to content

Commit f76d960

Browse files
committed
added auto init
1 parent 8e71152 commit f76d960

File tree

7 files changed

+78
-24
lines changed

7 files changed

+78
-24
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Convenience library to receive user location updates and geofence events with mi
1010

1111
- [Features](#features)
1212
- [Installation](#installation)
13+
- [Initialization](#initialization)
1314
- [Quick Start](#quick-start)
1415
- [Geofencing](#geofencing)
1516
- [Location Tracking](#location-tracking)
1617
- [API Reference](#api-reference)
1718
- [Permissions](#permissions)
18-
- [Publishing](#publishing)
1919
- [Contributing](#contributing)
2020
- [License](#license)
2121

@@ -27,7 +27,7 @@ Convenience library to receive user location updates and geofence events with mi
2727
- Geofence transitions: enter, exit, dwell
2828
- Continuous location updates via `SharedFlow`
2929
- Survives app kill and device reboot (via WorkManager)
30-
- Auto-initializes with AndroidX Startup
30+
- Auto-initializes via built-in ContentProvider (no external dependencies)
3131
- Configurable update intervals, displacement, and priority
3232
- Custom actions for geofence and location events
3333
- minSdk 23, targets Android 15+
@@ -58,6 +58,47 @@ Then add the dependency:
5858
implementation("com.github.kibotu:geofencer:latest")
5959
```
6060

61+
## Initialization
62+
63+
The library auto-initializes using a `ContentProvider` — no manual setup needed.
64+
65+
### Customizing init order
66+
67+
The `ContentProvider` uses `android:initOrder` to control initialization priority relative to other providers. The default value is `100`. Higher values mean the provider is initialized earlier.
68+
69+
Override the priority in your app's `res/values/integers.xml` (or any values resource file):
70+
71+
```xml
72+
<resources>
73+
<integer name="geofencer_init_priority">200</integer>
74+
</resources>
75+
```
76+
77+
### Disabling auto-initialization
78+
79+
Add a boolean resource override in your app's `res/values/` to disable the `ContentProvider` from initializing the library:
80+
81+
```xml
82+
<resources>
83+
<bool name="geofencer_auto_init_enabled">false</bool>
84+
</resources>
85+
```
86+
87+
Then initialize manually in your `Application.onCreate()`:
88+
89+
```kotlin
90+
Geofencer.init(applicationContext)
91+
```
92+
93+
Alternatively, you can remove the provider entirely via manifest merging:
94+
95+
```xml
96+
<provider
97+
android:name="net.kibotu.geofencer.internal.GeofencerInitializer"
98+
android:authorities="${applicationId}.geofencer-init"
99+
tools:node="remove" />
100+
```
101+
61102
## Quick Start
62103

63104
### Geofencing

geofencer/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66

77
android {
88
namespace = "net.kibotu.geofencer"
9+
resourcePrefix = "geofencer_"
910

1011
compileSdk {
1112
version = release(36) {
@@ -75,6 +76,5 @@ dependencies {
7576
implementation(libs.play.services.location)
7677
implementation(libs.androidx.core.ktx)
7778
implementation(libs.work.runtime.ktx)
78-
implementation(libs.startup.runtime)
7979
implementation(libs.timber)
8080
}

geofencer/src/main/AndroidManifest.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@
3434
</receiver>
3535

3636
<provider
37-
android:name="androidx.startup.InitializationProvider"
38-
android:authorities="${applicationId}.androidx-startup"
39-
android:exported="false">
40-
<meta-data
41-
android:name="net.kibotu.geofencer.internal.GeofencerInitializer"
42-
android:value="androidx.startup" />
43-
</provider>
37+
android:name="net.kibotu.geofencer.internal.GeofencerInitializer"
38+
android:authorities="${applicationId}.geofencer-init"
39+
android:exported="false"
40+
android:initOrder="@integer/geofencer_init_priority" />
4441

4542
</application>
4643

geofencer/src/main/java/net/kibotu/geofencer/Geofencer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object Geofencer {
2727

2828
private fun requireRepository(): GeofenceRepository {
2929
check(::repository.isInitialized) {
30-
"Geofencer not initialized. Add androidx.startup dependency or call Geofencer.init(context)."
30+
"Geofencer not initialized. Ensure GeofencerInitializer ContentProvider runs or call Geofencer.init(context) manually."
3131
}
3232
return repository
3333
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package net.kibotu.geofencer.internal
22

3-
import android.content.Context
4-
import androidx.startup.Initializer
3+
import android.content.ContentProvider
4+
import android.content.ContentValues
5+
import android.database.Cursor
6+
import android.net.Uri
57
import net.kibotu.geofencer.Geofencer
8+
import net.kibotu.geofencer.R
69

7-
internal class GeofencerInitializer : Initializer<Geofencer> {
10+
internal class GeofencerInitializer : ContentProvider() {
811

9-
override fun create(context: Context): Geofencer {
10-
Geofencer.init(context.applicationContext)
11-
return Geofencer
12+
override fun onCreate(): Boolean {
13+
val ctx = context?.applicationContext ?: return false
14+
val enabled = ctx.resources.getBoolean(R.bool.geofencer_auto_init_enabled)
15+
if (!enabled) return true
16+
Geofencer.init(ctx)
17+
return true
1218
}
1319

14-
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
20+
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? = null
21+
override fun getType(uri: Uri): String? = null
22+
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
23+
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
24+
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
1525
}
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<resources>
2-
<plurals name="num_locations_reported">
2+
<plurals name="geofencer_num_locations_reported">
33
<item quantity="zero">No location reported</item>
44
<item quantity="one">One location reported</item>
55
<item quantity="other">%d locations reported</item>
66
</plurals>
7-
<string name="dialog_rationale_coarse_location">We need GPS Location in background so that we can guide you when you are at that locations</string>
8-
<string name="button_allow">Allow</string>
9-
<string name="button_reject">Reject</string>
7+
<string name="geofencer_dialog_rationale_coarse_location">We need GPS Location in background so that we can guide you when you are at that locations</string>
8+
<string name="geofencer_button_allow">Allow</string>
9+
<string name="geofencer_button_reject">Reject</string>
10+
11+
<!-- ContentProvider init order for GeofencerInitializer.
12+
Higher values = earlier initialization relative to other providers.
13+
Override in your app's res/values to change priority. -->
14+
<integer name="geofencer_init_priority">100</integer>
15+
16+
<!-- Set to false in your app's res/values to disable auto-initialization. -->
17+
<bool name="geofencer_auto_init_enabled">true</bool>
1018
</resources>

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ lifecycleRuntimeKtx = "2.10.0"
1111
mapsCompose = "8.2.0"
1212
playServicesLocation = "21.3.0"
1313
preferenceKtx = "1.2.1"
14-
startupRuntime = "1.2.0"
1514
timber = "5.0.1"
1615
vanniktechMavenPublish = "0.36.0"
1716
workManager = "2.10.0"
@@ -38,7 +37,6 @@ maps-compose = { group = "com.google.maps.android", name = "maps-compose", versi
3837
maps-compose-utils = { group = "com.google.maps.android", name = "maps-compose-utils", version.ref = "mapsCompose" }
3938
play-services-location = { group = "com.google.android.gms", name = "play-services-location", version.ref = "playServicesLocation" }
4039
preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preferenceKtx" }
41-
startup-runtime = { group = "androidx.startup", name = "startup-runtime", version.ref = "startupRuntime" }
4240
timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
4341
work-runtime-ktx = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "workManager" }
4442

0 commit comments

Comments
 (0)