定位信息缓存,解决首次启动拿不到定位问题

This commit is contained in:
liujing
2021-06-30 10:45:29 +08:00
parent 6f9edbc6a1
commit b3051dd07f
4 changed files with 91 additions and 13 deletions

View File

@@ -15,6 +15,7 @@ import com.mogo.cloud.httpdns.listener.IHttpDnsCurrentLocation;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.commons.constants.SharedPrefsConstants;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.network.Utils;
import com.mogo.map.location.MogoLocation;
@@ -263,13 +264,35 @@ public class MogoApplication extends AbsMogoApplication {
.getSingletonLocationClient(getApp())
.getLastKnowLocation();
Logger.i(TAG, "刷新DNS" + mogoLocation);
if (mogoLocation != null) {
return new HttpDnsSimpleLocation(
HttpDnsSimpleLocation httpDnsSimpleLocation;
if (mogoLocation != null
&& mogoLocation.getLatitude() != 0
&& mogoLocation.getLongitude() != 0) {
httpDnsSimpleLocation = new HttpDnsSimpleLocation(
mogoLocation.getCityCode(),
mogoLocation.getLatitude(),
mogoLocation.getLongitude());
Logger.i(TAG, "使用实时GPS信息" + httpDnsSimpleLocation);
} else {
String ciyCode = SharedPrefsMgr.getInstance(getApp())
.getString(SharedPrefsConstants.LOCATION_CITY_CODE);
String latitude = SharedPrefsMgr.getInstance(getApp())
.getString(SharedPrefsConstants.LOCATION_LATITUDE);
String longitude = SharedPrefsMgr.getInstance(getApp())
.getString(SharedPrefsConstants.LOCATION_LONGITUDE);
try {
httpDnsSimpleLocation = new HttpDnsSimpleLocation(
ciyCode,
Double.parseDouble(latitude),
Double.parseDouble(longitude));
} catch (NumberFormatException e) {
e.printStackTrace();
httpDnsSimpleLocation = new HttpDnsSimpleLocation("010", 1, 1);
}
Logger.i(TAG, "使用缓存GPS信息" + httpDnsSimpleLocation);
}
return new HttpDnsSimpleLocation("010", 0, 0);
return httpDnsSimpleLocation;
}
});

View File

