1、“Mobile networks”字符串定义
我们想要的数据没有显示,从不显示的字符找起。“Mobile networks”不显示,那我们就从最开始跟下代码吧。
- 4.1/packages/apps/Settings/res/values/String.xml
-
- <string name="network_settings_title">Mobile networks</string>
2、network_settings_title引用
- 4.1/packages/apps/Settings/src/com/android/settings/WirelessSettings.java中找到:"mobile_network_settings"
- private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
把 network_settings_title赋值给 KEY_MOBILE_NETWORK_SETTINGS,再跟进一下 KEY_MOBILE_NETWORK_SETTINGS这个变量吧。
在相同的文件中我们找到:
-
- if (Utils.isWifiOnly(getActivity())) {
- getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
- }
3、我们去看下ifWifiOnly()是何方神圣?
packages/apps/Settings/src/com/android/settings/Utils.java中找到
- import android.net.ConnectivityManager;
-
- public static boolean isWifiOnly(Context context) {
- ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
- Context.CONNECTIVITY_SERVICE);
- return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
- }
getSystemService(Context.CONNECTIVITY_SERVICE);获得网络连接服务,然后判断是否支持TYPE_MOBILE,现在判断应该是不支持,也就是if判断中:Utils.isWifiOnly(getActivity())=1,所以把“移动网络”的选项移除,现在我们就重点去分析Utils.isWifiOnly(getActivity()),找到不显示的原因,也就是if()中的判断条件,跟进这段代码。我们继续往下看cm.isNetworkSupported。
4、 再分析isNetworkSupported
android.net.ConnectivityManager;找到
4.1/frameworks/base/core/java/android/net/ ConnectivityManager.java
isNetworkSupported的实现如下:
- public boolean isNetworkSupported(int networkType) {
- try {
- return mService.isNetworkSupported(networkType);
- } catch (RemoteException e) {}
- return false;
- }
- }
继续跟进 mService.isNetworkSupported这个函数的实现
5、 mService.isNetworkSupported的实现
/frameworks/base/services/java/com/android/server/ ConnectivityService.java
- @Override
- public boolean isNetworkSupported(int networkType) {
- enforceAccessPermission();
- loge("xu_bin"+"test typevalid = " + isNetworkTypeValid(networkType) + " trackers = " + (mNetTrackers[networkType] != null));
- return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null));
- }
有两个参数: isNetworkTypeValid(networkType)、 (mNetTrackers[networkType] != null))
我们要的值是ture,可返回是false,看下这两个值是什么,把这连个值打印出来:
loge("xu_bin"+"test typevalid = " + isNetworkTypeValid(networkType) + " trackers = " + (mNetTrackers[networkType] != null));
打印结果为:
打印结果为:
i/ConnectivityService( 1426): xu_bin test typevalid = true trackers = false
也就是说:(mNetTrackers[networkType] != null)这个地方出问题;
即mNetTrackers[networkType] ==null
6、我们在看下:mNetTrackers[networkType]的由来吧
其实下面这段代码我有点蒙,连猜带蒙的,
4.0.3_r1/frameworks/base/services/java/com/android/server/ ConnectivityManager.java
- public ConnectivityService(Context context, INetworkManagementService netd,
- INetworkStatsService statsService, INetworkPolicyManager policyManager)
- {
- if (DBG) log("ConnectivityService starting up");
- …………
-
- String[] raStrings = context.getResources().getStringArray(
- com.android.internal.R.array.radioAttributes);
- for (String raString : raStrings) {
- loge("xu_bin test raStrings" + raStrings);
- loge("xu_bin test raString" + raString);
- RadioAttributes r = new RadioAttributes(raString);
- if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
- loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
- continue;
- }
- if (mRadioAttributes[r.mType] != null) {
- loge("Error in radioAttributes - ignoring attempt to redefine type " +
- r.mType);
- continue;
- }
- mRadioAttributes[r.mType] = r;
- }
-
- String[] naStrings = context.getResources().getStringArray(
- com.android.internal.R.array.networkAttributes);
-
- for (String naString : naStrings) {
- loge("xu_bin test naStrings" + naStrings);
- loge("xu_bin test naString" + naString);
- try {
- NetworkConfig n = new NetworkConfig(naString);
- if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
- loge("Error in networkAttributes - ignoring attempt to define type " +
- n.type);
- continue;
- }
- if (mNetConfigs[n.type] != null) {
- loge("Error in networkAttributes - ignoring attempt to redefine type " +
- n.type);
- continue;
- }
- …………
-
-
-
-
-
-
-
- for (int netType : mPriorityList) {
- loge("xu_bin:" + netType + " " + mNetConfigs[netType].radio + " " + ConnectivityManager.TYPE_MOBILE);
- loge("xu_bin test mNetConfigs[netType].radio =" + (mNetConfigs[netType].radio));
- switch (mNetConfigs[netType].radio) {
-
- …………
- case ConnectivityManager.TYPE_MOBILE:
- mNetTrackers[netType] = new MobileDataStateTracker(netType,
- mNetConfigs[netType].name);
- mNetTrackers[netType].startMonitoring(context, mHandler);
- break;
- case ConnectivityManager.TYPE_DUMMY:
- mNetTrackers[netType] = new DummyDataStateTracker(netType,
- mNetConfigs[netType].name);
- mNetTrackers[netType].startMonitoring(context, mHandler);
- break;
- …………
- case ConnectivityManager.TYPE_ETHERNET:
- mNetTrackers[netType] = EthernetDataTracker.getInstance();
- mNetTrackers[netType].startMonitoring(context, mHandler);
- break;
- default:
- loge("Trying to create a DataStateTracker for an unknown radio type " +
- mNetConfigs[netType].radio);
- continue;
- }
- mCurrentLinkProperties[netType] = null;
- if (mNetTrackers[netType] != null && mNetConfigs[netType].isDefault()) {
- mNetTrackers[netType].reconnect();
- }
- }
-
- …………
- }
加入打印信息:
(1)、
loge("xu_bin test raStrings" + raStrings);
loge("xu_bin test raString" + raString);
(2)、
loge("xu_bin test naStrings" + naStrings);
loge("xu_bin test naString" + naString);
(3)、
loge("xu_bin:" + netType + " " + mNetConfigs[netType].radio + " " + ConnectivityManager.TYPE_MOBILE);
loge("xu_bin test mNetConfigs[netType].radio =" + (mNetConfigs[netType].radio));
- 打印出来的值如下:
- E/ConnectivityService( 1426): xu_bin test raString s[Ljava.lang.String;@411cf008
- E/ConnectivityService( 1426): xu_bin test raString 9,1
- E/ConnectivityService( 1426): xu_bin test naString s[Ljava.lang.String;@410e30c8
- E/ConnectivityService( 1426): xu_bin test naString ethernet,9,9,1,-1,true
- E/ConnectivityService( 1426): xu_bin:9 9 0
- E/ConnectivityService( 1426): xu_bin test mNetConfigs[netType].radio =9
- D/NetworkManagementService( 1426): Registering observer
- D/NetworkManagementService( 1426): Registering observer
- D/NetworkManagementService( 1426): Registering observer
- I/WifiService( 1426): WifiService starting up with Wi-Fi disabled
- E/ConnectivityService( 1426): xu_bintest typevalid = true trackers = false
- <span style="font-family:宋体;FONT-SIZE: 10.5pt" lang="EN-US">com.android.internal.R.array.networkAttributes</span><span style="font-family:宋体;FONT-SIZE: 10.5pt">的值在:<span lang="EN-US"><o:p></o:p></span></span>
我们在:4.0.3_r1/frameworks/base/core/res/res/values/config.xml
- <string-array translatable="false" name="networkAttributes">
- <item>"wifi,1,1,1,-1,true"</item>
- <item>"mobile,0,0,0,-1,true"</item>
- <item>"mobile_mms,2,0,2,60000,true"</item>
- <item>"mobile_supl,3,0,2,60000,true"</item>
- <item>"mobile_hipri,5,0,3,60000,true"</item>
- <item>"mobile_fota,10,0,2,60000,true"</item>
- <item>"mobile_ims,11,0,2,60000,true"</item>
- <item>"mobile_cbs,12,0,2,60000,true"</item>
- <item>"wifi_p2p,13,1,0,-1,true"</item>
- </string-array>
这里面并没有:ethernet,9,9,1,-1,true ,所以我们全局搜索一下:
grep -r -w ethernet ./*得到下面有用信息:
- ./android-samsung-dev.patch:- <item>"ethernet,9,9,2,-1,true"</item>
- ./android-samsung-dev.patch:+ // Following code will forcefully allow ethernet network as usable required for v310/c210.
- ./packages/providers/DownloadProvider/src/com/android/providers/downloads/DownloadThread.java: // Following code will forcefully allow ethernet network as usable required for v310/c210.
- ./device/samsung/smdkv310/overlay/frameworks/base/core/res/res/values/config.xml: <item>"ethernet,9,9,1,-1,true"</item>
-
对比代码发现:
./packages/providers/DownloadProvider/src/com/android/providers/downloads/DownloadThread.java中删除
- Following code will forcefully allow ethernet network as usable required for v310/c210.
- if (networkUsable != DownloadInfo.NETWORK_OK) {
- Log.i(Constants.TAG, " Forcing ethernet connection usable for download to work!!!");
- networkUsable = DownloadInfo.NETWORK_OK;
- }
4.0.3_r1/device/samsung/smdkv310/overlay/frameworks/base/core/res/res/values/config.xml中删除:
- <!-- This string array should be overridden by the device to present a list of network
- attributes. This is used by the connectivity manager to decide which networks can coexist
- based on the hardware -->
- <!-- An Array of "[Connection name],[ConnectivityManager connection type],
- [associated radio-type],[priority],[restoral-timer(ms)],[dependencyMet] -->
- <!-- the 5th element "resore-time" indicates the number of milliseconds to delay
- before automatically restore the default connection. Set -1 if the connection
- does not require auto-restore. -->
-
- <string-array translatable="false" name="networkAttributes">
- <item>"ethernet,9,9,1,-1,true"</item>
- </string-array>
-
- <!-- This string array should be overridden by the device to present a list of radio
- attributes. This is used by the connectivity manager to decide which networks can coexist
- based on the hardware -->
- <!-- An Array of "[ConnectivityManager connectionType],
- [# simultaneous connection types]" -->
- <string-array translatable="false" name="radioAttributes">
- <item>"9,1"</item>
- </string-array>
把上面两部分删除就可以了。其实真正的值是在4.0.3_r1/frameworks/base/core/res/res/values/config.xml中,通过上面函数解析。
正常情况下打印值为:如:wifi mobile mobile_mms mobile_supl等都是在config.xml中配置的
- D/ConnectivityService( 1397): ConnectivityService starting up
- E/ConnectivityService( 1397): xu_bin test restring s[Ljava.lang.String;@41240e38
- E/ConnectivityService( 1397): xu_bin test restring 1,1
- E/ConnectivityService( 1397): xu_bin test restring s[Ljava.lang.String;@41240e38
- E/ConnectivityService( 1397): xu_bin test restring 0,1
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString wifi,1,1,1,-1,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile,0,0,0,-1,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_mms,2,0,2,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_supl,3,0,2,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_hipri,5,0,3,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_fota,10,0,2,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_ims,11,0,2,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString mobile_cbs,12,0,2,60000,true
- E/ConnectivityService( 1397): xu_bin test naString s[Ljava.lang.String;@41241880
- E/ConnectivityService( 1397): xu_bin test naString wifi_p2p,13,1,0,-1,true