summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/UiModeManagerService.java
diff options
context:
space:
mode:
Diffstat (limited to 'services/java/com/android/server/UiModeManagerService.java')
-rw-r--r--services/java/com/android/server/UiModeManagerService.java594
1 files changed, 176 insertions, 418 deletions
diff --git a/services/java/com/android/server/UiModeManagerService.java b/services/java/com/android/server/UiModeManagerService.java
index d1f92a7..e9e3163 100644
--- a/services/java/com/android/server/UiModeManagerService.java
+++ b/services/java/com/android/server/UiModeManagerService.java
@@ -17,70 +17,54 @@
package com.android.server;
import android.app.Activity;
+import android.app.ActivityManager;
import android.app.ActivityManagerNative;
-import android.app.AlarmManager;
import android.app.IUiModeManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.StatusBarManager;
import android.app.UiModeManager;
-import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
-import android.location.Criteria;
-import android.location.Location;
-import android.location.LocationListener;
-import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Binder;
-import android.os.Bundle;
import android.os.Handler;
-import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
+import android.os.UserHandle;
import android.provider.Settings;
-import android.text.format.DateUtils;
-import android.text.format.Time;
+import android.service.dreams.DreamService;
+import android.service.dreams.IDreamManager;
import android.util.Slog;
import java.io.FileDescriptor;
import java.io.PrintWriter;
-import java.util.Iterator;
import com.android.internal.R;
import com.android.internal.app.DisableCarModeActivity;
+import com.android.server.TwilightService.TwilightState;
class UiModeManagerService extends IUiModeManager.Stub {
private static final String TAG = UiModeManager.class.getSimpleName();
private static final boolean LOG = false;
- private static final String KEY_LAST_UPDATE_INTERVAL = "LAST_UPDATE_INTERVAL";
-
// Enable launching of applications when entering the dock.
private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true;
private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true;
- private static final int MSG_UPDATE_TWILIGHT = 0;
- private static final int MSG_ENABLE_LOCATION_UPDATES = 1;
- private static final int MSG_GET_NEW_LOCATION_UPDATE = 2;
-
- private static final long LOCATION_UPDATE_MS = 24 * DateUtils.HOUR_IN_MILLIS;
- private static final long MIN_LOCATION_UPDATE_MS = 30 * DateUtils.MINUTE_IN_MILLIS;
- private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20;
- private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000;
- private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX = 15 * DateUtils.MINUTE_IN_MILLIS;
- private static final double FACTOR_GMT_OFFSET_LONGITUDE = 1000.0 * 360.0 / DateUtils.DAY_IN_MILLIS;
-
- private static final String ACTION_UPDATE_NIGHT_MODE = "com.android.server.action.UPDATE_NIGHT_MODE";
+ private static final int DEFAULT_SCREENSAVER_ENABLED = 1;
+ private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
private final Context mContext;
+ private final TwilightService mTwilightService;
+ private final Handler mHandler = new Handler();
final Object mLock = new Object();
@@ -106,11 +90,9 @@ class UiModeManagerService extends IUiModeManager.Stub {
private NotificationManager mNotificationManager;
- private AlarmManager mAlarmManager;
-
- private LocationManager mLocationManager;
- private Location mLocation;
private StatusBarManager mStatusBarManager;
+
+ private final PowerManager mPowerManager;
private final PowerManager.WakeLock mWakeLock;
static Intent buildHomeIntent(String category) {
@@ -136,81 +118,10 @@ class UiModeManagerService extends IUiModeManager.Stub {
return;
}
- final int enableFlags = intent.getIntExtra("enableFlags", 0);
- final int disableFlags = intent.getIntExtra("disableFlags", 0);
-
+ final int enableFlags = intent.getIntExtra("enableFlags", 0);
+ final int disableFlags = intent.getIntExtra("disableFlags", 0);
synchronized (mLock) {
- // Launch a dock activity
- String category = null;
- if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
- // Only launch car home when car mode is enabled and the caller
- // has asked us to switch to it.
- if (ENABLE_LAUNCH_CAR_DOCK_APP
- && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
- category = Intent.CATEGORY_CAR_DOCK;
- }
- } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(intent.getAction())) {
- // Only launch car home when desk mode is enabled and the caller
- // has asked us to switch to it. Currently re-using the car
- // mode flag since we don't have a formal API for "desk mode".
- if (ENABLE_LAUNCH_DESK_DOCK_APP
- && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
- category = Intent.CATEGORY_DESK_DOCK;
- }
- } else {
- // Launch the standard home app if requested.
- if ((disableFlags&UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
- category = Intent.CATEGORY_HOME;
- }
- }
-
- if (LOG) {
- Slog.v(TAG, String.format(
- "Handling broadcast result for action %s: enable=0x%08x disable=0x%08x category=%s",
- intent.getAction(), enableFlags, disableFlags, category));
- }
-
- if (category != null) {
- // This is the new activity that will serve as home while
- // we are in care mode.
- Intent homeIntent = buildHomeIntent(category);
-
- // Now we are going to be careful about switching the
- // configuration and starting the activity -- we need to
- // do this in a specific order under control of the
- // activity manager, to do it cleanly. So compute the
- // new config, but don't set it yet, and let the
- // activity manager take care of both the start and config
- // change.
- Configuration newConfig = null;
- if (mHoldingConfiguration) {
- mHoldingConfiguration = false;
- updateConfigurationLocked(false);
- newConfig = mConfiguration;
- }
- try {
- ActivityManagerNative.getDefault().startActivityWithConfig(
- null, homeIntent, null, null, null, 0, 0,
- newConfig, null);
- mHoldingConfiguration = false;
- } catch (RemoteException e) {
- Slog.w(TAG, e.getCause());
- }
- }
-
- if (mHoldingConfiguration) {
- mHoldingConfiguration = false;
- updateConfigurationLocked(true);
- }
- }
- }
- };
-
- private final BroadcastReceiver mTwilightUpdateReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
- mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT);
+ updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags);
}
}
};
@@ -236,116 +147,27 @@ class UiModeManagerService extends IUiModeManager.Stub {
}
};
- private final BroadcastReceiver mUpdateLocationReceiver = new BroadcastReceiver() {
+ private final TwilightService.TwilightListener mTwilightListener =
+ new TwilightService.TwilightListener() {
@Override
- public void onReceive(Context context, Intent intent) {
- if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
- if (!intent.getBooleanExtra("state", false)) {
- // Airplane mode is now off!
- mHandler.sendEmptyMessage(MSG_GET_NEW_LOCATION_UPDATE);
- }
- } else {
- // Time zone has changed!
- mHandler.sendEmptyMessage(MSG_GET_NEW_LOCATION_UPDATE);
- }
- }
- };
-
- // A LocationListener to initialize the network location provider. The location updates
- // are handled through the passive location provider.
- private final LocationListener mEmptyLocationListener = new LocationListener() {
- public void onLocationChanged(Location location) {
- }
-
- public void onProviderDisabled(String provider) {
- }
-
- public void onProviderEnabled(String provider) {
- }
-
- public void onStatusChanged(String provider, int status, Bundle extras) {
+ public void onTwilightStateChanged() {
+ updateTwilight();
}
};
- private final LocationListener mLocationListener = new LocationListener() {
-
- public void onLocationChanged(Location location) {
- final boolean hasMoved = hasMoved(location);
- final boolean hasBetterAccuracy = mLocation == null
- || location.getAccuracy() < mLocation.getAccuracy();
- if (hasMoved || hasBetterAccuracy) {
- synchronized (mLock) {
- mLocation = location;
- if (hasMoved && isDoingNightMode()
- && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
- mHandler.sendEmptyMessage(MSG_UPDATE_TWILIGHT);
- }
- }
- }
- }
-
- public void onProviderDisabled(String provider) {
- }
-
- public void onProviderEnabled(String provider) {
- }
-
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
-
- /*
- * The user has moved if the accuracy circles of the two locations
- * don't overlap.
- */
- private boolean hasMoved(Location location) {
- if (location == null) {
- return false;
- }
- if (mLocation == null) {
- return true;
- }
-
- /* if new location is older than the current one, the devices hasn't
- * moved.
- */
- if (location.getTime() < mLocation.getTime()) {
- return false;
- }
-
- /* Get the distance between the two points */
- float distance = mLocation.distanceTo(location);
-
- /* Get the total accuracy radius for both locations */
- float totalAccuracy = mLocation.getAccuracy() + location.getAccuracy();
-
- /* If the distance is greater than the combined accuracy of the two
- * points then they can't overlap and hence the user has moved.
- */
- return distance >= totalAccuracy;
- }
- };
-
- public UiModeManagerService(Context context) {
+ public UiModeManagerService(Context context, TwilightService twilight) {
mContext = context;
+ mTwilightService = twilight;
ServiceManager.addService(Context.UI_MODE_SERVICE, this);
- mAlarmManager =
- (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
- mLocationManager =
- (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
- mContext.registerReceiver(mTwilightUpdateReceiver,
- new IntentFilter(ACTION_UPDATE_NIGHT_MODE));
mContext.registerReceiver(mDockModeReceiver,
new IntentFilter(Intent.ACTION_DOCK_EVENT));
mContext.registerReceiver(mBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
- IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
- filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
- mContext.registerReceiver(mUpdateLocationReceiver, filter);
- PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
- mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
+ mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
+ mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
mConfiguration.setToDefaults();
@@ -360,6 +182,8 @@ class UiModeManagerService extends IUiModeManager.Stub {
mNightMode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO);
+
+ mTwilightService.registerListener(mTwilightListener, mHandler);
}
public void disableCarMode(int flags) {
@@ -419,8 +243,8 @@ class UiModeManagerService extends IUiModeManager.Stub {
synchronized (mLock) {
mSystemReady = true;
mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
+ updateComputedNightModeLocked();
updateLocked(0, 0);
- mHandler.sendEmptyMessage(MSG_ENABLE_LOCATION_UPDATES);
}
}
@@ -457,9 +281,8 @@ class UiModeManagerService extends IUiModeManager.Stub {
}
}
- final void updateConfigurationLocked(boolean sendIt) {
- int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION
- : mDefaultUiModeType;
+ final void updateConfigurationLocked() {
+ int uiMode = mTelevision ? Configuration.UI_MODE_TYPE_TELEVISION : mDefaultUiModeType;
if (mCarModeEnabled) {
uiMode = Configuration.UI_MODE_TYPE_CAR;
} else if (isDeskDockState(mDockState)) {
@@ -467,7 +290,7 @@ class UiModeManagerService extends IUiModeManager.Stub {
}
if (mCarModeEnabled) {
if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
- updateTwilightLocked();
+ updateComputedNightModeLocked();
uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
: Configuration.UI_MODE_NIGHT_NO;
} else {
@@ -487,17 +310,19 @@ class UiModeManagerService extends IUiModeManager.Stub {
}
mCurUiMode = uiMode;
-
- if (!mHoldingConfiguration && uiMode != mSetUiMode) {
- mSetUiMode = uiMode;
+ if (!mHoldingConfiguration) {
mConfiguration.uiMode = uiMode;
+ }
+ }
- if (sendIt) {
- try {
- ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
- } catch (RemoteException e) {
- Slog.w(TAG, "Failure communicating with activity manager", e);
- }
+ final void sendConfigurationLocked() {
+ if (mSetUiMode != mConfiguration.uiMode) {
+ mSetUiMode = mConfiguration.uiMode;
+
+ try {
+ ActivityManagerNative.getDefault().updateConfiguration(mConfiguration);
+ } catch (RemoteException e) {
+ Slog.w(TAG, "Failure communicating with activity manager", e);
}
}
}
@@ -520,7 +345,7 @@ class UiModeManagerService extends IUiModeManager.Stub {
adjustStatusBarCarModeLocked();
if (oldAction != null) {
- mContext.sendBroadcast(new Intent(oldAction));
+ mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
}
mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR;
action = UiModeManager.ACTION_ENTER_CAR_MODE;
@@ -528,7 +353,7 @@ class UiModeManagerService extends IUiModeManager.Stub {
} else if (isDeskDockState(mDockState)) {
if (!isDeskDockState(mLastBroadcastState)) {
if (oldAction != null) {
- mContext.sendBroadcast(new Intent(oldAction));
+ mContext.sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL);
}
mLastBroadcastState = mDockState;
action = UiModeManager.ACTION_ENTER_DESK_MODE;
@@ -554,45 +379,40 @@ class UiModeManagerService extends IUiModeManager.Stub {
Intent intent = new Intent(action);
intent.putExtra("enableFlags", enableFlags);
intent.putExtra("disableFlags", disableFlags);
- mContext.sendOrderedBroadcast(intent, null,
+ mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
mResultReceiver, null, Activity.RESULT_OK, null, null);
+
// Attempting to make this transition a little more clean, we are going
// to hold off on doing a configuration change until we have finished
// the broadcast and started the home activity.
mHoldingConfiguration = true;
+ updateConfigurationLocked();
} else {
- Intent homeIntent = null;
+ String category = null;
if (mCarModeEnabled) {
if (ENABLE_LAUNCH_CAR_DOCK_APP
- && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
- homeIntent = buildHomeIntent(Intent.CATEGORY_CAR_DOCK);
+ && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
+ category = Intent.CATEGORY_CAR_DOCK;
}
} else if (isDeskDockState(mDockState)) {
if (ENABLE_LAUNCH_DESK_DOCK_APP
- && (enableFlags&UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
- homeIntent = buildHomeIntent(Intent.CATEGORY_DESK_DOCK);
+ && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
+ category = Intent.CATEGORY_DESK_DOCK;
}
} else {
- if ((disableFlags&UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
- homeIntent = buildHomeIntent(Intent.CATEGORY_HOME);
+ if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
+ category = Intent.CATEGORY_HOME;
}
}
if (LOG) {
Slog.v(TAG, "updateLocked: null action, mDockState="
- + mDockState +", firing homeIntent: " + homeIntent);
+ + mDockState +", category=" + category);
}
- if (homeIntent != null) {
- try {
- mContext.startActivity(homeIntent);
- } catch (ActivityNotFoundException e) {
- }
- }
+ sendConfigurationAndStartDreamOrDockAppLocked(category);
}
- updateConfigurationLocked(true);
-
// keep screen on when charging and in car mode
boolean keepScreenOn = mCharging &&
((mCarModeEnabled && mCarModeKeepsScreenOn) ||
@@ -609,6 +429,111 @@ class UiModeManagerService extends IUiModeManager.Stub {
}
}
+ private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
+ // Launch a dock activity
+ String category = null;
+ if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
+ // Only launch car home when car mode is enabled and the caller
+ // has asked us to switch to it.
+ if (ENABLE_LAUNCH_CAR_DOCK_APP
+ && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
+ category = Intent.CATEGORY_CAR_DOCK;
+ }
+ } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
+ // Only launch car home when desk mode is enabled and the caller
+ // has asked us to switch to it. Currently re-using the car
+ // mode flag since we don't have a formal API for "desk mode".
+ if (ENABLE_LAUNCH_DESK_DOCK_APP
+ && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
+ category = Intent.CATEGORY_DESK_DOCK;
+ }
+ } else {
+ // Launch the standard home app if requested.
+ if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
+ category = Intent.CATEGORY_HOME;
+ }
+ }
+
+ if (LOG) {
+ Slog.v(TAG, String.format(
+ "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
+ + "category=%s",
+ action, enableFlags, disableFlags, category));
+ }
+
+ sendConfigurationAndStartDreamOrDockAppLocked(category);
+ }
+
+ private void sendConfigurationAndStartDreamOrDockAppLocked(String category) {
+ // Update the configuration but don't send it yet.
+ mHoldingConfiguration = false;
+ updateConfigurationLocked();
+
+ // Start the dock app, if there is one.
+ boolean dockAppStarted = false;
+ if (category != null) {
+ // Now we are going to be careful about switching the
+ // configuration and starting the activity -- we need to
+ // do this in a specific order under control of the
+ // activity manager, to do it cleanly. So compute the
+ // new config, but don't set it yet, and let the
+ // activity manager take care of both the start and config
+ // change.
+ Intent homeIntent = buildHomeIntent(category);
+ try {
+ int result = ActivityManagerNative.getDefault().startActivityWithConfig(
+ null, homeIntent, null, null, null, 0, 0,
+ mConfiguration, null, UserHandle.USER_CURRENT);
+ if (result >= ActivityManager.START_SUCCESS) {
+ dockAppStarted = true;
+ } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) {
+ Slog.e(TAG, "Could not start dock app: " + homeIntent
+ + ", startActivityWithConfig result " + result);
+ }
+ } catch (RemoteException ex) {
+ Slog.e(TAG, "Could not start dock app: " + homeIntent, ex);
+ }
+ }
+
+ // Send the new configuration.
+ sendConfigurationLocked();
+
+ // If we did not start a dock app, then start dreaming if supported.
+ if (category != null && !dockAppStarted
+ && isScreenSaverEnabled() && isScreenSaverActivatedOnDock()) {
+ Slog.i(TAG, "Activating dream while docked.");
+ try {
+ IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
+ ServiceManager.getService(DreamService.DREAM_SERVICE));
+ if (dreamManagerService != null && !dreamManagerService.isDreaming()) {
+ // Wake up.
+ // The power manager will wake up the system when it starts receiving power
+ // but there is a race between that happening and the UI mode manager
+ // starting a dream. We want the system to already be awake
+ // by the time this happens. Otherwise the dream may not start.
+ mPowerManager.wakeUp(SystemClock.uptimeMillis());
+
+ // Dream.
+ dreamManagerService.dream();
+ }
+ } catch (RemoteException ex) {
+ Slog.e(TAG, "Could not start dream when docked.", ex);
+ }
+ }
+ }
+
+ private boolean isScreenSaverEnabled() {
+ return Settings.Secure.getIntForUser(mContext.getContentResolver(),
+ Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED,
+ UserHandle.USER_CURRENT) != 0;
+ }
+
+ private boolean isScreenSaverActivatedOnDock() {
+ return Settings.Secure.getIntForUser(mContext.getContentResolver(),
+ Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
+ DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0;
+ }
+
private void adjustStatusBarCarModeLocked() {
if (mStatusBarManager == null) {
mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
@@ -643,197 +568,31 @@ class UiModeManagerService extends IUiModeManager.Stub {
mContext,
mContext.getString(R.string.car_mode_disable_notification_title),
mContext.getString(R.string.car_mode_disable_notification_message),
- PendingIntent.getActivity(mContext, 0, carModeOffIntent, 0));
- mNotificationManager.notify(0, n);
+ PendingIntent.getActivityAsUser(mContext, 0, carModeOffIntent, 0,
+ null, UserHandle.CURRENT));
+ mNotificationManager.notifyAsUser(null,
+ R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
} else {
- mNotificationManager.cancel(0);
+ mNotificationManager.cancelAsUser(null,
+ R.string.car_mode_disable_notification_title, UserHandle.ALL);
}
}
}
- private final Handler mHandler = new Handler() {
-
- boolean mPassiveListenerEnabled;
- boolean mNetworkListenerEnabled;
- boolean mDidFirstInit;
- long mLastNetworkRegisterTime = -MIN_LOCATION_UPDATE_MS;
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_UPDATE_TWILIGHT:
- synchronized (mLock) {
- if (isDoingNightMode() && mLocation != null
- && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
- updateTwilightLocked();
- updateLocked(0, 0);
- }
- }
- break;
- case MSG_GET_NEW_LOCATION_UPDATE:
- if (!mNetworkListenerEnabled) {
- // Don't do anything -- we are still trying to get a
- // location.
- return;
- }
- if ((mLastNetworkRegisterTime+MIN_LOCATION_UPDATE_MS)
- >= SystemClock.elapsedRealtime()) {
- // Don't do anything -- it hasn't been long enough
- // since we last requested an update.
- return;
- }
-
- // Unregister the current location monitor, so we can
- // register a new one for it to get an immediate update.
- mNetworkListenerEnabled = false;
- mLocationManager.removeUpdates(mEmptyLocationListener);
-
- // Fall through to re-register listener.
- case MSG_ENABLE_LOCATION_UPDATES:
- // enable network provider to receive at least location updates for a given
- // distance.
- boolean networkLocationEnabled;
- try {
- networkLocationEnabled =
- mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- } catch (Exception e) {
- // we may get IllegalArgumentException if network location provider
- // does not exist or is not yet installed.
- networkLocationEnabled = false;
- }
- if (!mNetworkListenerEnabled && networkLocationEnabled) {
- mNetworkListenerEnabled = true;
- mLastNetworkRegisterTime = SystemClock.elapsedRealtime();
- mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
- LOCATION_UPDATE_MS, 0, mEmptyLocationListener);
-
- if (!mDidFirstInit) {
- mDidFirstInit = true;
- if (mLocation == null) {
- retrieveLocation();
- }
- synchronized (mLock) {
- if (isDoingNightMode() && mLocation != null
- && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
- updateTwilightLocked();
- updateLocked(0, 0);
- }
- }
- }
- }
- // enable passive provider to receive updates from location fixes (gps
- // and network).
- boolean passiveLocationEnabled;
- try {
- passiveLocationEnabled =
- mLocationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
- } catch (Exception e) {
- // we may get IllegalArgumentException if passive location provider
- // does not exist or is not yet installed.
- passiveLocationEnabled = false;
- }
- if (!mPassiveListenerEnabled && passiveLocationEnabled) {
- mPassiveListenerEnabled = true;
- mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
- 0, LOCATION_UPDATE_DISTANCE_METER , mLocationListener);
- }
- if (!(mNetworkListenerEnabled && mPassiveListenerEnabled)) {
- long interval = msg.getData().getLong(KEY_LAST_UPDATE_INTERVAL);
- interval *= 1.5;
- if (interval == 0) {
- interval = LOCATION_UPDATE_ENABLE_INTERVAL_MIN;
- } else if (interval > LOCATION_UPDATE_ENABLE_INTERVAL_MAX) {
- interval = LOCATION_UPDATE_ENABLE_INTERVAL_MAX;
- }
- Bundle bundle = new Bundle();
- bundle.putLong(KEY_LAST_UPDATE_INTERVAL, interval);
- Message newMsg = mHandler.obtainMessage(MSG_ENABLE_LOCATION_UPDATES);
- newMsg.setData(bundle);
- mHandler.sendMessageDelayed(newMsg, interval);
- }
- break;
- }
- }
-
- private void retrieveLocation() {
- Location location = null;
- final Iterator<String> providers =
- mLocationManager.getProviders(new Criteria(), true).iterator();
- while (providers.hasNext()) {
- final Location lastKnownLocation =
- mLocationManager.getLastKnownLocation(providers.next());
- // pick the most recent location
- if (location == null || (lastKnownLocation != null &&
- location.getTime() < lastKnownLocation.getTime())) {
- location = lastKnownLocation;
- }
- }
- // In the case there is no location available (e.g. GPS fix or network location
- // is not available yet), the longitude of the location is estimated using the timezone,
- // latitude and accuracy are set to get a good average.
- if (location == null) {
- Time currentTime = new Time();
- currentTime.set(System.currentTimeMillis());
- double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE *
- (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
- location = new Location("fake");
- location.setLongitude(lngOffset);
- location.setLatitude(0);
- location.setAccuracy(417000.0f);
- location.setTime(System.currentTimeMillis());
- }
- synchronized (mLock) {
- mLocation = location;
+ private void updateTwilight() {
+ synchronized (mLock) {
+ if (isDoingNightMode() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
+ updateComputedNightModeLocked();
+ updateLocked(0, 0);
}
}
- };
-
- void updateTwilightLocked() {
- if (mLocation == null) {
- return;
- }
- final long currentTime = System.currentTimeMillis();
- boolean nightMode;
- // calculate current twilight
- TwilightCalculator tw = new TwilightCalculator();
- tw.calculateTwilight(currentTime,
- mLocation.getLatitude(), mLocation.getLongitude());
- if (tw.mState == TwilightCalculator.DAY) {
- nightMode = false;
- } else {
- nightMode = true;
- }
-
- // schedule next update
- long nextUpdate = 0;
- if (tw.mSunrise == -1 || tw.mSunset == -1) {
- // In the case the day or night never ends the update is scheduled 12 hours later.
- nextUpdate = currentTime + 12 * DateUtils.HOUR_IN_MILLIS;
- } else {
- final int mLastTwilightState = tw.mState;
- // add some extra time to be on the save side.
- nextUpdate += DateUtils.MINUTE_IN_MILLIS;
- if (currentTime > tw.mSunset) {
- // next update should be on the following day
- tw.calculateTwilight(currentTime
- + DateUtils.DAY_IN_MILLIS, mLocation.getLatitude(),
- mLocation.getLongitude());
- }
+ }
- if (mLastTwilightState == TwilightCalculator.NIGHT) {
- nextUpdate += tw.mSunrise;
- } else {
- nextUpdate += tw.mSunset;
- }
+ private void updateComputedNightModeLocked() {
+ TwilightState state = mTwilightService.getCurrentState();
+ if (state != null) {
+ mComputedNightMode = state.isNight();
}
-
- Intent updateIntent = new Intent(ACTION_UPDATE_NIGHT_MODE);
- PendingIntent pendingIntent =
- PendingIntent.getBroadcast(mContext, 0, updateIntent, 0);
- mAlarmManager.cancel(pendingIntent);
- mAlarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent);
-
- mComputedNightMode = nightMode;
}
@Override
@@ -858,9 +617,8 @@ class UiModeManagerService extends IUiModeManager.Stub {
pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode));
pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration);
pw.print(" mSystemReady="); pw.println(mSystemReady);
- if (mLocation != null) {
- pw.print(" mLocation="); pw.println(mLocation);
- }
+ pw.print(" mTwilightService.getCurrentState()=");
+ pw.println(mTwilightService.getCurrentState());
}
}
}