Skip to content

Commit 4557f7e

Browse files
committed
feat: migrate legacy db hostnames
1 parent 4615ccc commit 4557f7e

5 files changed

Lines changed: 38 additions & 16 deletions

File tree

0 Bytes
Loading

mobile/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ android {
2929
}
3030
buildTypes {
3131
release {
32-
applicationIdSuffix ".debug"
32+
applicationIdSuffix
3333
minifyEnabled false
3434
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
3535
ndk {
3636
debugSymbolLevel 'SYMBOL_TABLE'
3737
}
38+
signingConfig signingConfigs.debug
3839
}
3940
debug {
4041
applicationIdSuffix ".debug"

mobile/src/main/java/net/activitywatch/android/AWPreferences.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ class AWPreferences(context: Context) {
2626
editor.putBoolean("isFirstTime", true)
2727
editor.apply()
2828
}
29-
}
29+
30+
// To check if the hostname migration has already been run
31+
fun hasMigratedHostname(): Boolean {
32+
return sharedPreferences.getBoolean("hasMigratedHostname", false)
33+
}
34+
35+
// To mark the hostname migration as done so it won't run again
36+
fun setHostnameMigrated() {
37+
sharedPreferences.edit().putBoolean("hasMigratedHostname", true).apply()
38+
}
39+
}

mobile/src/main/java/net/activitywatch/android/BackgroundService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ class BackgroundService : Service() {
3838
// Start the server
3939
rustInterface.startServerTask()
4040

41+
// Run hostname migration exactly once (migrates buckets with "unknown" hostname to the real device name)
42+
val prefs = AWPreferences(this)
43+
if (!prefs.hasMigratedHostname()) {
44+
val hostname = rustInterface.getDeviceName(this)
45+
val result = rustInterface.migrateHostname(hostname)
46+
Log.i(TAG, "Hostname migration result: $result")
47+
prefs.setHostnameMigrated()
48+
}
49+
4150
// Start the sync scheduler
4251
syncScheduler.start()
4352

mobile/src/main/java/net/activitywatch/android/RustInterface.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.threeten.bp.Instant
1515

1616
private const val TAG = "RustInterface"
1717

18-
class RustInterface (context: Context? = null) {
18+
class RustInterface(context: Context? = null) {
1919

2020
private val appContext: Context? = context?.applicationContext
2121

@@ -50,6 +50,7 @@ class RustInterface (context: Context? = null) {
5050
external fun heartbeat(bucket_id: String, event: String, pulsetime: Double): String
5151
external fun query(query: String, timeperiods: String): String
5252
external fun androidQuery(timeperiods: String): String
53+
external fun migrateHostname(hostname: String): String
5354

5455
fun sayHello(to: String): String {
5556
return greeting(to)
@@ -88,18 +89,18 @@ class RustInterface (context: Context? = null) {
8889

8990
fun createBucketHelper(bucket_id: String, type: String, client: String = "aw-android") {
9091
val context =
91-
appContext
92-
?: throw IllegalStateException(
93-
"Context is required but was not provided during initialization"
94-
)
92+
appContext
93+
?: throw IllegalStateException(
94+
"Context is required but was not provided during initialization"
95+
)
9596
val hostname = getDeviceName(context)
9697
if (bucket_id in getBucketsJSON().keys().asSequence()) {
9798
Log.i(TAG, "Bucket with ID '$bucket_id', already existed. Not creating.")
9899
} else {
99100
val msg =
100-
createBucket(
101-
"""{"id": "$bucket_id", "type": "$type", "hostname": "$hostname", "client": "$client"}"""
102-
)
101+
createBucket(
102+
"""{"id": "$bucket_id", "type": "$type", "hostname": "$hostname", "client": "$client"}"""
103+
)
103104
Log.w(TAG, msg)
104105
}
105106
}
@@ -123,11 +124,11 @@ class RustInterface (context: Context? = null) {
123124
* @param pulsetime Time window for merging events (default: 60 seconds)
124125
*/
125126
fun heartbeatHelper(
126-
bucket_id: String,
127-
timestamp: Instant,
128-
duration: Double,
129-
data: JSONObject,
130-
pulsetime: Double = 60.0
127+
bucket_id: String,
128+
timestamp: Instant,
129+
duration: Double,
130+
data: JSONObject,
131+
pulsetime: Double = 60.0
131132
) {
132133
val event = Event(timestamp, duration, data)
133134
val msg = heartbeat(bucket_id, event.toString(), pulsetime)
@@ -170,9 +171,10 @@ class RustInterface (context: Context? = null) {
170171
JSONArray()
171172
}
172173
}
174+
173175
fun getDeviceName(context: Context): String {
174176
return Settings.Global.getString(context.contentResolver, Settings.Global.DEVICE_NAME)
175-
?: android.os.Build.MODEL ?: "Unknown"
177+
?: android.os.Build.MODEL ?: "Unknown"
176178
}
177179

178180
fun test() {

0 commit comments

Comments
 (0)