Merge "Fixed a crash when rotating a saved networks dialog." into lmp-dev
diff --git a/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java b/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
index a91d153..10c86dc 100644
--- a/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
+++ b/src/com/android/settings/wifi/SavedAccessPointsWifiSettings.java
@@ -53,6 +53,9 @@
     private Bundle mAccessPointSavedState;
     private AccessPoint mSelectedAccessPoint;
 
+    // Instance state key
+    private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -69,6 +72,13 @@
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
+
+        if (savedInstanceState != null) {
+            if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
+                mAccessPointSavedState =
+                    savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
+            }
+        }
     }
 
     private void initPreferences() {
@@ -150,6 +160,11 @@
     public Dialog onCreateDialog(int dialogId) {
         switch (dialogId) {
             case WifiSettings.WIFI_DIALOG_ID:
+                if (mDlgAccessPoint == null) { // For re-launch from saved state
+                    mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
+                    // Reset the saved access point data
+                    mAccessPointSavedState = null;
+                }
                 mSelectedAccessPoint = mDlgAccessPoint;
                 mDialog = new WifiDialog(getActivity(), this, mDlgAccessPoint, false);
                 return mDialog;
@@ -159,6 +174,20 @@
     }
 
     @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+
+        // If the dialog is showing, save its state.
+        if (mDialog != null && mDialog.isShowing()) {
+            if (mDlgAccessPoint != null) {
+                mAccessPointSavedState = new Bundle();
+                mDlgAccessPoint.saveWifiState(mAccessPointSavedState);
+                outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
+            }
+        }
+    }
+
+    @Override
     public void onClick(DialogInterface dialogInterface, int button) {
         if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
             mWifiManager.forget(mSelectedAccessPoint.networkId, null);