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:
@@ -278,7 +278,6 @@
|
|||||||
<activity android:name="Settings$VpnSettingsActivity"
|
<activity android:name="Settings$VpnSettingsActivity"
|
||||||
android:theme="@android:style/Theme.Holo"
|
android:theme="@android:style/Theme.Holo"
|
||||||
android:label="@string/vpn_settings_activity_title"
|
android:label="@string/vpn_settings_activity_title"
|
||||||
android:configChanges="orientation|keyboardHidden"
|
|
||||||
android:clearTaskOnLaunch="true">
|
android:clearTaskOnLaunch="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|||||||
@@ -182,6 +182,8 @@ public class SettingsPreferenceFragment extends PreferenceFragment
|
|||||||
+ DialogCreatable.class.getName());
|
+ DialogCreatable.class.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// restore mDialogFragment in mParentFragment
|
||||||
|
((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
|
||||||
}
|
}
|
||||||
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
|
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
|
||||||
}
|
}
|
||||||
@@ -204,6 +206,16 @@ public class SettingsPreferenceFragment extends PreferenceFragment
|
|||||||
public int getDialogId() {
|
public int getDialogId() {
|
||||||
return mDialogId;
|
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() {
|
protected boolean hasNextButton() {
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
|
|
||||||
private static final String KEY_ACTIVE_PROFILE = "ActiveProfile";
|
private static final String KEY_ACTIVE_PROFILE = "ActiveProfile";
|
||||||
private static final String KEY_PROFILE_CONNECTING = "ProfileConnecting";
|
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;
|
private static final int REQUEST_ADD_OR_EDIT_PROFILE = 1;
|
||||||
static final int REQUEST_SELECT_VPN_TYPE = 2;
|
static final int REQUEST_SELECT_VPN_TYPE = 2;
|
||||||
@@ -140,33 +141,6 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
addPreferencesFromResource(R.xml.vpn_settings);
|
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());
|
mVpnManager = new VpnManager(getActivity());
|
||||||
// restore VpnProfile list and construct VpnPreference map
|
// 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();
|
retrieveVpnListFromStorage();
|
||||||
restoreInstanceState(savedInstanceState);
|
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
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
// ignore vpn connectivity event
|
// ignore vpn connectivity event
|
||||||
@@ -205,8 +209,7 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
super.onResume();
|
super.onResume();
|
||||||
updatePreferenceMap();
|
updatePreferenceMap();
|
||||||
|
|
||||||
if (DEBUG)
|
if (DEBUG) Log.d(TAG, "onResume");
|
||||||
Log.d(TAG, "onResume");
|
|
||||||
|
|
||||||
// listen to vpn connectivity event
|
// listen to vpn connectivity event
|
||||||
mVpnManager.registerConnectivityReceiver(mConnectivityReceiver);
|
mVpnManager.registerConnectivityReceiver(mConnectivityReceiver);
|
||||||
@@ -249,17 +252,7 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void showDialog(int dialogId) {
|
public Dialog onCreateDialog (int id) {
|
||||||
super.showDialog(dialogId);
|
|
||||||
|
|
||||||
if (dialogId == DIALOG_CONNECT) {
|
|
||||||
mConnectDialogShowing = true;
|
|
||||||
setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
||||||
public void onDismiss(DialogInterface dialog) {
|
|
||||||
mConnectDialogShowing = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
setOnCancelListener(new DialogInterface.OnCancelListener() {
|
setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||||
public void onCancel(DialogInterface dialog) {
|
public void onCancel(DialogInterface dialog) {
|
||||||
if (mActiveProfile != null) {
|
if (mActiveProfile != null) {
|
||||||
@@ -272,12 +265,15 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
onIdle();
|
onIdle();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Dialog onCreateDialog (int id) {
|
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case DIALOG_CONNECT:
|
case DIALOG_CONNECT:
|
||||||
|
mConnectDialogShowing = true;
|
||||||
|
setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||||
|
public void onDismiss(DialogInterface dialog) {
|
||||||
|
mConnectDialogShowing = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
return createConnectDialog();
|
return createConnectDialog();
|
||||||
|
|
||||||
case DIALOG_SECRET_NOT_SET:
|
case DIALOG_SECRET_NOT_SET:
|
||||||
@@ -524,7 +520,6 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
String error = mConnectingActor.validateInputs(d);
|
String error = mConnectingActor.validateInputs(d);
|
||||||
if (error == null) {
|
if (error == null) {
|
||||||
mConnectingActor.connect(d);
|
mConnectingActor.connect(d);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
// show error dialog
|
// show error dialog
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
@@ -795,7 +790,6 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
|
|
||||||
private void changeState(VpnProfile p, VpnState state) {
|
private void changeState(VpnProfile p, VpnState state) {
|
||||||
VpnState oldState = p.getState();
|
VpnState oldState = p.getState();
|
||||||
if (oldState == state) return;
|
|
||||||
p.setState(state);
|
p.setState(state);
|
||||||
mVpnPreferenceMap.get(p.getName()).setSummary(
|
mVpnPreferenceMap.get(p.getName()).setSummary(
|
||||||
getProfileSummaryString(p));
|
getProfileSummaryString(p));
|
||||||
@@ -808,7 +802,9 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CONNECTING:
|
case CONNECTING:
|
||||||
mConnectingActor = getActor(p);
|
if (mConnectingActor == null) {
|
||||||
|
mConnectingActor = getActor(p);
|
||||||
|
}
|
||||||
// $FALL-THROUGH$
|
// $FALL-THROUGH$
|
||||||
case DISCONNECTING:
|
case DISCONNECTING:
|
||||||
mActiveProfile = p;
|
mActiveProfile = p;
|
||||||
@@ -883,12 +879,12 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
mVpnPreferenceMap = new LinkedHashMap<String, VpnPreference>();
|
mVpnPreferenceMap = new LinkedHashMap<String, VpnPreference>();
|
||||||
mVpnListContainer.removeAll();
|
mVpnListContainer.removeAll();
|
||||||
for (VpnProfile p : sVpnProfileList) {
|
for (VpnProfile p : sVpnProfileList) {
|
||||||
addPreferenceFor(p, false);
|
addPreferenceFor(p, true);
|
||||||
}
|
}
|
||||||
// reset the mActiveProfile if the profile has been removed from the
|
// reset the mActiveProfile if the profile has been removed from the
|
||||||
// other instance.
|
// other instance.
|
||||||
if ((mActiveProfile != null)
|
if ((mActiveProfile != null)
|
||||||
&& mVpnPreferenceMap.containsKey(mActiveProfile.getName())) {
|
&& !mVpnPreferenceMap.containsKey(mActiveProfile.getName())) {
|
||||||
onIdle();
|
onIdle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -924,11 +920,6 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
for (VpnProfile p : sVpnProfileList) {
|
for (VpnProfile p : sVpnProfileList) {
|
||||||
changeState(p, mVpnManager.getState(p));
|
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
|
// A sanity check. Returns true if the profile directory name and profile ID
|
||||||
|
|||||||
Reference in New Issue
Block a user