分析Android 11.0Settings源碼之主界面加載
本篇主要記錄AndroidR Settings源碼主界面加載流程,方便后續(xù)工作調(diào)試其流程。
Settings代碼路徑:
packages/app/Settings/
Settings代碼獲?。?/h2>
Setting 源碼下載地址:https://github.com/aosp-mirror/platform_packages_apps_settings
git地址:https://github.com/aosp-mirror/platform_packages_apps_settings.git
主界面加載:
首先我們來看 Settings 模塊中的 AndroidManifest.xml 文件,找到默認(rèn)啟動(dòng)入口Activity信息:
<activity android:name=".homepage.SettingsHomepageActivity" android:label="@string/settings_label_launcher" android:theme="@style/Theme.Settings.Home" android:taskAffinity="com.android.settings.root" android:launchMode="singleTask" android:configChanges="keyboard|keyboardHidden"> <intent-filter android:priority="1"> <action android:name="android.settings.SETTINGS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED" android:value="true" /> </activity> //activity-alias可用來設(shè)置某個(gè)Activity的快捷入口,可以放在桌面上或者通過該別名被其他組件快速調(diào)起。 //android:targetActivity為目標(biāo)Activity. <!-- Alias for launcher activity only, as this belongs to each profile. --> <activity-alias android:name="Settings" android:label="@string/settings_label_launcher" android:taskAffinity="com.android.settings.root" android:launchMode="singleTask" android:targetActivity=".homepage.SettingsHomepageActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/> </activity-alias>
可以看到Settings的桌面圖標(biāo)啟動(dòng)的主界面是Settings.java,但其xml定義了targetActivity屬性,實(shí)質(zhì)應(yīng)是SettingsHomepageActivity.java,從onCreate()方法開始:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.settings_homepage_container); final View root = findViewById(R.id.settings_homepage_container); root.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); setHomepageContainerPaddingTop(); final Toolbar toolbar = findViewById(R.id.search_action_bar); FeatureFactory.getFactory(this).getSearchFeatureProvider() .initSearchToolbar(this /* activity */, toolbar, SettingsEnums.SETTINGS_HOMEPAGE); final ImageView avatarView = findViewById(R.id.account_avatar); getLifecycle().addObserver(new AvatarViewMixin(this, avatarView)); getLifecycle().addObserver(new HideNonSystemOverlayMixin(this)); if (!getSystemService(ActivityManager.class).isLowRamDevice()) { // Only allow contextual feature on high ram devices. showFragment(new ContextualCardsFragment(), R.id.contextual_cards_content); } showFragment(new TopLevelSettings(), R.id.main_content); ((FrameLayout) findViewById(R.id.main_content)) .getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); }
可以看到主界面的layout為settings_homepage_container.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/settings_homepage_container" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.core.widget.NestedScrollView android:id="@+id/main_content_scrollable_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> <LinearLayout android:id="@+id/homepage_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <FrameLayout android:id="@+id/contextual_cards_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/contextual_card_side_margin" android:layout_marginEnd="@dimen/contextual_card_side_margin"/> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:background="?android:attr/windowBackground"/> </LinearLayout> </androidx.core.widget.NestedScrollView> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:touchscreenBlocksFocus="false" android:keyboardNavigationCluster="false"> <include layout="@layout/search_bar"/> </com.google.android.material.appbar.AppBarLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
主界面布局中主要包含兩部分:一個(gè)頂部快捷搜索欄,一個(gè)Id為main_content的FrameLayout就是用來顯示主設(shè)置內(nèi)容的,即Settings的一級(jí)菜單項(xiàng)界面。
回到onCreate()方法:
showFragment(new TopLevelSettings(), R.id.main_content);
可以看到一級(jí)菜單啟動(dòng)的是TopLevelSettings,TopLevelSettings繼承于DashboardFragment.java:
public class TopLevelSettings extends DashboardFragment implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
TopLevelSettings的構(gòu)造方法:
public TopLevelSettings() { final Bundle args = new Bundle(); // Disable the search icon because this page uses a full search view in actionbar. args.putBoolean(NEED_SEARCH_ICON_IN_ACTION_BAR, false); setArguments(args); }
可以看到通過構(gòu)造方法傳遞了一個(gè)參數(shù),從注釋中可以看出,該參數(shù)的用意是由于主界面使用完整的搜索視圖所以在主界面的actionbar中隱藏了搜索圖標(biāo)。然后再根據(jù)framgments生命周期先來看onAttach()方法:
@Override public void onAttach(Context context) { super.onAttach(context); use(SupportPreferenceController.class).setActivity(getActivity()); }
調(diào)用父類DashboardFragment.java的onAttach()方法:
@Override public void onAttach(Context context) { super.onAttach(context); mSuppressInjectedTileKeys = Arrays.asList(context.getResources().getStringArray( R.array.config_suppress_injected_tile_keys)); mDashboardFeatureProvider = FeatureFactory.getFactory(context). getDashboardFeatureProvider(context); // Load preference controllers from code final List<AbstractPreferenceController> controllersFromCode = createPreferenceControllers(context); // Load preference controllers from xml definition final List<BasePreferenceController> controllersFromXml = PreferenceControllerListHelper .getPreferenceControllersFromXml(context, getPreferenceScreenResId()); // Filter xml-based controllers in case a similar controller is created from code already. final List<BasePreferenceController> uniqueControllerFromXml = PreferenceControllerListHelper.filterControllers( controllersFromXml, controllersFromCode); // Add unique controllers to list. if (controllersFromCode != null) { mControllers.addAll(controllersFromCode); } mControllers.addAll(uniqueControllerFromXml); // And wire up with lifecycle. final Lifecycle lifecycle = getSettingsLifecycle(); uniqueControllerFromXml.forEach(controller -> { if (controller instanceof LifecycleObserver) { lifecycle.addObserver((LifecycleObserver) controller); } }); // Set metrics category for BasePreferenceController. final int metricCategory = getMetricsCategory(); mControllers.forEach(controller -> { if (controller instanceof BasePreferenceController) { ((BasePreferenceController) controller).setMetricsCategory(metricCategory); } }); mPlaceholderPreferenceController = new DashboardTilePlaceholderPreferenceController(context); mControllers.add(mPlaceholderPreferenceController); for (AbstractPreferenceController controller : mControllers) { addPreferenceController(controller); } }
通過方法注釋可以得知此方法主要是完成preference controllers的加載。
DashboardFragment.java的onCreate()方法:
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // Set ComparisonCallback so we get better animation when list changes. getPreferenceManager().setPreferenceComparisonCallback( new PreferenceManager.SimplePreferenceComparisonCallback()); if (icicle != null) { // Upon rotation configuration change we need to update preference states before any // editing dialog is recreated (that would happen before onResume is called). updatePreferenceStates(); } }
設(shè)置ComparisonCallback,以便在列表更改時(shí)獲得更好的動(dòng)畫效果。
第一次進(jìn)入時(shí),icicle為null,根據(jù)log定位發(fā)現(xiàn),其后調(diào)用DashboardFragment.java的onCreatePreferences()方法:
@Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { checkUiBlocker(mControllers); refreshAllPreferences(getLogTag()); mControllers.stream() .map(controller -> (Preference) findPreference(controller.getPreferenceKey())) .filter(Objects::nonNull) .forEach(preference -> { // Give all controllers a chance to handle click. preference.getExtras().putInt(CATEGORY, getMetricsCategory()); }); }
調(diào)用refreshAllPreferences():
/** * Refresh all preference items, including both static prefs from xml, and dynamic items from * DashboardCategory. */ private void refreshAllPreferences(final String tag) { final PreferenceScreen screen = getPreferenceScreen(); // First remove old preferences. if (screen != null) { // Intentionally do not cache PreferenceScreen because it will be recreated later. screen.removeAll(); } // Add resource based tiles. displayResourceTiles(); refreshDashboardTiles(tag); final Activity activity = getActivity(); if (activity != null) { Log.d(tag, "All preferences added, reporting fully drawn"); activity.reportFullyDrawn(); } updatePreferenceVisibility(mPreferenceControllers); }
刷新所有preference items,包括來自xml的靜態(tài)preference和來自DashboardCategory的動(dòng)態(tài)preference,靜態(tài)xml定義的prefs(調(diào)用displayResourceTiles()方法),動(dòng)態(tài)DashboardCategory動(dòng)態(tài)加載(調(diào)用refreshDashboardTiles(TAG)方法,其中TAG為 “TopLevelSettings”)。
displayResourceTiles():此方法主要是從xml資源文件中加載顯示prefs:
/** * Displays resource based tiles. */ private void displayResourceTiles() { final int resId = getPreferenceScreenResId(); if (resId <= 0) { return; } addPreferencesFromResource(resId); final PreferenceScreen screen = getPreferenceScreen(); screen.setOnExpandButtonClickListener(this); displayResourceTilesToScreen(screen); } /** * Perform {@link AbstractPreferenceController#displayPreference(PreferenceScreen)} * on all {@link AbstractPreferenceController}s. */ protected void displayResourceTilesToScreen(PreferenceScreen screen) { mPreferenceControllers.values().stream().flatMap(Collection::stream).forEach( controller -> controller.displayPreference(screen)); }
靜態(tài)加載
首先調(diào)用getPreferenceScreenResId()方法獲取所要加載的xml的ID,然后調(diào)用子類TopLevelSettings.java的getPreferenceScreenResId()方法:
@Override protected int getPreferenceScreenResId() { return R.xml.top_level_settings; }
可以看到Settings主界面加載的xml文件是top_level_settings:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:settings="http://schemas.android.com/apk/res-auto" android:key="top_level_settings"> <Preference android:key="top_level_network" android:title="@string/network_dashboard_title" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_network" android:order="-120" android:fragment="com.android.settings.network.NetworkDashboardFragment" settings:controller="com.android.settings.network.TopLevelNetworkEntryPreferenceController"/> <Preference android:key="top_level_connected_devices" android:title="@string/connected_devices_dashboard_title" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_connected_device" android:order="-110" android:fragment="com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment" settings:controller="com.android.settings.connecteddevice.TopLevelConnectedDevicesPreferenceController"/> <Preference android:key="top_level_apps_and_notifs" android:title="@string/app_and_notification_dashboard_title" android:summary="@string/app_and_notification_dashboard_summary" android:icon="@drawable/ic_homepage_apps" android:order="-100" android:fragment="com.android.settings.applications.AppAndNotificationDashboardFragment"/> <Preference android:key="top_level_battery" android:title="@string/power_usage_summary_title" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_battery" android:fragment="com.android.settings.fuelgauge.PowerUsageSummary" android:order="-90" settings:controller="com.android.settings.fuelgauge.TopLevelBatteryPreferenceController"/> <Preference android:key="top_level_display" android:title="@string/display_settings" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_display" android:order="-80" android:fragment="com.android.settings.DisplaySettings" settings:controller="com.android.settings.display.TopLevelDisplayPreferenceController"/> <Preference android:key="top_level_sound" android:title="@string/sound_settings" android:summary="@string/sound_dashboard_summary" android:icon="@drawable/ic_homepage_sound" android:order="-70" android:fragment="com.android.settings.notification.SoundSettings"/> <Preference android:key="top_level_storage" android:title="@string/storage_settings" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_storage" android:order="-60" android:fragment="com.android.settings.deviceinfo.StorageSettings" settings:controller="com.android.settings.deviceinfo.TopLevelStoragePreferenceController"/> <Preference android:key="top_level_privacy" android:title="@string/privacy_dashboard_title" android:summary="@string/privacy_dashboard_summary" android:icon="@drawable/ic_homepage_privacy" android:order="-55" android:fragment="com.android.settings.privacy.PrivacyDashboardFragment"/> <Preference android:key="top_level_location" android:title="@string/location_settings_title" android:summary="@string/location_settings_loading_app_permission_stats" android:icon="@drawable/ic_homepage_location" android:order="-50" android:fragment="com.android.settings.location.LocationSettings" settings:controller="com.android.settings.location.TopLevelLocationPreferenceController"/> <Preference android:key="top_level_security" android:title="@string/security_settings_title" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_security" android:order="-40" android:fragment="com.android.settings.security.SecuritySettings" settings:controller="com.android.settings.security.TopLevelSecurityEntryPreferenceController"/> <Preference android:key="top_level_accounts" android:title="@string/account_dashboard_title" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_accounts" android:order="-30" android:fragment="com.android.settings.accounts.AccountDashboardFragment" settings:controller="com.android.settings.accounts.TopLevelAccountEntryPreferenceController"/> <Preference android:key="top_level_accessibility" android:title="@string/accessibility_settings" android:summary="@string/accessibility_settings_summary" android:icon="@drawable/ic_homepage_accessibility" android:order="-20" android:fragment="com.android.settings.accessibility.AccessibilitySettings" settings:controller="com.android.settings.accessibility.TopLevelAccessibilityPreferenceController"/> <Preference android:key="top_level_system" android:title="@string/header_category_system" android:summary="@string/system_dashboard_summary" android:icon="@drawable/ic_homepage_system_dashboard" android:order="10" android:fragment="com.android.settings.system.SystemDashboardFragment"/> <Preference android:key="top_level_about_device" android:title="@string/about_settings" android:summary="@string/summary_placeholder" android:icon="@drawable/ic_homepage_about" android:order="20" android:fragment="com.android.settings.deviceinfo.aboutphone.MyDeviceInfoFragment" settings:controller="com.android.settings.deviceinfo.aboutphone.TopLevelAboutDevicePreferenceController"/> <Preference android:key="top_level_support" android:summary="@string/support_summary" android:title="@string/page_tab_title_support" android:icon="@drawable/ic_homepage_support" android:order="100" settings:controller="com.android.settings.support.SupportPreferenceController"/> </PreferenceScreen>
可以看到主要配置的是一些Preference菜單項(xiàng)如網(wǎng)絡(luò)和互聯(lián)網(wǎng)、已連接的設(shè)備、應(yīng)用和通知、電池等等,Preference的配置含義:
- key:唯一性ID;
- title:標(biāo)題;
- summary:簡介;
- ico:圖標(biāo);
- order:加載顯示優(yōu)先級(jí),order為負(fù)時(shí),絕對(duì)值越高,界面顯示越靠前;order為正時(shí),值越高,顯示越靠后;
- fragment:點(diǎn)擊此preference所跳轉(zhuǎn)的fragment界面;
- controller:控制管理類。
動(dòng)態(tài)加載
refreshDashboardTiles
總結(jié):
- Settings的主Activity實(shí)質(zhì)實(shí)現(xiàn)是在SettingsHomepageActivity.java內(nèi);
- Settings的主界面設(shè)置item的顯示是在fragment上,fragment為TopLevelSettings.java,加載顯示的布局為top_level_settings.xml;
- Settings主界面設(shè)置項(xiàng)item的加載顯示主要分為兩部分,一部分是xml定義的靜態(tài)加載,xml為top_level_settings.xml;一部分是DashboardCategory來獲取動(dòng)態(tài)加載;
- 每個(gè)設(shè)置項(xiàng)item均為一個(gè)preference,通過xml定義加載時(shí),必須要有一個(gè)controller,可以是在xml中定義"settings:controller"屬性聲明,名稱必須與類的包名路徑相同;也可直接在相關(guān)fragment中實(shí)現(xiàn)createPreferenceControllers()方法去調(diào)用構(gòu)造相關(guān)controller。此二者存其一即可。
- xml中配置preference時(shí),必須定義”android:key“屬性;
以上就是分析Android 11.0Settings源碼之主界面加載的詳細(xì)內(nèi)容,更多關(guān)于Android 11.0Settings源碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例
這篇文章主要介紹了Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Android9?雙屏異顯實(shí)現(xiàn)方式思路
這篇文章主要為大家介紹了Android9?雙屏異顯實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06android開發(fā)實(shí)現(xiàn)文件讀寫
這篇文章主要為大家詳細(xì)介紹了android開發(fā)實(shí)現(xiàn)文件讀寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android開發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹
這篇文章主要介紹了Android開發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹,本文直接用圖片說明Genymotion的安裝和模擬效果,并提供官網(wǎng),需要的朋友可以參考下2015-04-04Android 中使用 ViewPager實(shí)現(xiàn)屏幕頁面切換和頁面輪播效果
ViewPager是谷歌官方給我們提供的一個(gè)兼容低版本安卓設(shè)備的軟件包,里面包囊了只有在安卓3.0以上可以使用的api。下面我們就展示下ViewPager可以實(shí)現(xiàn)的兩種簡單效果,感興趣的朋友一起看看吧2016-12-12Android之IphoneTreeView帶組指示器的ExpandableListView效果
在正在顯示的最上面的組的標(biāo)簽位置添加一個(gè)和組視圖完全一樣的視圖,作為組標(biāo)簽。這個(gè)標(biāo)簽的位置要隨著列表的滑動(dòng)不斷變化,以保持總是顯示在最上方,并且該消失的時(shí)候就消失2013-06-06