Skip to content

Commit 4107bee

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Introduce immediate CONNECTIVITY_ACTION variant."
2 parents 4293064 + 961e304 commit 4107bee

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

core/java/android/net/ConnectivityManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.os.Binder;
2424
import android.os.Build.VERSION_CODES;
2525
import android.os.RemoteException;
26+
import android.provider.Settings;
2627

2728
import java.net.InetAddress;
2829

@@ -70,6 +71,15 @@ public class ConnectivityManager {
7071
*/
7172
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
7273

74+
/**
75+
* Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
76+
* applicable {@link Settings.Secure#CONNECTIVITY_CHANGE_DELAY}.
77+
*
78+
* @hide
79+
*/
80+
public static final String CONNECTIVITY_ACTION_IMMEDIATE =
81+
"android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
82+
7383
/**
7484
* The lookup key for a {@link NetworkInfo} object. Retrieve with
7585
* {@link android.content.Intent#getParcelableExtra(String)}.

core/res/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
<protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
9494
<protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
9595

96+
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE" />
97+
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE" />
98+
9699
<protected-broadcast android:name="android.nfc.action.LLCP_LINK_STATE_CHANGED" />
97100
<protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_ON_DETECTED" />
98101
<protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED" />

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.android.server;
1818

1919
import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
20+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
21+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
2022
import static android.net.ConnectivityManager.isNetworkTypeValid;
2123
import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
2224
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
@@ -1418,6 +1420,9 @@ private void handleDisconnect(NetworkInfo info) {
14181420
// do this before we broadcast the change
14191421
handleConnectivityChange(prevNetType, doReset);
14201422

1423+
final Intent immediateIntent = new Intent(intent);
1424+
immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1425+
sendStickyBroadcast(immediateIntent);
14211426
sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
14221427
/*
14231428
* If the failover network is already connected, then immediately send
@@ -1476,11 +1481,13 @@ private void tryFailover(int prevNetType) {
14761481
}
14771482

14781483
private void sendConnectedBroadcast(NetworkInfo info) {
1479-
sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1484+
sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1485+
sendGeneralBroadcast(info, CONNECTIVITY_ACTION);
14801486
}
14811487

14821488
private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
1483-
sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs);
1489+
sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1490+
sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs);
14841491
}
14851492

14861493
private void sendInetConditionBroadcast(NetworkInfo info) {
@@ -1559,6 +1566,10 @@ private void handleConnectionFailure(NetworkInfo info) {
15591566
}
15601567

15611568
intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
1569+
1570+
final Intent immediateIntent = new Intent(intent);
1571+
immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1572+
sendStickyBroadcast(immediateIntent);
15621573
sendStickyBroadcast(intent);
15631574
/*
15641575
* If the failover network is already connected, then immediately send
@@ -1576,8 +1587,7 @@ private void sendStickyBroadcast(Intent intent) {
15761587
}
15771588
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
15781589
if (DBG) {
1579-
log("sendStickyBroadcast: NetworkInfo=" +
1580-
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
1590+
log("sendStickyBroadcast: action=" + intent.getAction());
15811591
}
15821592

15831593
mContext.sendStickyBroadcast(intent);
@@ -1588,7 +1598,10 @@ private void sendStickyBroadcastDelayed(Intent intent, int delayMs) {
15881598
if (delayMs <= 0) {
15891599
sendStickyBroadcast(intent);
15901600
} else {
1591-
if (DBG) log("sendStickyBroadcastDelayed: delayMs=" + delayMs + " intent=" + intent);
1601+
if (DBG) {
1602+
log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action="
1603+
+ intent.getAction());
1604+
}
15921605
mHandler.sendMessageDelayed(mHandler.obtainMessage(
15931606
EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
15941607
}
@@ -2281,7 +2294,6 @@ public void handleMessage(Message msg) {
22812294
case EVENT_SEND_STICKY_BROADCAST_INTENT:
22822295
{
22832296
Intent intent = (Intent)msg.obj;
2284-
log("EVENT_SEND_STICKY_BROADCAST_INTENT: sendStickyBroadcast intent=" + intent);
22852297
sendStickyBroadcast(intent);
22862298
break;
22872299
}

services/java/com/android/server/net/NetworkPolicyManagerService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import static android.content.Intent.ACTION_PACKAGE_ADDED;
2727
import static android.content.Intent.ACTION_UID_REMOVED;
2828
import static android.content.Intent.EXTRA_UID;
29-
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
29+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
3030
import static android.net.ConnectivityManager.TYPE_ETHERNET;
3131
import static android.net.ConnectivityManager.TYPE_MOBILE;
3232
import static android.net.ConnectivityManager.TYPE_WIFI;
@@ -51,6 +51,7 @@
5151
import static android.net.NetworkTemplate.buildTemplateMobileAll;
5252
import static android.text.format.DateUtils.DAY_IN_MILLIS;
5353
import static com.android.internal.util.Preconditions.checkNotNull;
54+
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
5455
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readBooleanAttribute;
5556
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readIntAttribute;
5657
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readLongAttribute;
@@ -60,7 +61,6 @@
6061
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED;
6162
import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
6263
import static org.xmlpull.v1.XmlPullParser.START_TAG;
63-
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
6464

6565
import android.app.IActivityManager;
6666
import android.app.INotificationManager;
@@ -321,7 +321,7 @@ public void systemReady() {
321321
mContext.registerReceiver(mScreenReceiver, screenFilter, null, mHandler);
322322

323323
// watch for network interfaces to be claimed
324-
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION);
324+
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE);
325325
mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
326326

327327
// listen for package/uid changes to update policy

services/java/com/android/server/net/NetworkStatsService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static android.content.Intent.ACTION_SHUTDOWN;
2525
import static android.content.Intent.ACTION_UID_REMOVED;
2626
import static android.content.Intent.EXTRA_UID;
27-
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
27+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
2828
import static android.net.NetworkStats.IFACE_ALL;
2929
import static android.net.NetworkStats.SET_ALL;
3030
import static android.net.NetworkStats.SET_DEFAULT;
@@ -239,7 +239,7 @@ public void systemReady() {
239239
}
240240

241241
// watch for network interfaces to be claimed
242-
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION);
242+
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE);
243243
mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
244244

245245
// listen for periodic polling events

services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static android.content.Intent.ACTION_UID_REMOVED;
2020
import static android.content.Intent.EXTRA_UID;
21-
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
21+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
2222
import static android.net.ConnectivityManager.TYPE_WIFI;
2323
import static android.net.NetworkPolicy.LIMIT_DISABLED;
2424
import static android.net.NetworkPolicy.SNOOZE_NEVER;
@@ -511,7 +511,7 @@ public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
511511
future = expectMeteredIfacesChanged();
512512

513513
replay();
514-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
514+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
515515
future.get();
516516
verifyAndReset();
517517

@@ -615,7 +615,7 @@ public void testOverWarningLimitNotification() throws Exception {
615615
future = expectMeteredIfacesChanged(TEST_IFACE);
616616

617617
replay();
618-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
618+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
619619
future.get();
620620
verifyAndReset();
621621
}

services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static android.content.Intent.ACTION_UID_REMOVED;
2020
import static android.content.Intent.EXTRA_UID;
21-
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
21+
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
2222
import static android.net.ConnectivityManager.TYPE_MOBILE;
2323
import static android.net.ConnectivityManager.TYPE_WIFI;
2424
import static android.net.ConnectivityManager.TYPE_WIMAX;
@@ -180,7 +180,7 @@ public void testNetworkStatsWifi() throws Exception {
180180
expectNetworkStatsSummary(buildEmptyStats());
181181

182182
replay();
183-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
183+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
184184

185185
// verify service has empty history for wifi
186186
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -232,7 +232,7 @@ public void testStatsRebootPersist() throws Exception {
232232
expectNetworkStatsSummary(buildEmptyStats());
233233

234234
replay();
235-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
235+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
236236

237237
// verify service has empty history for wifi
238238
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -322,7 +322,7 @@ public void testStatsBucketResize() throws Exception {
322322
expectNetworkStatsSummary(buildEmptyStats());
323323

324324
replay();
325-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
325+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
326326
verifyAndReset();
327327

328328
// modify some number on wifi, and trigger poll event
@@ -372,7 +372,7 @@ public void testUidStatsAcrossNetworks() throws Exception {
372372
expectNetworkStatsSummary(buildEmptyStats());
373373

374374
replay();
375-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
375+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
376376
verifyAndReset();
377377

378378
// create some traffic on first network
@@ -410,7 +410,7 @@ public void testUidStatsAcrossNetworks() throws Exception {
410410
expectNetworkStatsPoll();
411411

412412
replay();
413-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
413+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
414414
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
415415
verifyAndReset();
416416

@@ -452,7 +452,7 @@ public void testUidRemovedIsMoved() throws Exception {
452452
expectNetworkStatsSummary(buildEmptyStats());
453453

454454
replay();
455-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
455+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
456456
verifyAndReset();
457457

458458
// create some traffic
@@ -509,7 +509,7 @@ public void testUid3g4gCombinedByTemplate() throws Exception {
509509
expectNetworkStatsSummary(buildEmptyStats());
510510

511511
replay();
512-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
512+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
513513
verifyAndReset();
514514

515515
// create some traffic
@@ -541,7 +541,7 @@ public void testUid3g4gCombinedByTemplate() throws Exception {
541541
expectNetworkStatsPoll();
542542

543543
replay();
544-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
544+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
545545
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
546546
verifyAndReset();
547547

@@ -575,7 +575,7 @@ public void testSummaryForAllUid() throws Exception {
575575
expectNetworkStatsSummary(buildEmptyStats());
576576

577577
replay();
578-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
578+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
579579
verifyAndReset();
580580

581581
// create some traffic for two apps
@@ -637,7 +637,7 @@ public void testForegroundBackground() throws Exception {
637637
expectNetworkStatsSummary(buildEmptyStats());
638638

639639
replay();
640-
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
640+
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
641641
verifyAndReset();
642642

643643
// create some initial traffic

0 commit comments

Comments
 (0)