Merge "Using AsyncChannel for wifi connectivity"

This commit is contained in:
Irfan Sheriff
2011-03-01 11:32:24 -08:00
committed by Android (Google) Code Review
7 changed files with 146 additions and 95 deletions

View File

@@ -21,30 +21,34 @@ import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Message;
import android.os.PowerManager;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import com.android.internal.util.AsyncChannel;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import android.widget.LinearLayout;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration.KeyMgmt;
/**
* An activity registered with connectivity manager broadcast
@@ -180,6 +184,24 @@ public class ConnectivityManagerTestActivity extends Activity {
}
}
private class WifiServiceHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
//AsyncChannel in msg.obj
} else {
log("Failed to establish AsyncChannel connection");
}
break;
default:
//Ignore
break;
}
}
}
public ConnectivityManagerTestActivity() {
mState = State.UNKNOWN;
scanResultAvailable = false;
@@ -216,6 +238,8 @@ public class ConnectivityManagerTestActivity extends Activity {
mCM = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// Get an instance of WifiManager
mWifiManager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifiManager.asyncConnect(this, new WifiServiceHandler());
initializeNetworkStates();
if (mWifiManager.isWifiEnabled()) {

View File

@@ -34,12 +34,15 @@ import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import com.android.internal.util.AsyncChannel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -75,6 +78,7 @@ public class WifiConnectionTest
enabledNetworks = getEnabledNetworks(mWifiManager.getConfiguredNetworks());
mAct = getActivity();
mWifiManager.asyncConnect(mAct, new WifiServiceHandler());
networks = mAct.loadNetworkConfigurations();
if (DEBUG) {
printNetworkConfigurations();
@@ -89,6 +93,24 @@ public class WifiConnectionTest
assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant());
}
private class WifiServiceHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
//AsyncChannel in msg.obj
} else {
log("Failed to establish AsyncChannel connection");
}
break;
default:
//Ignore
break;
}
}
}
private void printNetworkConfigurations() {
log("==== print network configurations parsed from XML file ====");
log("number of access points: " + networks.size());

View File

@@ -144,9 +144,6 @@ public class WifiService extends IWifiManager.Stub {
private static final String ACTION_DEVICE_IDLE =
"com.android.server.WifiManager.action.DEVICE_IDLE";
private static final int CMD_ENABLE_TRAFFIC_STATS_POLL = 1;
private static final int CMD_TRAFFIC_STATS_POLL = 2;
private boolean mIsReceiverRegistered = false;
@@ -237,24 +234,45 @@ public class WifiService extends IWifiManager.Stub {
ac.connect(mContext, this, msg.replyTo);
break;
}
case CMD_ENABLE_TRAFFIC_STATS_POLL: {
case WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL: {
mEnableTrafficStatsPoll = (msg.arg1 == 1);
mTrafficStatsPollToken++;
if (mEnableTrafficStatsPoll) {
notifyOnDataActivity();
sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL,
sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
}
break;
}
case CMD_TRAFFIC_STATS_POLL: {
case WifiManager.CMD_TRAFFIC_STATS_POLL: {
if (msg.arg1 == mTrafficStatsPollToken) {
notifyOnDataActivity();
sendMessageDelayed(Message.obtain(this, CMD_TRAFFIC_STATS_POLL,
sendMessageDelayed(Message.obtain(this, WifiManager.CMD_TRAFFIC_STATS_POLL,
mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
}
break;
}
case WifiManager.CMD_CONNECT_NETWORK: {
if (msg.obj != null) {
mWifiStateMachine.connectNetwork((WifiConfiguration)msg.obj);
} else {
mWifiStateMachine.connectNetwork(msg.arg1);
}
break;
}
case WifiManager.CMD_SAVE_NETWORK: {
mWifiStateMachine.saveNetwork((WifiConfiguration)msg.obj);
break;
}
case WifiManager.CMD_FORGET_NETWORK: {
mWifiStateMachine.forgetNetwork(msg.arg1);
break;
}
case WifiManager.CMD_START_WPS: {
//replyTo has the original source
mWifiStateMachine.startWps(msg.replyTo, (WpsConfiguration)msg.obj);
break;
}
default: {
Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg);
break;
@@ -844,42 +862,19 @@ public class WifiService extends IWifiManager.Stub {
mWifiStateMachine.clearBlacklist();
}
public void connectNetworkWithId(int networkId) {
enforceChangePermission();
mWifiStateMachine.connectNetwork(networkId);
}
public void connectNetworkWithConfig(WifiConfiguration config) {
enforceChangePermission();
mWifiStateMachine.connectNetwork(config);
}
public void saveNetwork(WifiConfiguration config) {
enforceChangePermission();
mWifiStateMachine.saveNetwork(config);
}
public void forgetNetwork(int netId) {
enforceChangePermission();
mWifiStateMachine.forgetNetwork(netId);
}
public WpsResult startWps(WpsConfiguration config) {
enforceChangePermission();
if (mWifiStateMachineChannel != null) {
return mWifiStateMachine.startWps(mWifiStateMachineChannel, config);
} else {
Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
return new WpsResult(WpsResult.Status.FAILURE);
}
}
/**
* Get a reference to handler. This is used by a client to establish
* an AsyncChannel communication with WifiService
*/
public Messenger getMessenger() {
/* Enforce the highest permissions
TODO: when we consider exposing the asynchronous API, think about
how to provide both access and change permissions seperately
*/
enforceAccessPermission();
enforceChangePermission();
return new Messenger(mAsyncServiceHandler);
}
@@ -1530,9 +1525,11 @@ public class WifiService extends IWifiManager.Stub {
private void evaluateTrafficStatsPolling() {
Message msg;
if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
msg = Message.obtain(mAsyncServiceHandler,
WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 1, 0);
} else {
msg = Message.obtain(mAsyncServiceHandler, CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
msg = Message.obtain(mAsyncServiceHandler,
WifiManager.CMD_ENABLE_TRAFFIC_STATS_POLL, 0, 0);
}
msg.sendToTarget();
}

View File

@@ -103,16 +103,6 @@ interface IWifiManager
void clearBlacklist();
void connectNetworkWithConfig(in WifiConfiguration wifiConfig);
void connectNetworkWithId(int networkId);
void saveNetwork(in WifiConfiguration wifiConfig);
void forgetNetwork(int networkId);
WpsResult startWps(in WpsConfiguration config);
Messenger getMessenger();
}

