fix(android): 优化设备定位并更新原生依赖

This commit is contained in:
tongtong committed 2026-11-10 21:20:48 +08:00
1 parent 828b12d13b
commit fcbec2e075
10 files changed
+114 -83

No files matched your search

@@ -11,7 +11,7 @@ import java.io.Serializable;
* 字段与后端 {@code SnOtherInfoReq} 一一对应。
* <p>
* 注意:后端 install_time / update_time_app 为 {@code LocalDateTime},
* Jackson 默认接受 "yyyy-MM-dd'T'HH:mm:ss" 格式的字符串,
* 后端 JacksonConfig 使用 "yyyy-MM-dd HH:mm:ss" 格式的字符串,
* 因此这里用 String 承载,由采集器负责格式化,避免时间戳数字反序列化失败。
*/
public class SnOtherInfoReq implements Serializable {
@@ -4,6 +4,8 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
@@ -29,6 +31,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class MapManager {
private static final String TAG = "MapManager";
private static final long LOCATION_TIMEOUT_MS = 30_000L;
private static final String[] REQUIRED_PERMISSIONS = {
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
@@ -41,6 +44,15 @@ public class MapManager {
private final MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private LocationClient mLocationClient;
private LocationClientOption mOption;
private final Handler mMainHandler = new Handler(Looper.getMainLooper());
private boolean mLocationActive;
private final Runnable mLocationTimeout = () -> {
if (mLocationActive) {
stopLocation();
saveLocationError("定位超时");
notifyLocationFailure("定位超时");
}
};
// 线程安全的回调列表
private final List<OnLocationListener> mListeners = new CopyOnWriteArrayList<>();
@@ -110,11 +122,22 @@ public class MapManager {
public void startLocation() {
// 确保先停止再启动,避免重复启动
if (mLocationClient.isStarted()) {
mLocationClient.stop();
if (Looper.myLooper() != Looper.getMainLooper()) {
mMainHandler.post(this::startLocation);
return;
}
mLocationClient.start();
if (mLocationActive) {
return;
}
LocationClient client = getLocationClient();
if (client == null) {
return;
}
mLocationActive = true;
if (mOption.isOnceLocation()) {
mMainHandler.postDelayed(mLocationTimeout, LOCATION_TIMEOUT_MS);
}
client.start();
}
/**
@@ -178,7 +201,9 @@ public class MapManager {
if (mOption == null) {
mOption = new LocationClientOption();
mOption.setCoorType("bd09ll"); // 百度坐标系(配合百度地图使用)
mOption.setScanSpan(0); // 仅定位一次(0表示单次定位)
// SDK 9.7.0 clamps setScanSpan(0) to 1000 ms.
mOption.setOnceLocation(true);
mOption.setFirstLocType(LocationClientOption.FirstLocType.ACCURACY_IN_FIRST_LOC);
mOption.setIsNeedAddress(true); // 需要地址信息
mOption.setIsNeedLocationDescribe(true); // 需要地址描述
mOption.setNeedDeviceDirect(false); // 不需要设备方向
@@ -296,12 +321,18 @@ public class MapManager {
private final BDAbstractLocationListener mLocationListener = new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (!mLocationActive) {
return;
}
// Finish a single request before notifying consumers or receiving queued SDK callbacks.
if (location == null || mOption.isOnceLocation()) {
stopLocation();
}
if (location == null) {
String error = "定位结果为空";
Logger.e(TAG, "onReceiveLocation", error);
saveLocationError(error);
notifyLocationFailure(error);
stopLocation();
return;
}
@@ -338,11 +369,6 @@ public class MapManager {
notifyLocationFailure(unknownError);
break;
}
// 单次定位完成后停止(如果是单次定位模式)
if (mOption != null && mOption.getScanSpan() == 0) {
stopLocation();
}
}
@Override
@@ -350,8 +376,6 @@ public class MapManager {
super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
String errorMsg = "定位诊断: " + diagnosticMessage;
Logger.e(TAG, "onLocDiagnosticMessage", errorMsg);
saveLocationError(errorMsg);
notifyLocationFailure(errorMsg);
}
};
@@ -359,7 +383,13 @@ public class MapManager {
* 停止定位并释放资源
*/
public void stopLocation() {
if (mLocationClient != null && mLocationClient.isStarted()) {
if (Looper.myLooper() != Looper.getMainLooper()) {
mMainHandler.post(this::stopLocation);
return;
}
mLocationActive = false;
mMainHandler.removeCallbacks(mLocationTimeout);
if (mLocationClient != null) {
mLocationClient.stop();
}
}
@@ -368,6 +398,10 @@ public class MapManager {
* 销毁定位客户端(建议在Application退出时调用)
*/
public void destroy() {
if (Looper.myLooper() != Looper.getMainLooper()) {
mMainHandler.post(this::destroy);
return;
}
stopLocation();
if (mLocationClient != null) {
mLocationClient.unRegisterLocationListener(mLocationListener);
@@ -78,7 +78,7 @@ public class DeviceInfoCollector {
/**
* 后端 LocalDateTime 默认接受的 ISO 格式。
*/
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 常见的 su 二进制路径,用于 Root 检测。
@@ -225,7 +225,7 @@ public class SherpaOnnxTtsManager {
mAudioTrack.flush();
mAudioTrack.play();
GenerationConfig genConfig = new GenerationConfig(sid, speed, 0, null, 0, null, 5, null);
GenerationConfig genConfig = new GenerationConfig(sid, speed, 0, null, 0, null, 5, null, null, null);
if (mSupertonic) {
Map<String, String> extra = new HashMap<>();
extra.put("lang", supertonicLang);
@@ -268,7 +268,7 @@ public class SherpaOnnxTtsManager {
if (mOfflineTts == null) initTts(context.getApplicationContext());
File output = new File(context.getCacheDir(), "hourly-chime.wav");
GeneratedAudio audio = mOfflineTts.generateWithConfigAndCallback(text,
new GenerationConfig(0, 1.0f, 0, null, 0, null, 5, null),
new GenerationConfig(0, 1.0f, 0, null, 0, null, 5, null, null, null),
samples -> 1);
if (!audio.save(output.getAbsolutePath())) throw new IOException("语音生成失败");
callback.onCompleted(output.getAbsolutePath());