VpnSettings: add status report.

Change-Id: Ia050d997524b39868e6acce82d12b0415909ab77
This commit is contained in:
Chia-chi Yeh
2011-07-04 03:17:33 -07:00
parent 5c9e37c295
commit 97fd85fd97
3 changed files with 65 additions and 32 deletions

View File

@@ -695,16 +695,20 @@
<item>IPSec VPN with certificates and hybrid authentication</item> <item>IPSec VPN with certificates and hybrid authentication</item>
</string-array> </string-array>
<!-- Match this with the constants in VpnProfile. --> <skip /> <!-- Match this with the constants in LegacyVpnInfo. --> <skip />
<!-- Status for a VPN network. [CHAR LIMIT=100] --> <!-- Status for a VPN network. [CHAR LIMIT=100] -->
<string-array name="vpn_states"> <string-array name="vpn_states">
<!-- Status message when VPN is disconnected. -->
<item>Disconnected</item>
<!-- Status message when VPN is initializing. -->
<item>Initializing\u2026</item>
<!-- Status message when VPN is connecting. --> <!-- Status message when VPN is connecting. -->
<item>Connecting\u2026</item> <item>Connecting\u2026</item>
<!-- Status message when VPN is connected. --> <!-- Status message when VPN is connected. -->
<item>Connected</item> <item>Connected</item>
<!-- Status message when VPN is disconnected. --> <!-- Status message when VPN is timeout. -->
<item>Disconnected</item> <item>Timeout</item>
<!-- Status message when VPN failed to connect. --> <!-- Status message when VPN is failed. -->
<item>Failed</item> <item>Failed</item>
</string-array> </string-array>
</resources> </resources>

View File

