SystemServer(一):恢复出厂设置后wifi、数据流量、gps设置关闭

3 篇文章 0 订阅
1 篇文章 0 订阅

SystemServer是在虚拟机启动后,开始初始化和运行。其它的系统服务在System Server进程的环境中运行。/base/services/java/com/android/server/SystemServer.java 

我们在initAndLoop()方法中很快就定位到控制wifi开启或关闭的方法WifiService wifi = null; wifi.checkAndStartWifi();

跟踪到WifiService中的checkAndStartWifi();

 public void checkAndStartWifi() {
        /* Check if wi-fi needs to be enabled */
        boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
                (wifiEnabled ? "enabled" : "disabled"));

        //FixL1860_Bug00000177,For setWifiEnabled may sometimes have exception.
        //So Make sure WifiWatchdogStateMachine is created first.
        mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
               makeWifiWatchdogStateMachine(mContext);

        // If we are already disabled (could be due to airplane mode), avoid changing persist
        // state here
        //if (wifiEnabled) setWifiEnabled(wifiEnabled);
        if (wifiEnabled) setWifiEnabledOnBoot(wifiEnabled);
    }
到此我们已经查到了wifi开启或者关闭是由 boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();控制的。

 synchronized boolean isWifiToggleEnabled() {
        if (!mCheckSavedStateAtBoot) {
            mCheckSavedStateAtBoot = true;
            if (testAndClearWifiSavedState()) return true;            
        }

        if (mAirplaneModeOn) {
            return mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE;
        } else {
            return mPersistWifiState != WIFI_DISABLED;
        }
    }

我们继续分析代码。发现mCheckSavedStateAtBoot是只我们修改过默认初始值后,手机再次重启将查询我们修改的值。

    private boolean testAndClearWifiSavedState() {
        final ContentResolver cr = mContext.getContentResolver();
        int wifiSavedState = 0;
        try {
            wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE);
            if(wifiSavedState == 1)
                Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
        } catch (Settings.SettingNotFoundException e) {
            ;
        }
        System.out.println("wifiSavedState="+wifiSavedState);
        return (wifiSavedState == 1);
    }

这段代码会Settings.Global.getInt 数据库里的wifi状态值,当然我们现在是想定位到系统默认值,那么就不做深入分析了,有兴趣的朋友可以自己看看。回到上isWifiToggleEnabled代码中去,继续分析,这么有一个判断如果是飞行模式则return mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE;不是则return mPersistWifiState != WIFI_DISABLED;。那么这个mPersistWifiState是什么呢?

private int mPersistWifiState = WIFI_DISABLED;它定义的初始值是WIFI_DISABLED,如果异常我们先关闭掉wifi,如果没有异常(这是防止空指针报错赋值),当然我们初始化

WifiSettingsStore对象时,将重新赋值。

  WifiSettingsStore(Context context) {
        mContext = context;
        mAirplaneModeOn = getPersistedAirplaneModeOn();
        mPersistWifiState = getPersistedWifiState();
        mScanAlwaysAvailable = getPersistedScanAlwaysAvailable();
    }
private int getPersistedWifiState() {
        final ContentResolver cr = mContext.getContentResolver();
        try {
            return Settings.Global.getInt(cr, Settings.Global.WIFI_ON);
        } catch (Settings.SettingNotFoundException e) {
            Settings.Global.putInt(cr, Settings.Global.WIFI_ON, WIFI_DISABLED);
            return WIFI_DISABLED;
        }
    }
这边就是我们获取数据库默认值的方法了。 Settings.Global.getInt(cr, Settings.Global.WIFI_ON); WIFI_ON又是什么呢?分析代码后发现其实是数据库的一个字符串。

       /**
        * Whether the Wi-Fi should be on.  Only the Wi-Fi service should touch this.
        */
       public static final String WIFI_ON = "wifi_on"
如何快速定位到这个数据库就要看个人经验了,其实我这边是用了个比较懒的方法,我直接在服务器中搜索的这个字段。如果很快就看到了databaseHelper类了。呵呵,是不是很偷懒。哈哈。

 loadBooleanSetting(stmt, Settings.Global.WIFI_ON,
                    R.bool.def_wifi_on);
这里就可以很清晰的看到wifi_on字段是有 R.bool.def_wifi_on控制的。

 private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
        loadSetting(stmt, key,
                mContext.getResources().getBoolean(resid) ? "1" : "0");
    }
大致思路就分析到这里了,很多具体的步骤因为太烦躁也没什么难点就不看了,有兴趣的小伙伴可以自己去看看。

同样移动数据,GPS关闭都是由此数据库控制的。

移动数据:

由设置中的DataUsageSummary类setMobileDataEnabled(boolean enabled) 方法获取

mMobileDataEnabled = mConnService.getMobileDataEnabled();boolean值

  public boolean getMobileDataEnabled() {
        try {
            return mService.getMobileDataEnabled();
        } catch (RemoteException e) {
            return true;
        }
    }
   public boolean getMobileDataEnabled() {
        // TODO: This detail should probably be in DataConnectionTracker's
        //       which is where we store the value and maybe make this
        //       asynchronous.
        enforceAccessPermission();
        boolean retVal = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.MOBILE_DATA, 1) == 1;
        if (VDBG) log("getMobileDataEnabled returning " + retVal);
        return retVal;
    }
</pre></p><p>MOBILE_DATA一样是DataBaseHelper创建的数据库中的字段,不同是他不是由bool值定义的,它SystemProperties一定的,如果没定义则默认返回false。</p><p><pre name="code" class="java">  // Mobile Data default, based on build
            loadSetting(stmt, Settings.Global.MOBILE_DATA,
                    "true".equalsIgnoreCase(
                            SystemProperties.get("ro.com.android.mobiledata",
                                    "false")) ? 1 : 0);
GPS:

设置中LocationSettings类。

    private boolean getAGPSSwitch() {
        int enable = Settings.Global.getInt(mContentResolver,
                Settings.Global.ASSISTED_GPS_ENABLED, AGPS_OFF);
        return enable == AGPS_ON ? true : false;
    }

      loadBooleanSetting(stmt, Settings.Global.ASSISTED_GPS_ENABLED,
                    R.bool.assisted_gps_enabled);



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值