feat: 添加自定义GPS位置支持,更新相关脚本和配置,移除旧的GPS实现

This commit is contained in:
2025-09-17 12:13:58 +08:00
parent 97dbd94b3a
commit df8fec5bbe
8 changed files with 306 additions and 1582 deletions

5
.gitignore vendored
View File

@@ -97,7 +97,8 @@ dkms.conf
.LSOverride
# Icon must end with two \r
Icon
Icon
# Thumbnails
._*
@@ -139,3 +140,5 @@ tags
[._]*.un~
# End of https://www.toptal.com/developers/gitignore/api/C,C++,Linux,macOS,vim
.idea
.vscode

View File

@@ -0,0 +1,160 @@
--- a/frameworks/base/services/core/jni/com_android_server_location_GnssLocationProvider.cpp
+++ b/frameworks/base/services/core/jni/com_android_server_location_GnssLocationProvider.cpp
@@ -66,6 +66,11 @@
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"
+#include <fstream>
+#include <sstream>
+#include <sys/stat.h>
+
+// Custom GPS location from file support
static jclass class_gnssPowerStats;
@@ -78,6 +83,15 @@
static jmethodID method_gnssPowerStatsCtor;
static jmethodID method_setSubHalPowerIndicationCapabilities;
+// Custom GPS location from file
+static double customLatitude = 0.0;
+static double customLongitude = 0.0;
+static bool useCustomLocation = false;
+static time_t lastFileModTime = 0;
+static const char* GPS_LOCATION_FILE = "/data/vendor/gps/custom_location.txt";
+
+// Function to read GPS coordinates from file
+static bool readGpsLocationFromFile();
+
using android::OK;
using android::sp;
using android::status_t;
@@ -200,6 +214,64 @@
std::unique_ptr<android::gnss::MeasurementCorrectionsInterface> gnssMeasurementCorrectionsIface =
nullptr;
+// Function to read GPS coordinates from file
+static bool readGpsLocationFromFile() {
+ struct stat fileStat;
+ if (stat(GPS_LOCATION_FILE, &fileStat) != 0) {
+ // File doesn't exist, disable custom location
+ useCustomLocation = false;
+ return false;
+ }
+
+ // Check if file has been modified
+ if (fileStat.st_mtime <= lastFileModTime) {
+ return useCustomLocation; // No change in file
+ }
+
+ std::ifstream file(GPS_LOCATION_FILE);
+ if (!file.is_open()) {
+ ALOGE("Failed to open GPS location file: %s", GPS_LOCATION_FILE);
+ useCustomLocation = false;
+ return false;
+ }
+
+ std::string line;
+ if (std::getline(file, line)) {
+ std::istringstream iss(line);
+ std::string latStr, lonStr;
+
+ // Expected format: "latitude,longitude" or "latitude longitude"
+ if (std::getline(iss, latStr, ',') && std::getline(iss, lonStr)) {
+ // Comma-separated format
+ } else {
+ // Space-separated format
+ iss.clear();
+ iss.str(line);
+ iss >> latStr >> lonStr;
+ }
+
+ try {
+ double lat = std::stod(latStr);
+ double lon = std::stod(lonStr);
+
+ // Validate coordinates
+ if (lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0) {
+ customLatitude = lat;
+ customLongitude = lon;
+ useCustomLocation = true;
+ lastFileModTime = fileStat.st_mtime;
+ ALOGI("Custom GPS location loaded: lat=%.6f, lon=%.6f", lat, lon);
+ file.close();
+ return true;
+ } else {
+ ALOGE("Invalid GPS coordinates: lat=%.6f, lon=%.6f", lat, lon);
+ }
+ } catch (const std::exception& e) {
+ ALOGE("Failed to parse GPS coordinates: %s", e.what());
+ }
+ }
+
+ file.close();
+ useCustomLocation = false;
+ return false;
+}
+
namespace android {
namespace {
@@ -367,6 +439,9 @@
}
static jboolean android_location_gnss_hal_GnssNative_start(JNIEnv* /* env */, jclass) {
+ // Try to read custom GPS location from file when GNSS starts
+ readGpsLocationFromFile();
+
return gnssHal->start();
}
@@ -443,8 +518,17 @@
jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees, jlong timestamp,
jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
jdouble elapsedRealtimeUncertaintyNanos) {
- gnssHal->injectBestLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
+
+ // Check for custom GPS location from file
+ if (readGpsLocationFromFile()) {
+ ALOGI("Using custom GPS location: lat=%.6f, lon=%.6f", customLatitude, customLongitude);
+ gnssHal->injectBestLocation(gnssLocationFlags, customLatitude, customLongitude,
+ altitudeMeters, speedMetersPerSec, bearingDegrees,
+ horizontalAccuracyMeters, verticalAccuracyMeters,
+ speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp,
+ elapsedRealtimeFlags, elapsedRealtimeNanos,
+ elapsedRealtimeUncertaintyNanos);
+ } else {
+ gnssHal->injectBestLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
altitudeMeters, speedMetersPerSec, bearingDegrees,
horizontalAccuracyMeters, verticalAccuracyMeters,
speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp,
elapsedRealtimeFlags, elapsedRealtimeNanos,
elapsedRealtimeUncertaintyNanos);
+ }
}
static void android_location_gnss_hal_GnssNative_inject_location(
@@ -455,8 +539,17 @@
jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees, jlong timestamp,
jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
jdouble elapsedRealtimeUncertaintyNanos) {
- gnssHal->injectLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees, altitudeMeters,
+
+ // Check for custom GPS location from file
+ if (readGpsLocationFromFile()) {
+ ALOGI("Using custom GPS location: lat=%.6f, lon=%.6f", customLatitude, customLongitude);
+ gnssHal->injectLocation(gnssLocationFlags, customLatitude, customLongitude, altitudeMeters,
+ speedMetersPerSec, bearingDegrees, horizontalAccuracyMeters,
+ verticalAccuracyMeters, speedAccuracyMetersPerSecond,
+ bearingAccuracyDegrees, timestamp, elapsedRealtimeFlags,
+ elapsedRealtimeNanos, elapsedRealtimeUncertaintyNanos);
+ } else {
+ gnssHal->injectLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees, altitudeMeters,
speedMetersPerSec, bearingDegrees, horizontalAccuracyMeters,
verticalAccuracyMeters, speedAccuracyMetersPerSecond,
bearingAccuracyDegrees, timestamp, elapsedRealtimeFlags,
elapsedRealtimeNanos, elapsedRealtimeUncertaintyNanos);
+ }
}
static jboolean android_location_gnss_hal_GnssNative_supports_psds(JNIEnv* /* env */, jclass) {

View File

@@ -1,67 +0,0 @@
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
cc_library_shared {
name: "libredroid_gps_jni",
srcs: [
"com_android_server_location_GnssLocationProvider.cpp",
],
cflags: [
"-Wall",
"-Werror",
"-Wextra",
"-std=c++17",
],
shared_libs: [
"libandroid_runtime",
"libbinder",
"libcutils",
"liblog",
"libnativehelper",
"libutils",
"libhardware",
"libhardware_legacy",
"libhidlbase",
"libhidltransport",
"android.hardware.gnss@1.0",
"android.hardware.gnss@1.1",
"android.hardware.gnss@2.0",
"android.hardware.gnss@2.1",
"android.hardware.gnss-V1-ndk",
"android.hardware.gnss-V1-cpp",
],
static_libs: [
"libc++fs",
],
header_libs: [
"jni_headers",
// "libandroid_runtime_headers",
],
include_dirs: [
"frameworks/base/services/core/jni",
"frameworks/base/core/jni",
],
vendor: true,
compile_multilib: "both",
relative_install_path: "hw",
}

View File

@@ -1,196 +0,0 @@
# Redroid 自定义GPS位置构建指南
本指南详细说明如何构建和使用Redroid的自定义GPS位置功能。
## 📋 修改的文件列表
### 1. 核心实现文件
- `gps/com_android_server_location_GnssLocationProvider.cpp` - 主要的JNI实现
- `gps/Android.bp` - 编译配置文件(新增)
- `gps/init.redroid.gps.sh` - GPS初始化脚本
### 2. 集成配置文件
- `vendor.mk` - 添加GPS JNI库到产品包
- `redroid.legacy.rc` - 调整GPS目录权限
## 🔧 构建步骤
### 1. 准备源码
确保所有修改的文件都在正确的位置:
```bash
vendor/redroid/
├── gps/
│ ├── com_android_server_location_GnssLocationProvider.cpp
│ ├── Android.bp
│ └── init.redroid.gps.sh
├── vendor.mk
└── redroid.legacy.rc
```
### 2. 编译系统
在Android源码根目录执行
```bash
# 设置环境
source build/envsetup.sh
# 选择目标(根据你的设备调整)
lunch aosp_arm64-eng
# 编译GPS JNI库
m libredroid_gps_jni
# 或者编译整个vendor模块
m vendor-redroid
```
### 3. 验证编译结果
检查编译输出:
```bash
# 检查生成的库文件
ls -la out/target/product/*/vendor/lib*/hw/libredroid_gps_jni.so
```
## 🚀 部署和使用
### 1. 系统启动
当Android系统启动时`redroid.legacy.rc`会自动:
- 创建`/data/vendor/gps`目录
- 执行`init.redroid.gps.sh`脚本
- 设置正确的权限
### 2. 默认位置
系统会创建默认的GPS位置文件
```bash
/data/vendor/gps/custom_location.txt
```
默认坐标(杭州):`30.281026,120.019348`
### 3. 自定义位置
修改GPS位置只需编辑文件
```bash
# 使用adb连接设备
adb shell
# 修改GPS位置例如北京天安门
echo "39.904989,116.405285" > /data/vendor/gps/custom_location.txt
# 或者使用adb push
echo "39.904989,116.405285" > custom_location.txt
adb push custom_location.txt /data/vendor/gps/
```
### 4. 实时生效
- 修改文件后新的GPS位置会在下次位置请求时自动生效
- 无需重启系统或GPS服务
- 系统会检测文件修改时间并自动加载新坐标
## 📍 支持的坐标格式
### 逗号分隔格式
```
纬度,经度
39.904989,116.405285
```
### 空格分隔格式
```
纬度 经度
39.904989 116.405285
```
### 坐标范围
- 纬度:-90° 到 +90°
- 经度:-180° 到 +180°
## 🧪 测试验证
### 1. 查看日志
```bash
# 查看GPS相关日志
adb logcat | grep -i gnss
adb logcat | grep -i "custom.*gps"
# 期望看到的日志:
# I/GnssLocationProviderJni: Custom GPS location loaded: lat=39.904989, lon=116.405285
# I/GnssLocationProviderJni: Using custom GPS location: lat=39.904989, lon=116.405285
```
### 2. 使用GPS应用
- 打开地图应用如Google Maps
- 启用位置服务
- 应该显示自定义的GPS位置
### 3. 验证位置变更
```bash
# 修改位置
echo "31.239666,121.499809" > /data/vendor/gps/custom_location.txt
# 在地图应用中观察位置变化
```
## 🔍 故障排除
### 编译错误
1. 确保Android编译环境正确设置
2. 检查依赖库是否存在
3. 验证Android.bp语法
### 运行时错误
1. 检查文件权限:
```bash
ls -la /data/vendor/gps/
```
2. 验证文件格式:
```bash
cat /data/vendor/gps/custom_location.txt
```
3. 查看详细日志:
```bash
adb logcat -v threadtime | grep -E "(Gnss|GPS)"
```
### 权限问题
如果遇到权限错误:
```bash
# 重新设置权限
adb shell
chown system:system /data/vendor/gps/custom_location.txt
chmod 644 /data/vendor/gps/custom_location.txt
```
## 📱 兼容性
### Android版本
- 支持Android 8.0+API 26+
- 测试过的版本Android 11、12、13
### 架构支持
- ARM64
- ARM32
- x86_64通过Android.bp的compile_multilib配置
## 🔄 回退到默认GPS
如果要禁用自定义GPS功能
```bash
# 删除自定义位置文件
rm /data/vendor/gps/custom_location.txt
# 系统会自动回退到默认GPS行为
```
## 📞 技术支持
如果遇到问题,请提供:
1. Android版本和设备信息
2. 完整的logcat日志
3. 文件权限和内容截图
4. 具体的错误现象描述

File diff suppressed because it is too large Load Diff

View File

@@ -1,82 +0,0 @@
#!/bin/bash
# Redroid GPS位置设置脚本
# 用法: ./set_gps_location.sh <纬度> <经度>
# 示例: ./set_gps_location.sh 39.904989 116.405285
if [ $# -ne 2 ]; then
echo "用法: $0 <纬度> <经度>"
echo ""
echo "示例位置:"
echo " 北京天安门: $0 39.904989 116.405285"
echo " 上海外滩: $0 31.239666 121.499809"
echo " 广州塔: $0 23.109831 113.324504"
echo " 深圳地王: $0 22.544249 114.095298"
echo " 杭州西湖: $0 30.281026 120.019348"
echo ""
echo "坐标范围:"
echo " 纬度: -90 到 90"
echo " 经度: -180 到 180"
exit 1
fi
LATITUDE=$1
LONGITUDE=$2
# 验证纬度范围
if (( $(echo "$LATITUDE < -90.0 || $LATITUDE > 90.0" | bc -l) )); then
echo "错误: 纬度必须在 -90 到 90 之间"
exit 1
fi
# 验证经度范围
if (( $(echo "$LONGITUDE < -180.0 || $LONGITUDE > 180.0" | bc -l) )); then
echo "错误: 经度必须在 -180 到 180 之间"
exit 1
fi
GPS_FILE="/data/vendor/gps/custom_location.txt"
echo "设置GPS位置为: 纬度 $LATITUDE, 经度 $LONGITUDE"
# 检查设备连接
if ! adb devices | grep -q "device$"; then
echo "错误: 未找到连接的Android设备"
echo "请确保:"
echo "1. 设备已连接并启用USB调试"
echo "2. 已授权计算机的USB调试请求"
exit 1
fi
# 创建临时文件
TEMP_FILE=$(mktemp)
echo "$LATITUDE,$LONGITUDE" > "$TEMP_FILE"
# 推送到设备
echo "正在更新GPS位置文件..."
adb push "$TEMP_FILE" "$GPS_FILE"
if [ $? -eq 0 ]; then
echo "✅ GPS位置更新成功"
# 设置正确权限
adb shell "chown system:system $GPS_FILE"
adb shell "chmod 644 $GPS_FILE"
# 验证文件内容
echo "📍 当前GPS位置"
adb shell "cat $GPS_FILE"
echo ""
echo "💡 提示:"
echo "- 新位置将在下次GPS请求时生效"
echo "- 打开地图应用验证位置是否正确"
echo "- 查看日志: adb logcat | grep -i 'custom.*gps'"
else
echo "❌ GPS位置更新失败"
echo "请检查设备权限和连接状态"
fi
# 清理临时文件
rm -f "$TEMP_FILE"

140
set_custom_gps.sh Normal file
View File

@@ -0,0 +1,140 @@
#!/bin/bash
# Android自定义GPS位置设置脚本
# 适用于修改了frameworks/base的Android系统
# 用法: ./set_custom_gps.sh <纬度> <经度>
# 示例: ./set_custom_gps.sh 39.904989 116.405285
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
if [ $# -ne 2 ]; then
echo -e "${RED}用法: $0 <纬度> <经度>${NC}"
echo ""
echo -e "${BLUE}示例位置:${NC}"
echo -e " ${GREEN}北京天安门:${NC} $0 39.904989 116.405285"
echo -e " ${GREEN}上海外滩:${NC} $0 31.239666 121.499809"
echo -e " ${GREEN}广州塔:${NC} $0 23.109831 113.324504"
echo -e " ${GREEN}深圳地王:${NC} $0 22.544249 114.095298"
echo -e " ${GREEN}杭州西湖:${NC} $0 30.281026 120.019348"
echo -e " ${GREEN}东京塔:${NC} $0 35.658651 139.745415"
echo -e " ${GREEN}纽约时代广场:${NC} $0 40.758896 -73.985130"
echo ""
echo -e "${YELLOW}坐标范围:${NC}"
echo -e " 纬度: -90 到 90"
echo -e " 经度: -180 到 180"
exit 1
fi
LATITUDE=$1
LONGITUDE=$2
# 验证数字格式
if ! [[ $LATITUDE =~ ^-?[0-9]+\.?[0-9]*$ ]]; then
echo -e "${RED}错误: 纬度必须是有效的数字${NC}"
exit 1
fi
if ! [[ $LONGITUDE =~ ^-?[0-9]+\.?[0-9]*$ ]]; then
echo -e "${RED}错误: 经度必须是有效的数字${NC}"
exit 1
fi
# 验证坐标范围
if (( $(echo "$LATITUDE < -90.0 || $LATITUDE > 90.0" | bc -l 2>/dev/null || echo "1") )); then
echo -e "${RED}错误: 纬度必须在 -90 到 90 之间${NC}"
exit 1
fi
if (( $(echo "$LONGITUDE < -180.0 || $LONGITUDE > 180.0" | bc -l 2>/dev/null || echo "1") )); then
echo -e "${RED}错误: 经度必须在 -180 到 180 之间${NC}"
exit 1
fi
GPS_FILE="/data/vendor/gps/custom_location.txt"
GPS_DIR="/data/vendor/gps"
echo -e "${BLUE}🌍 设置GPS位置为: 纬度 ${GREEN}$LATITUDE${BLUE}, 经度 ${GREEN}$LONGITUDE${NC}"
# 检查ADB连接
if ! command -v adb >/dev/null 2>&1; then
echo -e "${RED}❌ 错误: 未找到adb命令${NC}"
echo -e "${YELLOW}请确保Android SDK已安装并添加到PATH${NC}"
exit 1
fi
# 检查设备连接
DEVICE_COUNT=$(adb devices | grep -c "device$")
if [ "$DEVICE_COUNT" -eq 0 ]; then
echo -e "${RED}❌ 错误: 未找到连接的Android设备${NC}"
echo -e "${YELLOW}请确保:${NC}"
echo "1. 设备已连接并启用USB调试"
echo "2. 已授权计算机的USB调试请求"
echo "3. 使用 'adb devices' 验证设备连接"
exit 1
elif [ "$DEVICE_COUNT" -gt 1 ]; then
echo -e "${YELLOW}⚠️ 检测到多个设备,使用第一个设备${NC}"
fi
# 创建临时文件
TEMP_FILE=$(mktemp)
echo "$LATITUDE,$LONGITUDE" > "$TEMP_FILE"
echo -e "${BLUE}📁 正在创建GPS目录...${NC}"
adb shell "mkdir -p $GPS_DIR" 2>/dev/null
echo -e "${BLUE}📤 正在上传GPS位置文件...${NC}"
adb push "$TEMP_FILE" "$GPS_FILE" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ GPS位置文件上传成功${NC}"
# 设置正确权限
echo -e "${BLUE}🔐 正在设置文件权限...${NC}"
adb shell "chown system:system $GPS_FILE" 2>/dev/null
adb shell "chmod 644 $GPS_FILE" 2>/dev/null
# 验证文件内容
echo -e "${BLUE}📍 当前GPS位置配置${NC}"
CURRENT_LOCATION=$(adb shell "cat $GPS_FILE" 2>/dev/null | tr -d '\r\n')
if [ -n "$CURRENT_LOCATION" ]; then
echo -e " ${GREEN}$CURRENT_LOCATION${NC}"
else
echo -e " ${RED}无法读取配置文件${NC}"
fi
echo ""
echo -e "${GREEN}🎉 GPS位置设置完成${NC}"
echo ""
echo -e "${YELLOW}💡 使用提示:${NC}"
echo "• 新位置将在下次GPS请求时生效"
echo "• 重启GPS服务或应用以立即生效"
echo "• 打开地图应用验证位置是否正确"
echo ""
echo -e "${BLUE}🔍 调试命令:${NC}"
echo "• 查看GPS日志: ${GREEN}adb logcat | grep -i 'custom.*gps'${NC}"
echo "• 删除自定义位置: ${GREEN}adb shell 'rm $GPS_FILE'${NC}"
echo "• 重启位置服务: ${GREEN}adb shell 'setprop ctl.restart location'${NC}"
else
echo -e "${RED}❌ GPS位置文件上传失败${NC}"
echo -e "${YELLOW}可能的原因:${NC}"
echo "1. 设备没有root权限"
echo "2. 系统分区为只读"
echo "3. SELinux策略限制"
echo ""
echo -e "${BLUE}尝试解决方案:${NC}"
echo "• 确保设备已获得root权限"
echo "• 尝试: adb root && adb remount"
echo "• 检查SELinux设置: adb shell getenforce"
fi
# 清理临时文件
rm -f "$TEMP_FILE"
echo ""
echo -e "${BLUE}📱 支持的Android版本: Android 8.0+ (API Level 26+)${NC}"
echo -e "${BLUE}🔧 需要: 已修改frameworks/base的自定义Android系统${NC}"

View File

@@ -44,11 +44,8 @@ PRODUCT_COPY_FILES += \
######################
# gps config
######################
PRODUCT_PACKAGES += \
libredroid_gps_jni
# PRODUCT_PACKAGES += \
# android.hardware.gnss-service
# GPS functionality now integrated into frameworks/base
# No vendor overlay needed
PRODUCT_COPY_FILES += \
vendor/redroid/gps/init.redroid.gps.sh:$(TARGET_COPY_OUT_VENDOR)/bin/hw/init.redroid.gps.sh \