OSDN Git Service

Fix the NPE bug due to the process/activity restart.
authorChung-yih Wang <cywang@google.com>
Wed, 5 Jan 2011 02:21:01 +0000 (10:21 +0800)
committerChung-yih Wang <cywang@google.com>
Wed, 5 Jan 2011 02:40:31 +0000 (10:40 +0800)
The active profile and its state were not stored if the setting process
was killed due to the resource. Here we add the state persistence
mechanism to fix this bug.

bug:3293236

Change-Id: I8b8068972237bde06ca4b3f73ecc48c02c4f0833

src/com/android/settings/vpn/VpnSettings.java

index c11b2fd..0587619 100644 (file)
@@ -90,6 +90,9 @@ public class VpnSettings extends SettingsPreferenceFragment
     private static final String PROFILES_ROOT = VpnManager.getProfilePath() + "/";
     private static final String PROFILE_OBJ_FILE = ".pobj";
 
+    private static final String KEY_ACTIVE_PROFILE = "ActiveProfile";
+    private static final String KEY_PROFILE_CONNECTING = "ProfileConnecting";
+
     private static final int REQUEST_ADD_OR_EDIT_PROFILE = 1;
     static final int REQUEST_SELECT_VPN_TYPE = 2;
 
@@ -145,6 +148,28 @@ public class VpnSettings extends SettingsPreferenceFragment
     }
 
     @Override
+    public void onSaveInstanceState(Bundle savedInstanceState) {
+        if (mActiveProfile != null) {
+            savedInstanceState.putString(KEY_ACTIVE_PROFILE,
+                    mActiveProfile.getId());
+            savedInstanceState.putBoolean(KEY_PROFILE_CONNECTING,
+                    (mConnectingActor != null));
+        }
+        super.onSaveInstanceState(savedInstanceState);
+    }
+
+    private void restoreInstanceState(Bundle savedInstanceState) {
+        if (savedInstanceState == null) return;
+        String profileId = savedInstanceState.getString(KEY_ACTIVE_PROFILE);
+        if (profileId != null) {
+            mActiveProfile = getProfile(getProfileIndexFromId(profileId));
+            if (savedInstanceState.getBoolean(KEY_PROFILE_CONNECTING)) {
+                mConnectingActor = getActor(mActiveProfile);
+            }
+        }
+    }
+
+    @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
@@ -170,6 +195,7 @@ public class VpnSettings extends SettingsPreferenceFragment
 
         retrieveVpnListFromStorage();
         checkVpnConnectionStatusInBackground();
+        restoreInstanceState(savedInstanceState);
     }
 
     @Override