am 62cc79ef: Make VpnSettings use new synchronous VpnService API.

* commit '62cc79ef964affaea5ec59c111c01e89f2e0578e':
  Make VpnSettings use new synchronous VpnService API.
This commit is contained in:
Hung-ying Tyan
2011-01-19 22:45:23 -08:00
committed by Android Git Automerger
3 changed files with 16 additions and 182 deletions

View File

@@ -19,16 +19,10 @@ package com.android.settings.vpn;
import com.android.settings.R;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.net.vpn.IVpnService;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnProfile;
import android.net.vpn.VpnState;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
@@ -117,102 +111,13 @@ public class AuthenticationActor implements VpnProfileActor {
return mContext;
}
private void connect(final String username, final String password) {
mVpnManager.startVpnService();
ServiceConnection c = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
try {
boolean success = IVpnService.Stub.asInterface(service)
.connect(mProfile, username, password);
if (!success) {
Log.d(TAG, "~~~~~~ connect() failed!");
} else {
Log.d(TAG, "~~~~~~ connect() succeeded!");
}
} catch (Throwable e) {
Log.e(TAG, "connect()", e);
broadcastConnectivity(VpnState.IDLE,
VpnManager.VPN_ERROR_CONNECTION_FAILED);
} finally {
mContext.unbindService(this);
}
}
public void onServiceDisconnected(ComponentName className) {
checkStatus();
}
};
if (!bindService(c)) {
broadcastConnectivity(VpnState.IDLE,
VpnManager.VPN_ERROR_CONNECTION_FAILED);
}
private void connect(String username, String password) {
mVpnManager.connect(mProfile, username, password);
}
//@Override
public void disconnect() {
ServiceConnection c = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
try {
IVpnService.Stub.asInterface(service).disconnect();
} catch (RemoteException e) {
Log.e(TAG, "disconnect()", e);
checkStatus();
} finally {
mContext.unbindService(this);
}
}
public void onServiceDisconnected(ComponentName className) {
checkStatus();
}
};
if (!bindService(c)) {
checkStatus();
}
}
//@Override
public void checkStatus() {
final ConditionVariable cv = new ConditionVariable();
cv.close();
ServiceConnection c = new ServiceConnection() {
public synchronized void onServiceConnected(ComponentName className,
IBinder service) {
cv.open();
try {
IVpnService.Stub.asInterface(service).checkStatus(mProfile);
} catch (RemoteException e) {
Log.e(TAG, "checkStatus()", e);
broadcastConnectivity(VpnState.IDLE);
} finally {
mContext.unbindService(this);
}
}
public void onServiceDisconnected(ComponentName className) {
cv.open();
broadcastConnectivity(VpnState.IDLE);
mContext.unbindService(this);
}
};
if (bindService(c)) {
// wait for a second, let status propagate
if (!cv.block(1000)) broadcastConnectivity(VpnState.IDLE);
}
}
private boolean bindService(ServiceConnection c) {
return mVpnManager.bindVpnService(c);
}
private void broadcastConnectivity(VpnState s) {
mVpnManager.broadcastConnectivity(mProfile.getName(), s);
}
private void broadcastConnectivity(VpnState s, int errorCode) {
mVpnManager.broadcastConnectivity(mProfile.getName(), s, errorCode);
mVpnManager.disconnect();
}
private void setSavedUsername(String name) throws IOException {

View File

@@ -54,11 +54,4 @@ public interface VpnProfileActor {
* Tears down the connection.
*/
void disconnect();
/**
* Checks the current status. The result is expected to be broadcast.
* Use {@link VpnManager#registerConnectivityReceiver()} to register a
* broadcast receiver and to receives the broadcast events.
*/
void checkStatus();
}

View File

@@ -23,12 +23,9 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.vpn.IVpnService;
import android.net.vpn.L2tpIpsecProfile;
import android.net.vpn.L2tpIpsecPskProfile;
import android.net.vpn.L2tpProfile;
@@ -37,9 +34,6 @@ import android.net.vpn.VpnProfile;
import android.net.vpn.VpnState;
import android.net.vpn.VpnType;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.IBinder;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
@@ -139,8 +133,6 @@ public class VpnSettings extends SettingsPreferenceFragment
private Dialog mShowingDialog;
private StatusChecker mStatusChecker = new StatusChecker();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -216,7 +208,7 @@ public class VpnSettings extends SettingsPreferenceFragment
mUnlockAction = null;
getActivity().runOnUiThread(action);
}
checkVpnConnectionStatusInBackground();
checkVpnConnectionStatus();
}
@Override
@@ -620,14 +612,10 @@ public class VpnSettings extends SettingsPreferenceFragment
saveProfileToStorage(p);
mVpnProfileList.add(p);
addPreferenceFor(p);
addPreferenceFor(p, true);
disableProfilePreferencesIfOneActive();
}
private VpnPreference addPreferenceFor(VpnProfile p) {
return addPreferenceFor(p, true);
}
// Adds a preference in mVpnListContainer
private VpnPreference addPreferenceFor(
VpnProfile p, boolean addToContainer) {
@@ -886,18 +874,23 @@ public class VpnSettings extends SettingsPreferenceFragment
return p1.getName().compareTo(p2.getName());
}
});
// Delay adding preferences to mVpnListContainer until states are
// obtained so that the user won't see initial state transition.
for (VpnProfile p : mVpnProfileList) {
Preference pref = addPreferenceFor(p, false);
}
disableProfilePreferencesIfOneActive();
}
private void checkVpnConnectionStatusInBackground() {
new Thread(new Runnable() {
public void run() {
mStatusChecker.check(mVpnProfileList);
}
}).start();
private void checkVpnConnectionStatus() {
for (VpnProfile p : mVpnProfileList) {
changeState(p, mVpnManager.getState(p));
}
// make preferences appear
for (VpnProfile p : mVpnProfileList) {
VpnPreference pref = mVpnPreferenceMap.get(p.getName());
mVpnListContainer.addPreference(pref);
}
}
// A sanity check. Returns true if the profile directory name and profile ID
@@ -1074,61 +1067,4 @@ public class VpnSettings extends SettingsPreferenceFragment
}
}
}
// managing status check in a background thread
private class StatusChecker {
synchronized void check(final List<VpnProfile> list) {
final ConditionVariable cv = new ConditionVariable();
cv.close();
mVpnManager.startVpnService();
ServiceConnection c = new ServiceConnection() {
public synchronized void onServiceConnected(
ComponentName className, IBinder binder) {
cv.open();
IVpnService service = IVpnService.Stub.asInterface(binder);
for (VpnProfile p : list) {
try {
service.checkStatus(p);
} catch (Throwable e) {
Log.e(TAG, " --- checkStatus(): " + p.getName(), e);
changeState(p, VpnState.IDLE);
}
}
if (getActivity() == null) return;
getActivity().unbindService(this);
showPreferences();
}
public void onServiceDisconnected(ComponentName className) {
cv.open();
setDefaultState(list);
if (getActivity() == null) return;
getActivity().unbindService(this);
showPreferences();
}
};
if (mVpnManager.bindVpnService(c)) {
if (!cv.block(1000)) {
Log.d(TAG, "checkStatus() bindService failed");
setDefaultState(list);
}
} else {
setDefaultState(list);
}
}
private void showPreferences() {
for (VpnProfile p : mVpnProfileList) {
VpnPreference pref = mVpnPreferenceMap.get(p.getName());
mVpnListContainer.addPreference(pref);
}
}
private void setDefaultState(List<VpnProfile> list) {
for (VpnProfile p : list) changeState(p, VpnState.IDLE);
showPreferences();
}
}
}