`
836811384
  • 浏览: 547836 次
文章分类
社区版块
存档分类
最新评论

[Android源码分析]从蓝牙界面看设置中fragment的实现

 
阅读更多

本文和上一篇博文的关联性较大,基本属于后续分析篇。

上一篇博文地址:http://blog.csdn.net/u011960402/article/details/12612173

2. Settings中蓝牙的fragment

其实在开机情况下,我们还会看到另外一个界面,就是点击一下蓝牙的那个header,会显示如图2所示的界面。这个界面是如何实现的呢,我们来着重分析一下。

我们知道点击header,其实就是调用xml文件中对应的fragment。(为什么?请看androiddeveloper中的设置的介绍,或者看我翻译的版本:http://blog.csdn.net/u011960402/article/details/12569539,哈哈),ok,我们来看一下和蓝牙比较相关的fragment,也就是BluetoothSettings的实现。

2,蓝牙header点击后的显示界面

先来看一下xmlfragment的设置:

android:fragment="com.android.settings.bluetooth.BluetoothSettings"

所以,很明确,我们要去看BluetoothSettings了。

public final class BluetoothSettings extends DeviceListPreferenceFragment

可以很明确地看到BluetoothSettings是扩展DeviceListPreferenceFragment,而BluetoothSettings中并没有重写onCreate()方法,所以,我们去DeviceListPreferenceFragment看看。

从代码中可以清晰看到,DeviceListPreferenceFragment是有onCreate方法的,我们来研究一下:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//得到localmanager
        mLocalManager = LocalBluetoothManager.getInstance(getActivity());
        if (mLocalManager == null) {
            Log.e(TAG, "Bluetooth is not supported on this device");
            return;
        }
//得到localadapter
        mLocalAdapter = mLocalManager.getBluetoothAdapter();
//这个会在子类中实现,所以,我们会回到BluetoothSettings去看一下,果然重写了这个函数,具体看2.1
        addPreferencesForActivity();
//得到KEY_BT_DEVICE_LIST(bt_device_list)的preference,他其实是一个PreferenceCategory
        mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
        if (this instanceof DevicePickerFragment) {
//若是DevicePickerFragment的实例,则把device picker位置起来
            ((ProgressCategory)mDeviceListGroup).setDevicePicker(true);
        }
    }

2.1 BluetoothSettingsaddPreferencesForActivity分析

@Override
    void addPreferencesForActivity() {
//首先load bluetooth_settings,这个xml文件见2.1.1
        addPreferencesFromResource(R.xml.bluetooth_settings);
//得到调用这个fragment的activity
        Activity activity = getActivity();
//新建一个开关,这个开关从名字上就可以清晰地看出来是actionbar上面的那个开关
        Switch actionBarSwitch = new Switch(activity);
//看activity是否是PreferenceActivity的实例,我们是从setting那边过来的,这个if肯定满足了
        if (activity instanceof PreferenceActivity) {
//转换为PreferenceActivity
            PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
// 
            if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
//得到开关的pixel
                final int padding = activity.getResources().getDimensionPixelSize(
                        R.dimen.action_bar_switch_padding);
//根据上面得到的pixel进行padding
                actionBarSwitch.setPadding(0, 0, padding, 0);
//显示定制的view
            activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                        ActionBar.DISPLAY_SHOW_CUSTOM);
//设置定制的view
                activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
                        ActionBar.LayoutParams.WRAP_CONTENT,
                        ActionBar.LayoutParams.WRAP_CONTENT,
                        Gravity.CENTER_VERTICAL | Gravity.RIGHT));
            }
        }
//这里构建了一个BluetoothEnabler,见1.1的分析
        mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
//这个是为menu键服务的,否则下面的onCreateOptionsMenu就没有用武之地了
//这里就顺便看一下menu键的处理,见2.1.2
        setHasOptionsMenu(true);
    }

2.1.1 bluetooth_settingsxml文件分析

我们发现,其实bluetooth_settings文件很简单,就只有一个名字而已。一个PreferenceScreen

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/bluetooth_settings" >

</PreferenceScreen>

2.1.2蓝牙设置界面的menu键处理

在未打开蓝牙的时候,我们按menu键后的显示如图3。可以看到大概弹出了3menu的框,那这些是怎么实现的呢?我们来看代码:

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//其实menu键的显示和蓝牙是否打开,以及是否可以发现是密切相关的
//先得到他们的状态
        boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
        boolean isDiscovering = mLocalAdapter.isDiscovering();
//就是正在扫描的时候显示”正在搜索。。”,否则显示“搜索设备”
        int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
            R.string.bluetooth_search_for_devices;
//注意到没有,这里是SHOW_AS_ACTION_IF_ROOM,意思就是有地方的话,我就在actionbar上面显示为一个按钮吧,所以,在不按menu键的时候它也是存在的,按了menu键之后,在图3中反而被挡住了,可以参见图2 ,图2中没有按menu键,就可以看到了。
 menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
//蓝牙没有打开的时候以及正在搜索的时候都会反灰
                .setEnabled(bluetoothIsEnabled && !isDiscovering)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//修改名字的按钮,在蓝牙没有打开的时候是返灰的
//他的显示方式是SHOW_AS_ACTION_NEVER,就是不在action bar中显示
        menu.add(Menu.NONE, MENU_ID_RENAME_DEVICE, 0, R.string.bluetooth_rename_device)
                .setEnabled(bluetoothIsEnabled)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
//可发现超时的menu
        menu.add(Menu.NONE, MENU_ID_VISIBILITY_TIMEOUT, 0, R.string.bluetooth_visibility_timeout)
                .setEnabled(bluetoothIsEnabled)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
//显示接收到的文件,这个注意一下,即使蓝牙关闭他也没有返灰哦,毕竟蓝牙关闭了额,他也是可以显示接收到的文件嘛
        menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }


3,蓝牙关闭的时候menu键后的显示

2.2 onResume的分析

至此,onCreate就已经分析完成了,细心的同学会发现,好像这个ui还有很多东西啊,比如中间那个“要监测设备,请打卡蓝牙功能”,这样的语句都是什么时候显示的,怎么没有看到啊?呵呵,不急,我们之后activityoncreate之后还会走到onResume,我们来继续分析onResume的实现,看是否会给我们什么惊喜,哈哈~~

 @Override
    public void onResume() {
        // resume BluetoothEnabler before calling super.onResume() so we don't get
        // any onDeviceAdded() callbacks before setting up view in updateContent()
//这里大概的意思就是先把BluetoothEnabler的resume调用一下,这样在我们updateContent的时候,就不会收到onDeviceAdded的回调了。见2.2.1
        mBluetoothEnabler.resume();
        super.onResume();
		//这个地方开始的时候是没有的
        if (mDiscoverableEnabler != null) {
            mDiscoverableEnabler.resume();
        }
		//注册了一个local name change的reveiver
        getActivity().registerReceiver(mReceiver, mIntentFilter);
	//这个就比较重要了,详细见2.2.2,从UI的角度来看,就是刷新中间我们见到的内容
        updateContent(mLocalAdapter.getBluetoothState(), mActivityStarted);
    }

2.2.1 BluetoothEnablerresume分析

 public void resume() {
//没有蓝牙,就会把switch 返灰
        if (mLocalAdapter == null) {
            mSwitch.setEnabled(false);
            return;
        }

        if (WirelessSettings.isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) {
            // Bluetooth state is not sticky, so set it manually
//手动设置蓝牙的状态。就是根据状态设置按钮的状态,不详细解释了
            handleStateChanged(mLocalAdapter.getBluetoothState());
        } else {
//否则就把按钮直接干掉,好残忍。。
            mSwitch.setChecked(false);
            mSwitch.setEnabled(false);
        }
//这个不多解释了
        if (mSupportBtWifiCoexist == false && isWifiAndWifiApStateDisabled() == false) {
            mSwitch.setChecked(false);
            mSwitch.setEnabled(false);
        }
       //注册一个receiver,主要用来接收state change和airplane mode的change
mContext.registerReceiver(mReceiver, mIntentFilter);
//还有一个按钮的监听,这个就是我们按了按钮之后的处理了,在以后再分析
        mSwitch.setOnCheckedChangeListener(this);
    }

2.2.2 updateContent的详细分析

这个函数其实就是刷新中间的内容,根据不同的蓝牙状态来进行的。这里我们先只关心蓝牙关闭的时候情况:

 private void updateContent(int bluetoothState, boolean scanState) {
//先得到root preference
        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        int messageId = 0;

        switch (bluetoothState) {
		//这个暂时先不管
            case BluetoothAdapter.STATE_ON:
……
			//但是要关注的是这里是return,不是break,小心!!
return;
           case BluetoothAdapter.STATE_TURNING_OFF:
			//正在关闭蓝牙。。
                messageId = R.string.bluetooth_turning_off;
                break;

            case BluetoothAdapter.STATE_OFF:
			//这个就是那句“要监测设备,请打开蓝牙功能”的这句话了
                messageId = R.string.bluetooth_empty_list_bluetooth_off;
                break;

            case BluetoothAdapter.STATE_TURNING_ON:
			//正在打开蓝牙。。
                messageId = R.string.bluetooth_turning_on;
                break;
        }
		//这里就是把显示的东西都清空掉
		//只留一个text
        setDeviceListGroup(preferenceScreen);
		//这里包含了停止扫描
        removeAllDevices();
		//留下的text中显示内容
        mEmptyView.setText(messageId);
//这里通知系统menu发生了改变,这样才会在不同的蓝牙状态下显示不同的menu
        getActivity().invalidateOptionsMenu();
}

至此,在蓝牙关闭的情况下,设置中能见到的关于蓝牙的UI内容就全部分析结束了。你还有疑问吗?

若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics