diff --git a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/location/ALocationClient.java b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/location/ALocationClient.java index a551c302bc..89dfeac7f4 100644 --- a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/location/ALocationClient.java +++ b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/location/ALocationClient.java @@ -26,6 +26,7 @@ import java.util.Set; public class ALocationClient implements IMogoLocationClient { private static final String TAG = "LocationClient"; + private final Context mContext; private Set< IMogoLocationListener > sListeners = new HashSet<>( 10 ); private MogoLocation mLastLocation; @@ -34,12 +35,7 @@ public class ALocationClient implements IMogoLocationClient { private boolean mIsDestroyed = false; public ALocationClient( Context context ) { - mClient = new AMapLocationClient( context ); - mClient.setLocationListener( mListener ); - mLastLocation = ObjectUtils.fromAMap( mClient.getLastKnownLocation() ); - if ( mLastLocation == null ) { - mLastLocation = new MogoLocation(); - } + mContext = context; } private AMapLocationClient mClient; @@ -51,11 +47,12 @@ public class ALocationClient implements IMogoLocationClient { @Override public void start( long interval ) { - if ( mIsDestroyed ) { - destroyWarming(); - return; + if ( mClient == null ) { + mClient = new AMapLocationClient( mContext ); + mClient.setLocationListener( mListener ); + mLastLocation = ObjectUtils.fromAMap( mClient.getLastKnownLocation() ); } - if ( mClient != null ) { + if ( !mClient.isStarted() ) { AMapLocationClientOption option = new AMapLocationClientOption(); option.setLocationMode( AMapLocationClientOption.AMapLocationMode.Hight_Accuracy ); option.setNeedAddress( true ); @@ -109,6 +106,9 @@ public class ALocationClient implements IMogoLocationClient { destroyWarming(); return null; } + if ( mLastLocation == null ) { + mLastLocation = new MogoLocation(); + } return mLastLocation; } @@ -140,7 +140,7 @@ public class ALocationClient implements IMogoLocationClient { aMapLocation.getLongitude() == 0.0D ) { return; } - Trace.beginSection("timer.onLocationChanged"); + Trace.beginSection( "timer.onLocationChanged" ); mLastLocation = ObjectUtils.fromAMap( aMapLocation ); synchronized ( sListeners ) { Iterator< IMogoLocationListener > listenerIterator = sListeners.iterator(); diff --git a/modules/mogo-module-extensions/src/main/java/com/mogo/module/extensions/entrance/EntranceFragment.java b/modules/mogo-module-extensions/src/main/java/com/mogo/module/extensions/entrance/EntranceFragment.java index 29508a207e..726b1ed0a6 100644 --- a/modules/mogo-module-extensions/src/main/java/com/mogo/module/extensions/entrance/EntranceFragment.java +++ b/modules/mogo-module-extensions/src/main/java/com/mogo/module/extensions/entrance/EntranceFragment.java @@ -75,7 +75,7 @@ public class EntranceFragment extends MvpFragment { MogoServices.getInstance().init( AbsMogoApplication.getApp() ); }, 5_000L ); diff --git a/modules/mogo-module-service/src/main/java/com/mogo/module/service/MogoServices.java b/modules/mogo-module-service/src/main/java/com/mogo/module/service/MogoServices.java index 046bc4b0be..7e44a00665 100644 --- a/modules/mogo-module-service/src/main/java/com/mogo/module/service/MogoServices.java +++ b/modules/mogo-module-service/src/main/java/com/mogo/module/service/MogoServices.java @@ -56,6 +56,7 @@ import com.mogo.service.statusmanager.IMogoStatusChangedListener; import com.mogo.service.statusmanager.IMogoStatusManager; import com.mogo.service.statusmanager.StatusDescriptor; import com.mogo.utils.TipToast; +import com.mogo.utils.UiThreadHandler; import com.mogo.utils.WorkThreadHandler; import com.mogo.utils.logger.Logger; @@ -63,6 +64,8 @@ import org.json.JSONObject; import java.util.List; +import retrofit2.http.HEAD; + /** * @author congtaowang * @since 2020-01-03 @@ -273,12 +276,13 @@ public class MogoServices implements IMogoMapListener, private boolean mIsMainPageFirstResume = true; + private boolean mIsFirstAccOn = true; + public void init( Context context ) { mContext = context; initWorkThread(); - MarkerServiceHandler.init( mContext ); mRefreshModel = new RefreshModel( context ); mMogoMapService = MarkerServiceHandler.getMapService(); mUiController = mMogoMapService.getMapUIController(); @@ -366,6 +370,7 @@ public class MogoServices implements IMogoMapListener, if ( msg.obj instanceof RefreshObject ) { RefreshObject ro = ( ( RefreshObject ) msg.obj ); if ( ro.mLonLat == null ) { + Logger.w( TAG, "lonLat is null." ); return; } mRefreshModel.refreshData( ro.mLonLat, ro.mRadius, ro.mAmount, ro.mCallback ); @@ -619,9 +624,7 @@ public class MogoServices implements IMogoMapListener, // 自动刷新触发 final MogoLatLng point = new MogoLatLng( location.getLatitude(), location.getLongitude() ); if ( mLastAutoRefreshLocation == null ) { - mLastAutoRefreshLocation = point; - mLoopRequest = true; - notifyRefreshData( mLastAutoRefreshLocation, getQueryRadius(), mAutoRefreshCallback ); + startFirstLocationRequest( point ); return; } float distance = Utils.calculateLineDistance( mLastAutoRefreshLocation, point ); @@ -635,8 +638,27 @@ public class MogoServices implements IMogoMapListener, } } + /** + * 首次定位成功后,执行道路事件的刷新 + * + * @param point + */ + private void startFirstLocationRequest( MogoLatLng point ) { + mLastAutoRefreshLocation = point; + mLoopRequest = true; + notifyRefreshData( mLastAutoRefreshLocation, getQueryRadius(), mAutoRefreshCallback ); + } + @Override public void onCarLocationChanged2( Location latLng ) { + if ( latLng == null ) { + return; + } + // poi 定位无法获取时,使用该定位 + if ( mLastAutoRefreshLocation == null ) { + MogoLatLng point = new MogoLatLng( latLng.getLatitude(), latLng.getLongitude() ); + startFirstLocationRequest( point ); + } } @Override @@ -717,18 +739,20 @@ public class MogoServices implements IMogoMapListener, break; case ACC_STATUS: if ( isTrue ) { + if ( mIsFirstAccOn ) { + mIsFirstAccOn = false; + return; + } initLocationServiceProcess( mContext ); mADASController.showADAS(); - refreshStrategy(); MarkerServiceHandler.getApis().getMapServiceApi().getSingletonLocationClient( mContext ).start(); + MarkerServiceHandler.getApis().getMapServiceApi().getMarkerManager( mContext ).removeMarkers(); + UiThreadHandler.postDelayed( () -> { + refreshStrategy(); + }, 3_000L ); } else { - try { - MarkerServiceHandler.getApis().getMapServiceApi().getMarkerManager( mContext ).removeMarkers(); - MarkerServiceHandler.getApis().getMapServiceApi().getSingletonLocationClient( mContext ).stop(); - } catch ( Exception e ) { - Logger.e( TAG, e, "error." ); - } - refreshStrategy(); + MarkerServiceHandler.getApis().getMapServiceApi().getSingletonLocationClient( mContext ).stop(); + MarkerServiceHandler.getApis().getMapServiceApi().getMarkerManager( mContext ).removeMarkers(); } break; } diff --git a/modules/mogo-module-service/src/main/java/com/mogo/module/service/intent/AccStatusIntentHandler.java b/modules/mogo-module-service/src/main/java/com/mogo/module/service/intent/AccStatusIntentHandler.java index 5a9def8162..38a6de48b4 100644 --- a/modules/mogo-module-service/src/main/java/com/mogo/module/service/intent/AccStatusIntentHandler.java +++ b/modules/mogo-module-service/src/main/java/com/mogo/module/service/intent/AccStatusIntentHandler.java @@ -3,10 +3,11 @@ package com.mogo.module.service.intent; import android.content.Context; import android.content.Intent; +import com.mogo.module.common.utils.CarSeries; import com.mogo.module.service.MarkerServiceHandler; import com.mogo.module.service.ServiceConst; import com.mogo.module.service.receiver.AccStatusReceiver; -import com.mogo.module.service.receiver.MogoReceiver; +import com.mogo.utils.TipToast; import com.mogo.utils.logger.Logger; public @@ -18,16 +19,21 @@ public */ class AccStatusIntentHandler implements IntentHandler { + public static final byte ACC_ON = 1; + public static final byte ACC_OFF = 0; + public static final byte ACC_OFF_DELAY = 3; + private static final String TAG = "AccStatusIntentHandler"; private static volatile AccStatusIntentHandler sInstance; - private AccStatusIntentHandler(){} + private AccStatusIntentHandler() { + } - public static AccStatusIntentHandler getInstance(){ - if( sInstance == null ){ - synchronized( AccStatusIntentHandler.class ) { - if( sInstance == null ){ + public static AccStatusIntentHandler getInstance() { + if ( sInstance == null ) { + synchronized ( AccStatusIntentHandler.class ) { + if ( sInstance == null ) { sInstance = new AccStatusIntentHandler(); } } @@ -35,7 +41,7 @@ class AccStatusIntentHandler implements IntentHandler { return sInstance; } - public synchronized void release(){ + public synchronized void release() { sInstance = null; } @@ -43,15 +49,26 @@ class AccStatusIntentHandler implements IntentHandler { public void handle( Context context, Intent intent ) { String action = intent.getAction(); if ( Intent.ACTION_POWER_CONNECTED.equals( action ) ) { - MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, true ); - Logger.d( TAG, "acc status: %s", true ); + if ( CarSeries.getSeries() == CarSeries.CAR_SERIES_F80X ) { + MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, true ); + Logger.d( TAG, "acc status: %s", true ); + } } else if ( Intent.ACTION_POWER_DISCONNECTED.equals( action ) ) { - MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, false ); - Logger.d( TAG, "acc status: %s", false ); + if ( CarSeries.getSeries() == CarSeries.CAR_SERIES_F80X ) { + MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, false ); + Logger.d( TAG, "acc status: %s", false ); + } } else if ( AccStatusReceiver.ACTION_NWD_ACC.equals( action ) ) { - int state = intent.getByteExtra( AccStatusReceiver.PARAM_ACC_STATUS, ( byte ) 0 ); - Logger.d( TAG, "acc status: %s", state == 1 ); - MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, state == 1 ); + int state = intent.getByteExtra( AccStatusReceiver.PARAM_ACC_STATUS, ACC_OFF ); + if ( state != ACC_OFF && state != ACC_ON && state != ACC_OFF_DELAY ) { + return; + } + boolean accOn = state == ACC_ON; + if ( MarkerServiceHandler.getMogoStatusManager().isAccOn() == accOn ) { + return; + } + Logger.d( TAG, "acc status: %s", state ); + MarkerServiceHandler.getMogoStatusManager().setAccStatus( ServiceConst.TYPE, accOn ); } } } diff --git a/modules/mogo-module-service/src/main/java/com/mogo/module/service/marker/MapMarkerManager.java b/modules/mogo-module-service/src/main/java/com/mogo/module/service/marker/MapMarkerManager.java index 9595d20a9b..1d0189338f 100644 --- a/modules/mogo-module-service/src/main/java/com/mogo/module/service/marker/MapMarkerManager.java +++ b/modules/mogo-module-service/src/main/java/com/mogo/module/service/marker/MapMarkerManager.java @@ -77,7 +77,6 @@ public class MapMarkerManager implements IMogoMarkerClickListener, private Rect mMarkerDisplayBounds; private MapMarkerManager() { - mContext = AbsMogoApplication.getApp(); } public static synchronized MapMarkerManager getInstance() { @@ -96,6 +95,14 @@ public class MapMarkerManager implements IMogoMarkerClickListener, * @param context */ public void init( Context context ) { + + if ( mContext != null ) { + return; + } + + Logger.d( TAG, "初始化" ); + + mContext = context.getApplicationContext(); mRefreshModel = new RefreshModel( mContext ); // 长连接