@@ -1625,6 +1625,10 @@ private void handleDisconnect(NetworkInfo info) {
16251625 int prevNetType = info .getType ();
16261626
16271627 mNetTrackers [prevNetType ].setTeardownRequested (false );
1628+
1629+ // Remove idletimer previously setup in {@code handleConnect}
1630+ removeDataActivityTracking (prevNetType );
1631+
16281632 /*
16291633 * If the disconnected network is not the active one, then don't report
16301634 * this as a loss of connectivity. What probably happened is that we're
@@ -1905,6 +1909,8 @@ void systemReady() {
19051909 private void handleConnect (NetworkInfo info ) {
19061910 final int type = info .getType ();
19071911
1912+ setupDataActivityTracking (type );
1913+
19081914 // snapshot isFailover, because sendConnectedBroadcast() resets it
19091915 boolean isFailover = info .isFailover ();
19101916 final NetworkStateTracker thisNet = mNetTrackers [type ];
@@ -1976,6 +1982,58 @@ private void handleConnect(NetworkInfo info) {
19761982 }
19771983 }
19781984
1985+ /**
1986+ * Setup data activity tracking for the given network interface.
1987+ *
1988+ * Every {@code setupDataActivityTracking} should be paired with a
1989+ * {@link removeDataActivityTracking} for cleanup.
1990+ */
1991+ private void setupDataActivityTracking (int type ) {
1992+ final NetworkStateTracker thisNet = mNetTrackers [type ];
1993+ final String iface = thisNet .getLinkProperties ().getInterfaceName ();
1994+
1995+ final int timeout ;
1996+
1997+ if (ConnectivityManager .isNetworkTypeMobile (type )) {
1998+ timeout = Settings .Secure .getInt (mContext .getContentResolver (),
1999+ Settings .Secure .DATA_ACTIVITY_TIMEOUT_MOBILE ,
2000+ 0 );
2001+ // Canonicalize mobile network type
2002+ type = ConnectivityManager .TYPE_MOBILE ;
2003+ } else if (ConnectivityManager .TYPE_WIFI == type ) {
2004+ timeout = Settings .Secure .getInt (mContext .getContentResolver (),
2005+ Settings .Secure .DATA_ACTIVITY_TIMEOUT_WIFI ,
2006+ 0 );
2007+ } else {
2008+ // do not track any other networks
2009+ timeout = 0 ;
2010+ }
2011+
2012+ if (timeout > 0 && iface != null ) {
2013+ try {
2014+ mNetd .addIdleTimer (iface , timeout , Integer .toString (type ));
2015+ } catch (RemoteException e ) {
2016+ }
2017+ }
2018+ }
2019+
2020+ /**
2021+ * Remove data activity tracking when network disconnects.
2022+ */
2023+ private void removeDataActivityTracking (int type ) {
2024+ final NetworkStateTracker net = mNetTrackers [type ];
2025+ final String iface = net .getLinkProperties ().getInterfaceName ();
2026+
2027+ if (iface != null && (ConnectivityManager .isNetworkTypeMobile (type ) ||
2028+ ConnectivityManager .TYPE_WIFI == type )) {
2029+ try {
2030+ // the call fails silently if no idletimer setup for this interface
2031+ mNetd .removeIdleTimer (iface );
2032+ } catch (RemoteException e ) {
2033+ }
2034+ }
2035+ }
2036+
19792037 /**
19802038 * After a change in the connectivity state of a network. We're mainly
19812039 * concerned with making sure that the list of DNS servers is set up
0 commit comments