diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5d89add..1d85656 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -18,19 +18,14 @@ android { applicationId = "com.lzxnone.openloc" minSdk = 28 targetSdk = 36 - versionCode = 8 - versionName = "1.3.0" + versionCode = 9 + versionName = "1.3.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } - splits { - abi { - isEnable = true - reset() - include("armeabi-v7a", "arm64-v8a", "x86", "x86_64") - isUniversalApk = true - } + base { + archivesName.set("OpenLoc-v${defaultConfig.versionName}") } diff --git a/app/src/main/java/com/lzxnone/openloc/core/provider/LocationConfigProvider.kt b/app/src/main/java/com/lzxnone/openloc/core/provider/LocationConfigProvider.kt index 0f8852b..93cb2d4 100644 --- a/app/src/main/java/com/lzxnone/openloc/core/provider/LocationConfigProvider.kt +++ b/app/src/main/java/com/lzxnone/openloc/core/provider/LocationConfigProvider.kt @@ -30,8 +30,11 @@ class LocationConfigProvider : ContentProvider() { val prefs = ctx.getSharedPreferences("openloc_prefs", Context.MODE_PRIVATE) val isEnabled = prefs.getBoolean("mock_service_enabled", false) - val lat = prefs.getString("mock_latitude", "39.908722")?.toDoubleOrNull() ?: 39.908722 - val lng = prefs.getString("mock_longitude", "116.397496")?.toDoubleOrNull() ?: 116.397496 + val runtime = AppPreferences.getRuntimeMockState() + val lat = if (isEnabled) runtime.latitude else (prefs.getString("mock_latitude", "39.908722")?.toDoubleOrNull() ?: 39.908722) + val lng = if (isEnabled) runtime.longitude else (prefs.getString("mock_longitude", "116.397496")?.toDoubleOrNull() ?: 116.397496) + val speed = if (isEnabled) runtime.speed else 0.0f + val bearing = if (isEnabled) runtime.bearing else 0.0f val name = prefs.getString("mock_location_name", "") ?: "" val mode = prefs.getString("app_mock_mode", "GLOBAL") ?: "GLOBAL" val whitelistJson = prefs.getString("whitelist_apps", "[]") ?: "[]" @@ -47,6 +50,10 @@ class LocationConfigProvider : ContentProvider() { putBoolean(ConfigContract.KEY_ENABLED, isEnabled) putDouble(ConfigContract.KEY_LATITUDE, lat) putDouble(ConfigContract.KEY_LONGITUDE, lng) + putDouble("altitude", 50.0) + putFloat("accuracy", 5.0f) + putFloat("bearing", bearing) + putFloat("speed", speed) putString(ConfigContract.KEY_LOCATION_NAME, name) putString(ConfigContract.KEY_APP_MOCK_MODE, mode) putString(ConfigContract.KEY_WHITELIST_APPS, whitelistJson) diff --git a/app/src/main/java/com/lzxnone/openloc/data/preference/AppPreferences.kt b/app/src/main/java/com/lzxnone/openloc/data/preference/AppPreferences.kt index 7685938..646402b 100644 --- a/app/src/main/java/com/lzxnone/openloc/data/preference/AppPreferences.kt +++ b/app/src/main/java/com/lzxnone/openloc/data/preference/AppPreferences.kt @@ -201,6 +201,19 @@ object AppPreferences { @Volatile private var activeBearing: Float = 0.0f @Volatile private var hasActivePoint: Boolean = false + internal data class RuntimeMockState( + val latitude: Double, + val longitude: Double, + val speed: Float, + val bearing: Float + ) + + internal fun getRuntimeMockState(): RuntimeMockState { + val pushLat = if (hasActivePoint) activeLat else mockLatitude + val pushLng = if (hasActivePoint) activeLng else mockLongitude + return RuntimeMockState(pushLat, pushLng, activeSpeed, activeBearing) + } + private val tickerThread by lazy { HandlerThread("OpenLoc-1Hz-Ticker").apply { start() } } diff --git a/app/src/main/java/com/lzxnone/openloc/hook/client/ThirdPartyNlpHook.kt b/app/src/main/java/com/lzxnone/openloc/hook/client/ThirdPartyNlpHook.kt index fc20487..55946db 100644 --- a/app/src/main/java/com/lzxnone/openloc/hook/client/ThirdPartyNlpHook.kt +++ b/app/src/main/java/com/lzxnone/openloc/hook/client/ThirdPartyNlpHook.kt @@ -4,6 +4,7 @@ import android.content.Context import android.location.Location import android.os.Bundle import android.os.SystemClock +import com.lzxnone.openloc.core.util.CoordinateConverter import com.lzxnone.openloc.core.util.TraceLogger import com.lzxnone.openloc.hook.ipc.ConfigFetcher import com.lzxnone.openloc.hook.model.GlobalMockMemory @@ -374,8 +375,9 @@ object ThirdPartyNlpHook { return null } - val lat = GlobalMockMemory.latitude - val lng = GlobalMockMemory.longitude + val gcj = CoordinateConverter.wgs84ToGcj02(GlobalMockMemory.latitude, GlobalMockMemory.longitude) + val lat = gcj[0] + val lng = gcj[1] val alt = GlobalMockMemory.altitude val accuracy = GlobalMockMemory.accuracy val speed = GlobalMockMemory.speed @@ -452,7 +454,27 @@ object ThirdPartyNlpHook { if (bdLocationClass != null) { TraceLogger.i("ThirdPartyNLP", "[$packageName] Detected BDLocation, arming Baidu hooks...") + val getCoorTypeMethod = try { + bdLocationClass.getMethod("getCoorType") + } catch (_: Throwable) { + null + } + fun resolveBaiduCoords(instance: Any?): DoubleArray? { + val coorType = try { + (getCoorTypeMethod?.invoke(instance) as? String)?.lowercase() + } catch (_: Throwable) { + null + } + return when { + coorType == "bd09mc" || (coorType != null && coorType.contains("mc")) -> null + coorType == "wgs84" -> doubleArrayOf(GlobalMockMemory.latitude, GlobalMockMemory.longitude) + coorType == "gcj02" -> CoordinateConverter.wgs84ToGcj02(GlobalMockMemory.latitude, GlobalMockMemory.longitude) + else -> CoordinateConverter.wgs84ToBd09(GlobalMockMemory.latitude, GlobalMockMemory.longitude) + } + } + + var lastBdLogTime = 0L for (method in bdLocationClass.declaredMethods) { when (method.name) { "getLatitude" -> { @@ -460,7 +482,16 @@ object ThirdPartyNlpHook { XposedBridge.hookMethod(method, object : XC_MethodHook() { override fun afterHookedMethod(param: MethodHookParam) { if (GlobalMockMemory.isEnabled && GlobalMockMemory.shouldMock(packageName)) { - param.result = GlobalMockMemory.latitude + val orig = param.result as? Double ?: return + if (orig == Double.MIN_VALUE || orig == 0.0 || kotlin.math.abs(orig) > 90.0) return + val coords = resolveBaiduCoords(param.thisObject) ?: return + val now = System.currentTimeMillis() + if (now - lastBdLogTime > 2000L) { + lastBdLogTime = now + val coorType = try { getCoorTypeMethod?.invoke(param.thisObject) as? String } catch (_: Throwable) { null } + TraceLogger.d("ThirdPartyNLP", "[$packageName] BDLocation.getLatitude orig=$orig, coorType=$coorType -> ${coords[0]}") + } + param.result = coords[0] } } }) @@ -471,7 +502,10 @@ object ThirdPartyNlpHook { XposedBridge.hookMethod(method, object : XC_MethodHook() { override fun afterHookedMethod(param: MethodHookParam) { if (GlobalMockMemory.isEnabled && GlobalMockMemory.shouldMock(packageName)) { - param.result = GlobalMockMemory.longitude + val orig = param.result as? Double ?: return + if (orig == Double.MIN_VALUE || orig == 0.0 || kotlin.math.abs(orig) > 180.0) return + val coords = resolveBaiduCoords(param.thisObject) ?: return + param.result = coords[1] } } }) @@ -526,7 +560,10 @@ object ThirdPartyNlpHook { XposedBridge.hookMethod(method, object : XC_MethodHook() { override fun afterHookedMethod(param: MethodHookParam) { if (GlobalMockMemory.isEnabled && GlobalMockMemory.shouldMock(packageName)) { - param.result = 61 // 61: GPS 定位成功 + val orig = param.result as? Int + if (orig == 161 || orig == 66 || orig == 65 || orig == 68) { + param.result = 61 // 将网络/离线成功态统一伪装为 GPS 定位成功 + } } } }) diff --git a/app/src/main/java/com/lzxnone/openloc/hook/ipc/ConfigFetcher.kt b/app/src/main/java/com/lzxnone/openloc/hook/ipc/ConfigFetcher.kt index 1a8def8..1b31482 100644 --- a/app/src/main/java/com/lzxnone/openloc/hook/ipc/ConfigFetcher.kt +++ b/app/src/main/java/com/lzxnone/openloc/hook/ipc/ConfigFetcher.kt @@ -72,6 +72,9 @@ object ConfigFetcher { isFetching.set(true) return try { + // 优先走 system_server 内存隧道(携带实时推进坐标、speed 与 bearing),失败再回退 ContentProvider + fetchFromSystemServer(context, now)?.let { return it } + val bundle = context.contentResolver.call( PROVIDER_URI, ConfigContract.METHOD_GET_CONFIG, @@ -88,10 +91,10 @@ object ConfigFetcher { isEnabled = bundle.getBoolean(ConfigContract.KEY_ENABLED, false), latitude = bundle.getDouble(ConfigContract.KEY_LATITUDE, 39.908722), longitude = bundle.getDouble(ConfigContract.KEY_LONGITUDE, 116.397496), - altitude = bundle.getDouble("altitude", 50.0), - accuracy = bundle.getFloat("accuracy", 5.0f), - bearing = bundle.getFloat("bearing", 0.0f), - speed = bundle.getFloat("speed", 0.0f), + altitude = bundle.getDouble("altitude", GlobalMockMemory.altitude), + accuracy = bundle.getFloat("accuracy", GlobalMockMemory.accuracy), + bearing = bundle.getFloat("bearing", GlobalMockMemory.bearing), + speed = bundle.getFloat("speed", GlobalMockMemory.speed), mode = mode, whitelist = parseSet(whitelistJson), blacklist = parseSet(blacklistJson), diff --git a/app/src/main/java/com/lzxnone/openloc/hook/model/GlobalMockMemory.kt b/app/src/main/java/com/lzxnone/openloc/hook/model/GlobalMockMemory.kt index e0774c7..ed87e82 100644 --- a/app/src/main/java/com/lzxnone/openloc/hook/model/GlobalMockMemory.kt +++ b/app/src/main/java/com/lzxnone/openloc/hook/model/GlobalMockMemory.kt @@ -113,13 +113,17 @@ object GlobalMockMemory { * 生成标准可用的 Mock Location 对象 */ fun createLocation(provider: String = "gps"): Location { + val currentSpeed = this@GlobalMockMemory.speed + val hasValidBearing = currentSpeed > 0.1f return Location(provider).apply { latitude = this@GlobalMockMemory.latitude longitude = this@GlobalMockMemory.longitude altitude = this@GlobalMockMemory.altitude accuracy = this@GlobalMockMemory.accuracy - bearing = this@GlobalMockMemory.bearing - speed = this@GlobalMockMemory.speed + speed = currentSpeed + if (hasValidBearing) { + bearing = this@GlobalMockMemory.bearing + } time = System.currentTimeMillis() elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos() @@ -127,7 +131,9 @@ object GlobalMockMemory { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { verticalAccuracyMeters = 1.5f speedAccuracyMetersPerSecond = 0.1f - bearingAccuracyDegrees = 1.0f + if (hasValidBearing) { + bearingAccuracyDegrees = 1.0f + } } } } diff --git a/app/src/main/java/com/lzxnone/openloc/hook/system/SystemServerHook.kt b/app/src/main/java/com/lzxnone/openloc/hook/system/SystemServerHook.kt index bcab065..ce7f251 100644 --- a/app/src/main/java/com/lzxnone/openloc/hook/system/SystemServerHook.kt +++ b/app/src/main/java/com/lzxnone/openloc/hook/system/SystemServerHook.kt @@ -36,7 +36,9 @@ object SystemServerHook { // 活跃监听器与其所属目标包名的映射表(以 IBinder 为 key,彻底消除 Class 静态闭包污染) private val activeListeners = ConcurrentHashMap() + private val activeListenerInstances = ConcurrentHashMap>() private val activeCallbacks = ConcurrentHashMap() + private val pushExecutor = java.util.concurrent.Executors.newSingleThreadExecutor() // 已挂钩 onLocationChanged 的类名集合,防止重复 Hook private val hookedListenerClasses = Collections.synchronizedSet(mutableSetOf()) @@ -90,6 +92,9 @@ object SystemServerHook { } else if (extras != null) { GlobalMockMemory.updateFromBundle(extras) TraceLogger.i("SystemServer", "Config Synced -> Enabled=${GlobalMockMemory.isEnabled}, Target=(${GlobalMockMemory.latitude}, ${GlobalMockMemory.longitude}), Mode=${GlobalMockMemory.mode}, Whitelist=${GlobalMockMemory.whitelist.size}, Blacklist=${GlobalMockMemory.blacklist.size}") + if (GlobalMockMemory.isEnabled) { + pushLocationToActiveListeners() + } param.result = true } } @@ -194,10 +199,12 @@ object SystemServerHook { val binder = (arg as? IInterface)?.asBinder() ?: (arg as? IBinder) if (binder != null) { activeListeners[binder] = pkg + activeListenerInstances[binder] = arg to provider TraceLogger.i("SystemServer", "Bound listener binder=$binder to pkg=$pkg") try { binder.linkToDeath({ activeListeners.remove(binder) + activeListenerInstances.remove(binder) TraceLogger.d("SystemServer", "Listener binder dead for pkg=$pkg") }, 0) } catch (_: Throwable) {} @@ -327,6 +334,25 @@ object SystemServerHook { } } + /** + * 1Hz 配置同步时主动向已登记的目标应用监听器推送最新坐标(解决未勾选目标软件时跑步模式不动的问题) + */ + private fun pushLocationToActiveListeners() { + if (!GlobalMockMemory.isEnabled || activeListenerInstances.isEmpty()) return + pushExecutor.execute { + for ((binder, entry) in activeListenerInstances) { + val pkg = activeListeners[binder] ?: continue + if (!GlobalMockMemory.shouldMock(pkg)) continue + if (!binder.isBinderAlive) { + activeListeners.remove(binder) + activeListenerInstances.remove(binder) + continue + } + sendInitialMockLocation(entry.first, entry.second) + } + } + } + /** * 首帧秒推辅助方法(对齐 GhostMapX Lcf.a 144-200行) */