From 5fb4f650df42cd39536b78061085bab04206ee4c Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Mon, 8 Dec 2014 14:50:12 -0800 Subject: [PATCH] Direct notification of network interface changes. Connectivity broadcasts recently changed and are no longer sent for certain types of network changes. For example, when stacked network interfaces change for a mobile network. To ensure that we pick up all these details, directly wire the two services together. Also remove some unused code for split network types. Bug: 18666753 Change-Id: I0467bd5b330c0e0cb51af2306d821b41ad16337a --- .../android/net/INetworkStatsService.aidl | 4 + core/java/android/net/NetworkIdentity.java | 4 + core/java/android/net/NetworkTemplate.java | 4 + .../server/net/NetworkStatsService.java | 76 ++++--------------- 4 files changed, 25 insertions(+), 63 deletions(-) diff --git a/core/java/android/net/INetworkStatsService.aidl b/core/java/android/net/INetworkStatsService.aidl index b7b8731855..2c3881c4f7 100644 --- a/core/java/android/net/INetworkStatsService.aidl +++ b/core/java/android/net/INetworkStatsService.aidl @@ -40,8 +40,12 @@ interface INetworkStatsService { /** Mark given UID as being in foreground for stats purposes. */ void setUidForeground(int uid, boolean uidForeground); + + /** Force update of ifaces. */ + void forceUpdateIfaces(); /** Force update of statistics. */ void forceUpdate(); + /** Advise persistance threshold; may be overridden internally. */ void advisePersistThreshold(long thresholdBytes); diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java index 6864749875..a9de23ef1b 100644 --- a/core/java/android/net/NetworkIdentity.java +++ b/core/java/android/net/NetworkIdentity.java @@ -41,7 +41,11 @@ public class NetworkIdentity implements Comparable { /** * When enabled, combine all {@link #mSubType} together under * {@link #SUBTYPE_COMBINED}. + * + * @deprecated we no longer offer to collect statistics on a per-subtype + * basis; this is always disabled. */ + @Deprecated public static final boolean COMBINE_SUBTYPE_ENABLED = true; public static final int SUBTYPE_COMBINED = -1; diff --git a/core/java/android/net/NetworkTemplate.java b/core/java/android/net/NetworkTemplate.java index 6cfab92360..57eef83431 100644 --- a/core/java/android/net/NetworkTemplate.java +++ b/core/java/android/net/NetworkTemplate.java @@ -49,7 +49,9 @@ import java.util.Objects; public class NetworkTemplate implements Parcelable { public static final int MATCH_MOBILE_ALL = 1; + @Deprecated public static final int MATCH_MOBILE_3G_LOWER = 2; + @Deprecated public static final int MATCH_MOBILE_4G = 3; public static final int MATCH_WIFI = 4; public static final int MATCH_ETHERNET = 5; @@ -293,6 +295,7 @@ public class NetworkTemplate implements Parcelable { /** * Check if mobile network classified 3G or lower with matching IMSI. */ + @Deprecated private boolean matchesMobile3gLower(NetworkIdentity ident) { ensureSubtypeAvailable(); if (ident.mType == TYPE_WIMAX) { @@ -311,6 +314,7 @@ public class NetworkTemplate implements Parcelable { /** * Check if mobile network classified 4G with matching IMSI. */ + @Deprecated private boolean matchesMobile4g(NetworkIdentity ident) { ensureSubtypeAvailable(); if (ident.mType == TYPE_WIMAX) { diff --git a/services/core/java/com/android/server/net/NetworkStatsService.java b/services/core/java/com/android/server/net/NetworkStatsService.java index 61f9a267fe..856a07667d 100644 --- a/services/core/java/com/android/server/net/NetworkStatsService.java +++ b/services/core/java/com/android/server/net/NetworkStatsService.java @@ -26,9 +26,7 @@ import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_USER_REMOVED; import static android.content.Intent.EXTRA_UID; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; -import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; import static android.net.ConnectivityManager.isNetworkTypeMobile; -import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_DEFAULT; @@ -55,8 +53,6 @@ import static android.provider.Settings.Global.NETSTATS_UID_TAG_BUCKET_DURATION; import static android.provider.Settings.Global.NETSTATS_UID_TAG_DELETE_AGE; import static android.provider.Settings.Global.NETSTATS_UID_TAG_PERSIST_BYTES; import static android.provider.Settings.Global.NETSTATS_UID_TAG_ROTATE_AGE; -import static android.telephony.PhoneStateListener.LISTEN_DATA_CONNECTION_STATE; -import static android.telephony.PhoneStateListener.LISTEN_NONE; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; @@ -102,7 +98,6 @@ import android.os.SystemClock; import android.os.UserHandle; import android.provider.Settings; import android.provider.Settings.Global; -import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.text.format.DateUtils; import android.util.ArrayMap; @@ -308,10 +303,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { bootstrapStatsLocked(); } - // watch for network interfaces to be claimed - final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE); - mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); - // watch for tethering changes final IntentFilter tetherFilter = new IntentFilter(ACTION_TETHER_STATE_CHANGED); mContext.registerReceiver(mTetherReceiver, tetherFilter, null, mHandler); @@ -338,12 +329,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // ignored; service lives in system_server } - // watch for networkType changes that aren't broadcast through - // CONNECTIVITY_ACTION_IMMEDIATE above. - if (!COMBINE_SUBTYPE_ENABLED) { - mTeleManager.listen(mPhoneListener, LISTEN_DATA_CONNECTION_STATE); - } - registerPollAlarmLocked(); registerGlobalAlert(); } @@ -358,16 +343,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } private void shutdownLocked() { - mContext.unregisterReceiver(mConnReceiver); mContext.unregisterReceiver(mTetherReceiver); mContext.unregisterReceiver(mPollReceiver); mContext.unregisterReceiver(mRemovedReceiver); mContext.unregisterReceiver(mShutdownReceiver); - if (!COMBINE_SUBTYPE_ENABLED) { - mTeleManager.listen(mPhoneListener, LISTEN_NONE); - } - final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); @@ -619,6 +599,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } } + @Override + public void forceUpdateIfaces() { + mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); + assertBandwidthControlEnabled(); + + final long token = Binder.clearCallingIdentity(); + try { + updateIfaces(); + } finally { + Binder.restoreCallingIdentity(token); + } + } + @Override public void forceUpdate() { mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); @@ -675,20 +668,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mGlobalAlertBytes = mSettings.getGlobalAlertBytes(mPersistThreshold); } - /** - * Receiver that watches for {@link IConnectivityManager} to claim network - * interfaces. Used to associate {@link TelephonyManager#getSubscriberId()} - * with mobile interfaces. - */ - private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - // on background handler thread, and verified CONNECTIVITY_INTERNAL - // permission above. - updateIfaces(); - } - }; - /** * Receiver that watches for {@link Tethering} to claim interface pairs. */ @@ -784,35 +763,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } }; - private int mLastPhoneState = TelephonyManager.DATA_UNKNOWN; - private int mLastPhoneNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; - - /** - * Receiver that watches for {@link TelephonyManager} changes, such as - * transitioning between network types. - */ - private PhoneStateListener mPhoneListener = new PhoneStateListener() { - @Override - public void onDataConnectionStateChanged(int state, int networkType) { - final boolean stateChanged = state != mLastPhoneState; - final boolean networkTypeChanged = networkType != mLastPhoneNetworkType; - - if (networkTypeChanged && !stateChanged) { - // networkType changed without a state change, which means we - // need to roll our own update. delay long enough for - // ConnectivityManager to process. - // TODO: add direct event to ConnectivityService instead of - // relying on this delay. - if (LOGV) Slog.v(TAG, "triggering delayed updateIfaces()"); - mHandler.sendMessageDelayed( - mHandler.obtainMessage(MSG_UPDATE_IFACES), SECOND_IN_MILLIS); - } - - mLastPhoneState = state; - mLastPhoneNetworkType = networkType; - } - }; - private void updateIfaces() { synchronized (mStatsLock) { mWakeLock.acquire();