下拉框中电池显示流程

作者: CodeMyLove | 来源:发表于2017-06-05 11:09 被阅读64次

    statusbar

    电池状态是在statusbar显示的,statusbar 的代码在 frameworks/base/packages/SystemUI 中实现,而且是通过MVC的架构来管理的。

    Model

    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsModel.java
    QuickSettingsModel 实现了状态的callback,并且持有View的实例,代码如下:

    class QuickSettingsModel implements 
            BatteryStateChangeCallback {
        ···
        static class BatteryState extends State {
            int batteryLevel;
            boolean pluggedIn;
        }
        private QuickSettingsTileView mBatteryTile;
        private RefreshCallback mBatteryCallback;
        private BatteryState mBatteryState = new BatteryState();
        ···
        // Battery
        void addBatteryTile(QuickSettingsTileView view, RefreshCallback cb) {
            mBatteryTile = view;
            mBatteryCallback = cb;
            mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
        }
        // BatteryController callback
        @Override
        public void onBatteryLevelChanged(int level, boolean pluggedIn) {
            mBatteryState.batteryLevel = level;
            mBatteryState.pluggedIn = pluggedIn;
            mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
        }
        void refreshBatteryTile() {
            mBatteryCallback.refreshView(mBatteryTile, mBatteryState);
        }
    

    View

    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsTileView.java

    class QuickSettingsTileView extends FrameLayout {
    }
    

    Controller

    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java
    BatteryController 通过广播接收器,获取到电池现在的状态后,回调所有在这里注册了的callback

    public class BatteryController extends BroadcastReceiver {
        public void addStateChangedCallback(BatteryStateChangeCallback cb) {
            mChangeCallbacks.add(cb);
        }
        public void onReceive(Context context, Intent intent) {
                ...
                for (BatteryStateChangeCallback cb : mChangeCallbacks) {
                    cb.onBatteryLevelChanged(level, plugged);
                }
                ...
            }
        }
    }
    

    MVC

    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java
    QuickSettings 持有一个model和batteryController,

    class QuickSettings {
        private QuickSettingsModel mModel;
        ...
        void setup(NetworkController networkController, BluetoothController bluetoothController,
                BatteryController batteryController, LocationController locationController,
                RotationLockController rotationLockController) {
            ...
           //batteryController会回调model的Callback,也就是说C会控制M
            batteryController.addStateChangedCallback(mModel);
            ...
        }
        private void addSystemTiles(ViewGroup parent, LayoutInflater inflater) {
                // Battery
            final QuickSettingsTileView batteryTile = (QuickSettingsTileView)
                    inflater.inflate(R.layout.quick_settings_tile, parent, false);
            batteryTile.setContent(R.layout.quick_settings_tile_battery, inflater);
            batteryTile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startSettingsActivity(Intent.ACTION_POWER_USAGE_SUMMARY);
                }
            });
    
            //Model控制View的刷新
            mModel.addBatteryTile(batteryTile, new QuickSettingsModel.RefreshCallback() {
                @Override
                public void refreshView(QuickSettingsTileView unused, State state) {
                    QuickSettingsModel.BatteryState batteryState =
                            (QuickSettingsModel.BatteryState) state;
                    String t;
                    if (batteryState.batteryLevel == 100) {
                        t = mContext.getString(R.string.quick_settings_battery_charged_label);
                    } else {
                        t = batteryState.pluggedIn
                            ? mContext.getString(R.string.quick_settings_battery_charging_label,
                                    batteryState.batteryLevel)
                            : mContext.getString(R.string.status_bar_settings_battery_meter_format,
                                    batteryState.batteryLevel);
                    }
                    ((TextView)batteryTile.findViewById(R.id.text)).setText(t);
                    batteryTile.setContentDescription(
                            mContext.getString(R.string.accessibility_quick_settings_battery, t));
                }
            });
            parent.addView(batteryTile);
        }
    }
    

    相关文章

      网友评论

        本文标题:下拉框中电池显示流程

        本文链接:https://www.haomeiwen.com/subject/tedifxtx.html