SystemUI简介
作为系统应用,SystemUI 包含的内容较多,比如状态栏、通知栏、下拉菜单、导航栏、锁屏、最近任务、低电提示等系统页面。在源码目录中位于: framework/base/packages 目录下, 可见 SystemUI 和 framework 是关联的, SystemUI 依赖了很多内部 API , 系统资源, SystemUI 编译是要依赖系统源码的。
本文介绍的版本为Android11。
编译和调试
由于编译环境较为苛刻,所以我直接使用了系统工程师在Linux服务器上搭建的源码编译环境,应用单独编译只需要四五分钟,还是比较块。编译之后的目录位于/system_ext/priv-app/SystemUI/SystemUI.apk,这个目录和安卓系统里放置系统应用的目录一模一样。编译完从服务器上把这个应用文件拉下来,adb获取系统root权限和系统文件读写权限后直接push到对应的目录即可。
快捷面板布局框架
所有和快捷面板相关的布局都是在StatusBar这个类里面加载的,路径src\com\android\systemui\statusbar\phone\StatusBar.java
onStart()方法是入口,里面有个createAndAddWindows(result)方法用来加载布局,继续追踪createAndAddWindows(result)->makeStatusBarView()->inflateStatusBarWindow():
private void inflateStatusBarWindow() {
mNotificationShadeWindowView = mSuperStatusBarViewFactory.getNotificationShadeWindowView();
StatusBarComponent statusBarComponent = mStatusBarComponentBuilder.get()
.statusBarWindowView(mNotificationShadeWindowView).build();
mNotificationShadeWindowViewController = statusBarComponent
.getNotificationShadeWindowViewController();
mNotificationShadeWindowController.setNotificationShadeView(mNotificationShadeWindowView);
mNotificationShadeWindowViewController.setupExpandedStatusBar();
mStatusBarWindowController = statusBarComponent.getStatusBarWindowController();
mPhoneStatusBarWindow = mSuperStatusBarViewFactory.getStatusBarWindowView();
mNotificationPanelViewController = statusBarComponent.getNotificationPanelViewController();
}
这里加载了两个外层布局,一个是NotificationShadeWindowView,另一个是PhoneStatusBarWindow。
NotificationShadeWindowView
NotificationShadeWindowView是根布局,mNotificationShadeWindowView = mSuperStatusBarViewFactory.getNotificationShadeWindowView(),对应的layout是super_notification_shade.xml
如下:
<!-- This is the notification shade window. -->
<com.android.systemui.statusbar.phone.NotificationShadeWindowView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sysui="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.android.systemui.statusbar.BackDropView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
sysui:ignoreRightInset="true">
<ImageView android:id="@+id/backdrop_back"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="match_parent" />
<ImageView android:id="@+id/backdrop_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="invisible" />
</com.android.systemui.statusbar.BackDropView>
<com.android.systemui.statusbar.ScrimView
android:id="@+id/scrim_behind"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no"
sysui:ignoreRightInset="true"
/>
<include layout="@layout/status_bar_expanded"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
<include layout="@layout/brightness_mirror" />
<com.android.systemui.statusbar.ScrimView
android:id="@+id/scrim_in_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no"
sysui:ignoreRightInset="true"
/>
<LinearLayout
android:id="@+id/lock_icon_container"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_height"
android:layout_gravity="top|center_horizontal">
<com.android.systemui.statusbar.phone.LockIcon
android:id="@+id/lock_icon"
android:layout_width="@dimen/keyguard_lock_width"
android:layout_height="@dimen/keyguard_lock_height"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/keyguard_lock_padding"
android:contentDescription="@string/accessibility_unlock_button"
android:src="@*android:drawable/ic_lock"
android:scaleType="center" />
<com.android.keyguard.KeyguardMessageArea
android:id="@+id/keyguard_message_area"
style="@style/Keyguard.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyguard_lock_padding"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true" />
</LinearLayout>
</com.android.systemui.statusbar.phone.NotificationShadeWindowView>
主要的layout包括:
1.BackDropView
BackDropView backdrop = mNotificationShadeWindowView.findViewById(R.id.backdrop);
mMediaManager.setup(backdrop, backdrop.findViewById(R.id.backdrop_front),
backdrop.findViewById(R.id.backdrop_back), mScrimController, mLockscreenWallpaper);
由NotificationMediaManager来管理,忘问而知这个是处理音视频相关的通知。
2.ScrimView 状态栏下拉后的背景,半透明灰色。这个view有两个,一个是背景,一个是前景。
3.include进去的brightness_mirror.xml,亮度进度条。
4.include进去的status_bar_expanded,快捷面板。
5.lock_icon_container 锁屏布局。
NotificationPanelView
因此主要页面就集中在status_bar_expanded.xml,它的代码如下:
<com.android.systemui.statusbar.phone.NotificationPanelView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:systemui="http://schemas.android.com/apk/res-auto"
android:id="@+id/notification_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<FrameLayout
android:id="@+id/big_clock_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include
layout="@layout/keyguard_status_view"
android:visibility="gone" />
<com.android.systemui.statusbar.phone.NotificationsQuickSettingsContainer
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:id="@+id/notification_container_parent"
android:clipToPadding="false"
android:clipChildren="false">
<include layout="@layout/dock_info_overlay" />
<FrameLayout
android:id="@+id/qs_frame"
android:layout="@layout/qs_panel"
android:layout_width="@dimen/qs_panel_width"
android:layout_height="match_parent"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:clipToPadding="false"
android:clipChildren="false"
systemui:viewType="com.android.systemui.plugins.qs.QS" />
<com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout
android:id="@+id/notification_stack_scroller"
android:layout_marginTop="@dimen/notification_panel_margin_top"
android:layout_width="@dimen/notification_panel_width"
android:layout_height="match_parent"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:layout_marginBottom="@dimen/close_handle_underlap" />
<include layout="@layout/ambient_indication"
android:id="@+id/ambient_indication_container" />
<include layout="@layout/photo_preview_overlay" />
<ViewStub
android:id="@+id/keyguard_user_switcher"
android:layout="@layout/keyguard_user_switcher"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<include
layout="@layout/keyguard_status_bar"
android:visibility="invisible" />
<Button
android:id="@+id/report_rejected_touch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_header_height_keyguard"
android:text="@string/report_rejected_touch"
android:visibility="gone" />
</com.android.systemui.statusbar.phone.NotificationsQuickSettingsContainer>
<include
layout="@layout/keyguard_bottom_area"
android:visibility="gone" />
<com.android.systemui.statusbar.AlphaOptimizedView
android:id="@+id/qs_navbar_scrim"
android:layout_height="96dp"
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:visibility="invisible"
android:background="@drawable/qs_navbar_scrim" />
<include layout="@layout/status_bar_expanded_plugin_frame"/>
</com.android.systemui.statusbar.phone.NotificationPanelView>
此页面就是快捷面板里的内容了,包含的主要布局如下:
1.qs_panel 快捷面板 对应的view就是QSFragment。
2.NotificationStackScrollLayout 通知栏
3.NotificationStackScrollLayout
4.其他和锁屏相关的就不介绍了
因此最终要修改快捷面板改动QSFragment就行了。
QSContainerImpl
QSContainerImpl即快捷面板页面,对应的布局为qs_panel.xml,详细的加载在QSFragment里面。
qs_panel.xml
<com.android.systemui.qs.QSContainerImpl
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/quick_settings_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:clipChildren="false" >
<!-- Main QS background -->
<View
android:id="@+id/quick_settings_background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:elevation="4dp"
android:background="@drawable/qs_background_primary" />
<!-- Black part behind the status bar -->
<View
android:id="@+id/quick_settings_status_bar_background"
android:layout_width="match_parent"
android:layout_height="@*android:dimen/quick_qs_offset_height"
android:clipToPadding="false"
android:clipChildren="false"
android:background="#ff000000" />
<!-- Gradient view behind QS -->
<View
android:id="@+id/quick_settings_gradient_view"
android:layout_width="match_parent"
android:layout_height="126dp"
android:layout_marginTop="@*android:dimen/quick_qs_offset_height"
android:clipToPadding="false"
android:clipChildren="false"
android:background="@drawable/qs_bg_gradient" />
//快捷面板展开之后的布局
<com.android.systemui.qs.NonInterceptingScrollView
android:id="@+id/expanded_qs_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:importantForAccessibility="no"
android:layout_weight="1">
<com.android.systemui.qs.QSPanel
android:id="@+id/quick_settings_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:focusable="true"
android:accessibilityTraversalBefore="@android:id/edit">
<include layout="@layout/qs_footer_impl" />
<include layout="@layout/qs_media_divider"
android:id="@+id/divider"/>
</com.android.systemui.qs.QSPanel>
</com.android.systemui.qs.NonInterceptingScrollView>
//快界面版展开之前的布局
<include layout="@layout/quick_status_bar_expanded_header" />
//点击WiFi或者蓝牙弹出的列表详情
<include android:id="@+id/qs_detail" layout="@layout/qs_detail" />
<include android:id="@+id/qs_customize" layout="@layout/qs_customize_panel"
android:visibility="gone" />
<FrameLayout
android:id="@+id/qs_drag_handle_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:elevation="4dp"
android:paddingBottom="5dp">
<View
android:layout_width="46dp"
android:layout_height="3dp"
android:background="@drawable/qs_footer_drag_handle" />
</FrameLayout>
</com.android.systemui.qs.QSContainerImpl>
PhoneStatusBarWindow
PhoneStatusBarView主要用来显示系统状态、通知等,主要包括 notification icons 和 status bar icons。mPhoneStatusBarWindow = mSuperStatusBarViewFactory.getStatusBarWindowView(),对应的layout是super_status_bar.xml,里面包含了一个frameLayout,最终指向了CollapsedStatusBarFragment,对应的布局是status_bar.xml。
下面是PhoneStatusBarView的view 树形图: