Android-源码分析-分析手机热点里的AP Band(频段)被隐藏/置灰的原因?

本博文记录寻找手机热点中AP Band(频段)被隐藏/置灰的原因,相似问题同理去查找解决。

先放上一张MTK平台手机-热点-AP Band界面效果图:在这里插入图片描述MTK平台手机-热点-AP Band界面
很明显,界面中的AP Band选项变灰,无法点击编辑修改内容,如果是AP Band 被隐藏或者其他选项(Hotspot password。。。)也被置灰或隐藏也是一样的步骤去寻找原因解决问题。

步骤一:找到控制选项AP Band 的源码位置

  1. 进入服务器源码目录:我这里是Ubuntu服务器上存放的Android源码
  2. 在/package/app目录下使用查找命令
grep -r "AP Band"

运行查找命令与运行结果如图:grep -r "AP Band"
3. 所以找到了AP Band 选项控制源码文件的位置:

\packages\apps\Car\Settings\src\com\android\car\settings\wifi\WifiTetherApBandPreferenceController.java
  1. WifiTetherApBandPreferenceController翻译为无线路由器接入点频段首选控制器

步骤二:分析源码结构与内容

代码不算多,但直接看这个结构会比较省事:

——包-- com.android.car.settings.wifi
       ——类--WifiTetherApBandPreferenceController
             ——方法-- validateSelection(int band)---验证选择
                      updatePreferenceEntries--更新首选项-
                      updateApBand()---更新接入点频段
                      getBandEntry()---获取频段条目
                      is5GhzBandSupported() ---支持is5Ghz频段
                      。。。。。。

WifiTetherApBandPreferenceController.java代码内容如下:(选择性看)

package com.android.car.settings.wifi;

import android.car.drivingstate.CarUxRestrictions;
import android.content.Context;
import android.content.res.Resources;
import android.net.wifi.SoftApConfiguration;
import android.util.Log;
import androidx.preference.ListPreference;
import com.android.car.settings.R;
import com.android.car.settings.common.FragmentController;

/**
 * Controls WiFi Hotspot AP Band configuration.
 */
