Skip to content

Commit 628b085

Browse files
author
Wink Saville
committed
Delay connectivity change notifications.
Because changes to the route tables take time to propagate we add a delay when sending out change notifications. This allows applications, such as GTalk, to create sockets without encountering a 3 minute timeout. Bug: 5008488 Change-Id: I0eefb03a5d6358a58ea6ae5b4f697ff302b5511d
1 parent 1a23088 commit 628b085

2 files changed

Lines changed: 76 additions & 9 deletions

File tree

core/java/android/provider/Settings.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,18 @@ public static final String getBluetoothInputDevicePriorityKey(String address) {
30223022
*/
30233023
public static final String TTY_MODE_ENABLED = "tty_mode_enabled";
30243024

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+
30253037
/**
30263038
* Controls whether settings backup is enabled.
30273039
* Type: int ( 0 = disabled, 1 = enabled )

services/java/com/android/server/ConnectivityService.java

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ public class ConnectivityService extends IConnectivityManager.Stub {
216216
private static final int EVENT_SET_DEPENDENCY_MET =
217217
MAX_NETWORK_STATE_TRACKER_EVENT + 10;
218218

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+
219225
private Handler mHandler;
220226

221227
// list of DeathRecipients used to make sure features are turned off when
@@ -511,6 +517,17 @@ private void handleSetNetworkPreference(int preference) {
511517
}
512518
}
513519

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+
514531
private int getPersistedNetworkPreference() {
515532
final ContentResolver cr = mContext.getContentResolver();
516533

@@ -1243,13 +1260,14 @@ private void handleDisconnect(NetworkInfo info) {
12431260
// do this before we broadcast the change
12441261
handleConnectivityChange(prevNetType, doReset);
12451262

1246-
sendStickyBroadcast(intent);
1263+
sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
12471264
/*
12481265
* If the failover network is already connected, then immediately send
12491266
* out a followup broadcast indicating successful failover
12501267
*/
12511268
if (mActiveDefaultNetwork != -1) {
1252-
sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
1269+
sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
1270+
getConnectivityChangeDelay());
12531271
}
12541272
}
12551273

@@ -1303,11 +1321,15 @@ private void sendConnectedBroadcast(NetworkInfo info) {
13031321
sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
13041322
}
13051323

1324+
private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
1325+
sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs);
1326+
}
1327+
13061328
private void sendInetConditionBroadcast(NetworkInfo info) {
13071329
sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
13081330
}
13091331

1310-
private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1332+
private Intent makeGeneralIntent(NetworkInfo info, String bcastType) {
13111333
Intent intent = new Intent(bcastType);
13121334
intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
13131335
if (info.isFailover()) {
@@ -1322,7 +1344,15 @@ private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
13221344
info.getExtraInfo());
13231345
}
13241346
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);
13261356
}
13271357

13281358
/**
@@ -1387,10 +1417,25 @@ private void sendStickyBroadcast(Intent intent) {
13871417
mInitialBroadcast = new Intent(intent);
13881418
}
13891419
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+
13901425
mContext.sendStickyBroadcast(intent);
13911426
}
13921427
}
13931428

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+
13941439
void systemReady() {
13951440
IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
13961441
mNetd = INetworkManagementService.Stub.asInterface(b);
@@ -1466,7 +1511,7 @@ private void handleConnect(NetworkInfo info) {
14661511
thisNet.setTeardownRequested(false);
14671512
updateNetworkSettings(thisNet);
14681513
handleConnectivityChange(type, false);
1469-
sendConnectedBroadcast(info);
1514+
sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay());
14701515
}
14711516

14721517
/**
@@ -2036,6 +2081,13 @@ public void handleMessage(Message msg) {
20362081
handleSetDependencyMet(msg.arg2, met);
20372082
break;
20382083
}
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+
}
20392091
}
20402092
}
20412093
}
@@ -2222,10 +2274,13 @@ private void handleInetConditionHoldEnd(int netType, int sequence) {
22222274
if (DBG) log("event hold for obsolete network - aborting");
22232275
return;
22242276
}
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+
//}
22292284
NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
22302285
if (networkInfo.isConnected() == false) {
22312286
if (DBG) log("default network not connected - aborting");

0 commit comments

Comments
 (0)