Fix first app request http dns is wrong
This commit is contained in:
@@ -16,6 +16,7 @@ import com.mogo.cloud.passport.IMoGoTokenCallback;
|
||||
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;
|
||||
@@ -192,14 +193,34 @@ public class MogoApplication extends AbsMogoApplication {
|
||||
.getApis().getMapServiceApi()
|
||||
.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;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
|
||||
|
||||
import com.alibaba.android.arouter.facade.annotation.Route;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
import com.mogo.commons.constants.SharedPrefsConstants;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.commons.network.Utils;
|
||||
import com.mogo.httpdns.HttpDnsConst;
|
||||
@@ -23,6 +24,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;
|
||||
@@ -37,6 +39,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
|
||||
@@ -52,7 +55,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);
|
||||
@@ -68,7 +71,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);
|
||||
@@ -86,7 +89,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>());
|
||||
}
|
||||
@@ -105,17 +108,42 @@ 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) {
|
||||
httpDnsHelper = new HttpDnsHelper.Builder().setContext(context).setEnv(DebugConfig.getNetMode()).setSn(MoGoAiCloudClientConfig.getInstance().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();
|
||||
httpDnsHelper.setAddressChangedListener(this);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.mogo.commons.constants;
|
||||
|
||||
/**
|
||||
* @author xiaoyuzhou
|
||||
* @date 2021/6/24 5:09 下午
|
||||
* <p>
|
||||
* 本地存储使用的常量
|
||||
*/
|
||||
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";
|
||||
|
||||
}
|
||||
@@ -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.map.IMogoMap;
|
||||
import com.mogo.map.IMogoMapView;
|
||||
@@ -37,6 +38,7 @@ import com.mogo.map.uicontroller.VisualAngleMode;
|
||||
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;
|
||||
@@ -769,6 +771,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自动判断当前地图的模式
|
||||
|
||||
Reference in New Issue
Block a user