From f2589150e9747b761f21d72293492d0dd5304072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=AE=8F=E5=AE=87?= Date: Thu, 24 Jun 2021 18:02:52 +0800 Subject: [PATCH] Fix first app request http dns is wrong --- .../com/mogo/launcher/MogoApplication.java | 29 ++++++++++-- .../com/mogo/httpdns/mogo/MogoHttpDns.java | 44 +++++++++++++++---- .../constants/SharedPrefsConstants.java | 17 +++++++ .../mogo/map/impl/custom/AMapViewWrapper.java | 11 +++++ 4 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 foudations/mogo-commons/src/main/java/com/mogo/commons/constants/SharedPrefsConstants.java diff --git a/app/src/main/java/com/mogo/launcher/MogoApplication.java b/app/src/main/java/com/mogo/launcher/MogoApplication.java index a9f0f172b1..453037681a 100644 --- a/app/src/main/java/com/mogo/launcher/MogoApplication.java +++ b/app/src/main/java/com/mogo/launcher/MogoApplication.java @@ -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; } }); diff --git a/foudations/httpdns-mogo/src/main/java/com/mogo/httpdns/mogo/MogoHttpDns.java b/foudations/httpdns-mogo/src/main/java/com/mogo/httpdns/mogo/MogoHttpDns.java index 3d068539e3..76dc9a700b 100644 --- a/foudations/httpdns-mogo/src/main/java/com/mogo/httpdns/mogo/MogoHttpDns.java +++ b/foudations/httpdns-mogo/src/main/java/com/mogo/httpdns/mogo/MogoHttpDns.java @@ -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 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 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()); } @@ -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); diff --git a/foudations/mogo-commons/src/main/java/com/mogo/commons/constants/SharedPrefsConstants.java b/foudations/mogo-commons/src/main/java/com/mogo/commons/constants/SharedPrefsConstants.java new file mode 100644 index 0000000000..1ead72700c --- /dev/null +++ b/foudations/mogo-commons/src/main/java/com/mogo/commons/constants/SharedPrefsConstants.java @@ -0,0 +1,17 @@ +package com.mogo.commons.constants; + +/** + * @author xiaoyuzhou + * @date 2021/6/24 5:09 下午 + *

+ * 本地存储使用的常量 + */ +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"; + +} diff --git a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java index fa50c2e6bf..ace1c26a15 100644 --- a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java +++ b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java @@ -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自动判断当前地图的模式