Status bar of android11SystemUI

Status bar of Android 11 SystemUi

Icon controller for the status bar

The icons on the status bar are implemented by an interface, this interface is
StatusBarIconController, and its implementation class is
StatusBarIconControllerImpl

/**
 * Receives the callbacks from CommandQueue related to icons and tracks the state of
 * all the icons. Dispatches this state to any IconManagers that are currently
 * registered with it.
 */
@Singletonpublic class StatusBarIconControllerImpl extends StatusBarIconList implements 
Tunable,        ConfigurationListener, Dumpable, CommandQueue.Callbacks, 
StatusBarIconController {

The comment here is that it receives a callback from the CommandQueue about the icon, and then distributes it to the registered IconManager. However, its function is not only that, SystemUI can call it anywhere to set the icon for the status bar, because it also implements the StatusBarIconController interface.

public interface StatusBarIconController {
    // 设置图标
    public void setIcon(String slot, int resourceId, CharSequence contentDescription);
    public void setIcon(String slot, StatusBarIcon icon);
    // 设置wifi图标
    public void setSignalIcon(String slot, WifiIconState state);
    // 设置手机信号图标
    public void setMobileIcons(String slot, List<MobileIconState> states);
    // 设置图标可见性
    public void setIconVisibility(String slot, boolean b);
}

Continue to look at the constructor

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

public StatusBarIconControllerImpl(Context context) {
    mContext = context;
    
    // 通过父类保存状态栏上图标的名字
    super(context.getResources().getStringArray(
            com.android.internal.R.array.config_statusBarIcons));
            
    // 注册CommandQueue的回调
    SysUiServiceProvider.getComponent(context, CommandQueue.class)
            .addCallback(this);
    
    // 实现黑名单的功能,原理是监听数据库中黑名单的值
    Dependency.get(TunerService.class).addTunable(this, ICON_BLACKLIST);

}

Here are three main things to save the name of the icon on the status bar. Register callbacks with CommandQueue. Monitor the database blacklist. The function of the blacklist is very simple. It is to monitor the changes of the URI of the blacklist in the database, then update the blacklist, and then check the blacklist when setting the icon, and then decide how to set it. CommandQueue is a Binder class. It is registered in StatusBarManagerService by StatusBar#start() to receive messages from the server. In fact, it is equivalent to registering a callback on the server. The StatusBarIconControllerImpl registers a callback with the CommandQueue in order to obtain information about the icon so that it can correctly set the icon on the status bar. Now look at the array of saved icon names config_statusBarIcons // frameworks/base/core/res/res/values/config.xml

<string-array name="config_statusBarIcons">
    <item><xliff:g id="id">@string/status_bar_alarm_clock</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_rotate</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_headset</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_data_saver</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_ime</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_sync_failing</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_sync_active</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_nfc</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_tty</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_speakerphone</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_cdma_eri</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_data_connection</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_phone_evdo_signal</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_phone_signal</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_secure</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_managed_profile</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_cast</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_vpn</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_bluetooth</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_camera</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_microphone</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_location</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_mute</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_volume</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_zen</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_ethernet</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_wifi</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_hotspot</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_mobile</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_airplane</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_battery</xliff:g></item>
    <item><xliff:g id="id">@string/status_bar_sensors_off</xliff:g></item>
</string-array>

Copy code This array not only defines the name of the icon, but also defines the order of the icon, for example, wifi must be displayed before the battery icon. Register Icon Manager This article discusses the control of status icons. According to the above, you must first register an icon manager with StatusBarIconControllerImpl. According to the previous article, you can guess that this is implemented in CollapsedStatusBarFragment // frameworks/base/packages /SystemUI/src/com/android/systemui/statusbar/phone/CollapsedStatusBarFragment.java

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    // ...
    
    // R.id.statusIcons代表状态图标区,例如bt, wifi
    // 创建一个状态图标区的图标管理器
    mDarkIconManager = new DarkIconManager(view.findViewById(R.id.statusIcons));

    // 向StatusBarIconControllerImpl注册图标管理器
    Dependency.get(StatusBarIconController.class).addIconGroup(mDarkIconManager);  
    
    // ...
}

Copy the code to set the status icon. Everything is ready, so now let's see how to set the status icon? In StatusBar#start(), it is set by some strategies // frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java

public void start() {
    // ...
    
    // 调用图标策略安装/更新所有图标
    mIconPolicy = new PhoneStatusBarPolicy(mContext, mIconController);
    mSignalPolicy = new StatusBarSignalPolicy(mContext, mIconController);
    
    // ...
}        

All the status icons of the copied code are set in these two policy classes. Let's see how the mobile phone signal icon is set. It is in StatusBarSignalPolicy public StatusBarSignalPolicy(Context context, StatusBarIconController iconController) { mContext = context;

    // 获取信号图标的名字
    mSlotMobile   = mContext.getString(com.android.internal.R.string.status_bar_mobile);

    // 这个就是StatusBarIconContrllerImpl对象
    mIconController = iconController;
    
    // 注册网络监听
    mNetworkController = Dependency.get(NetworkController.class);
    mNetworkController.addCallback(this);
}

Copy the code To set an icon, you must first obtain the name of the icon, which can be obtained from the array mentioned above. Then register the network listener so that you can get events about the mobile phone signal @Override
public void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType, int
qsType, boolean activityIn, boolean activityOut, int volteIcon,
String typeContentDescription, String description, boolean isWide,
int subId, boolean roaming) { // ...

    // 设置信号图标,第一个参数表示图标名字,第二个参数表示状态
    mIconController.setMobileIcons(mSlotMobile, MobileIconState.copyStates(mMobileStates));

    // ..
}

Copying the code, you can see that it is implemented through StatusBarIconController#setMobileIcons(), and the specific details will not be in-depth. The routine of setting the icon through the StatusBarIconController interface is the same. Obtain the icon name and listen to the event. Set the icon through the corresponding method of the StatusBarIconController.
Before setting the icon without SIM card, the company needs to set an icon when there is no SIM card. This interface is actually provided by the system. Just add the icon when there is no card in StatusBarSignalPolicy wzx 0801

public void setNoSims(boolean show, boolean simDetected) {  
String mobileSlot = 
mContext.getString(com.android.internal.R.string.status_bar_mobile);   
if (show && !simDetected) {       
mIconController.setIcon(mobileSlot, R.drawable.stat_sys_no_sims, "no_sim");        mIconController.setIconVisibility(mobileSlot, true); 
} else {     
mIconController.setIconVisibility(mobileSlot, false);  
}}

Guess you like

Origin blog.csdn.net/wzx311/article/details/126134413