diff options
author | Paul Jensen <pauljensen@google.com> | 2014-08-21 12:44:07 -0400 |
---|---|---|
committer | Paul Jensen <pauljensen@google.com> | 2014-08-22 08:55:44 -0400 |
commit | 79a08051c5588d8420656813b21993d490e93dd0 (patch) | |
tree | a7a86a9f6dc381fa7b0ef4747d6e7cfb18e68a3b /services/core/java/com/android/server/connectivity/NetworkMonitor.java | |
parent | b9e468cb9d77abf57473436e29042e1b27d9f70b (diff) | |
download | frameworks_base-79a08051c5588d8420656813b21993d490e93dd0.zip frameworks_base-79a08051c5588d8420656813b21993d490e93dd0.tar.gz frameworks_base-79a08051c5588d8420656813b21993d490e93dd0.tar.bz2 |
Use alarm rather than delayed message to end lingering.
Ending lingering is critical to good battery life. Delayed messages
are not delivered when the device is in a deep sleep, alarms are
however.
bug:17140462
Change-Id: I07e905479f316217d816b36e8c4d96d5daed0047
Diffstat (limited to 'services/core/java/com/android/server/connectivity/NetworkMonitor.java')
-rw-r--r-- | services/core/java/com/android/server/connectivity/NetworkMonitor.java | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/services/core/java/com/android/server/connectivity/NetworkMonitor.java b/services/core/java/com/android/server/connectivity/NetworkMonitor.java index b09298c..98a5b61 100644 --- a/services/core/java/com/android/server/connectivity/NetworkMonitor.java +++ b/services/core/java/com/android/server/connectivity/NetworkMonitor.java @@ -16,6 +16,7 @@ package com.android.server.connectivity; +import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ComponentName; @@ -213,6 +214,7 @@ public class NetworkMonitor extends StateMachine { private final NetworkAgentInfo mNetworkAgentInfo; private final TelephonyManager mTelephonyManager; private final WifiManager mWifiManager; + private final AlarmManager mAlarmManager; private String mServer; private boolean mIsCaptivePortalCheckEnabled = false; @@ -238,6 +240,7 @@ public class NetworkMonitor extends StateMachine { mNetworkAgentInfo = networkAgentInfo; mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); addState(mDefaultState); addState(mOfflineState, mDefaultState); @@ -598,10 +601,38 @@ public class NetworkMonitor extends StateMachine { } private class LingeringState extends State { + private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired"; + private static final String EXTRA_NETID = "lingerExpiredNetId"; + private static final String EXTRA_TOKEN = "lingerExpiredToken"; + + private class LingerExpiredBroadcastReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(ACTION_LINGER_EXPIRED) && + Integer.parseInt(intent.getStringExtra(EXTRA_NETID)) == + mNetworkAgentInfo.network.netId) { + sendMessage(CMD_LINGER_EXPIRED, + Integer.parseInt(intent.getStringExtra(EXTRA_TOKEN))); + } + } + } + + private BroadcastReceiver mBroadcastReceiver; + private PendingIntent mIntent; + @Override public void enter() { - Message message = obtainMessage(CMD_LINGER_EXPIRED, ++mLingerToken, 0); - sendMessageDelayed(message, mLingerDelayMs); + mBroadcastReceiver = new LingerExpiredBroadcastReceiver(); + mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(ACTION_LINGER_EXPIRED)); + + Intent intent = new Intent(ACTION_LINGER_EXPIRED, null); + intent.putExtra(EXTRA_NETID, String.valueOf(mNetworkAgentInfo.network.netId)); + intent.putExtra(EXTRA_TOKEN, String.valueOf(++mLingerToken)); + mIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); + long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs; + mAlarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, + // Give a specific window so we aren't subject to unknown inexactitude. + mLingerDelayMs / 6, mIntent); } @Override @@ -622,6 +653,12 @@ public class NetworkMonitor extends StateMachine { return NOT_HANDLED; } } + + @Override + public void exit() { + mAlarmManager.cancel(mIntent); + mContext.unregisterReceiver(mBroadcastReceiver); + } } /** |