一、概述
本文主要对android M源码中的设置进行分析,此篇文章主要对第一次进入设置的过程进行分析。
二、入口
AndroidManifest.xml中的<intent-filter> 定义apk默认入口如果有很多就默认第一个,设置入口Settings.java
<activity android:name="Settings"
android:taskAffinity="com.android.settings"
...
<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" />
Settings.java中只定义了一些静态内部类,接着看Settings的继承 SettingsActivity,SettingsActivity继承自Activity那么我们就可以从SettingsActivity的onCreate函数对这个SettingsActivity进行分析了。
三、SettingsActivity
onCreate()
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
...
getMetaData();
final Intent intent = getIntent();
//...
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
//...
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);
getFragmentManager().addOnBackStackChangedListener(this);
//...
if (savedState != null) {
//...
} else {
if (!mIsShowingDashboard) {
...
Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
switchToFragment(initialFragmentName, initialArguments, true, false,
mInitialTitleResId, mInitialTitle, false);
} else {
...
switchToFragment(DashboardSummary.class.getName(), null, false, false,
mInitialTitleResId, mInitialTitle, false);
}
}
//...
}
Class判断
第一个函数getMetaData;获得AndroidManifest中activyt的meta-data,给mFragmentClass给个值在getIntent中getStartingFragmentClass,可以看出此函数作用是打开的界面是属于Settings的话就直接返回这个Class
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null) return;
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
} catch (NameNotFoundException nnfe) {
// No recovery
Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
}
}
布局
接下来布局设置,mIsShowingDashboard = true,所以是settings_main_dashboard,打开可以看到里边的布局里边只有一个FrameLayout,所以可以看出主界面就是一个FrameLayout的容器
状态
addOnBackStackChangedListener为back stack加上监听第一次进入savedState为空,savedState作用是对界面状态的保存在SettingsAcitvity中的onSaveInstanceState对状态保存一些操作,这里就不详述了。
载入
接着走到了switchToFragment(DashboardSummary.class.getName()....switchToFragment的作用就是加载一个fragment。
private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
if (validate && !isValidFragment(fragmentName)) {
throw new IllegalArgumentException("Invalid fragment for this activity: "
+ fragmentName);
}
Fragment f = Fragment.instantiate(this, fragmentName, args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, f);
if (withTransition) {
TransitionManager.beginDelayedTransition(mContent);
}
if (addToBackStack) {
transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
}
if (titleResId > 0) {
transaction.setBreadCrumbTitle(titleResId);
} else if (title != null) {
transaction.setBreadCrumbTitle(title);
}
transaction.commitAllowingStateLoss();
getFragmentManager().executePendingTransactions();
return f;
}
在SettingsActivity中,onCreate基本就走完了,onResume只是一些广播的注册数据的监听,接下来就看看这个载入DashboardSummary.java
四、DashboardSummary
此类主要管理主界面的菜单解析,加载整个创建过程代码很少,onCreateView中看到,布局是一个ViewGroup,在onResume中调用sendRebuildUI方法开始加载界面
private void sendRebuildUI() {
if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
mHandler.sendEmptyMessage(MSG_REBUILD_UI);
}
}
接着在mHandler中调用rebuildUI
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REBUILD_UI: {
final Context context = getActivity();
rebuildUI(context);
} break;
}
}
};
然后看最后的加载函数rebuildUI
private void rebuildUI(Context context) {
...
List<DashboardCategory> categories =
((SettingsActivity) context).getDashboardCategories(true);
final int count = categories.size();
for (int n = 0; n < count; n++) {
...
mDashboard.addView(categoryView);
}
...
}
里边最关键的是调用了SettingsActivity的getDashboardCategories方法而在getDashboardCategories主要调用buildDashboardCategories
private void buildDashboardCategories(List<DashboardCategory> categories) {
categories.clear();
loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
updateTilesList(categories);
}
最后,我们打开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"
/>
<!-- Bluetooth -->
<dashboard-tile
android:id="@+id/bluetooth_settings"
android:title="@string/bluetooth_settings_title"
android:fragment="com.android.settings.bluetooth.BluetoothSettings"
android:icon="@drawable/ic_settings_bluetooth"
/>
五、总结
设置进入后的初始化流程主要是通过SettingsActvity来加载DashboardSummary
最后再DashboardSummary进行初始化解析加载出界面。