4444import android .os .PowerManager ;
4545import android .os .RemoteException ;
4646import android .os .ServiceManager ;
47+ import android .os .SystemClock ;
4748import android .provider .Settings ;
4849import android .text .format .DateUtils ;
4950import android .text .format .Time ;
@@ -64,11 +65,13 @@ class UiModeManagerService extends IUiModeManager.Stub {
6465
6566 private static final int MSG_UPDATE_TWILIGHT = 0 ;
6667 private static final int MSG_ENABLE_LOCATION_UPDATES = 1 ;
68+ private static final int MSG_GET_NEW_LOCATION_UPDATE = 2 ;
6769
68- private static final long LOCATION_UPDATE_MS = 30 * DateUtils .MINUTE_IN_MILLIS ;
70+ private static final long LOCATION_UPDATE_MS = 24 * DateUtils .HOUR_IN_MILLIS ;
71+ private static final long MIN_LOCATION_UPDATE_MS = 30 * DateUtils .MINUTE_IN_MILLIS ;
6972 private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20 ;
7073 private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000 ;
71- private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 5 * DateUtils .MINUTE_IN_MILLIS ;
74+ private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 15 * DateUtils .MINUTE_IN_MILLIS ;
7275 private static final double FACTOR_GMT_OFFSET_LONGITUDE = 1000.0 * 360.0 / DateUtils .DAY_IN_MILLIS ;
7376
7477 private static final String ACTION_UPDATE_NIGHT_MODE = "com.android.server.action.UPDATE_NIGHT_MODE" ;
@@ -215,6 +218,21 @@ public void onReceive(Context context, Intent intent) {
215218 }
216219 };
217220
221+ private final BroadcastReceiver mUpdateLocationReceiver = new BroadcastReceiver () {
222+ @ Override
223+ public void onReceive (Context context , Intent intent ) {
224+ if (Intent .ACTION_AIRPLANE_MODE_CHANGED .equals (intent .getAction ())) {
225+ if (!intent .getBooleanExtra ("state" , false )) {
226+ // Airplane mode is now off!
227+ mHandler .sendEmptyMessage (MSG_GET_NEW_LOCATION_UPDATE );
228+ }
229+ } else {
230+ // Time zone has changed!
231+ mHandler .sendEmptyMessage (MSG_GET_NEW_LOCATION_UPDATE );
232+ }
233+ }
234+ };
235+
218236 // A LocationListener to initialize the network location provider. The location updates
219237 // are handled through the passive location provider.
220238 private final LocationListener mEmptyLocationListener = new LocationListener () {
@@ -304,6 +322,9 @@ public UiModeManagerService(Context context) {
304322 new IntentFilter (Intent .ACTION_DOCK_EVENT ));
305323 mContext .registerReceiver (mBatteryReceiver ,
306324 new IntentFilter (Intent .ACTION_BATTERY_CHANGED ));
325+ IntentFilter filter = new IntentFilter (Intent .ACTION_AIRPLANE_MODE_CHANGED );
326+ filter .addAction (Intent .ACTION_TIMEZONE_CHANGED );
327+ mContext .registerReceiver (mUpdateLocationReceiver , filter );
307328
308329 PowerManager powerManager = (PowerManager )context .getSystemService (Context .POWER_SERVICE );
309330 mWakeLock = powerManager .newWakeLock (PowerManager .FULL_WAKE_LOCK , TAG );
@@ -586,7 +607,9 @@ private void adjustStatusBarCarModeLocked() {
586607
587608 boolean mPassiveListenerEnabled ;
588609 boolean mNetworkListenerEnabled ;
589-
610+ boolean mDidFirstInit ;
611+ long mLastNetworkRegisterTime = -MIN_LOCATION_UPDATE_MS ;
612+
590613 @ Override
591614 public void handleMessage (Message msg ) {
592615 switch (msg .what ) {
@@ -599,6 +622,25 @@ public void handleMessage(Message msg) {
599622 }
600623 }
601624 break ;
625+ case MSG_GET_NEW_LOCATION_UPDATE :
626+ if (!mNetworkListenerEnabled ) {
627+ // Don't do anything -- we are still trying to get a
628+ // location.
629+ return ;
630+ }
631+ if ((mLastNetworkRegisterTime +MIN_LOCATION_UPDATE_MS )
632+ >= SystemClock .elapsedRealtime ()) {
633+ // Don't do anything -- it hasn't been long enough
634+ // since we last requested an update.
635+ return ;
636+ }
637+
638+ // Unregister the current location monitor, so we can
639+ // register a new one for it to get an immediate update.
640+ mNetworkListenerEnabled = false ;
641+ mLocationManager .removeUpdates (mEmptyLocationListener );
642+
643+ // Fall through to re-register listener.
602644 case MSG_ENABLE_LOCATION_UPDATES :
603645 // enable network provider to receive at least location updates for a given
604646 // distance.
@@ -613,17 +655,21 @@ public void handleMessage(Message msg) {
613655 }
614656 if (!mNetworkListenerEnabled && networkLocationEnabled ) {
615657 mNetworkListenerEnabled = true ;
658+ mLastNetworkRegisterTime = SystemClock .elapsedRealtime ();
616659 mLocationManager .requestLocationUpdates (LocationManager .NETWORK_PROVIDER ,
617660 LOCATION_UPDATE_MS , 0 , mEmptyLocationListener );
618661
619- if (mLocation == null ) {
620- retrieveLocation ();
621- }
622- synchronized (mLock ) {
623- if (isDoingNightMode () && mLocation != null
624- && mNightMode == UiModeManager .MODE_NIGHT_AUTO ) {
625- updateTwilightLocked ();
626- updateLocked (0 , 0 );
662+ if (!mDidFirstInit ) {
663+ mDidFirstInit = true ;
664+ if (mLocation == null ) {
665+ retrieveLocation ();
666+ }
667+ synchronized (mLock ) {
668+ if (isDoingNightMode () && mLocation != null
669+ && mNightMode == UiModeManager .MODE_NIGHT_AUTO ) {
670+ updateTwilightLocked ();
671+ updateLocked (0 , 0 );
672+ }
627673 }
628674 }
629675 }
0 commit comments