Merge "Introduce immediate CONNECTIVITY_ACTION variant."

This commit is contained in:
Jeff Sharkey
2011-08-29 20:00:12 -07:00
committed by Android (Google) Code Review
7 changed files with 50 additions and 25 deletions

View File

@@ -23,6 +23,7 @@ import android.annotation.SdkConstant.SdkConstantType;
import android.os.Binder; import android.os.Binder;
import android.os.Build.VERSION_CODES; import android.os.Build.VERSION_CODES;
import android.os.RemoteException; import android.os.RemoteException;
import android.provider.Settings;
import java.net.InetAddress; import java.net.InetAddress;
@@ -70,6 +71,15 @@ public class ConnectivityManager {
*/ */
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
/**
* Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
* applicable {@link Settings.Secure#CONNECTIVITY_CHANGE_DELAY}.
*
* @hide
*/
public static final String CONNECTIVITY_ACTION_IMMEDIATE =
"android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
/** /**
* The lookup key for a {@link NetworkInfo} object. Retrieve with * The lookup key for a {@link NetworkInfo} object. Retrieve with
* {@link android.content.Intent#getParcelableExtra(String)}. * {@link android.content.Intent#getParcelableExtra(String)}.

View File

@@ -93,6 +93,9 @@
<protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> <protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /> <protected-broadcast android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE" />
<protected-broadcast android:name="android.nfc.action.LLCP_LINK_STATE_CHANGED" /> <protected-broadcast android:name="android.nfc.action.LLCP_LINK_STATE_CHANGED" />
<protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_ON_DETECTED" /> <protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_ON_DETECTED" />
<protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED" /> <protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED" />

View File

@@ -17,6 +17,8 @@
package com.android.server; package com.android.server;
import static android.Manifest.permission.MANAGE_NETWORK_POLICY; import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
import static android.net.ConnectivityManager.isNetworkTypeValid; import static android.net.ConnectivityManager.isNetworkTypeValid;
import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
@@ -1418,6 +1420,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
// do this before we broadcast the change // do this before we broadcast the change
handleConnectivityChange(prevNetType, doReset); handleConnectivityChange(prevNetType, doReset);
final Intent immediateIntent = new Intent(intent);
immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
sendStickyBroadcast(immediateIntent);
sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
/* /*
* If the failover network is already connected, then immediately send * If the failover network is already connected, then immediately send
@@ -1476,11 +1481,13 @@ public class ConnectivityService extends IConnectivityManager.Stub {
} }
private void sendConnectedBroadcast(NetworkInfo info) { private void sendConnectedBroadcast(NetworkInfo info) {
sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION); sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
sendGeneralBroadcast(info, CONNECTIVITY_ACTION);
} }
private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs); sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs);
} }
private void sendInetConditionBroadcast(NetworkInfo info) { private void sendInetConditionBroadcast(NetworkInfo info) {
@@ -1559,6 +1566,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
} }
intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
final Intent immediateIntent = new Intent(intent);
immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
sendStickyBroadcast(immediateIntent);
sendStickyBroadcast(intent); sendStickyBroadcast(intent);
/* /*
* If the failover network is already connected, then immediately send * If the failover network is already connected, then immediately send
@@ -1576,8 +1587,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
} }
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
if (DBG) { if (DBG) {
log("sendStickyBroadcast: NetworkInfo=" + log("sendStickyBroadcast: action=" + intent.getAction());
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
} }
mContext.sendStickyBroadcast(intent); mContext.sendStickyBroadcast(intent);
@@ -1588,7 +1598,10 @@ public class ConnectivityService extends IConnectivityManager.Stub {
if (delayMs <= 0) { if (delayMs <= 0) {
sendStickyBroadcast(intent); sendStickyBroadcast(intent);
} else { } else {
if (DBG) log("sendStickyBroadcastDelayed: delayMs=" + delayMs + " intent=" + intent); if (DBG) {
log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action="
+ intent.getAction());
}
mHandler.sendMessageDelayed(mHandler.obtainMessage( mHandler.sendMessageDelayed(mHandler.obtainMessage(
EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
} }
@@ -2281,7 +2294,6 @@ public class ConnectivityService extends IConnectivityManager.Stub {
case EVENT_SEND_STICKY_BROADCAST_INTENT: case EVENT_SEND_STICKY_BROADCAST_INTENT:
{ {
Intent intent = (Intent)msg.obj; Intent intent = (Intent)msg.obj;
log("EVENT_SEND_STICKY_BROADCAST_INTENT: sendStickyBroadcast intent=" + intent);
sendStickyBroadcast(intent); sendStickyBroadcast(intent);
break; break;
} }

View File

@@ -26,7 +26,7 @@ import static android.Manifest.permission.READ_PHONE_STATE;
import static android.content.Intent.ACTION_PACKAGE_ADDED; import static android.content.Intent.ACTION_PACKAGE_ADDED;
import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
import static android.net.ConnectivityManager.TYPE_ETHERNET; import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
@@ -51,6 +51,7 @@ import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.NetworkTemplate.buildTemplateMobileAll; import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readBooleanAttribute; import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readBooleanAttribute;
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readIntAttribute; import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readIntAttribute;
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readLongAttribute; import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readLongAttribute;
@@ -60,7 +61,6 @@ import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeL
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED;
import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
import static org.xmlpull.v1.XmlPullParser.START_TAG; import static org.xmlpull.v1.XmlPullParser.START_TAG;
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
import android.app.IActivityManager; import android.app.IActivityManager;
import android.app.INotificationManager; import android.app.INotificationManager;
@@ -321,7 +321,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
mContext.registerReceiver(mScreenReceiver, screenFilter, null, mHandler); mContext.registerReceiver(mScreenReceiver, screenFilter, null, mHandler);
// watch for network interfaces to be claimed // watch for network interfaces to be claimed
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE);
mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
// listen for package/uid changes to update policy // listen for package/uid changes to update policy

View File

@@ -24,7 +24,7 @@ import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY;
import static android.content.Intent.ACTION_SHUTDOWN; import static android.content.Intent.ACTION_SHUTDOWN;
import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT;
@@ -239,7 +239,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
// watch for network interfaces to be claimed // watch for network interfaces to be claimed
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE);
mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
// listen for periodic polling events // listen for periodic polling events

View File

@@ -18,7 +18,7 @@ package com.android.server;
import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.NetworkPolicy.LIMIT_DISABLED; import static android.net.NetworkPolicy.LIMIT_DISABLED;
import static android.net.NetworkPolicy.SNOOZE_NEVER; import static android.net.NetworkPolicy.SNOOZE_NEVER;
@@ -511,7 +511,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
future = expectMeteredIfacesChanged(); future = expectMeteredIfacesChanged();
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
future.get(); future.get();
verifyAndReset(); verifyAndReset();
@@ -615,7 +615,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
future = expectMeteredIfacesChanged(TEST_IFACE); future = expectMeteredIfacesChanged(TEST_IFACE);
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
future.get(); future.get();
verifyAndReset(); verifyAndReset();
} }

View File

@@ -18,7 +18,7 @@ package com.android.server;
import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIMAX; import static android.net.ConnectivityManager.TYPE_WIMAX;
@@ -180,7 +180,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
// verify service has empty history for wifi // verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -232,7 +232,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
// verify service has empty history for wifi // verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
@@ -322,7 +322,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// modify some number on wifi, and trigger poll event // modify some number on wifi, and trigger poll event
@@ -372,7 +372,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// create some traffic on first network // create some traffic on first network
@@ -410,7 +410,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsPoll(); expectNetworkStatsPoll();
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
verifyAndReset(); verifyAndReset();
@@ -452,7 +452,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// create some traffic // create some traffic
@@ -509,7 +509,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// create some traffic // create some traffic
@@ -541,7 +541,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsPoll(); expectNetworkStatsPoll();
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
verifyAndReset(); verifyAndReset();
@@ -575,7 +575,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// create some traffic for two apps // create some traffic for two apps
@@ -637,7 +637,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsSummary(buildEmptyStats());
replay(); replay();
mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
verifyAndReset(); verifyAndReset();
// create some initial traffic // create some initial traffic