View File

@@ -18,6 +18,7 @@ package android.net.wifi;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.content.Context;
import android.net.DhcpInfo;
import android.os.Binder;
import android.os.IBinder;
@@ -26,6 +27,8 @@ import android.os.RemoteException;
import android.os.WorkSource;
import android.os.Messenger;
import com.android.internal.util.AsyncChannel;
import java.util.List;
/**
@@ -447,6 +450,9 @@ public class WifiManager {
/* Number of currently active WifiLocks and MulticastLocks */
private int mActiveLockCount;
/* For communication with WifiService */
private AsyncChannel mAsyncChannel = new AsyncChannel();
/**
* Create a new WifiManager instance.
* Applications will almost always want to use
@@ -1032,6 +1038,37 @@ public class WifiManager {
}
/* TODO: deprecate synchronous API and open up the following API */
/* Commands to WifiService */
/** @hide */
public static final int CMD_CONNECT_NETWORK = 1;
/** @hide */
public static final int CMD_FORGET_NETWORK = 2;
/** @hide */
public static final int CMD_SAVE_NETWORK = 3;
/** @hide */
public static final int CMD_START_WPS = 4;
/* Events from WifiService */
/** @hide */
public static final int CMD_WPS_COMPLETED = 11;
/* For system use only */
/** @hide */
public static final int CMD_ENABLE_TRAFFIC_STATS_POLL = 21;
/** @hide */
public static final int CMD_TRAFFIC_STATS_POLL = 22;
/**
* Initiate an asynchronous channel connection setup
* @param srcContext is the context of the source
* @param srcHandler is the handler on which the source receives messages
* @hide
*/
public void asyncConnect(Context srcContext, Handler srcHandler) {
mAsyncChannel.connect(srcContext, srcHandler, getMessenger());
}
/**
* Connect to a network with the given configuration. The network also
* gets added to the supplicant configuration.
@@ -1048,9 +1085,7 @@ public class WifiManager {
if (config == null) {
return;
}
try {
mService.connectNetworkWithConfig(config);
} catch (RemoteException e) { }
mAsyncChannel.sendMessage(CMD_CONNECT_NETWORK, config);
}
/**
@@ -1067,9 +1102,7 @@ public class WifiManager {
if (networkId < 0) {
return;
}
try {
mService.connectNetworkWithId(networkId);
} catch (RemoteException e) { }
mAsyncChannel.sendMessage(CMD_CONNECT_NETWORK, networkId);
}
/**
@@ -1091,9 +1124,8 @@ public class WifiManager {
if (config == null) {
return;
}
try {
mService.saveNetwork(config);
} catch (RemoteException e) { }
mAsyncChannel.sendMessage(CMD_SAVE_NETWORK, config);
}
/**
@@ -1110,25 +1142,22 @@ public class WifiManager {
if (netId < 0) {
return;
}
try {
mService.forgetNetwork(netId);
} catch (RemoteException e) { }
mAsyncChannel.sendMessage(CMD_FORGET_NETWORK, netId);
}
/**
* Start Wi-fi Protected Setup
*
* @param config WPS configuration
* @return WpsResult containing pin and status
* @hide
* TODO: with use of AsyncChannel, return value should go away
*/
public WpsResult startWps(WpsConfiguration config) {
try {
return mService.startWps(config);
} catch (RemoteException e) {
return new WpsResult(WpsResult.Status.FAILURE);
public void startWps(WpsConfiguration config) {
if (config == null) {
return;
}
mAsyncChannel.sendMessage(CMD_START_WPS, config);
}
/**

View File

@@ -59,6 +59,7 @@ import android.os.Binder;
import android.os.IBinder;
import android.os.INetworkManagementService;
import android.os.Message;
import android.os.Messenger;
import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException;
@@ -805,22 +806,10 @@ public class WifiStateMachine extends HierarchicalStateMachine {
sendMessage(obtainMessage(CMD_FORGET_NETWORK, netId, 0));
}
public WpsResult startWps(AsyncChannel channel, WpsConfiguration config) {
WpsResult result;
switch (config.setup) {
case PIN_FROM_DEVICE:
case PBC:
case PIN_FROM_ACCESS_POINT:
//TODO: will go away with AsyncChannel use from settings
Message resultMsg = channel.sendMessageSynchronously(CMD_START_WPS, config);
result = (WpsResult) resultMsg.obj;
resultMsg.recycle();
break;
default:
result = new WpsResult(Status.FAILURE);
break;
}
return result;
public void startWps(Messenger replyTo, WpsConfiguration config) {
Message msg = obtainMessage(CMD_START_WPS, config);
msg.replyTo = replyTo;
sendMessage(msg);
}
public void enableRssiPolling(boolean enabled) {
@@ -1588,7 +1577,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
break;
case CMD_START_WPS:
/* Return failure when the state machine cannot handle WPS initiation*/
mReplyChannel.replyToMessage(message, message.what,
mReplyChannel.replyToMessage(message, WifiManager.CMD_WPS_COMPLETED,
new WpsResult(Status.FAILURE));
break;
default:

View File

@@ -110,7 +110,7 @@ class WpsStateMachine extends HierarchicalStateMachine {
Log.e(TAG, "Invalid setup for WPS");
break;
}
mReplyChannel.replyToMessage(message, message.what, result);
mReplyChannel.replyToMessage(message, WifiManager.CMD_WPS_COMPLETED, result);
if (result.status == Status.SUCCESS) {
transitionTo(mActiveState);
} else {
@@ -172,7 +172,7 @@ class WpsStateMachine extends HierarchicalStateMachine {
break;
case WifiStateMachine.CMD_START_WPS:
/* Ignore request and send an in progress message */
mReplyChannel.replyToMessage(message, message.what,
mReplyChannel.replyToMessage(message, WifiManager.CMD_WPS_COMPLETED,
new WpsResult(Status.IN_PROGRESS));
break;
default: