Clear mDialogFragment when it's detached
and re-associate it when it's re-created.
Before this CL, the association is gone when fragment goes through the pause-resume
cycle.
Similarly, restore onDismiss and onCancel listeners in VpnSettings.onCreateDialog(),
restore states in onCreate() instead of onActivityCreated() so that screen rotation
can be handled correctly.
Now that profiles are shared between Settings instances, always handle state change
in VpnSettings.changeState() so that state changed in one instance can be conveyed
to the other and preferences can be correctly enabled/disabled.
In additions, fix some trivial mistakes in VpnSettings.
Bug: 3396394
Change-Id: I242e1ed6c6d410b4dfefb373d8f98266fc9b46d0
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 77f703c..17bf02d 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -182,6 +182,8 @@
+ DialogCreatable.class.getName());
}
}
+ // restore mDialogFragment in mParentFragment
+ ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
}
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
}
@@ -204,6 +206,16 @@
public int getDialogId() {
return mDialogId;
}
+
+ @Override
+ public void onDetach() {
+ super.onDetach();
+
+ // in case the dialog is not explicitly removed by removeDialog()
+ if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
+ ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
+ }
+ }
}
protected boolean hasNextButton() {
diff --git a/src/com/android/settings/vpn/VpnSettings.java b/src/com/android/settings/vpn/VpnSettings.java
index 318a5bf..539a51e 100644
--- a/src/com/android/settings/vpn/VpnSettings.java
+++ b/src/com/android/settings/vpn/VpnSettings.java
@@ -86,6 +86,7 @@
private static final String KEY_ACTIVE_PROFILE = "ActiveProfile";
private static final String KEY_PROFILE_CONNECTING = "ProfileConnecting";
+ private static final String KEY_CONNECT_DIALOG_SHOWING = "ConnectDialogShowing";
private static final int REQUEST_ADD_OR_EDIT_PROFILE = 1;
static final int REQUEST_SELECT_VPN_TYPE = 2;
@@ -140,33 +141,6 @@
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.vpn_settings);
- }
-
- @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);
mVpnManager = new VpnManager(getActivity());
// restore VpnProfile list and construct VpnPreference map
@@ -182,14 +156,44 @@
}
});
- // for long-press gesture on a profile preference
- registerForContextMenu(getListView());
-
retrieveVpnListFromStorage();
restoreInstanceState(savedInstanceState);
}
@Override
+ public void onSaveInstanceState(Bundle savedInstanceState) {
+ if (mActiveProfile != null) {
+ savedInstanceState.putString(KEY_ACTIVE_PROFILE,
+ mActiveProfile.getId());
+ savedInstanceState.putBoolean(KEY_PROFILE_CONNECTING,
+ (mConnectingActor != null));
+ savedInstanceState.putBoolean(KEY_CONNECT_DIALOG_SHOWING,
+ mConnectDialogShowing);
+ }
+ 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);
+ }
+ mConnectDialogShowing = savedInstanceState.getBoolean(KEY_CONNECT_DIALOG_SHOWING);
+ }
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ // for long-press gesture on a profile preference
+ registerForContextMenu(getListView());
+ }
+
+ @Override
public void onPause() {
// ignore vpn connectivity event
mVpnManager.unregisterConnectivityReceiver(mConnectivityReceiver);
@@ -205,8 +209,7 @@
super.onResume();
updatePreferenceMap();
- if (DEBUG)
- Log.d(TAG, "onResume");
+ if (DEBUG) Log.d(TAG, "onResume");
// listen to vpn connectivity event
mVpnManager.registerConnectivityReceiver(mConnectivityReceiver);
@@ -249,17 +252,7 @@
}
@Override
- protected void showDialog(int dialogId) {
- super.showDialog(dialogId);
-
- if (dialogId == DIALOG_CONNECT) {
- mConnectDialogShowing = true;
- setOnDismissListener(new DialogInterface.OnDismissListener() {
- public void onDismiss(DialogInterface dialog) {
- mConnectDialogShowing = false;
- }
- });
- }
+ public Dialog onCreateDialog (int id) {
setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
if (mActiveProfile != null) {
@@ -272,12 +265,15 @@
onIdle();
}
});
- }
- @Override
- public Dialog onCreateDialog (int id) {
switch (id) {
case DIALOG_CONNECT:
+ mConnectDialogShowing = true;
+ setOnDismissListener(new DialogInterface.OnDismissListener() {
+ public void onDismiss(DialogInterface dialog) {
+ mConnectDialogShowing = false;
+ }
+ });
return createConnectDialog();
case DIALOG_SECRET_NOT_SET:
@@ -524,7 +520,6 @@
String error = mConnectingActor.validateInputs(d);
if (error == null) {
mConnectingActor.connect(d);
- return;
} else {
// show error dialog
final Activity activity = getActivity();
@@ -795,7 +790,6 @@
private void changeState(VpnProfile p, VpnState state) {
VpnState oldState = p.getState();
- if (oldState == state) return;
p.setState(state);
mVpnPreferenceMap.get(p.getName()).setSummary(
getProfileSummaryString(p));
@@ -808,7 +802,9 @@
break;
case CONNECTING:
- mConnectingActor = getActor(p);
+ if (mConnectingActor == null) {
+ mConnectingActor = getActor(p);
+ }
// $FALL-THROUGH$
case DISCONNECTING:
mActiveProfile = p;
@@ -883,12 +879,12 @@
mVpnPreferenceMap = new LinkedHashMap<String, VpnPreference>();
mVpnListContainer.removeAll();
for (VpnProfile p : sVpnProfileList) {
- addPreferenceFor(p, false);
+ addPreferenceFor(p, true);
}
// reset the mActiveProfile if the profile has been removed from the
// other instance.
if ((mActiveProfile != null)
- && mVpnPreferenceMap.containsKey(mActiveProfile.getName())) {
+ && !mVpnPreferenceMap.containsKey(mActiveProfile.getName())) {
onIdle();
}
}
@@ -924,11 +920,6 @@
for (VpnProfile p : sVpnProfileList) {
changeState(p, mVpnManager.getState(p));
}
- // make preferences appear
- for (VpnProfile p : sVpnProfileList) {
- VpnPreference pref = mVpnPreferenceMap.get(p.getName());
- mVpnListContainer.addPreference(pref);
- }
}
// A sanity check. Returns true if the profile directory name and profile ID