一.MtkSettings启动逻辑
1.进入MtkSettings的AndroidManifest.xml,找启动Activity,activity-alias是标签别名,真正的启动是targetActivity中配置的Settings类
2.Settings.java,Settings->SettingsActivity,Settings继承自SettingsActivity,里面所有的类都只是定义,没有实现,eg:
public class Settings extends SettingsActivity {
public static class AssistGestureSettingsActivity extends SettingsActivity { }
public static class BluetoothSettingsActivity extends SettingsActivity { }
public static class CreateShortcutActivity extends SettingsActivity { }
public static class FaceSettingsActivity extends SettingsActivity { }
public static class FingerprintSettingsActivity extends SettingsActivity { }
public static class TetherSettingsActivity extends SettingsActivity {
3. SettingsActivity.java,查看SettingsActivity中的onCreate方法,第一次启动进入launchSettingFragment方法,这里初始化相关的Fragment,具体其他的细节可参考源码
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
Log.d(LOG_TAG, "Starting onCreate");
long startTime = System.currentTimeMillis();
final FeatureFactory factory = FeatureFactory.getFactory(this);
mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
// Should happen before any call to getIntent()
getMetaData();
final Intent intent = getIntent();
if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
}
// Getting Intent properties can only be done after the super.onCreate(...)
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
// This is a "Sub Settings" when:
// - this is a real SubSettings
// - or :settings:show_fragment_as_subsetting is passed to the Intent
final boolean isSubSettings = this instanceof SubSettings ||
intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
// If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
// insets.
// If this is in setup flow, don't apply theme. Because light theme needs to be applied
// in SettingsBaseActivity#onCreate().
if (isSubSettings && !WizardManagerHelper.isAnySetupWizard(getIntent())) {
setTheme(R.style.Theme_SubSettings);
}
setContentView(R.layout.settings_main_prefs);
v188HeaderTextEmporia = (V188HeaderTextEmporia) findViewById(R.id.settings_header_text);
getSupportFragmentManager().addOnBackStackChangedListener(this);
if (savedState != null) {
// We are restarting from a previous saved state; used that to initialize, instead
// of starting fresh.
setTitleFromIntent(intent);
ArrayList categories =
savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
if (categories != null) {
mCategories.clear();
mCategories.addAll(categories);
setTitleFromBackStack();
}
} else {
launchSettingFragment(initialFragmentName, intent);
}
@VisibleForTesting
void launchSettingFragment(String initialFragmentName, Intent intent) {
if (initialFragmentName != null) {
setTitleFromIntent(intent);
Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
switchToFragment(initialFragmentName, initialArguments, true,
mInitialTitleResId, mInitialTitle);
} else {
// Show search icon as up affordance if we are displaying the main Dashboard
mInitialTitleResId = R.string.dashboard_title;
switchToFragment(TopLevelSettings.class.getName(), null , false,
mInitialTitleResId, mInitialTitle);
}
}
private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
int titleResId, CharSequence title) {
Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
if (validate && !isValidFragment(fragmentName)) {
throw new IllegalArgumentException("Invalid fragment for this activity: "
+ fragmentName);
}
Fragment f = Utils.getTargetFragment(this, fragmentName, args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, f);
if (titleResId > 0) {
transaction.setBreadCrumbTitle(titleResId);
} else if (title != null) {
transaction.setBreadCrumbTitle(title);
}
transaction.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
return f;
}
二.MtkSettings一二级菜单添加的方法
1.MtkSettings所在的目录位置
/alps/vendor/mediatek/proprietary/packages/apps/MtkSettings
2.MtkSettings常用菜单一二级效果图
3.MtkSettings一级菜单top_level_settings.xml布局介绍
1‘.布局文件
2‘.Preference元素的通用XML属性
android:key //唯一标识,SharedPreferences也将通过此Key值进行数据保存,也可以通过key值获取保存的信息 android:defaultValue //默认值, eg: CheckPreference的默认值可为"true",默认为选中状态 android:enabled //表示该Preference是否可用状态 android:title //每个Preference在PreferenceScreen布局上显示的标题——大标题 android:summary //每个Preference在PreferenceScreen布局上显示的概述——小标题(可以没有) android:persistent //表示Preference元素所对应的值是否写入sharedPreferen文件中,如果是true,则表示写入;否则,则表示不写入该Preference元素的值。 android:layout //preferenceactivity的偏好布局,用于填充view android:dependency //表示一个Preference(用A表示)的可用状态依赖另外一个Preference(用B表示,相当于是父类),B可用,则A可用;B不可用,则A不可用。 android:disableDependentsState //与android:dependency相反。B可用,则A不可用;B不可用,则A可用 android:icon //偏好图标,对于偏好选择图标必须是一个引用到另一个资源的形式或以packagetypename主题属性 android:order //表示偏好顺序,值越大,优先级越高,菜单越靠前 android:widgetLayout //可以做小部部件部分的布局 android:selectable //偏好是否是可选的 android:fragment //使用时,可以碎片化PreferenceActivity
3’.Preference配置注意项
配置文件Preference时,order优先级控制一级菜单显示,优先级值越大,优先级越高,菜单越靠前,如果需要增加一级菜单,则需要在top_level_settings.xml中添加对应的Preference项即可
4'.MtkSettings一级菜单Preference点击跳转事件,添加key, "mobile_network_settings"
1’'.给 Preference 指定 Intent 属性,这里可以分为 action 和 targetPackage+targetClass 两种方式,eg:
2''.DashboardFragment.java中onPreferenceTreeClick拦截点击事件处理,包名:(com.android.settings.dashboard),继承关系为:
DashboardFragment->SettingsPreferenceFragment->InstrumentedPreferenceFragment->PreferenceFragmentCompat,eg:拦截key为mobile_network_settings的事件
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if(preference.getKey().equals("mobile_network_settings")){
Toast.makeText(mContext,R.string.mobile_insert_sim_card,Toast.LENGTH_LONG).show();
return true;
}
final Collection controllers =
mPreferenceControllers.values();
for (List controllerList : controllers) {
for (AbstractPreferenceController controller : controllerList) {
if (controller.handlePreferenceTreeClick(preference)) {
// log here since calling super.onPreferenceTreeClick will be skipped
writePreferenceClickMetric(preference);
return true;
}
}
}
return super.onPreferenceTreeClick(preference);
}
4.MtkSettings二级菜单添加的方法,这里以display为例
1'.找到对应的二级菜单xml,display_settings.xml
2'.添加或者隐藏相应的preference菜单



