Android8.1 System UI StatusBar状态栏添加静音图标

首先,按下按键后会走到PhoneWindowManager中。

//frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java
public class PhoneWindowManager implements WindowManagerPolicy {
    /** {@inheritDoc} */
    @Override
    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
.........
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
                || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
            if (mUseTvRouting || mHandleVolumeKeysInWM) {
                // On TVs or when the configuration is enabled, volume keys never
                // go to the foreground app.
                dispatchDirectAudioEvent(event);
                return -1;
            }
.........
        // Let the application handle the key.
        return 0;
    }

这里并没有调用dispatchDirectAudioEvent,由于mUseTvRouting 和mHandleVolumeKeysInWM都为false,所以会继续向下分发。有System UI中有定义VolumeDialogImpl,按下音量调节后会音量条视图弹出。
如下所示。

https://blog.csdn.net/qq_25094789/article/details/90237770?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3

VolumeDialogImpl中调节音量(无论是增加还是减少)是调用updateVolumeRowHvlevel是音量,因此新增代码是以vlevel为0为判断条件,发送广播。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\volume\VolumeDialogImpl.java
public class VolumeDialogImpl implements VolumeDialog, TunerService.Tunable {
     .........................

    private void updateVolumeRowH(VolumeRow row) {
        Log.i(TAG, "updateVolumeRowH s=" + row.stream);
     .........................
        // update slider
        final boolean enableSlider = !zenMuted;
        final int vlevel = row.ss.muted && (!isRingStream && !zenMuted) ? 0
                : row.ss.level;
        updateVolumeRowSliderH(row, enableSlider, vlevel);

        if(vlevel == 0){
            Intent intent = new Intent();
            intent.setAction("android.media.silent");
            mContext.sendBroadcast(intent);
        }else {
            Intent intent = new Intent();
            intent.setAction("android.media.normal");
            mContext.sendBroadcast(intent);
        }
    }

根据Android N SystemUI-状态栏可知,状态栏的图标是分成通知图标和系统图标的。添加静音图标算是系统图标的添加。

PhoneStatusBarPolicy处理status bar的图标的显示,因此需要添加2个地方1.addAction 2.mIconController.setIconVisibility
具体修改如下。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBarPolicy.java
public class PhoneStatusBarPolicy implements Callback, Callbacks,
        RotationLockControllerCallback, Listener, LocationChangeCallback,
        ZenModeController.Callback, DeviceProvisionedListener, KeyguardMonitor.Callback {
            .........................
    @VisibleForTesting
    public PhoneStatusBarPolicy(Context context, StatusBarIconController iconController) {
        mContext = context;
        mIconController = iconController;
        ......................... 
         filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);                        
        .........................
        filter.addAction("android.media.silent");
        filter.addAction("android.media.normal");
            .........................
     }


    @VisibleForTesting
    /*private*/ BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION) ||
                    action.equals(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION)) {
                updateVolumeZen();
            }else if (action.equals("android.media.silent")){
                mIconController.setIcon(mSlotVolume, R.drawable.stat_sys_ringer_silent, null);
                mIconController.setIconVisibility(mSlotVolume, true);
            }else if (action.equals("android.media.normal")){
                mIconController.setIconVisibility(mSlotVolume, false);
            }
            .........................
    }
}

关于Slot,可以简单参考下面的文章。
vue 里面的slot属性

这里注意要用stat_sys_ringer_silent这个stat开头的图标,不然显示出来,跟其他图标不协调。
颜色控制部分在StatusBarIconController中,涉及到xfermode的使用,具体就不展开分析了。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\statusbar\phone\StatusBarIconController.java
        public void setTint(int color) {
            mColor = color;
            for (int i = 0; i < mGroup.getChildCount(); i++) {
                View child = mGroup.getChildAt(i);
                if (child instanceof StatusBarIconView) {
                    StatusBarIconView icon = (StatusBarIconView) child;
                    icon.setStaticDrawableColor(mColor);
                }
            }
        }

参考链接:
SystemUI 状态栏 system icon 和 notification icon 的修改
状态栏 添加 system icon
SystemUI源码分析三(StatusBar的加载流程)
Android 8.0 SystemUI(三):一说顶部 StatusBar
android(cm11)状态栏源代码分析(一)
android 7.0 system UI之快速启动栏的分析(一)
Android N SystemUI-状态栏

Android 修改系统音量条UI

xfermode的基本用法
Android-使用 SetColorFilter 神奇地改变图片的颜色

你可能感兴趣的:(Android8.1 System UI StatusBar状态栏添加静音图标)