android 4.0 "移动网络" 选项 不存在

关键词:android 4.0 移动网络 Mobile networks
平台信息:
内核:linux2.6/linux3.0
系统:android/android4.0
平台:S5PV310(samsung exynos4210)

作者:xubin341719(欢迎转载,请注明作者)

前几天调试3G模块,最后发现我拿到的BSP,在“设置”中没有关于3G的选项,状态栏也没有显示3G状态的图标,如下图所示,这个可好,把3G的驱动加入,进程也起来了,就是不知道怎么连接。最可恶的就是在网上看到一哥们也遇到相同的问题,他发贴子求帮助,最后问题解决了,结贴时写一个“问题解决,结贴”,问他怎么搞的,他也没回,有点不“厚道”呀,嘿嘿!还是自己找吧……

1、“Mobile networks”字符串定义

我们想要的数据没有显示,从不显示的字符找起,顺藤摸瓜吧,这些对我们这些做驱动的有点难度。“Mobile networks”不显示,那我们就从最开始跟下代码吧。

4.0.3_r1/packages/apps/Settings/res/values/String.xml
  <!-- Wireless controls, item title to go into the network settings --> 
  <string name="network_settings_title">Mobile networks</string>

2、network_settings_title引用

4.0.3_r1/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这个变量吧。

在相同的文件中我们找到:

 // Remove Mobile Network Settings if it's a wifi-only device. 
  if (Utils.isWifiOnly(getActivity())) { 
  getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS)); 
  } 

getSystemService(Context.CONNECTIVITY_SERVICE);获得网络连接服务,然后判断是否支持TYPE_MOBILE,现在判断应该是不支持,也就是if判断中:Utils.isWifiOnly(getActivity())=1

所以把移动网络的选项移除,现在我们就重点去分析Utils.isWifiOnly(getActivity()),找到不显示的原因,也就是if()中的判断条件,跟进这段代码。

3、我们去看下ifWifiOnly()是何方神圣?

4.0.3_r1/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);
}

Utils.isWifiOnly(getActivity())=1也就是

cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) = false,可是我们现在要的值是true,为什么会出现false呢,我们继续往下看cm.isNetworkSupported

4、 再分析isNetworkSupported

android.net.ConnectivityManager;找到

4.0.3_r1/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的实现

4.0.3_r1/frameworks/base/services/java/com/android/server/ ConnectivityManager.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));

打印结果为:

打印结果为:

E/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");
           …………
  // Load device network attributes from resources
  String[] raStrings = context.getResources().getStringArray(
  com.android.internal.R.array.radioAttributes);
  for (String raString : raStrings) {
          loge("xu_bin test raStrings" + raStrings); // 1
          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);//2
          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;
  }
  …………
  /*
  * Create the network state trackers for Wi-Fi and mobile
  * data. Maybe this could be done with a factory class,
  * but it's not clear that it's worth it, given that
  * the number of different network types is not going
  * to change very often.
  */
  for (int netType : mPriorityList) {
loge("xu_bin:" + netType + " " + mNetConfigs[netType].radio + " " + ConnectivityManager.TYPE_MOBILE);
loge("xu_bin test mNetConfigs[netType].radio =" + (mNetConfigs[netType].radio));//3
  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
com.android.internal.R.array.networkAttributes的值在: 

我们在: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. --> 
  <!-- the 6th element indicates boot-time dependency-met value. --> 
  <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

下面就可以看到“移动网络”了:

android 4.0 "移动网络" 选项 不存在_第1张图片


 

你可能感兴趣的:(android,网络,String,mobile,NetWork)