概述
SystemServer中调用startsystemui函数启动SystemUIService,在SystemUIService的onCreate()函数中执行一下这个语句
((SystemUIApplication) getApplication()).startServicesIfNeeded();
SystemBar继承SystemUI,因此被启动了。
那接下来就进入SystemBar这个类中看onStart()函数
public void start(){
if(DEBUG)Log.d(TAG,"start");
//创建监听器对象
mServiceMonitor = new ServiceMonitor(TAG,DEBUG,mContext,Settings.Secure.BAR_SERVICE_COMPONENT,this);
//开始监听
mServiceMonitor.start();
}
ServiceMonitor.java
public void start() {
// listen for setting changes
ContentResolver cr = mContext.getContentResolver();
cr.registerContentObserver(Settings.Secure.getUriFor(mSettingKey),
false /*notifyForDescendents*/, mSettingObserver, UserHandle.USER_ALL);
// listen for package/component changes
//监听应用的安装卸载变化
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_addED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
mContext.registerReceiver(mBroadcastReceiver, filter);
//发送消息给主线程,启动Service
mHandler.sendEmptyMessage(MSG_START_SERVICE);
}
/*处理接收到的消息*/
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MSG_START_SERVICE:
startService();
break;
case MSG_CONTINUE_START_SERVICE:
continueStartService();
break;
case MSG_STOP_SERVICE:
stopService();
break;
case MSG_PACKAGE_INTENT:
packageIntent((Intent)msg.obj);
break;
case MSG_CHECK_BOUND:
checkBound();
break;
case MSG_SERVICE_DISCONNECTED:
serviceDisconnected((ComponentName)msg.obj);
break;
}
}
};
private void startService() {
mServiceName = getComponentNameFromSetting();
if (mDebug) Log.d(mTag, "startService mServiceName=" + mServiceName);
if (mServiceName == null) {
mBound = false;
mCallbacks.onNoService();
} else {
long delay = mCallbacks.onServiceStartAttempt();
mHandler.sendEmptyMessageDelayed(MSG_CONTINUE_START_SERVICE, delay);
}
}
先获取Service的componentName,如果为null则回调onNoService方法。如果不为null,则通过mHandler发送消息给主线程。
private void continueStartService() {
if (mDebug) Log.d(mTag, "continueStartService");
Intent intent = new Intent().setComponent(mServiceName);
try {
mServiceConnection = new SC();
mBound = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
if (mDebug) Log.d(mTag, "mBound: " + mBound);
} catch (Throwable t) {
Log.w(mTag, "Error binding to service: " + mServiceName, t);
}
if (!mBound) {
mCallbacks.onNoService();
}
}
获取的componentName不管是不是空,最终都会回调onNoService()。
public void onNoService() {
if (DEBUG) Log.d(TAG, "onNoService");
createStatusBarFromConfig(); // fallback to using an in-process implementation
}
private void createStatusBarFromConfig() {
if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
//取出className
final String clsName = mContext.getString(R.string.config_statusBarComponent);
if (clsName == null || clsName.length() == 0) {
throw andLog("No status bar component configured", null);
}
Class<?> cls = null;
try {
//动态加载class文件
cls = mContext.getClassLoader().loadClass(clsName);
} catch (Throwable t) {
throw andLog("Error loading status bar component: " + clsName, t);
}
try {
//创建BaseStatusBar实例
mStatusBar = (BaseStatusBar) cls.newInstance();
} catch (Throwable t) {
throw andLog("Error creating status bar component: " + clsName, t);
}
mStatusBar.mContext = mContext;
mStatusBar.mComponents = mComponents;
//启动BaseStatusBar
mStatusBar.start();
if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
}
frameworksbasepackagesSystemUIresvaluesconfig.xml
R.string.config_statusBarComponent的值是:
<string name="config_statusBarComponent" translatable="false">com.android.systemui.statusbar.phone.PhoneStatusBar</string>
PhoneStatusBar继承自BaseStatusBar
启动BaseStatusBar,那就接着来看BaseStatusBar.java,这个类继承自SystemUI
public void start(){
createAndAddWindows();
}
/**
* Create all windows necessary for the status bar (including navigation, overlay panels, etc)
* and add them to the window manager.
*/
protected abstract void createAndAddWindows();
是个抽象函数,PhoneStatusBar继承自BaseStatusBar,那我们来看下PhoneStatusBar
public void start() {
……
super.start(); // calls createAndAddWindows()
}
果然是PhoneStatusBar启动时调用了父类(BaseStatusBar)的createAndAddWindows(),这里PhoneStatusBar实现了父类的抽象方法。主要是添加状态栏、添加导航栏、更新图标
一、添加状态栏
@Override
public void createAndAddWindows() {
addStatusBarWindow();
}
private void addStatusBawWindow(){
makeStatusBarView();
mStatusBarWindowManager = new StatusBarWindowManager(mContext);
mStatusBarWindowManager.add(mStatusBarWindow, getStatusBarHeight());
}
这个函数做了两件事
(1)创建PhoneStatusBarView并初始化PhoneStatusBarView中的控件。
(2)创建StatusBarWindowManager对象,调用它的add()函数为PhoneStatusBarView创建窗口,包括窗口的大小、位置、是否透明等属性。
先来分析makeStatusBarView()函数。
protected PhoneStatusBarView makeStatusBarView() {
final Context context = mContext;
Resources res = context.getResources();
//获取屏幕参数
updateDisplaySize(); // populates mDisplayMetrics
//更新Panels资源数据,statusbar包含很多panel,在创建PhoneStatusBarView时需要更新panel数据
updateResources();
//引入StatusBarWindowView布局
mStatusBarWindow = (StatusBarWindowView) View.inflate(context,
R.layout.super_status_bar, null);
//statusbarwindow
mStatusBarWindow.setService(this);
//设置触摸事件监听器
mStatusBarWindow.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
checkUserAutohide(v, event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mExpandedVisible) {
animateCollapsePanels();
}
}
return mStatusBarWindow.onTouchEvent(event);
}
});
//初始化statusbarview
mStatusBarView = (PhoneStatusBarView) mStatusBarWindow.findViewById(R.id.status_bar);
mStatusBarView.setBar(this);
//初始化panelholder
PanelHolder holder = (PanelHolder) mStatusBarWindow.findViewById(R.id.panel_holder);
mStatusBarView.setPanelHolder(holder);
//初始化通知panel
mNotificationPanel = (NotificationPanelView) mStatusBarWindow.findViewById(
R.id.notification_panel);
mNotificationPanel.setStatusBar(this);
if (!mHighEnd) {
mStatusBarWindow.setBackground(null);
//设置notificationpanel背景
mNotificationPanel.setBackground(new FastColorDrawable(context.getColor(
R.color.notification_panel_solid_background)));
}
//通知样式
mHeadsUpManager = new HeadsUpManager(context, mStatusBarWindow);
mHeadsUpManager.setBar(this);
mHeadsUpManager.addListener(this);
mHeadsUpManager.addListener(mNotificationPanel);
mNotificationPanel.setHeadsUpManager(mHeadsUpManager);
mNotificationData.setHeadsUpManager(mHeadsUpManager);
if (MULTIUSER_DEBUG) {
mNotificationPanelDebugText = (TextView) mNotificationPanel.findViewById(
最后
以上就是大意心情为你收集整理的SystemUI源码分析三(StatusBar的加载流程)的全部内容,希望文章能够帮你解决SystemUI源码分析三(StatusBar的加载流程)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复