public class WifiTetherApBandPreferenceController extends
        WifiTetherBasePreferenceController<ListPreference> {
    private static final String TAG = "CarWifiTetherApBandPref";

    private String[] mBandEntries;
    private String[] mBandSummaries;
    private int mBand;

    public WifiTetherApBandPreferenceController(Context context, String preferenceKey,
            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
        super(context, preferenceKey, fragmentController, uxRestrictions);
    }

    @Override
    protected Class<ListPreference> getPreferenceType() {
        return ListPreference.class;
    }

    @Override
    protected void onCreateInternal() {
        super.onCreateInternal();
        updatePreferenceEntries();
        getPreference().setEntries(mBandSummaries);
        getPreference().setEntryValues(mBandEntries);
    }

    @Override
    public void updateState(ListPreference preference) {
        super.updateState(preference);

        SoftApConfiguration config = getCarSoftApConfig();
        if (config == null) {
            mBand = SoftApConfiguration.BAND_2GHZ;
        } else if (!is5GhzBandSupported() && config.getBand() == SoftApConfiguration.BAND_5GHZ) {
            SoftApConfiguration newConfig = new SoftApConfiguration.Builder(config)
                    .setBand(SoftApConfiguration.BAND_2GHZ)
                    .build();
            setCarSoftApConfig(newConfig);
            mBand = newConfig.getBand();
        } else {
            mBand = validateSelection(config.getBand());
        }

        if (!is5GhzBandSupported()) {
            preference.setEnabled(false);
            preference.setSummary(R.string.wifi_ap_choose_2G);
        } else {
            preference.setValue(Integer.toString(config.getBand()));
            preference.setSummary(getSummary());
        }

    }

    @Override
    protected String getSummary() {
        if (!is5GhzBandSupported()) {
            return getContext().getString(R.string.wifi_ap_choose_2G);
        }
        switch (mBand) {
            case SoftApConfiguration.BAND_2GHZ | SoftApConfiguration.BAND_5GHZ:
                return getContext().getString(R.string.wifi_ap_prefer_5G);
            case SoftApConfiguration.BAND_2GHZ:
                return mBandSummaries[0];
            case SoftApConfiguration.BAND_5GHZ:
                return mBandSummaries[1];
            default:
                Log.e(TAG, "Unknown band: " + mBand);
                return getContext().getString(R.string.wifi_ap_prefer_5G);
        }
    }

    @Override
    protected String getDefaultSummary() {
        return null;
    }

    @Override
    public boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
        mBand = validateSelection(Integer.parseInt((String) newValue));
        updateApBand(); // updating AP band because mBandIndex may have been assigned a new value.
        refreshUi();
        return true;
    }

    private int validateSelection(int band) {
        // unsupported states:
        // 1: BAND_5GHZ only - include 2GHZ since some of countries doesn't support 5G hotspot
        // 2: no 5 GHZ support means we can't have BAND_5GHZ - default to 2GHZ
        if (SoftApConfiguration.BAND_5GHZ == band) {
            if (!is5GhzBandSupported()) {
                return SoftApConfiguration.BAND_2GHZ;
            }
            return SoftApConfiguration.BAND_5GHZ | SoftApConfiguration.BAND_2GHZ;
        }
        return band;
    }

    private void updatePreferenceEntries{
        Resources res = getContext().getResources();
        int entriesRes = R.array.wifi_ap_band;
        int summariesRes = R.array.wifi_ap_band_summary;
        mBandEntries = res.getStringArray(entriesRes);
        mBandSummaries = res.getStringArray(summariesRes);
    }

    private void updateApBand() {
        SoftApConfiguration config = new SoftApConfiguration.Builder(getCarSoftApConfig())
                .setBand(mBand)
                .build();
        setCarSoftApConfig(config);
        getPreference().setValue(getBandEntry());
    }

    private String getBandEntry() {
        switch (mBand) {
            case SoftApConfiguration.BAND_2GHZ | SoftApConfiguration.BAND_5GHZ:
            case SoftApConfiguration.BAND_2GHZ:
                return mBandEntries[0];
            case SoftApConfiguration.BAND_5GHZ:
                return mBandEntries[1];
            default:
                Log.e(TAG, "Unknown band: " + mBand + ", defaulting to 2GHz");
                return mBandEntries[0];
        }
    }

    private boolean is5GhzBandSupported() {
        String countryCode = getCarWifiManager().getCountryCode();
        return getCarWifiManager().is5GhzBandSupported() && countryCode != null;
    }
}

步骤三:找到关键代码分析原因

在上面的代码中,分析每个方法的具体作用以及是如何调用的,
发现:方法is5GhzBandSupported()在updateState()方法里被调用:

if (!is5GhzBandSupported()) {
            preference.setEnabled(false);
        	preference.setSummary(R.string.wifi_ap_choose_2G);
        }

里面的 preference.setEnabled(false); setEnabled使能控件
由此可以分析
原因: 需要禁用并置灰preference时,调用mPreference.setEnable(false)
我们的AP Band 选项变灰是因为这里的条件判断语句:不支持5GHz时设置使能控件值为false选项被禁用了。

拓展:

1.setEnabled使能控件

设置为false,该控件永远不会活动,不管设置为什么属性,都无效;
设置为true,表明激活该控件,控件处于活动状态,处于活动状态,就能响应事件了,比如触摸、点击、按键事件等;
setEnabled就相当于总开关一样,只有总开关打开了,才能使用其他事件。

2.如果是AP Band 频段被隐藏呢?

同样的方法先找到AP Band的位置,对源码进行分析理解,其中隐藏Preference有两种方法
一、先在xml布局里面删,然后在java里面删掉调用的相关部分,但如果很多地方都有调用,那么删除就很麻烦;
二、用removePreference(Preference preference) 方法 删除;(推荐方法)

3.对Android——Preference进行更多了解学习

我这里就不再赘述,直接放上几个相关内容其他开发者的博文链接以供查阅:

Android——Preference详解之Preference系的基本应用和管理(二)
Android APP:Preference使用详解和实例(附源码)

4.对源码中涉及的部分关键字进行理解

这个内容也是比较琐碎,放上几个学习查阅链接:
Java中的关键字有哪些?「Java中53个关键字的意义及使用方法」
Java 关键字—w3schools

小结

  1. 解决问题前至少要有一个大致思路,以此不在不相干的问题上消耗过多的时间精力。
  2. 解决问题的过程中有不理解的地方就要及时搜索查阅资料,不然容易变浆糊。
  3. 解决问题后及时复盘。
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值