@@ -8,6 +8,7 @@ import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.commons.constants.SharedPrefsConstants;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.network.Utils;
import com.mogo.httpdns.HttpDnsConst;
@@ -22,6 +23,7 @@ import com.mogo.httpdnshelper.sdk.listener.OnAddressChangedListener;
import com.mogo.utils.httpdns.HttpSimpleLocation;
import com.mogo.utils.logger.Logger;
import com.mogo.utils.network.HttpDns;
import com.mogo.utils.storage.SharedPrefsMgr;
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -36,6 +38,7 @@ import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP;
@Keep
@Route(path = HttpDnsConst.PATH)
public class MogoHttpDns implements IMogoHttpDns, HttpDns, OnAddressChangedListener {
private static final String TAG = "MogoHttpDns";
private HttpDnsHelper httpDnsHelper;
@Nullable
@@ -51,7 +54,7 @@ public class MogoHttpDns implements IMogoHttpDns, HttpDns, OnAddressChangedListe
@Override
public String syncGetHttpDns(String host, int type, boolean useCache) {
Logger.d("MogoHttpDns", "getHttpDnsIp host: " + host + " type: " + type);
Logger.d(TAG, "getHttpDnsIp host: " + host + " type: " + type);
Map<String, String> map = httpDnsHelper.getAllAddress();
if (useCache) {
String address = httpDnsHelper.getHttpDnsCachedAddress(type, host);
@@ -67,7 +70,7 @@ public class MogoHttpDns implements IMogoHttpDns, HttpDns, OnAddressChangedListe
@Override
public void getHttpDnsIp(String host, int type, boolean useCache, IHttpDnsCallback callback) {
Logger.d("MogoHttpDns", "getHttpDnsIp host: " + host + " type: " + type);
Logger.d(TAG, "getHttpDnsIp host: " + host + " type: " + type);
Map<String, String> map = httpDnsHelper.getAllAddress();
if (useCache) {
String address = httpDnsHelper.getHttpDnsCachedAddress(type, host);
@@ -85,7 +88,7 @@ public class MogoHttpDns implements IMogoHttpDns, HttpDns, OnAddressChangedListe
@Override
public void addHttpDnsTtlCallback(String host, int type, IHttpDnsTtlCallback callback) {
Logger.d("MogoHttpDns", "addHttpDnsTtlCallback: host: " + host + " type: " + type);
Logger.d(TAG, "addHttpDnsTtlCallback: host: " + host + " type: " + type);
if (ttlCallbackMap.get(type + "-" + host) == null) {
ttlCallbackMap.put(type + "-" + host, new ArrayList<IHttpDnsTtlCallback>());
}
@@ -104,26 +107,52 @@ public class MogoHttpDns implements IMogoHttpDns, HttpDns, OnAddressChangedListe
}
@Override
public void init(Context context, final IHttpDnsLocationChanged locationChanged) {
// .setDefaultUrl("dzt.zhidaozhixing.com")
public void init(final Context context, final IHttpDnsLocationChanged locationChanged) { // .setDefaultUrl("dzt.zhidaozhixing.com")
httpDnsHelper = new HttpDnsHelper.Builder().setContext(context).setEnv(DebugConfig.getNetMode()).setSn(Utils.getSn()).setShowDebugLog(true).setLoopCheckDelay(15 * 60 * 1000).setCurrentLocation(new IHttpDnsCurrentLocation() {
@org.jetbrains.annotations.Nullable
@Override
public HttpDnsSimpleLocation getCurrentLocation() {
HttpSimpleLocation simpleLocation = locationChanged.getLocation();
if (simpleLocation != null) {
return new HttpDnsSimpleLocation("010", simpleLocation.getLat(), simpleLocation.getLon());
HttpDnsSimpleLocation httpDnsSimpleLocation;
if (simpleLocation != null
&& simpleLocation.getLat() != 0
&& simpleLocation.getLon() != 0) {
httpDnsSimpleLocation = new HttpDnsSimpleLocation(
simpleLocation.getCityCode(),
simpleLocation.getLat(),
simpleLocation.getLon());
Logger.i(TAG, "使用实时GPS信息" + httpDnsSimpleLocation);
} else {
String ciyCode = SharedPrefsMgr.getInstance(context)
.getString(SharedPrefsConstants.LOCATION_CITY_CODE);
String latitude = SharedPrefsMgr.getInstance(context)
.getString(SharedPrefsConstants.LOCATION_LATITUDE);
String longitude = SharedPrefsMgr.getInstance(context)
.getString(SharedPrefsConstants.LOCATION_LONGITUDE);
try {
httpDnsSimpleLocation = new HttpDnsSimpleLocation(
ciyCode,
Double.parseDouble(latitude),
Double.parseDouble(longitude));
} catch (NumberFormatException e) {
e.printStackTrace();
httpDnsSimpleLocation = new HttpDnsSimpleLocation("010", 1, 1);
}
Logger.i(TAG, "使用缓存GPS信息" + httpDnsSimpleLocation);
}
return null;
return httpDnsSimpleLocation;
}
}).build();
}).
build();
httpDnsHelper.setAddressChangedListener(this);
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
String cacheIp = httpDnsHelper.getHttpDnsCachedAddress(HTTP_DNS_ADDRESS_TYPE_HTTP, hostname);
Logger.d("MogoHttpDnsLook", "lookup host: " + hostname + " cacheIp: " + cacheIp);
Logger.d(TAG, "lookup host: " + hostname + " cacheIp: " + cacheIp);
if (cacheIp == null || TextUtils.isEmpty(cacheIp)) {
cacheIp = httpDnsHelper.getHttpDnsAddress(HttpDnsHelper.HTTP_DNS_TYPE_HTTP, hostname);
Logger.d("MogoHttpDnsLook", "==== lookup host: " + hostname + " cacheIp: " + cacheIp);

View File

@@ -0,0 +1,15 @@
package com.mogo.commons.constants;
/**
* @author liujing
* @description 描述
* @since: 6/29/21
*/
public class SharedPrefsConstants {
// 当前城市编码
public static final String LOCATION_CITY_CODE = "location_city_code";
// 当前城市经纬度
public static final String LOCATION_LATITUDE = "location_latitude";
public static final String LOCATION_LONGITUDE = "location_longitude";
}

View File

@@ -16,6 +16,7 @@ import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.TextView;
import com.mogo.commons.constants.SharedPrefsConstants;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.voice.AIAssist;
import com.mogo.map.IMogoMap;
@@ -39,6 +40,7 @@ import com.mogo.realtime.entity.ADASRecognizedResult;
import com.mogo.utils.TipToast;
import com.mogo.utils.UiThreadHandler;
import com.mogo.utils.logger.Logger;
import com.mogo.utils.storage.SharedPrefsMgr;
import com.zhidaoauto.map.sdk.open.MapAutoApi;
import com.zhidaoauto.map.sdk.open.abs.MapStatusListener;
import com.zhidaoauto.map.sdk.open.abs.OnCameraChangeListener;
@@ -759,6 +761,15 @@ public class AMapViewWrapper implements IMogoMapView,
sysLocation.setBearing(location.getHeading());
sysLocation.setSpeed(location.getSpeed());
// 暂存本地提供给下一次的Http-DNS使用防止首次请求位置获取不到
SharedPrefsMgr.getInstance(mMapView.getContext())
.putString(SharedPrefsConstants.LOCATION_CITY_CODE, location.getCityCode());
SharedPrefsMgr.getInstance(mMapView.getContext())
.putString(SharedPrefsConstants.LOCATION_LATITUDE, String.valueOf(location.getLat()));
SharedPrefsMgr.getInstance(mMapView.getContext())
.putString(SharedPrefsConstants.LOCATION_LONGITUDE, String.valueOf(location.getLon()));
NaviClient.getInstance(getContext()).syncCarLocation(sysLocation);
if (checkAMapView() && mMapLoaded) {
// 地图初始化完成后每隔5s自动判断当前地图的模式