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
This commit is contained in:
Hung-ying Tyan
2011-01-28 16:17:27 +08:00
parent a5082305e8
commit 18eb39d085
3 changed files with 59 additions and 57 deletions

View File

@@ -278,7 +278,6 @@
<activity android:name="Settings$VpnSettingsActivity"
android:theme="@android:style/Theme.Holo"
android:label="@string/vpn_settings_activity_title"
android:configChanges="orientation|keyboardHidden"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@@ -182,6 +182,8 @@ public class SettingsPreferenceFragment extends PreferenceFragment
+ DialogCreatable.class.getName());
}
}
// restore mDialogFragment in mParentFragment
((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
}
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
}
@@ -204,6 +206,16 @@ public class SettingsPreferenceFragment extends PreferenceFragment
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() {

View File

@@ -86,6 +86,7 @@ public class VpnSettings extends SettingsPreferenceFragment
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 class VpnSettings extends SettingsPreferenceFragment
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,13 +156,43 @@ public class VpnSettings extends SettingsPreferenceFragment
}
});
// 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
@@ -205,8 +209,7 @@ public class VpnSettings extends SettingsPreferenceFragment
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 @@ public class VpnSettings extends SettingsPreferenceFragment
}
@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 @@ public class VpnSettings extends SettingsPreferenceFragment
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 @@ public class VpnSettings extends SettingsPreferenceFragment
String error = mConnectingActor.validateInputs(d);
if (error == null) {
mConnectingActor.connect(d);
return;
} else {
// show error dialog
final Activity activity = getActivity();
@@ -795,7 +790,6 @@ public class VpnSettings extends SettingsPreferenceFragment
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 @@ public class VpnSettings extends SettingsPreferenceFragment
break;
case CONNECTING:
mConnectingActor = getActor(p);
if (mConnectingActor == null) {
mConnectingActor = getActor(p);
}
// $FALL-THROUGH$
case DISCONNECTING:
mActiveProfile = p;
@@ -883,12 +879,12 @@ public class VpnSettings extends SettingsPreferenceFragment
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 @@ public class VpnSettings extends SettingsPreferenceFragment
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