Android知识Android技术知识

下拉框中电池显示流程

2017-06-05  本文已影响64人  CodeMyLove

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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读