Skip to content

Commit f49fccb

Browse files
committed
feat: add live floating speed controls for joystick and routes
1 parent 6df5f81 commit f49fccb

15 files changed

Lines changed: 249 additions & 60 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Windows 使用 `gradlew.bat`,PowerShell 用 `$env:JAVA_TOOL_OPTIONS='--enable-
7070

7171
本机编译、打包及 JVM 单元测试不依赖 Android Emulator;设备界面测试需要模拟器或测试手机。CI 的界面验证工作流继续使用独立安装的模拟器。
7272

73-
版本由显式输入确定:`APP_VERSION_NAME` 默认 `1.4.0`、`APP_VERSION_CODE` 默认 `1790000003`、`BUILD_REVISION` 默认 `unknown`。版本码沿用比旧时间戳版本更大的固定起点,后续发布必须递增;不再用版本码推断构建时间。构建配置不查询公网 IP、主机名、工作目录或当前时间,不执行 Git 命令。依赖、SDK 下载仍可能需要网络。未声称跨机器 APK 字节完全相同。
73+
版本由显式输入确定:`APP_VERSION_NAME` 默认 `1.5.0`、`APP_VERSION_CODE` 默认 `1790000005`、`BUILD_REVISION` 默认 `unknown`。版本码沿用比旧时间戳版本更大的固定起点,后续发布必须递增;不再用版本码推断构建时间。构建配置不查询公网 IP、主机名、工作目录或当前时间,不执行 Git 命令。依赖、SDK 下载仍可能需要网络。未声称跨机器 APK 字节完全相同。
7474

75-
导出目录 `app/build/outputs/distribution/`:`LocationService-v1.4.0-all.apk`、`-arm64.apk`、`-x86_64.apk`。导出使用 AGP 公共 artifacts API。未配置签名时 Release APK 未签名;分发安装请设置 `KEYSTORE_PATH`、`KEYSTORE_PASSWORD`、`KEY_ALIAS`、`KEY_PASSWORD`。PR CI 不依赖签名或地图 secrets。
75+
导出目录 `app/build/outputs/distribution/`:`LocationService-v1.5.0-all.apk`、`-arm64.apk`、`-x86_64.apk`。导出使用 AGP 公共 artifacts API。未配置签名时 Release APK 未签名;分发安装请设置 `KEYSTORE_PATH`、`KEYSTORE_PASSWORD`、`KEY_ALIAS`、`KEY_PASSWORD`。PR CI 不依赖签名或地图 secrets。
7676

7777
## 验证边界与来源
7878

‎app/build.gradle.kts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ plugins {
66
alias(libs.plugins.jetbrains.kotlin.serialization)
77
}
88

9-
val releaseVersionName = providers.gradleProperty("APP_VERSION_NAME").orElse("1.4.1")
10-
val releaseVersionCode = providers.gradleProperty("APP_VERSION_CODE").orElse("1790000004").map(String::toInt)
9+
val releaseVersionName = providers.gradleProperty("APP_VERSION_NAME").orElse("1.5.0")
10+
val releaseVersionCode = providers.gradleProperty("APP_VERSION_CODE").orElse("1790000005").map(String::toInt)
1111
val revision = providers.gradleProperty("BUILD_REVISION").orElse("unknown")
1212
require(releaseVersionName.get().matches(Regex("[0-9]+\\.[0-9]+\\.[0-9]+"))) { "APP_VERSION_NAME must be major.minor.patch" }
1313
require(releaseVersionCode.get() in 1..2_100_000_000) { "Invalid APP_VERSION_CODE" }

‎app/src/androidTest/java/com/system/location/service/RockerOverlayTest.kt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
1515
import androidx.test.platform.app.InstrumentationRegistry
1616
import com.system.location.service.runtime.ScenarioRuntime
1717
import com.system.location.service.ui.mock.RockerOverlay
18+
import com.system.location.service.ext.speed
19+
import com.system.location.service.ext.sharedPrefs
20+
import kotlinx.coroutines.delay
21+
import kotlinx.coroutines.withTimeout
1822
import kotlinx.coroutines.runBlocking
1923
import org.junit.Assert.*
2024
import org.junit.Test
@@ -40,6 +44,9 @@ class RockerOverlayTest {
4044
}
4145
val preferences = context.getSharedPreferences("rocker_overlay", Context.MODE_PRIVATE)
4246
val previouslyEnabled = preferences.getBoolean("enabled", true)
47+
val hadSpeed = context.sharedPrefs.contains("speed")
48+
val savedSpeed = context.speed
49+
context.speed = 3.05
4350
preferences.edit().remove("enabled").commit()
4451
try {
4552
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
@@ -50,8 +57,16 @@ class RockerOverlayTest {
5057
}
5158
overlay(R.id.rocker_minimized).check(matches(isDisplayed())).perform(click())
5259
overlay(R.id.rocker).check(matches(isDisplayed()))
60+
overlay(R.id.speed_title).check(matches(withText(R.string.rocker_speed)))
61+
overlay(R.id.speed_slider).check(matches(isDisplayed()))
62+
overlay(R.id.speed_up).perform(click())
63+
runBlocking { withTimeout(5000) { while (kotlin.math.abs(context.speed - 3.15) > 0.001) delay(10) } }
64+
overlay(R.id.speed).check(matches(withText(context.getString(R.string.overlay_speed_value, context.speed))))
5365
overlay(R.id.expand_menu).perform(click())
5466
overlay(R.id.rocker_minimized).check(matches(isDisplayed()))
67+
overlay(R.id.rocker_minimized).perform(click())
68+
overlay(R.id.speed).check(matches(withText(context.getString(R.string.overlay_speed_value, context.speed))))
69+
overlay(R.id.expand_menu).perform(click())
5570
scenario.onActivity { it.findNavController(R.id.nav_host_fragment_content_main).navigate(R.id.nav_runtime) }
5671
scenario.recreate()
5772
overlay(R.id.rocker_minimized).check(matches(isDisplayed()))
@@ -72,6 +87,7 @@ class RockerOverlayTest {
7287
} finally {
7388
instrumentation.runOnMainSync { RockerOverlay.setEnabled(false) }
7489
preferences.edit().putBoolean("enabled", previouslyEnabled).commit()
90+
if (hadSpeed) context.speed = savedSpeed else context.sharedPrefs.edit().remove("speed").commit()
7591
if (!overlayWasAllowed) overlayPermission("default")
7692
}
7793
}

‎app/src/main/java/com/system/location/service/runtime/ScenarioRuntime.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.system.location.service.core.runtime.*
1313
import com.system.location.service.core.scenario.Scenario
1414
import com.system.location.service.ext.experimentalOrbitMotion
1515
import com.system.location.service.ext.experimentalOrbitRadius
16+
import com.system.location.service.ext.speed
1617
import kotlinx.coroutines.*
1718
import kotlinx.coroutines.sync.Mutex
1819
import kotlinx.coroutines.sync.withLock
@@ -87,6 +88,22 @@ object ScenarioRuntime {
8788
fun refreshDiagnostics() = command { controller.refreshDiagnostics(); true }
8889
fun resume() = command { controller.resume() }
8990
fun motion(bearing: Double, moving: Boolean) = command { controller.setMotion(bearing, moving) }
91+
fun setSpeed(speed: Double): Deferred<Boolean> {
92+
val expected = state.value
93+
return command {
94+
val current = state.value
95+
if (!speed.isFinite() || speed !in 0.0..1000.0 ||
96+
current.scenarioId != expected.scenarioId || current.startedAt != expected.startedAt ||
97+
current.phase != expected.phase) return@command false
98+
val accepted = if (current.isActive) controller.setSpeed(speed) else true
99+
if (accepted) context.speed = speed
100+
if (current.isActive && !state.value.isActive) {
101+
if (state.value.phase == RuntimePhase.STOPPED) preferences.edit().putBoolean("interrupted", false).commit()
102+
finishService()
103+
}
104+
accepted
105+
}
106+
}
90107
fun stop() = command {
91108
val stopped = controller.stop()
92109
if (stopped) {

‎app/src/main/java/com/system/location/service/ui/mock/Rocker.kt‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import android.view.View
1010
import android.view.ViewConfiguration
1111
import android.view.WindowManager
1212
import android.widget.TextView
13+
import com.google.android.material.slider.Slider
1314
import com.system.location.service.R
1415
import com.system.location.service.android.widget.RockerView
1516
import com.system.location.service.ext.rockerCoords
1617
import kotlin.math.abs
18+
import kotlin.math.ceil
19+
import kotlin.math.round
1720

1821
/** A single application-owned overlay; it never retains an Activity or Fragment. */
1922
@SuppressLint("ClickableViewAccessibility", "RtlHardcoded")
@@ -39,8 +42,23 @@ class Rocker(private val context: Context) {
3942
private set
4043
private var autoStatus = false
4144
private var autoCardVisible = false
45+
private val speedSlider = root.findViewById<Slider>(R.id.speed_slider)
46+
private val speedText = root.findViewById<TextView>(R.id.speed)
47+
private var displayedSpeed = 0.0
48+
private var draggingSpeed = false
49+
var onSpeedChanged: ((Double) -> Unit)? = null
4250

4351
init {
52+
speedSlider.addOnChangeListener { _, value, fromUser ->
53+
if (!fromUser) return@addOnChangeListener
54+
changeSpeed(round(value * 10.0) / 10.0)
55+
}
56+
speedSlider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
57+
override fun onStartTrackingTouch(slider: Slider) { draggingSpeed = true }
58+
override fun onStopTrackingTouch(slider: Slider) { draggingSpeed = false }
59+
})
60+
root.findViewById<View>(R.id.speed_down).setOnClickListener { changeSpeed(displayedSpeed - 0.1) }
61+
root.findViewById<View>(R.id.speed_up).setOnClickListener { changeSpeed(displayedSpeed + 0.1) }
4462
compact.setOnClickListener { minimize(false) }
4563
root.findViewById<View>(R.id.expand_menu).setOnClickListener { minimize(true) }
4664
attachDrag(compact)
@@ -105,6 +123,31 @@ class Rocker(private val context: Context) {
105123
rockerView.listener = listener
106124
}
107125

126+
fun setSpeedState(route: Boolean, speed: Double) {
127+
val title = context.getString(if (route) R.string.overlay_route_speed else R.string.rocker_speed)
128+
root.findViewById<TextView>(R.id.speed_title).text = title
129+
speedSlider.contentDescription = title
130+
if (!draggingSpeed) renderSpeed(speed)
131+
}
132+
133+
private fun changeSpeed(speed: Double) {
134+
val value = (round(speed * 100.0) / 100.0).coerceIn(0.0, 1000.0)
135+
renderSpeed(value)
136+
onSpeedChanged?.invoke(value)
137+
}
138+
139+
private fun renderSpeed(speed: Double) {
140+
displayedSpeed = speed.coerceIn(0.0, 1000.0)
141+
speedText.text = context.getString(R.string.overlay_speed_value, displayedSpeed)
142+
// Keep a useful range for walking/driving without truncating a faster saved scenario.
143+
val upper = maxOf(50.0, ceil(displayedSpeed / 10.0) * 10.0).toFloat()
144+
if (speedSlider.value > upper) speedSlider.value = upper
145+
speedSlider.valueTo = upper
146+
if (!draggingSpeed) speedSlider.value = displayedSpeed.toFloat()
147+
root.findViewById<View>(R.id.speed_down).isEnabled = displayedSpeed > 0
148+
root.findViewById<View>(R.id.speed_up).isEnabled = displayedSpeed < 1000
149+
}
150+
108151
private fun attachDrag(handle: View) {
109152
val slop = ViewConfiguration.get(context).scaledTouchSlop
110153
var downX = 0f

‎app/src/main/java/com/system/location/service/ui/mock/RockerOverlay.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.system.location.service.ui.mock
22

33
import android.content.Context
4+
import android.content.SharedPreferences
45
import android.hardware.display.DisplayManager
56
import android.util.Log
67
import android.view.ContextThemeWrapper
78
import android.view.Display
89
import android.view.WindowManager
10+
import android.widget.Toast
911
import com.system.location.service.LocationServiceApp
1012
import com.system.location.service.R
1113
import com.system.location.service.android.widget.RockerView
1214
import com.system.location.service.android.window.OverlayUtils
1315
import com.system.location.service.core.runtime.RuntimePhase
16+
import com.system.location.service.ext.sharedPrefs
17+
import com.system.location.service.ext.speed
1418
import com.system.location.service.runtime.ScenarioRuntime
1519
import kotlinx.coroutines.CoroutineScope
1620
import kotlinx.coroutines.Dispatchers
@@ -29,6 +33,9 @@ object RockerOverlay {
2933
private var moving = false
3034
private var locked = false
3135
private var bearing = 0.0
36+
private val speedPreferenceListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
37+
if (key == "speed") scope.launch { updateSpeedState() }
38+
}
3239
val isEnabled get() = preferences.getBoolean("enabled", true)
3340
val isVisible get() = rocker?.isStart == true
3441

@@ -42,6 +49,7 @@ object RockerOverlay {
4249
}
4350
if (!observing) {
4451
observing = true
52+
context.sharedPrefs.registerOnSharedPreferenceChangeListener(speedPreferenceListener)
4553
scope.launch {
4654
ScenarioRuntime.state.map { Triple(it.phase, it.scenarioId, it.startedAt) }
4755
.distinctUntilChanged().collect {
@@ -52,6 +60,10 @@ object RockerOverlay {
5260
updatePlaybackState()
5361
}
5462
}
63+
scope.launch {
64+
ScenarioRuntime.state.map { Triple(it.phase, it.routeId, it.configuredSpeedMps) }
65+
.distinctUntilChanged().collect { updateSpeedState() }
66+
}
5567
}
5668
return try {
5769
val window = rocker ?: Rocker(ContextThemeWrapper(
@@ -60,6 +72,15 @@ object RockerOverlay {
6072
.createWindowContext(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null), R.style.AppTheme
6173
)).also { view ->
6274
rocker = view
75+
view.onSpeedChanged = { speed ->
76+
val change = ScenarioRuntime.setSpeed(speed)
77+
scope.launch {
78+
if (!change.await()) {
79+
updateSpeedState()
80+
Toast.makeText(context, R.string.overlay_speed_failed, Toast.LENGTH_SHORT).show()
81+
}
82+
}
83+
}
6384
view.setRockerListener(object : RockerView.Companion.OnMoveListener {
6485
override fun onStarted() {
6586
bearing = ScenarioRuntime.state.value.sample?.bearing?.toDouble() ?: bearing
@@ -79,6 +100,7 @@ object RockerOverlay {
79100
})
80101
}
81102
updatePlaybackState()
103+
updateSpeedState()
82104
window.show()
83105
true
84106
} catch (error: RuntimeException) {
@@ -112,4 +134,10 @@ object RockerOverlay {
112134
else -> "未在模拟 · 请在主页启动"
113135
})
114136
}
137+
138+
private fun updateSpeedState() {
139+
val state = ScenarioRuntime.state.value
140+
rocker?.setSpeedState(state.isActive && state.routeId != null,
141+
if (state.isActive) state.configuredSpeedMps ?: context.speed else context.speed)
142+
}
115143
}

‎app/src/main/res/layout/layout_rocker.xml‎

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -206,75 +206,63 @@
206206
app:contentPadding="4dp"
207207
app:cardCornerRadius="4dp"
208208
app:cardBackgroundColor="@color/white"
209-
android:visibility="gone"
210209
app:cardElevation="2dp">
211210

212-
<androidx.constraintlayout.widget.ConstraintLayout
211+
<LinearLayout
213212
android:id="@+id/expand_menu_layout"
214213
android:layout_width="match_parent"
215-
android:layout_height="wrap_content">
216-
217-
<androidx.constraintlayout.widget.ConstraintLayout
218-
android:layout_width="match_parent"
219-
android:layout_height="wrap_content"
220-
app:layout_constraintTop_toTopOf="parent"
221-
app:layout_constraintStart_toStartOf="parent">
214+
android:layout_height="wrap_content"
215+
android:orientation="vertical">
222216

223-
<com.google.android.material.textview.MaterialTextView
224-
style="@style/TextAppearance.Material3.LabelSmall"
217+
<TextView
218+
android:id="@+id/speed_title"
225219
android:layout_width="match_parent"
226220
android:layout_height="wrap_content"
227221
android:text="@string/rocker_speed"
228-
android:textSize="8sp"
229-
android:layout_weight="1"
230-
android:maxLines="1"
231-
android:gravity="center|start"
232-
app:layout_constraintTop_toTopOf="parent"
233-
app:layout_constraintStart_toStartOf="parent"
234-
tools:ignore="SmallSp" />
222+
android:textSize="14sp"
223+
android:gravity="center"
224+
android:textColor="#373737" />
235225

236-
<com.google.android.material.textview.MaterialTextView
237-
android:id="@+id/speed"
238-
style="@style/TextAppearance.Material3.LabelSmall"
226+
<LinearLayout
239227
android:layout_width="match_parent"
240228
android:layout_height="wrap_content"
229+
android:gravity="center_vertical"
230+
android:orientation="horizontal">
231+
<Button
232+
android:id="@+id/speed_down"
233+
android:layout_width="48dp"
234+
android:layout_height="48dp"
235+
android:padding="0dp"
236+
android:text="−"
237+
android:contentDescription="@string/overlay_speed_decrease" />
238+
<TextView
239+
android:id="@+id/speed"
240+
android:layout_width="0dp"
241+
android:layout_height="wrap_content"
241242
android:text="@string/default_speed"
242243
android:layout_weight="1"
243-
android:textSize="8sp"
244-
android:gravity="center|end"
245-
android:maxLines="1"
246-
app:layout_constraintTop_toTopOf="parent"
247-
app:layout_constraintEnd_toEndOf="parent"
248-
tools:ignore="SmallSp" />
244+
android:textSize="12sp"
245+
android:gravity="center"
246+
android:textColor="#373737" />
247+
<Button
248+
android:id="@+id/speed_up"
249+
android:layout_width="48dp"
250+
android:layout_height="48dp"
251+
android:padding="0dp"
252+
android:text="+"
253+
android:contentDescription="@string/overlay_speed_increase" />
254+
</LinearLayout>
249255

250256
<com.google.android.material.slider.Slider
251257
android:id="@+id/speed_slider"
252258
android:layout_width="match_parent"
253259
android:layout_height="wrap_content"
254-
android:stepSize="5.0"
260+
android:contentDescription="@string/rocker_speed"
255261
android:valueFrom="0.0"
256262
android:valueTo="50.0"
257263
app:tickVisible="false"
258-
app:thumbElevation="2dp"
259-
android:padding="0dp"
260-
app:thumbHeight="8dp"
261-
app:thumbRadius="4dp"
262-
app:trackHeight="4dp"
263-
app:layout_constraintBottom_toBottomOf="parent"
264-
app:layout_constraintStart_toStartOf="parent" />
265-
266-
<com.google.android.material.divider.MaterialDivider
267-
android:id="@+id/divider"
268-
android:layout_width="match_parent"
269-
android:layout_height="wrap_content"
270-
android:layout_marginTop="24dp"
271-
app:layout_constraintTop_toBottomOf="@id/speed"
272-
app:layout_constraintStart_toStartOf="parent" />
273-
274-
</androidx.constraintlayout.widget.ConstraintLayout>
275-
276-
277-
</androidx.constraintlayout.widget.ConstraintLayout>
264+
app:trackHeight="4dp" />
265+
</LinearLayout>
278266
</androidx.cardview.widget.CardView>
279267

280268
</LinearLayout>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
<string name="overlay_route_speed">ルート速度</string>
3+
<string name="overlay_speed_value">%1$.2f m/s</string>
4+
<string name="overlay_speed_decrease">速度を 0.1 m/s 下げる</string>
5+
<string name="overlay_speed_increase">速度を 0.1 m/s 上げる</string>
6+
<string name="overlay_speed_failed">シーンの状態が変わったか、速度変更に失敗しました。再試行してください</string>
7+
</resources>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
<string name="overlay_route_speed">路线速度</string>
3+
<string name="overlay_speed_value">%1$.2f m/s</string>
4+
<string name="overlay_speed_decrease">速度降低 0.1 米/秒</string>
5+
<string name="overlay_speed_increase">速度增加 0.1 米/秒</string>
6+
<string name="overlay_speed_failed">场景状态已变化或调速失败,请重试</string>
7+
</resources>

0 commit comments

Comments
 (0)