frameworks/base
Revision | 090df1dc4188e5b9ef10a0aca5081a196085ff56 (tree) |
---|---|
Time | 2011-08-05 08:10:11 |
Author | Wink Saville <wink@goog...> |
Commiter | Android (Google) Code Review |
Merge "Delay connectivity change notifications." into honeycomb-LTE
@@ -3023,6 +3023,18 @@ public final class Settings { | ||
3023 | 3023 | public static final String TTY_MODE_ENABLED = "tty_mode_enabled"; |
3024 | 3024 | |
3025 | 3025 | /** |
3026 | + * The number of milliseconds to delay before sending out Connectivyt Change broadcasts | |
3027 | + * @hide | |
3028 | + */ | |
3029 | + public static final String CONNECTIVITY_CHANGE_DELAY = "connectivity_change_delay"; | |
3030 | + | |
3031 | + /** | |
3032 | + * Default value for CONNECTIVITY_CHANGE_DELAY in milliseconds. | |
3033 | + * @hide | |
3034 | + */ | |
3035 | + public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000; | |
3036 | + | |
3037 | + /** | |
3026 | 3038 | * Controls whether settings backup is enabled. |
3027 | 3039 | * Type: int ( 0 = disabled, 1 = enabled ) |
3028 | 3040 | * @hide |
@@ -216,6 +216,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
216 | 216 | private static final int EVENT_SET_DEPENDENCY_MET = |
217 | 217 | MAX_NETWORK_STATE_TRACKER_EVENT + 10; |
218 | 218 | |
219 | + /** | |
220 | + * used internally to send a sticky broadcast delayed. | |
221 | + */ | |
222 | + private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = | |
223 | + MAX_NETWORK_STATE_TRACKER_EVENT + 11; | |
224 | + | |
219 | 225 | private Handler mHandler; |
220 | 226 | |
221 | 227 | // list of DeathRecipients used to make sure features are turned off when |
@@ -511,6 +517,17 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
511 | 517 | } |
512 | 518 | } |
513 | 519 | |
520 | + private int getConnectivityChangeDelay() { | |
521 | + final ContentResolver cr = mContext.getContentResolver(); | |
522 | + | |
523 | + /** Check system properties for the default value then use secure settings value, if any. */ | |
524 | + int defaultDelay = SystemProperties.getInt( | |
525 | + "conn." + Settings.Secure.CONNECTIVITY_CHANGE_DELAY, | |
526 | + Settings.Secure.CONNECTIVITY_CHANGE_DELAY_DEFAULT); | |
527 | + return Settings.Secure.getInt(cr, Settings.Secure.CONNECTIVITY_CHANGE_DELAY, | |
528 | + defaultDelay); | |
529 | + } | |
530 | + | |
514 | 531 | private int getPersistedNetworkPreference() { |
515 | 532 | final ContentResolver cr = mContext.getContentResolver(); |
516 | 533 |
@@ -1243,13 +1260,14 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1243 | 1260 | // do this before we broadcast the change |
1244 | 1261 | handleConnectivityChange(prevNetType, doReset); |
1245 | 1262 | |
1246 | - sendStickyBroadcast(intent); | |
1263 | + sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); | |
1247 | 1264 | /* |
1248 | 1265 | * If the failover network is already connected, then immediately send |
1249 | 1266 | * out a followup broadcast indicating successful failover |
1250 | 1267 | */ |
1251 | 1268 | if (mActiveDefaultNetwork != -1) { |
1252 | - sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo()); | |
1269 | + sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), | |
1270 | + getConnectivityChangeDelay()); | |
1253 | 1271 | } |
1254 | 1272 | } |
1255 | 1273 |
@@ -1303,11 +1321,15 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1303 | 1321 | sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION); |
1304 | 1322 | } |
1305 | 1323 | |
1324 | + private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { | |
1325 | + sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs); | |
1326 | + } | |
1327 | + | |
1306 | 1328 | private void sendInetConditionBroadcast(NetworkInfo info) { |
1307 | 1329 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
1308 | 1330 | } |
1309 | 1331 | |
1310 | - private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { | |
1332 | + private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { | |
1311 | 1333 | Intent intent = new Intent(bcastType); |
1312 | 1334 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
1313 | 1335 | if (info.isFailover()) { |
@@ -1322,7 +1344,15 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1322 | 1344 | info.getExtraInfo()); |
1323 | 1345 | } |
1324 | 1346 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
1325 | - sendStickyBroadcast(intent); | |
1347 | + return intent; | |
1348 | + } | |
1349 | + | |
1350 | + private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { | |
1351 | + sendStickyBroadcast(makeGeneralIntent(info, bcastType)); | |
1352 | + } | |
1353 | + | |
1354 | + private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { | |
1355 | + sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); | |
1326 | 1356 | } |
1327 | 1357 | |
1328 | 1358 | /** |
@@ -1387,10 +1417,25 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1387 | 1417 | mInitialBroadcast = new Intent(intent); |
1388 | 1418 | } |
1389 | 1419 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
1420 | + if (DBG) { | |
1421 | + log("sendStickyBroadcast: NetworkInfo=" + | |
1422 | + intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); | |
1423 | + } | |
1424 | + | |
1390 | 1425 | mContext.sendStickyBroadcast(intent); |
1391 | 1426 | } |
1392 | 1427 | } |
1393 | 1428 | |
1429 | + private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { | |
1430 | + if (delayMs <= 0) { | |
1431 | + sendStickyBroadcast(intent); | |
1432 | + } else { | |
1433 | + if (DBG) log("sendStickyBroadcastDelayed: delayMs=" + delayMs + " intent=" + intent); | |
1434 | + mHandler.sendMessageDelayed(mHandler.obtainMessage( | |
1435 | + EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); | |
1436 | + } | |
1437 | + } | |
1438 | + | |
1394 | 1439 | void systemReady() { |
1395 | 1440 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
1396 | 1441 | mNetd = INetworkManagementService.Stub.asInterface(b); |
@@ -1466,7 +1511,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1466 | 1511 | thisNet.setTeardownRequested(false); |
1467 | 1512 | updateNetworkSettings(thisNet); |
1468 | 1513 | handleConnectivityChange(type, false); |
1469 | - sendConnectedBroadcast(info); | |
1514 | + sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); | |
1470 | 1515 | } |
1471 | 1516 | |
1472 | 1517 | /** |
@@ -2036,6 +2081,13 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
2036 | 2081 | handleSetDependencyMet(msg.arg2, met); |
2037 | 2082 | break; |
2038 | 2083 | } |
2084 | + case EVENT_SEND_STICKY_BROADCAST_INTENT: | |
2085 | + { | |
2086 | + Intent intent = (Intent)msg.obj; | |
2087 | + log("EVENT_SEND_STICKY_BROADCAST_INTENT: sendStickyBroadcast intent=" + intent); | |
2088 | + sendStickyBroadcast(intent); | |
2089 | + break; | |
2090 | + } | |
2039 | 2091 | } |
2040 | 2092 | } |
2041 | 2093 | } |
@@ -2222,10 +2274,13 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
2222 | 2274 | if (DBG) log("event hold for obsolete network - aborting"); |
2223 | 2275 | return; |
2224 | 2276 | } |
2225 | - if (mDefaultInetConditionPublished == mDefaultInetCondition) { | |
2226 | - if (DBG) log("no change in condition - aborting"); | |
2227 | - return; | |
2228 | - } | |
2277 | + // TODO: Figure out why this optimization sometimes causes a | |
2278 | + // change in mDefaultInetCondition to be missed and the | |
2279 | + // UI to not be updated. | |
2280 | + //if (mDefaultInetConditionPublished == mDefaultInetCondition) { | |
2281 | + // if (DBG) log("no change in condition - aborting"); | |
2282 | + // return; | |
2283 | + //} | |
2229 | 2284 | NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
2230 | 2285 | if (networkInfo.isConnected() == false) { |
2231 | 2286 | if (DBG) log("default network not connected - aborting"); |