From aaebc86386c8bb44c25dd06fe573e52ef6b60fbe Mon Sep 17 00:00:00 2001 From: Patrick Scott Date: Wed, 16 Sep 2009 09:59:12 -0400 Subject: Remove the network connection logic from RequestQueue. This doesn't always work right and is causing some problems if the network comes back up without sending the broadcast. --- core/java/android/net/http/RequestQueue.java | 182 ++------------------------- 1 file changed, 11 insertions(+), 171 deletions(-) (limited to 'core/java/android/net/http/RequestQueue.java') diff --git a/core/java/android/net/http/RequestQueue.java b/core/java/android/net/http/RequestQueue.java index e14af66..875caa0 100644 --- a/core/java/android/net/http/RequestQueue.java +++ b/core/java/android/net/http/RequestQueue.java @@ -26,7 +26,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; -import android.net.NetworkConnectivityListener; import android.net.NetworkInfo; import android.net.Proxy; import android.net.WebAddress; @@ -50,151 +49,22 @@ import org.apache.http.HttpHost; */ public class RequestQueue implements RequestFeeder { - private Context mContext; /** * Requests, indexed by HttpHost (scheme, host, port) */ - private LinkedHashMap> mPending; - - /** true if connected */ - boolean mNetworkConnected = true; + private final LinkedHashMap> mPending; + private final Context mContext; + private final ActivePool mActivePool; + private final ConnectivityManager mConnectivityManager; private HttpHost mProxyHost = null; private BroadcastReceiver mProxyChangeReceiver; - private ActivePool mActivePool; - /* default simultaneous connection count */ private static final int CONNECTION_COUNT = 4; /** - * This intent broadcast when http is paused or unpaused due to - * net availability toggling - */ - public final static String HTTP_NETWORK_STATE_CHANGED_INTENT = - "android.net.http.NETWORK_STATE"; - public final static String HTTP_NETWORK_STATE_UP = "up"; - - /** - * Listen to platform network state. On a change, - * (1) kick stack on or off as appropriate - * (2) send an intent to my host app telling - * it what I've done - */ - private NetworkStateTracker mNetworkStateTracker; - class NetworkStateTracker { - - final static int EVENT_DATA_STATE_CHANGED = 100; - - Context mContext; - NetworkConnectivityListener mConnectivityListener; - NetworkInfo.State mLastNetworkState = NetworkInfo.State.CONNECTED; - int mCurrentNetworkType; - - NetworkStateTracker(Context context) { - mContext = context; - } - - /** - * register for updates - */ - protected void enable() { - if (mConnectivityListener == null) { - /* - * Initializing the network type is really unnecessary, - * since as soon as we register with the NCL, we'll - * get a CONNECTED event for the active network, and - * we'll configure the HTTP proxy accordingly. However, - * as a fallback in case that doesn't happen for some - * reason, initializing to type WIFI would mean that - * we'd start out without a proxy. This seems better - * than thinking we have a proxy (which is probably - * private to the carrier network and therefore - * unreachable outside of that network) when we really - * shouldn't. - */ - mCurrentNetworkType = ConnectivityManager.TYPE_WIFI; - mConnectivityListener = new NetworkConnectivityListener(); - mConnectivityListener.registerHandler(mHandler, EVENT_DATA_STATE_CHANGED); - mConnectivityListener.startListening(mContext); - } - } - - protected void disable() { - if (mConnectivityListener != null) { - mConnectivityListener.unregisterHandler(mHandler); - mConnectivityListener.stopListening(); - mConnectivityListener = null; - } - } - - private Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - switch (msg.what) { - case EVENT_DATA_STATE_CHANGED: - networkStateChanged(); - break; - } - } - }; - - int getCurrentNetworkType() { - return mCurrentNetworkType; - } - - void networkStateChanged() { - if (mConnectivityListener == null) - return; - - - NetworkConnectivityListener.State connectivityState = mConnectivityListener.getState(); - NetworkInfo info = mConnectivityListener.getNetworkInfo(); - if (info == null) { - /** - * We've been seeing occasional NPEs here. I believe recent changes - * have made this impossible, but in the interest of being totally - * paranoid, check and log this here. - */ - HttpLog.v("NetworkStateTracker: connectivity broadcast" - + " has null network info - ignoring"); - return; - } - NetworkInfo.State state = info.getState(); - - if (HttpLog.LOGV) { - HttpLog.v("NetworkStateTracker " + info.getTypeName() + - " state= " + state + " last= " + mLastNetworkState + - " connectivityState= " + connectivityState.toString()); - } - - boolean newConnection = - state != mLastNetworkState && state == NetworkInfo.State.CONNECTED; - - if (state == NetworkInfo.State.CONNECTED) { - mCurrentNetworkType = info.getType(); - setProxyConfig(); - } - - mLastNetworkState = state; - if (connectivityState == NetworkConnectivityListener.State.NOT_CONNECTED) { - setNetworkState(false); - broadcastState(false); - } else if (newConnection) { - setNetworkState(true); - broadcastState(true); - } - - } - - void broadcastState(boolean connected) { - Intent intent = new Intent(HTTP_NETWORK_STATE_CHANGED_INTENT); - intent.putExtra(HTTP_NETWORK_STATE_UP, connected); - mContext.sendBroadcast(intent); - } - } - - /** * This class maintains active connection threads */ class ActivePool implements ConnectionManager { @@ -230,10 +100,6 @@ public class RequestQueue implements RequestFeeder { } } - public boolean isNetworkConnected() { - return mNetworkConnected; - } - void startConnectionThread() { synchronized (RequestQueue.this) { RequestQueue.this.notify(); @@ -349,6 +215,9 @@ public class RequestQueue implements RequestFeeder { mActivePool = new ActivePool(connectionCount); mActivePool.startup(); + + mConnectivityManager = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); } /** @@ -368,18 +237,6 @@ public class RequestQueue implements RequestFeeder { mContext.registerReceiver(mProxyChangeReceiver, new IntentFilter(Proxy.PROXY_CHANGE_ACTION)); } - - /* Network state notification is broken on the simulator - don't register for notifications on SIM */ - String device = SystemProperties.get("ro.product.device"); - boolean simulation = TextUtils.isEmpty(device); - - if (!simulation) { - if (mNetworkStateTracker == null) { - mNetworkStateTracker = new NetworkStateTracker(mContext); - } - mNetworkStateTracker.enable(); - } } /** @@ -389,10 +246,6 @@ public class RequestQueue implements RequestFeeder { public synchronized void disablePlatformNotifications() { if (HttpLog.LOGV) HttpLog.v("RequestQueue.disablePlatformNotifications() network"); - if (mNetworkStateTracker != null) { - mNetworkStateTracker.disable(); - } - if (mProxyChangeReceiver != null) { mContext.unregisterReceiver(mProxyChangeReceiver); mProxyChangeReceiver = null; @@ -404,7 +257,8 @@ public class RequestQueue implements RequestFeeder { * synchronize setting the proxy */ private synchronized void setProxyConfig() { - if (mNetworkStateTracker.getCurrentNetworkType() == ConnectivityManager.TYPE_WIFI) { + NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); + if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) { mProxyHost = null; } else { String host = Proxy.getHost(mContext); @@ -489,19 +343,6 @@ public class RequestQueue implements RequestFeeder { } /** - * Called by the NetworkStateTracker -- updates when network connectivity - * is lost/restored. - * - * If isNetworkConnected is true, start processing requests - */ - public void setNetworkState(boolean isNetworkConnected) { - if (HttpLog.LOGV) HttpLog.v("RequestQueue.setNetworkState() " + isNetworkConnected); - mNetworkConnected = isNetworkConnected; - if (isNetworkConnected) - mActivePool.startConnectionThread(); - } - - /** * @return true iff there are any non-active requests pending */ synchronized boolean requestsPending() { @@ -546,7 +387,7 @@ public class RequestQueue implements RequestFeeder { public synchronized Request getRequest() { Request ret = null; - if (mNetworkConnected && !mPending.isEmpty()) { + if (!mPending.isEmpty()) { ret = removeFirst(mPending); } if (HttpLog.LOGV) HttpLog.v("RequestQueue.getRequest() => " + ret); @@ -559,7 +400,7 @@ public class RequestQueue implements RequestFeeder { public synchronized Request getRequest(HttpHost host) { Request ret = null; - if (mNetworkConnected && mPending.containsKey(host)) { + if (mPending.containsKey(host)) { LinkedList reqList = mPending.get(host); ret = reqList.removeFirst(); if (reqList.isEmpty()) { @@ -635,7 +476,6 @@ public class RequestQueue implements RequestFeeder { * This interface is exposed to each connection */ interface ConnectionManager { - boolean isNetworkConnected(); HttpHost getProxyHost(); Connection getConnection(Context context, HttpHost host); boolean recycleConnection(HttpHost host, Connection connection); -- cgit v1.1