@@ -18,5 +18,6 @@
android:title="@string/vpn_title"> android:title="@string/vpn_title">
<Preference android:key="add_network" <Preference android:key="add_network"
android:title="@string/vpn_create" android:title="@string/vpn_create"
android:order="1"
android:persistent="false"/> android:persistent="false"/>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -37,6 +37,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig; import com.android.internal.net.VpnConfig;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -49,13 +50,8 @@ public class VpnSettings extends SettingsPreferenceFragment implements
private static final String TAG = "VpnSettings"; private static final String TAG = "VpnSettings";
// Match these constants with R.array.vpn_states. private final IConnectivityManager mService = IConnectivityManager.Stub
private static final int STATE_NONE = -1; .asInterface(ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
private static final int STATE_CONNECTING = 0;
private static final int STATE_CONNECTED = 1;
private static final int STATE_DISCONNECTED = 2;
private static final int STATE_FAILED = 3;
private final KeyStore mKeyStore = KeyStore.getInstance(); private final KeyStore mKeyStore = KeyStore.getInstance();
private boolean mUnlocking = false; private boolean mUnlocking = false;
@@ -63,6 +59,7 @@ public class VpnSettings extends SettingsPreferenceFragment implements
private VpnDialog mDialog; private VpnDialog mDialog;
private Handler mUpdater; private Handler mUpdater;
private LegacyVpnInfo mInfo;
// The key of the profile for the current ContextMenu. // The key of the profile for the current ContextMenu.
private String mSelectedKey; private String mSelectedKey;
@@ -263,8 +260,17 @@ public class VpnSettings extends SettingsPreferenceFragment implements
} }
if (preference instanceof VpnPreference) { if (preference instanceof VpnPreference) {
mDialog = new VpnDialog(getActivity(), this, VpnProfile profile = ((VpnPreference) preference).getProfile();
((VpnPreference) preference).getProfile(), false); if (mInfo != null && profile.key.equals(mInfo.key) &&
mInfo.state == LegacyVpnInfo.STATE_CONNECTED) {
try {
mInfo.intent.send();
return true;
} catch (Exception e) {
// ignore
}
}
mDialog = new VpnDialog(getActivity(), this, profile, false);
} else { } else {
// Generate a new key. Here we just use the current time. // Generate a new key. Here we just use the current time.
long millis = System.currentTimeMillis(); long millis = System.currentTimeMillis();
@@ -284,20 +290,30 @@ public class VpnSettings extends SettingsPreferenceFragment implements
mUpdater.removeMessages(0); mUpdater.removeMessages(0);
if (isResumed()) { if (isResumed()) {
try {
LegacyVpnInfo info = mService.getLegacyVpnInfo();
if (mInfo != null) {
VpnPreference preference = mPreferences.get(mInfo.key);
if (preference != null) {
preference.update(-1);
}
mInfo = null;
}
if (info != null) {
VpnPreference preference = mPreferences.get(info.key);
if (preference != null) {
preference.update(info.state);
mInfo = info;
}
}
} catch (Exception e) {
// ignore
}
mUpdater.sendEmptyMessageDelayed(0, 1000); mUpdater.sendEmptyMessageDelayed(0, 1000);
} }
return true; return true;
} }
private static IConnectivityManager getService() {
return IConnectivityManager.Stub.asInterface(
ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
}
private void connect(VpnProfile profile) { private void connect(VpnProfile profile) {
String[] racoon = null; String[] racoon = null;
switch (profile.type) { switch (profile.type) {
@@ -346,6 +362,7 @@ public class VpnSettings extends SettingsPreferenceFragment implements
} }
VpnConfig config = new VpnConfig(); VpnConfig config = new VpnConfig();
config.packagz = profile.key;
config.session = profile.name; config.session = profile.name;
config.routes = profile.routes; config.routes = profile.routes;
if (!profile.searchDomains.isEmpty()) { if (!profile.searchDomains.isEmpty()) {
@@ -353,23 +370,30 @@ public class VpnSettings extends SettingsPreferenceFragment implements
} }
try { try {
getService().doLegacyVpn(config, racoon, mtpd); mService.startLegacyVpn(config, racoon, mtpd);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "connect", e); Log.e(TAG, "connect", e);
} }
} }
private void disconnect(String key) { private void disconnect(String key) {
if (mInfo != null && key.equals(mInfo.key)) {
try {
mService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN);
} catch (Exception e) {
// ignore
}
}
} }
private class VpnPreference extends Preference { private class VpnPreference extends Preference {
private VpnProfile mProfile; private VpnProfile mProfile;
private int mState = STATE_NONE; private int mState = -1;
VpnPreference(Context context, VpnProfile profile) { VpnPreference(Context context, VpnProfile profile) {
super(context); super(context);
setPersistent(false); setPersistent(false);
setOrder(0);
setOnPreferenceClickListener(VpnSettings.this); setOnPreferenceClickListener(VpnSettings.this);
mProfile = profile; mProfile = profile;
@@ -385,18 +409,23 @@ public class VpnSettings extends SettingsPreferenceFragment implements
update(); update();
} }
void update(int state) {
mState = state;
update();
}
void update() { void update() {
if (mState != STATE_NONE) { if (mState < 0) {
String[] states = getContext().getResources()
.getStringArray(R.array.vpn_states);
setSummary(states[mState]);
} else {
String[] types = getContext().getResources() String[] types = getContext().getResources()
.getStringArray(R.array.vpn_types_long); .getStringArray(R.array.vpn_types_long);
setSummary(types[mProfile.type]); setSummary(types[mProfile.type]);
} else {
String[] states = getContext().getResources()
.getStringArray(R.array.vpn_states);
setSummary(states[mState]);
} }
setTitle(mProfile.name); setTitle(mProfile.name);
notifyChanged(); notifyHierarchyChanged();
} }
@Override @Override
@@ -404,7 +433,6 @@ public class VpnSettings extends SettingsPreferenceFragment implements
int result = -1; int result = -1;
if (preference instanceof VpnPreference) { if (preference instanceof VpnPreference) {
VpnPreference another = (VpnPreference) preference; VpnPreference another = (VpnPreference) preference;
if ((result = another.mState - mState) == 0 && if ((result = another.mState - mState) == 0 &&
(result = mProfile.name.compareTo(another.mProfile.name)) == 0 && (result = mProfile.name.compareTo(another.mProfile.name)) == 0 &&
(result = mProfile.type - another.mProfile.type) == 0) { (result = mProfile.type - another.mProfile.type) == 0) {