From 9b2886e24301e5d4e7052ec4a6eaff273d3f516c Mon Sep 17 00:00:00 2001 From: Robert Greenwalt Date: Wed, 31 Aug 2011 11:46:42 -0700 Subject: [PATCH] Create new isNetworkSupported API Useful for checking if on a wifi-only device. Similar to asking for NetworkInfo for a network type and checking for null, though here the intent is explicit. bug:5087537 Change-Id: Ia3ddd09b6b735b8b3ceb7a347891e015fd96b218 --- .../java/android/net/ConnectivityManager.java | 18 ++++++++++++ .../android/net/IConnectivityManager.aidl | 2 ++ .../statusbar/policy/NetworkController.java | 20 ++++++------- .../android/server/ConnectivityService.java | 6 ++++ .../java/com/android/server/SystemServer.java | 2 +- .../com/android/internal/telephony/RIL.java | 10 +++---- .../net/wifi/WifiWatchdogStateMachine.java | 28 ++++++------------- 7 files changed, 50 insertions(+), 36 deletions(-) diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 3441217026f8c..530122c3b2e47 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -814,4 +814,22 @@ public class ConnectivityManager { } catch (RemoteException e) { } } + + /** + * Returns true if the hardware supports the given network type + * else it returns false. This doesn't indicate we have coverage + * or are authorized onto a network, just whether or not the + * hardware supports it. For example a gsm phone without a sim + * should still return true for mobile data, but a wifi only tablet + * would return false. + * @param networkType The nework type we'd like to check + * @return true if supported, else false + * @hide + */ + public boolean isNetworkSupported(int networkType) { + try { + return mService.isNetworkSupported(networkType); + } catch (RemoteException e) {} + return false; + } } diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl index c9553c05d5888..eef658e4591a1 100644 --- a/core/java/android/net/IConnectivityManager.aidl +++ b/core/java/android/net/IConnectivityManager.aidl @@ -43,6 +43,8 @@ interface IConnectivityManager NetworkInfo getNetworkInfo(int networkType); NetworkInfo[] getAllNetworkInfo(); + boolean isNetworkSupported(int networkType); + LinkProperties getActiveLinkProperties(); LinkProperties getLinkProperties(int networkType); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java index 60dfdacddf04a..3c85814c68c49 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java @@ -130,6 +130,8 @@ public class NetworkController extends BroadcastReceiver { int mLastDataTypeIconId = -1; String mLastLabel = ""; + private boolean mHasMobileDataFeature; + boolean mDataAndWifiStacked = false; // yuck -- stop doing this here and put it in the framework @@ -147,6 +149,10 @@ public class NetworkController extends BroadcastReceiver { public NetworkController(Context context) { mContext = context; + ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService( + Context.CONNECTIVITY_SERVICE); + mHasMobileDataFeature = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); + // set up the default wifi icon, used when no radios have ever appeared updateWifiIcons(); @@ -229,7 +235,7 @@ public class NetworkController extends BroadcastReceiver { mWifiIconId, mWifiActivityIconId); cluster.setMobileDataIndicators( - hasMobileDataFeature(), + mHasMobileDataFeature, mPhoneSignalIconId, mMobileActivityIconId, mDataTypeIconId); @@ -376,12 +382,6 @@ public class NetworkController extends BroadcastReceiver { } } - private boolean hasMobileDataFeature() { - // XXX: HAX: replace when a more reliable method is available - return (! "wifi-only".equals(SystemProperties.get("ro.carrier"))); - } - - private void updateAirplaneMode() { mAirplaneMode = (Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1); @@ -828,8 +828,8 @@ public class NetworkController extends BroadcastReceiver { label = context.getString(R.string.status_bar_settings_signal_meter_disconnected); // On devices without mobile radios, we want to show the wifi icon combinedSignalIconId = - hasMobileDataFeature() ? mDataSignalIconId : mWifiIconId; - mContentDescriptionCombinedSignal = hasMobileDataFeature() + mHasMobileDataFeature ? mDataSignalIconId : mWifiIconId; + mContentDescriptionCombinedSignal = mHasMobileDataFeature ? mContentDescriptionDataType : mContentDescriptionWifi; mDataTypeIconId = 0; } @@ -866,7 +866,7 @@ public class NetworkController extends BroadcastReceiver { mWifiIconId, mWifiActivityIconId); cluster.setMobileDataIndicators( - hasMobileDataFeature(), + mHasMobileDataFeature, mPhoneSignalIconId, mMobileActivityIconId, mDataTypeIconId); diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java index 3815c3b39b534..2348d76bb6169 100644 --- a/services/java/com/android/server/ConnectivityService.java +++ b/services/java/com/android/server/ConnectivityService.java @@ -703,6 +703,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { return result.toArray(new NetworkInfo[result.size()]); } + @Override + public boolean isNetworkSupported(int networkType) { + enforceAccessPermission(); + return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null)); + } + /** * Return LinkProperties for the active (i.e., connected) default * network interface. It is assumed that at most one default network diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index d0e8b5ea123dd..2714fc55377a3 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -350,7 +350,6 @@ class ServerThread extends Thread { Slog.i(TAG, "Wi-Fi Service"); wifi = new WifiService(context); ServiceManager.addService(Context.WIFI_SERVICE, wifi); - wifi.checkAndStartWifi(); } catch (Throwable e) { reportWtf("starting Wi-Fi Service", e); } @@ -361,6 +360,7 @@ class ServerThread extends Thread { ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); networkStats.bindConnectivityManager(connectivity); networkPolicy.bindConnectivityManager(connectivity); + wifi.checkAndStartWifi(); wifiP2p.connectivityServiceReady(); } catch (Throwable e) { reportWtf("starting Connectivity Service", e); diff --git a/telephony/java/com/android/internal/telephony/RIL.java b/telephony/java/com/android/internal/telephony/RIL.java index 3e13a86ebb921..bd35058f61465 100644 --- a/telephony/java/com/android/internal/telephony/RIL.java +++ b/telephony/java/com/android/internal/telephony/RIL.java @@ -29,6 +29,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.net.ConnectivityManager; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.os.AsyncResult; @@ -230,8 +231,6 @@ public final class RIL extends BaseCommands implements CommandsInterface { Object mLastNITZTimeInfo; - private static final String WIFI_ONLY_CARRIER = "wifi-only"; - //***** Events static final int EVENT_SEND = 1; @@ -626,10 +625,9 @@ public final class RIL extends BaseCommands implements CommandsInterface { Looper looper = mSenderThread.getLooper(); mSender = new RILSender(looper); - // TODO: Provide a common API for determining if a - // device is wifi-only. bug: 3480713 - String carrier = SystemProperties.get("ro.carrier"); - if (WIFI_ONLY_CARRIER.equals(carrier)) { + ConnectivityManager cm = (ConnectivityManager)context.getSystemService( + Context.CONNECTIVITY_SERVICE); + if (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) { riljLog("Not starting RILReceiver: wifi-only"); } else { riljLog("Starting RILReceiver"); diff --git a/wifi/java/android/net/wifi/WifiWatchdogStateMachine.java b/wifi/java/android/net/wifi/WifiWatchdogStateMachine.java index 274edae67c020..c52142de1ea28 100644 --- a/wifi/java/android/net/wifi/WifiWatchdogStateMachine.java +++ b/wifi/java/android/net/wifi/WifiWatchdogStateMachine.java @@ -36,6 +36,7 @@ import android.os.SystemProperties; import android.provider.Settings; import android.provider.Settings.Secure; import android.util.Slog; +import android.util.Log; import com.android.internal.util.Protocol; import com.android.internal.util.State; @@ -174,7 +175,7 @@ public class WifiWatchdogStateMachine extends StateMachine { * It triggers a disableNetwork call if a DNS check fails. */ public boolean mDisableAPNextFailure = false; - private ConnectivityManager mConnectivityManager; + private static boolean sWifiOnly = false; private boolean mNotificationShown; public boolean mHasConnectedWifiManager = false; @@ -219,9 +220,14 @@ public class WifiWatchdogStateMachine extends StateMachine { public static WifiWatchdogStateMachine makeWifiWatchdogStateMachine(Context context) { ContentResolver contentResolver = context.getContentResolver(); + + ConnectivityManager cm = (ConnectivityManager) context.getSystemService( + Context.CONNECTIVITY_SERVICE); + sWifiOnly = (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); + // Disable for wifi only devices. if (Settings.Secure.getString(contentResolver, Settings.Secure.WIFI_WATCHDOG_ON) == null && - "wifi-only".equals(SystemProperties.get("ro.carrier"))) { + sWifiOnly) { putSettingsBoolean(contentResolver, Settings.Secure.WIFI_WATCHDOG_ON, false); } WifiWatchdogStateMachine wwsm = new WifiWatchdogStateMachine(context); @@ -508,22 +514,6 @@ public class WifiWatchdogStateMachine extends StateMachine { } } - /** - * @return true if there is definitely no mobile data (we'll be less aggressive) - */ - private boolean hasNoMobileData() { - if (mConnectivityManager == null) { - mConnectivityManager = (ConnectivityManager) mContext.getSystemService( - Context.CONNECTIVITY_SERVICE); - } - NetworkInfo mobileNetInfo = mConnectivityManager.getNetworkInfo( - ConnectivityManager.TYPE_MOBILE); - if (mobileNetInfo == null || !mobileNetInfo.isAvailable()) { - return true; - } - return false; - } - class DefaultState extends State { @Override public boolean processMessage(Message msg) { @@ -941,7 +931,7 @@ public class WifiWatchdogStateMachine extends StateMachine { if (mDisableAPNextFailure || mNumCheckFailures >= mBssids.size() || mNumCheckFailures >= mMaxSsidBlacklists) { - if (hasNoMobileData()) { + if (sWifiOnly) { Slog.w(WWSM_TAG, "Would disable bad network, but device has no mobile data!" + " Going idle..."); // This state should be called idle -- will be changing flow.