diff options
3 files changed, 164 insertions, 48 deletions
diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index 9bd8f36ad2ba..6001be92a322 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -56,13 +56,21 @@ public final class NfcAdapterExtras { public static final String ACTION_RF_FIELD_OFF_DETECTED = "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; - // protected by NfcAdapterExtras.class, and final after first construction + // protected by NfcAdapterExtras.class, and final after first construction, + // except for attemptDeadServiceRecovery() when NFC crashes - we accept a + // best effort recovery + private static NfcAdapter sAdapter; private static INfcAdapterExtras sService; private static NfcAdapterExtras sSingleton; private static NfcExecutionEnvironment sEmbeddedEe; private static CardEmulationRoute sRouteOff; private static CardEmulationRoute sRouteOnWhenScreenOn; + /** get service handles */ + private static void initService() { + sService = sAdapter.getNfcAdapterExtrasInterface(); + } + /** * Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}. * @@ -76,12 +84,13 @@ public final class NfcAdapterExtras { synchronized(NfcAdapterExtras.class) { if (sSingleton == null) { try { - sService = adapter.getNfcAdapterExtrasInterface(); - sEmbeddedEe = new NfcExecutionEnvironment(sService); + sAdapter = adapter; sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); + sSingleton = new NfcAdapterExtras(); + sEmbeddedEe = new NfcExecutionEnvironment(sSingleton); sRouteOnWhenScreenOn = new CardEmulationRoute( CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); - sSingleton = new NfcAdapterExtras(); + initService(); } finally { if (sSingleton == null) { sService = null; @@ -136,6 +145,19 @@ public final class NfcAdapterExtras { } /** + * NFC service dead - attempt best effort recovery + */ + void attemptDeadServiceRecovery(Exception e) { + Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); + sAdapter.attemptDeadServiceRecovery(e); + initService(); + } + + INfcAdapterExtras getService() { + return sService; + } + + /** * Get the routing state of this NFC EE. * * <p class="note"> @@ -150,7 +172,7 @@ public final class NfcAdapterExtras { sRouteOff : sRouteOnWhenScreenOn; } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); return sRouteOff; } } @@ -169,7 +191,7 @@ public final class NfcAdapterExtras { try { sService.setCardEmulationRoute(route.route); } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); } } @@ -190,7 +212,7 @@ public final class NfcAdapterExtras { try { sService.registerTearDownApdus(packageName, apdus); } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); } } @@ -198,7 +220,7 @@ public final class NfcAdapterExtras { try { sService.unregisterTearDownApdus(packageName); } catch (RemoteException e) { - Log.e(TAG, "", e); + attemptDeadServiceRecovery(e); } } } diff --git a/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java b/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java index 3efe49236de9..eb2f6f859191 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcExecutionEnvironment.java @@ -29,7 +29,7 @@ import android.os.IBinder; import android.os.RemoteException; public class NfcExecutionEnvironment { - private final INfcAdapterExtras mService; + private final NfcAdapterExtras mExtras; /** * Broadcast Action: An ISO-DEP AID was selected. @@ -55,8 +55,8 @@ public class NfcExecutionEnvironment { */ public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID"; - NfcExecutionEnvironment(INfcAdapterExtras service) { - mService = service; + NfcExecutionEnvironment(NfcAdapterExtras extras) { + mExtras = extras; } /** @@ -75,10 +75,11 @@ public class NfcExecutionEnvironment { */ public void open() throws IOException { try { - Bundle b = mService.open(new Binder()); + Bundle b = mExtras.getService().open(new Binder()); throwBundle(b); } catch (RemoteException e) { - return; + mExtras.attemptDeadServiceRecovery(e); + throw new IOException("NFC Service was dead, try again"); } } @@ -92,9 +93,10 @@ public class NfcExecutionEnvironment { */ public void close() throws IOException { try { - throwBundle(mService.close()); + throwBundle(mExtras.getService().close()); } catch (RemoteException e) { - return; + mExtras.attemptDeadServiceRecovery(e); + throw new IOException("NFC Service was dead"); } } @@ -109,9 +111,10 @@ public class NfcExecutionEnvironment { public byte[] transceive(byte[] in) throws IOException { Bundle b; try { - b = mService.transceive(in); + b = mExtras.getService().transceive(in); } catch (RemoteException e) { - throw new IOException(e.getMessage()); + mExtras.attemptDeadServiceRecovery(e); + throw new IOException("NFC Service was dead, need to re-open"); } throwBundle(b); return b.getByteArray("out"); diff --git a/wifi/java/android/net/wifi/WifiStateTracker.java b/wifi/java/android/net/wifi/WifiStateTracker.java index bf2d03376d78..90295e0cb80d 100644 --- a/wifi/java/android/net/wifi/WifiStateTracker.java +++ b/wifi/java/android/net/wifi/WifiStateTracker.java @@ -23,6 +23,18 @@ import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING; import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN; import android.app.ActivityManagerNative; +import android.app.AlarmManager; +import android.app.Notification; +import android.app.PendingIntent; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothHeadset; +import android.bluetooth.BluetoothA2dp; +import android.content.BroadcastReceiver; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.database.ContentObserver; import android.net.NetworkInfo; import android.net.NetworkStateTracker; import android.net.DhcpInfo; @@ -32,8 +44,10 @@ import android.net.NetworkInfo.DetailedState; import android.net.NetworkInfo.State; import android.os.Message; import android.os.Parcelable; +import android.os.PowerManager; import android.os.Handler; import android.os.HandlerThread; +import android.os.SystemClock; import android.os.SystemProperties; import android.os.Looper; import android.os.RemoteException; @@ -44,15 +58,6 @@ import android.text.TextUtils; import android.util.EventLog; import android.util.Log; import android.util.Config; -import android.app.Notification; -import android.app.PendingIntent; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHeadset; -import android.bluetooth.BluetoothA2dp; -import android.content.ContentResolver; -import android.content.Intent; -import android.content.Context; -import android.database.ContentObserver; import com.android.internal.app.IBatteryStats; import java.net.UnknownHostException; @@ -91,15 +96,16 @@ public class WifiStateTracker extends NetworkStateTracker { private static final int EVENT_INTERFACE_CONFIGURATION_FAILED = 7; private static final int EVENT_POLL_INTERVAL = 8; private static final int EVENT_DHCP_START = 9; - private static final int EVENT_DEFERRED_DISCONNECT = 10; - private static final int EVENT_DEFERRED_RECONNECT = 11; + private static final int EVENT_DHCP_RENEW = 10; + private static final int EVENT_DEFERRED_DISCONNECT = 11; + private static final int EVENT_DEFERRED_RECONNECT = 12; /** * The driver is started or stopped. The object will be the state: true for * started, false for stopped. */ - private static final int EVENT_DRIVER_STATE_CHANGED = 12; - private static final int EVENT_PASSWORD_KEY_MAY_BE_INCORRECT = 13; - private static final int EVENT_MAYBE_START_SCAN_POST_DISCONNECT = 14; + private static final int EVENT_DRIVER_STATE_CHANGED = 13; + private static final int EVENT_PASSWORD_KEY_MAY_BE_INCORRECT = 14; + private static final int EVENT_MAYBE_START_SCAN_POST_DISCONNECT = 15; /** * The driver state indication. @@ -218,6 +224,15 @@ public class WifiStateTracker extends NetworkStateTracker { private boolean mUseStaticIp = false; private int mReconnectCount; + private AlarmManager mAlarmManager; + private PendingIntent mDhcpRenewalIntent; + private PowerManager.WakeLock mDhcpRenewWakeLock; + private static final String WAKELOCK_TAG = "*wifi*"; + + private static final int DHCP_RENEW = 0; + private static final String ACTION_DHCP_RENEW = "android.net.wifi.DHCP_RENEW"; + + /* Tracks if any network in the configuration is disabled */ private AtomicBoolean mIsAnyNetworkDisabled = new AtomicBoolean(false); @@ -386,6 +401,27 @@ public class WifiStateTracker extends NetworkStateTracker { mDhcpInfo = new DhcpInfo(); mRunState = RUN_STATE_STARTING; + mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); + Intent dhcpRenewalIntent = new Intent(ACTION_DHCP_RENEW, null); + mDhcpRenewalIntent = PendingIntent.getBroadcast(mContext, DHCP_RENEW, dhcpRenewalIntent, 0); + + mContext.registerReceiver( + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + //DHCP renew + if (mDhcpTarget != null) { + Log.d(TAG, "Sending a DHCP renewal"); + //acquire a 40s wakelock to finish DHCP renewal + mDhcpRenewWakeLock.acquire(40000); + mDhcpTarget.sendEmptyMessage(EVENT_DHCP_RENEW); + } + } + },new IntentFilter(ACTION_DHCP_RENEW)); + + PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); + mDhcpRenewWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); + // Setting is in seconds NOTIFICATION_REPEAT_DELAY_MS = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l; @@ -2389,7 +2425,7 @@ public class WifiStateTracker extends NetworkStateTracker { private class DhcpHandler extends Handler { - private Handler mTarget; + private Handler mWifiStateTrackerHandler; /** * Whether to skip the DHCP result callback to the target. For example, @@ -2410,10 +2446,10 @@ public class WifiStateTracker extends NetworkStateTracker { * in an error state and we will not disable coexistence. */ private BluetoothHeadset mBluetoothHeadset; - + public DhcpHandler(Looper looper, Handler target) { super(looper); - mTarget = target; + mWifiStateTrackerHandler = target; mBluetoothHeadset = new BluetoothHeadset(mContext, null); } @@ -2423,7 +2459,7 @@ public class WifiStateTracker extends NetworkStateTracker { switch (msg.what) { case EVENT_DHCP_START: - + case EVENT_DHCP_RENEW: boolean modifiedBluetoothCoexistenceMode = false; int powerMode = DRIVER_POWER_MODE_AUTO; @@ -2465,14 +2501,70 @@ public class WifiStateTracker extends NetworkStateTracker { // A new request is being made, so assume we will callback mCancelCallback = false; } - Log.d(TAG, "DhcpHandler: DHCP request started"); - if (NetworkUtils.runDhcp(mInterfaceName, mDhcpInfo)) { - event = EVENT_INTERFACE_CONFIGURATION_SUCCEEDED; - if (LOCAL_LOGD) Log.v(TAG, "DhcpHandler: DHCP request succeeded"); - } else { - event = EVENT_INTERFACE_CONFIGURATION_FAILED; - Log.i(TAG, "DhcpHandler: DHCP request failed: " + - NetworkUtils.getDhcpError()); + + if (msg.what == EVENT_DHCP_START) { + Log.d(TAG, "DHCP request started"); + if (NetworkUtils.runDhcp(mInterfaceName, mDhcpInfo)) { + event = EVENT_INTERFACE_CONFIGURATION_SUCCEEDED; + Log.d(TAG, "DHCP succeeded with lease: " + mDhcpInfo.leaseDuration); + //Do it a bit earlier than half the lease duration time + //to beat the native DHCP client and avoid extra packets + //48% for one hour lease time = 29 minutes + mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, + SystemClock.elapsedRealtime() + + mDhcpInfo.leaseDuration * 480, //in milliseconds + mDhcpRenewalIntent); + } else { + event = EVENT_INTERFACE_CONFIGURATION_FAILED; + Log.e(TAG, "DHCP request failed: " + NetworkUtils.getDhcpError()); + } + synchronized (this) { + if (!mCancelCallback) { + mWifiStateTrackerHandler.sendEmptyMessage(event); + } + } + + } else if (msg.what == EVENT_DHCP_RENEW) { + Log.d(TAG, "DHCP renewal started"); + int oIp = mDhcpInfo.ipAddress; + int oGw = mDhcpInfo.gateway; + int oMsk = mDhcpInfo.netmask; + int oDns1 = mDhcpInfo.dns1; + int oDns2 = mDhcpInfo.dns2; + + if (NetworkUtils.runDhcpRenew(mInterfaceName, mDhcpInfo)) { + Log.d(TAG, "DHCP renewal with lease: " + mDhcpInfo.leaseDuration); + + boolean changed = + (oIp != mDhcpInfo.ipAddress || + oGw != mDhcpInfo.gateway || + oMsk != mDhcpInfo.netmask || + oDns1 != mDhcpInfo.dns1 || + oDns2 != mDhcpInfo.dns2); + + if (changed) { + Log.d(TAG, "IP config change on renewal"); + mWifiInfo.setIpAddress(mDhcpInfo.ipAddress); + NetworkUtils.resetConnections(mInterfaceName); + msg = mTarget.obtainMessage(EVENT_CONFIGURATION_CHANGED, + mNetworkInfo); + msg.sendToTarget(); + } + + mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, + SystemClock.elapsedRealtime() + + mDhcpInfo.leaseDuration * 480, + mDhcpRenewalIntent); + } else { + event = EVENT_INTERFACE_CONFIGURATION_FAILED; + Log.d(TAG, "DHCP renewal failed: " + NetworkUtils.getDhcpError()); + + synchronized (this) { + if (!mCancelCallback) { + mWifiStateTrackerHandler.sendEmptyMessage(event); + } + } + } } if (powerMode != DRIVER_POWER_MODE_ACTIVE) { @@ -2485,17 +2577,15 @@ public class WifiStateTracker extends NetworkStateTracker { WifiNative.BLUETOOTH_COEXISTENCE_MODE_SENSE); } - synchronized (this) { - if (!mCancelCallback) { - mTarget.sendEmptyMessage(event); - } - } break; } } public synchronized void setCancelCallback(boolean cancelCallback) { mCancelCallback = cancelCallback; + if (cancelCallback) { + mAlarmManager.cancel(mDhcpRenewalIntent); + } } /** @@ -2511,6 +2601,7 @@ public class WifiStateTracker extends NetworkStateTracker { int state = mBluetoothHeadset.getState(mBluetoothHeadset.getCurrentHeadset()); return state == BluetoothHeadset.STATE_DISCONNECTED; } + } private void checkUseStaticIp() { |