@@ -182,6 +182,9 @@ public class GpsLocationProvider extends ILocationProvider.Stub {
182182
183183 // true if GPS is navigating
184184 private boolean mNavigating ;
185+
186+ // true if GPS engine is on
187+ private boolean mEngineOn ;
185188
186189 // requested frequency of fixes, in seconds
187190 private int mFixInterval = 1 ;
@@ -556,13 +559,17 @@ public synchronized void disable() {
556559 mNetworkThread = null ;
557560 }
558561
562+ // do this before releasing wakelock
563+ native_cleanup ();
564+
559565 // The GpsEventThread does not wait for the GPS to shutdown
560566 // so we need to report the GPS_STATUS_ENGINE_OFF event here
561567 if (mNavigating ) {
568+ reportStatus (GPS_STATUS_SESSION_END );
569+ }
570+ if (mEngineOn ) {
562571 reportStatus (GPS_STATUS_ENGINE_OFF );
563572 }
564-
565- native_cleanup ();
566573 }
567574
568575 public boolean isEnabled () {
@@ -874,9 +881,24 @@ private void reportStatus(int status) {
874881
875882 synchronized (mListeners ) {
876883 boolean wasNavigating = mNavigating ;
877- mNavigating = (status == GPS_STATUS_SESSION_BEGIN );
878884
879- if (mNavigating && !mWakeLock .isHeld ()) {
885+ switch (status ) {
886+ case GPS_STATUS_SESSION_BEGIN :
887+ mNavigating = true ;
888+ break ;
889+ case GPS_STATUS_SESSION_END :
890+ mNavigating = false ;
891+ break ;
892+ case GPS_STATUS_ENGINE_ON :
893+ mEngineOn = true ;
894+ break ;
895+ case GPS_STATUS_ENGINE_OFF :
896+ mEngineOn = false ;
897+ break ;
898+ }
899+
900+ // beware, the events can come out of order
901+ if ((mNavigating || mEngineOn ) && !mWakeLock .isHeld ()) {
880902 if (DEBUG ) Log .d (TAG , "Acquiring wakelock" );
881903 mWakeLock .acquire ();
882904 }
@@ -919,7 +941,8 @@ private void reportStatus(int status) {
919941 mContext .sendBroadcast (intent );
920942 }
921943
922- if (status == GPS_STATUS_ENGINE_OFF && mWakeLock .isHeld ()) {
944+ // beware, the events can come out of order
945+ if (!mNavigating && !mEngineOn && mWakeLock .isHeld ()) {
923946 if (DEBUG ) Log .d (TAG , "Releasing wakelock" );
924947 mWakeLock .release ();
925948 }
0 commit comments