From 4b7ba09c8bf773dbd045b4bbe7831fa16e33653d Mon Sep 17 00:00:00 2001 From: Wink Saville Date: Wed, 20 Oct 2010 15:37:41 -0700 Subject: [PATCH] Change removeNetowrk to a synchronous channel call. Change-ID: Iad0380d56972826615e044fa2aaee418b617d732 --- .../java/com/android/server/WifiService.java | 48 ++++++++++++++++++- .../android/net/wifi/WifiStateMachine.java | 32 +++++++++---- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java index 1241a7e9e487d..9cd8fbf8e7b30 100644 --- a/services/java/com/android/server/WifiService.java +++ b/services/java/com/android/server/WifiService.java @@ -65,6 +65,7 @@ import java.io.FileDescriptor; import java.io.PrintWriter; import com.android.internal.app.IBatteryStats; +import com.android.internal.util.AsyncChannel; import com.android.server.am.BatteryStatsService; import com.android.internal.R; @@ -197,6 +198,45 @@ public class WifiService extends IWifiManager.Stub { */ private int mNumScansSinceNetworkStateChange; + /** + * Asynchronous channel to WifiStateMachine + */ + private AsyncChannel mChannel; + + /** + * TODO: Possibly change WifiService into an AsyncService. + */ + private class WifiServiceHandler extends Handler { + private AsyncChannel mWshChannel; + + WifiServiceHandler(android.os.Looper looper, Context context) { + super(looper); + mWshChannel = new AsyncChannel(); + mWshChannel.connect(context, this, mWifiStateMachine.getHandler(), 0); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { + if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { + mChannel = mWshChannel; + } else { + Slog.d(TAG, "WifiServicehandler.handleMessage could not connect error=" + + msg.arg1); + mChannel = null; + } + break; + } + default: { + Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg); + break; + } + } + } + } + WifiServiceHandler mHandler; + /** * Temporary for computing UIDS that are responsible for starting WIFI. * Protected by mWifiStateTracker lock. @@ -218,6 +258,7 @@ public class WifiService extends IWifiManager.Stub { HandlerThread wifiThread = new HandlerThread("WifiService"); wifiThread.start(); + mHandler = new WifiServiceHandler(wifiThread.getLooper(), context); mContext.registerReceiver( new BroadcastReceiver() { @@ -602,7 +643,12 @@ public class WifiService extends IWifiManager.Stub { */ public boolean removeNetwork(int netId) { enforceChangePermission(); - return mWifiStateMachine.syncRemoveNetwork(netId); + if (mChannel != null) { + return mWifiStateMachine.syncRemoveNetwork(mChannel, netId); + } else { + Slog.e(TAG, "mChannel is not initialized"); + return false; + } } /** diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java index f2fdbad866300..65b910b7202c2 100644 --- a/wifi/java/android/net/wifi/WifiStateMachine.java +++ b/wifi/java/android/net/wifi/WifiStateMachine.java @@ -70,6 +70,7 @@ import android.bluetooth.BluetoothProfile; import android.content.Intent; import android.content.Context; import com.android.internal.app.IBatteryStats; +import com.android.internal.util.AsyncChannel; import com.android.internal.util.HierarchicalState; import com.android.internal.util.HierarchicalStateMachine; @@ -148,6 +149,9 @@ public class WifiStateMachine extends HierarchicalStateMachine { * and load configuration afterwards */ private boolean mWpsStarted = false; + // Channel for sending replies. + private AsyncChannel mReplyChannel = new AsyncChannel(); + // Event log tags (must be in sync with event-log-tags) private static final int EVENTLOG_WIFI_STATE_CHANGED = 50021; private static final int EVENTLOG_WIFI_EVENT_HANDLED = 50022; @@ -301,7 +305,6 @@ public class WifiStateMachine extends HierarchicalStateMachine { /* Start Wi-Fi protected setup */ private static final int CMD_START_WPS = 93; - /** * Interval in milliseconds between polling for connection * status items that are not sent via asynchronous events. @@ -699,8 +702,21 @@ public class WifiStateMachine extends HierarchicalStateMachine { * * @param networkId id of the network to be removed */ - public boolean syncRemoveNetwork(int networkId) { - return sendSyncMessage(obtainMessage(CMD_REMOVE_NETWORK, networkId, 0)).boolValue; + public boolean syncRemoveNetwork(AsyncChannel channel, int networkId) { + Message resultMsg = channel.sendMessageSynchronously(CMD_REMOVE_NETWORK, networkId); + boolean result = resultMsg.arg1 != 0; + resultMsg.recycle(); + return result; + } + + /** + * Return the result of a removeNetwork + * + * @param srcMsg is the original message + * @param result is true if successfully removed + */ + private void removeNetworkReply(Message srcMsg, boolean result) { + mReplyChannel.replyToMessage(srcMsg, CMD_REMOVE_NETWORK, result ? 1 : 0); } private class EnableNetParams { @@ -1583,7 +1599,6 @@ public class WifiStateMachine extends HierarchicalStateMachine { break; /* Synchronous call returns */ case CMD_PING_SUPPLICANT: - case CMD_REMOVE_NETWORK: case CMD_ENABLE_NETWORK: case CMD_DISABLE_NETWORK: case CMD_ADD_OR_UPDATE_NETWORK: @@ -1598,6 +1613,9 @@ public class WifiStateMachine extends HierarchicalStateMachine { syncParams.mSyncReturn.stringValue = null; notifyOnMsgObject(message); break; + case CMD_REMOVE_NETWORK: + removeNetworkReply(message, false); + break; case CMD_ENABLE_RSSI_POLL: mEnableRssiPolling = (message.arg1 == 1); mSupplicantStateTracker.sendMessage(CMD_ENABLE_RSSI_POLL); @@ -2023,10 +2041,8 @@ public class WifiStateMachine extends HierarchicalStateMachine { break; case CMD_REMOVE_NETWORK: EventLog.writeEvent(EVENTLOG_WIFI_EVENT_HANDLED, message.what); - syncParams = (SyncParams) message.obj; - syncParams.mSyncReturn.boolValue = WifiConfigStore.removeNetwork( - message.arg1); - notifyOnMsgObject(message); + boolean ok = WifiConfigStore.removeNetwork(message.arg1); + removeNetworkReply(message, ok); break; case CMD_ENABLE_NETWORK: EventLog.writeEvent(EVENTLOG_WIFI_EVENT_HANDLED, message.what);