Android 10.0 状态栏系统图标显示分析

2024-08-27 02:44

本文主要是介绍Android 10.0 状态栏系统图标显示分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SystemUI中StatusBar的图标控制器实现类为StatusBarIconControllerImpl,其继承了StatusBarIconController的接口,用于跟踪所有图标的状态,并将对应的状态发送给注册的图标管理器(IconManagers)。当我们在StatusBar中获取到它的实例后,还会将它传给PhoneStatusBarPolicy和StatusBarSignalPolicy对象。PhoneStatusBarPolicy控制启动时装载哪些图标(蓝牙,定位等),而StatusBarSignalPolicy控制网络信号图标(移动网络,WiFi,以太网)的变化。
一起来看 StatuBar 的 start() 方法:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarSignalPolicy.java

@Override
public void start() {// 省略部分代码......// 创建整个SystemUI视图并添加到WindowManager中createAndAddWindows();//这个重点方法,创建相关的视图// 省略部分代码......// Lastly, call to the icon policy to install/update all the icons.mIconPolicy.init();mSignalPolicy = new StatusBarSignalPolicy(mContext, mIconController);// 省略部分代码......
}

这里的 mIconPolicy 就是 PhoneStatusBarPolicy对象,mSignalPolicy 就是 StatusBarSignalPolicy 对象。我们这里以 StatusBarSignalPolicy 为例去研究。
StatusBarSignalPolicy实现了NetworkControllerImpl.SignalCallback接口,SignalCallback接口定义在NetworkControllerImpl实现的接口NetworkController中。
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java

    @Injectpublic NetworkControllerImpl(Context context, @Background Looper bgLooper,DeviceProvisionedController deviceProvisionedController,BroadcastDispatcher broadcastDispatcher, ConnectivityManager connectivityManager,TelephonyManager telephonyManager, WifiManager wifiManager,NetworkScoreManager networkScoreManager) {this(context, connectivityManager,telephonyManager,wifiManager,networkScoreManager,SubscriptionManager.from(context), Config.readConfig(context), bgLooper,new CallbackHandler(),new AccessPointControllerImpl(context),new DataUsageController(context),new SubscriptionDefaults(),deviceProvisionedController,broadcastDispatcher);mReceiverHandler.post(mRegisterListeners);}private final Runnable mRegisterListeners = new Runnable() {@Overridepublic void run() {registerListeners();}};void registerListeners() {for (int i = 0; i < mMobileSignalControllers.size(); i++) {MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i);mobileSignalController.registerListener();}if (mSubscriptionListener == null) {mSubscriptionListener = new SubListener();}mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);mPhone.listen(mPhoneStateListener, LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);// broadcastsIntentFilter filter = new IntentFilter();// wifi相关// wifi信号强度广播filter.addAction(WifiManager.RSSI_CHANGED_ACTION);// wifi状态变化广播filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);// wifi连接状态改变filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);// 移动网络相关// SIM卡状态改变filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);// 数据语音订阅修改filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);filter.addAction(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION);// 连接状态相关// 网络连接状态发生变化filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);// 网络连接可能不好filter.addAction(ConnectivityManager.INET_CONDITION_ACTION);// 切换飞行模式时filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);mContext.registerReceiver(this, filter, null, mReceiverHandler);mListening = true;// 省略部分代码......// 4.更新移动网络控制器updateMobileControllers();}

在NetworkControllerImpl 的构造方法里,最终会调用到:registerListeners() 方法进行广播的注册。
广播处理:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java

