Setting 的首页加载过程

时间:2021-09-30 00:00:44

<1> 在manifest.xml中可以知道settings类为入口类

setting.java:

public class Settings extends SettingsActivity {

/*
* Settings subclasses for launching independently.
*/

public static class BluetoothSettingsActivity extends SettingsActivity { /* empty */ }
........
public static class AppDrawOverlaySettingsActivity extends SettingsActivity { /* empty */ }
public static class AppWriteSettingsActivity extends SettingsActivity { /* empty */ }
}

可以知道settings中定义了很多类,而没有具体的实现,settings类只是一个管理类,定义设置中的Activity,具体实现类是在其父类SettingsActivity 中

<2>SettingsActivity 加载主页的过程

SettingsActivity :

@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);

final Intent intent = getIntent();
final ComponentName cn = intent.getComponent();
final String className = cn.getClassName();
mIsShowingDashboard = className.equals(Settings.class.getName());
setContentView(mIsShowingDashboard ?
R.layout.settings_main_dashboard : R.layout.settings_main_prefs);

mContent = (ViewGroup) findViewById(R.id.main_content);

if (savedState != null) {

} else {
if (!mIsShowingDashboard) {

} else {

switchToFragment(DashboardSummary.class.getName(), null, false, false, mInitialTitleResId, mInitialTitle, false);
}
}

}

可以看到settingActivity的onCreate()方法(只留下主界面加载相关的逻辑代码),setting第一次启动时savedState=null,className =”Settings”,这就可以知道setting加载的是R.layout.settings_main_dashboard布局,切换到DashboardSummary的fragment,下面来看
settings_main_dashboard布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>

布局就只有一个控件。所以界面元素实在DashboardSummary中加载的
DashboardSummary:

 private void rebuildUI(Context context) {

List<DashboardCategory> categories =
((SettingsActivity) context).getDashboardCategories(true);

final int count = categories.size();

for (int n = 0; n < count; n++) {
DashboardCategory category = categories.get(n);

View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
false);

TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
categoryLabel.setText(category.getTitle(res));
ViewGroup categoryContent =
(ViewGroup) categoryView.findViewById(R.id.category_content);

final int tilesCount = category.getTilesCount();
for (int i = 0; i < tilesCount; i++) {
DashboardTile tile = category.getTile(i);

DashboardTileView tileView = new DashboardTileView(context);
updateTileView(context, res, tile, tileView.getImageView(),
tileView.getTitleTextView(), tileView.getStatusTextView());

tileView.setTile(tile);

categoryContent.addView(tileView);
}

// Add the category
mDashboard.addView(categoryView);
}

}

通过分析代码可以知道,是通过rebuildUI方法加载界面的,而主界面的数据又是通过SettingsActivity的getDashboardCategories的方法获取的

<3>SettingsActivity 通过getDashboardCategories获取主页相关数据

public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
if (forceRefresh || mCategories.size() == 0) {
buildDashboardCategories(mCategories);
}
return mCategories;
}
private void buildDashboardCategories(List<DashboardCategory> categories) {
categories.clear();
loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
updateTilesList(categories);
}
public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
Context context) {
解析dashboard_categories.xml的数据

由上可知主页的相关数据都是保存在dashboard_categories.xml中,通过解析xml中的数据生成 List<DashboardCategory> 数据列表再在DashboardSummary的rebuildUI方法中将数据填充到界面里,感觉就像直接加载dashboard_categories布局一样

<4>dashboard_categories.xml的神秘面纱

<dashboard-categories
xmlns:android="http://schemas.android.com/apk/res/android">


<!-- WIRELESS and NETWORKS -->
<dashboard-category
android:id="@+id/wireless_section"
android:key="@string/category_key_wireless"
android:title="@string/header_category_wireless_networks" >


<!-- Wifi -->
<dashboard-tile
android:id="@+id/wifi_settings"
android:title="@string/wifi_settings_title"
android:fragment="com.android.settings.wifi.WifiSettings"
android:icon="@drawable/ic_settings_wireless"
/>

<!-- Other wireless and network controls -->
<dashboard-tile
android:id="@+id/wireless_settings"
android:title="@string/radio_controls_title"
android:fragment="com.android.settings.WirelessSettings"
android:icon="@drawable/ic_settings_more"
/>


</dashboard-category>

<!-- DEVICE -->
<dashboard-category
android:id="@+id/device_section"
android:key="@string/category_key_device"
android:title="@string/header_category_device" >


<!-- Manage users -->
<!-- <dashboard-tile
android:id="@+id/user_settings"
android:title="@string/user_settings_title"
android:fragment="com.android.settings.users.UserSettings"
android:icon="@drawable/ic_settings_multiuser"
/> -->


<!-- Manage NFC payment apps -->
<dashboard-tile
android:id="@+id/nfc_payment_settings"
android:title="@string/nfc_payment_settings_title"
android:fragment="com.android.settings.nfc.PaymentSettings"
android:icon="@drawable/ic_settings_nfc_payment"
/>

</dashboard-category>
</dashboard-categories>

可以看到dashboard_categories由一些dashboard-category,dashboard-tile节点组成一个用于分类,一个是具体的项

<4>总结

setting加载主页是通过DashboardSummary的rebuildui方法将dashboard_categories的数据添加到view中,看起来像是直接加载dashboard_categories