@Override
public void onReceive(Context context, Intent intent) {if (CHATTY) {Log.d(TAG, "onReceive: intent=" + intent);}final String action = intent.getAction();switch (action) {case ConnectivityManager.CONNECTIVITY_ACTION:case ConnectivityManager.INET_CONDITION_ACTION:// 省略部分代码......break;case Intent.ACTION_AIRPLANE_MODE_CHANGED:// 省略部分代码......break;case TelephonyManager.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED:// We are using different subs now, we might be able to make calls.// 省略部分代码......break;case TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED:// Notify every MobileSignalController so they can know whether they are the// data sim or not.// 省略部分代码......break;case Intent.ACTION_SIM_STATE_CHANGED:// Avoid rebroadcast because SysUI is direct boot aware.// 省略部分代码......break;case Intent.ACTION_SERVICE_STATE:// 省略部分代码......break;case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED:// 省略部分代码......break;case ImsManager.ACTION_IMS_SERVICE_UP:case ImsManager.ACTION_IMS_SERVICE_DOWN:// 省略部分代码......break;case ACTION_HIGH_DEF_AUDIO_SUPPORT:// 省略部分代码......break;case ACTION_MODEM_CHANGE:// 省略部分代码......break;default:int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,SubscriptionManager.INVALID_SUBSCRIPTION_ID);if (SubscriptionManager.isValidSubscriptionId(subId)) {if (mMobileSignalControllers.indexOfKey(subId) >= 0) {mMobileSignalControllers.get(subId).handleBroadcast(intent);} else {// Can't find this subscription...  We must be out of date.updateMobileControllers();}} else {// wifi状态图标处理// No sub id, must be for the wifi.mWifiSignalController.handleBroadcast(intent);}break;}
}

这里以 wifi状态图标处理 为例;接下来看WifiSignalController#handleBroadcast():
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/WifiSignalController.java

public void handleBroadcast(Intent intent) {mWifiTracker.handleBroadcast(intent);mCurrentState.enabled = mWifiTracker.enabled;mCurrentState.connected = mWifiTracker.connected;mCurrentState.ssid = mWifiTracker.ssid;mCurrentState.rssi = mWifiTracker.rssi;mCurrentState.level = mWifiTracker.level;mCurrentState.statusLabel = mWifiTracker.statusLabel;notifyListenersIfNecessary();
}

在WifiSignalController#handleBroadcast()方法中,就两个实现,一个是获取 WiFi 的状态,一个是通知更新状态。
我们直接看通知SignalController# notifyListenersIfNecessary() :
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/SignalController.java

public void notifyListenersIfNecessary() {if (isDirty()) {saveLastState();    // 保持此时的状态notifyListeners();    // 通知监听器}
}
public final void notifyListeners() {notifyListeners(mCallbackHandler);
}
public abstract void notifyListeners(SignalCallback callback);

notifyListener()方法的实现在WifiSignalController类中:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/WifiSignalController.java

@Override
public void notifyListeners(SignalCallback callback) {// only show wifi in the cluster if connected or if wifi-onlyboolean visibleWhenEnabled = mContext.getResources().getBoolean(R.bool.config_showWifiIndicatorWhenEnabled);boolean wifiVisible = mCurrentState.enabled && ((mCurrentState.connected && mCurrentState.inetCondition == 1)|| !mHasMobileDataFeature || mWifiTracker.isDefaultNetwork|| visibleWhenEnabled);String wifiDesc = mCurrentState.connected ? mCurrentState.ssid : null;boolean ssidPresent = wifiVisible && mCurrentState.ssid != null;String contentDescription = getTextIfExists(getContentDescription()).toString();if (mCurrentState.inetCondition == 0) {contentDescription += ("," + mContext.getString(R.string.data_connection_no_internet));}IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), contentDescription);IconState qsIcon = new IconState(mCurrentState.connected,mWifiTracker.isCaptivePortal ? R.drawable.ic_qs_wifi_disconnected: getQsCurrentIconId(), contentDescription);// callback为 CallbackHandler对象callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon,ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut,wifiDesc, mCurrentState.isTransient, mCurrentState.statusLabel);
}

可以看到,这里回调了StatusBarSignalPolicy#setWifiIndicators() 方法:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarSignalPolicy.java

@Override
public void setWifiIndicators(boolean enabled, IconState statusIcon, IconState qsIcon,boolean activityIn, boolean activityOut, String description, boolean isTransient,String statusLabel) {boolean visible = statusIcon.visible && !mBlockWifi;boolean in = activityIn && mActivityEnabled && visible;boolean out = activityOut && mActivityEnabled && visible;WifiIconState newState = mWifiIconState.copy();newState.visible = visible;newState.resId = statusIcon.icon;newState.activityIn = in;newState.activityOut = out;newState.slot = mSlotWifi;newState.airplaneSpacerVisible = mIsAirplaneMode;newState.contentDescription = statusIcon.contentDescription;MobileIconState first = getFirstMobileState();newState.signalSpacerVisible = first != null && first.typeId != 0;updateWifiIconWithState(newState);mWifiIconState = newState;
}
private void updateWifiIconWithState(WifiIconState state) {if (state.visible && state.resId > 0) {mIconController.setSignalIcon(mSlotWifi, state);mIconController.setIconVisibility(mSlotWifi, true);} else {mIconController.setIconVisibility(mSlotWifi, false);}
}

通过StatusBarIconController接口设置图标的套路都是一样的:

  • 获取图标名字
  • 监听事件
  • 通过StatusBarIconControllerImpl相应的方法设置图标。
    接下来再看StatusBarIconControllerImpl#setSignalIcon():
    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconControllerImpl.java
/*** Signal icons need to be handled differently, because they can be* composite views*/
@Override
public void setSignalIcon(String slot, WifiIconState state) {int index = getSlotIndex(slot);if (state == null) {removeIcon(index, 0);return;}StatusBarIconHolder holder = getIcon(index, 0);if (holder == null) {holder = StatusBarIconHolder.fromWifiIconState(state);setIcon(index, holder);} else {holder.setWifiState(state);handleSet(index, holder);}
}

首先设置WiFi的状态信息,遍历mIconGroups分别执行StatusBarIconController接口中静态类IconManager中的onIconAdded()和onSetIconHolder()的回调。
IconManager用于将信息从StatusBarIconController转换为ViewGroup中的ImageViews(com.android.systemui.statusbar.AlphaOptimizedImageView)。
接着看IconManager中的onIconAdded()和onSetIconHolder()方法:这两个方法一个用于添加、一个用于更新。
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconController.java

public interface StatusBarIconController {....public static class DarkIconManager extends IconManager {....public DarkIconManager(LinearLayout linearLayout) {// 将布局传入IconManagersuper(linearLayout);mIconHPadding = mContext.getResources().getDimensionPixelSize(R.dimen.status_bar_icon_padding);mDarkIconDispatcher = Dependency.get(DarkIconDispatcher.class);}....@Overrideprotected void onIconAdded(int index, String slot, boolean blocked,StatusBarIconHolder holder) {// 调用到父类的addHolder方法StatusIconDisplayable view = addHolder(index, slot, blocked, holder);....}}public static class IconManager implements DemoMode {....protected final ViewGroup mGroup;protected final Context mContext;public IconManager(ViewGroup group) {mGroup = group;mContext = group.getContext();mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.status_bar_height);....}....protected StatusIconDisplayable addHolder(int index, String slot, boolean blocked,StatusBarIconHolder holder) {switch (holder.getType()) {case TYPE_ICON:return addIcon(index, slot, blocked, holder.getIcon());case TYPE_WIFI:return addSignalIcon(index, slot, holder.getWifiState());case TYPE_MOBILE:return addMobileIcon(index, slot, holder.getMobileState());}return null;}@VisibleForTestingprotected StatusBarWifiView addSignalIcon(int index, String slot, WifiIconState state) {// 创建一个StatusBarWifiView StatusBarWifiView view = onCreateStatusBarWifiView(slot);view.applyWifiState(state);// 将view 添加进ViewGroupmGroup.addView(view, index, onCreateLayoutParams());if (mIsInDemoMode) {mDemoStatusIcons.addDemoWifiView(state);}return view;}private StatusBarWifiView onCreateStatusBarWifiView(String slot) {StatusBarWifiView view = StatusBarWifiView.fromContext(mContext, slot);return view;}....public void onSetIconHolder(int viewIndex, StatusBarIconHolder holder) {switch (holder.getType()) {case TYPE_ICON:onSetIcon(viewIndex, holder.getIcon());return;case TYPE_WIFI:onSetSignalIcon(viewIndex, holder.getWifiState());return;case TYPE_MOBILE:onSetMobileIcon(viewIndex, holder.getMobileState());default:break;}}public void onSetSignalIcon(int viewIndex, WifiIconState state) {StatusBarWifiView wifiView = (StatusBarWifiView) mGroup.getChildAt(viewIndex);if (wifiView != null) {wifiView.applyWifiState(state);}if (mIsInDemoMode) {mDemoStatusIcons.updateWifiState(state);}}....}}

这里根据不同的StatusBarIconHolder类型,设置不同的网络Icon,上面列出了 Wifi 图标相关的方法。
SystemUI状态栏图标根据源码可大体分为三种:

  1. StatusBarIconView
  2. StatusBarWifiView
  3. StatusBarMobileView

这里主要以Wifi 相关图标(StatusBarWifiView)进行分析,添加Icon时首先会创建一个
StatusBarWifiView,然后调用StatusBarWifiView的applyWifiState更新其显示状态,最后将其加入到CollapsedStatusBarFragment中放置Icon的ViewGroup中,这样就完成了添加过程;
再来看看 CollapsedStatusBarFragment:
SystemUI/src/com/android/systemui/statusbar/phone/CollapsedStatusBarFragment.java

public class CollapsedStatusBarFragment extends Fragment implements CommandQueue.Callbacks {....private DarkIconManager mDarkIconManager;....@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.status_bar, container, false);}@Overridepublic void onViewCreated(View view, @Nullable Bundle savedInstanceState) {....// 这里可以看出status_bar布局中的statusIcons就是我们展示各种Icon的区域mDarkIconManager = new DarkIconManager(view.findViewById(R.id.statusIcons));mDarkIconManager.setShouldLog(true);Dependency.get(StatusBarIconController.class).addIconGroup(mDarkIconManager);....   }
}

补充:
notifyListenersIfNecessary()在其父类SignalController中定义,
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/SignalController.java

    private final CallbackHandler mCallbackHandler;public void notifyListenersIfNecessary() {if (isDirty()) {saveLastState();notifyListeners();}}// 在这里注意了,在这里的的参数是 CallbackHandler 的对象public final void notifyListeners() {notifyListeners(mCallbackHandler);}
// callback 则是 CallbackHandler 的对象。public abstract void notifyListeners(SignalCallback callback);

CallbackHandler维护了所有需要监听的SignalCallback接口对象,我们的StatusBarSignalPolicy就实现了该接口。
StatusBarSignalPolicy主要执行网络图标的刷新动作,其实现了NetworkControllerImpl.SignalCallback接口,然后注册到NetworkController,其具体实现类NetworkControllerImpl会根据WIFI,SIM等状态广播来进一步派发给具体的Controller,例如WifiSignalController,每个Controller只与CallbackHandler交互,然后CallbackHandler继续转交给维护的SignalCallback接口的具体实现类,例如StatusBarSignalPolicy

这篇关于Android 10.0 状态栏系统图标显示分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110407

相关文章

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种