设置中的菜单主要分为两种添加方式:
``
1.静态加载
所谓自主添加,就是通过xml文件,自主通过添加布局到Fragment、Activity等加载到设置菜单中。一般每个自主添加菜单都拥有一个Controller。主要分为以下两种情况处理:
1.拥有Conroller
@Override
public boolean isAvailable() {
return false;
} // 通过继承类往上查找,发现实际上去除布局的方式仍然是调用了removePreference函数来删除控件
2.没有Controller
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle)
final PreferenceScreen screen = getPreferenceScreen();
Preference backupPref = screen.findPreference("backup_settings");
screen.removePreference(backupPref);
}
2.动态加载
所谓动态加载,就是说菜单不是通过xml文件自主定义添加的内容,而是通过Category来动态加载布置在各个应用下的菜单页面。
vendor
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/notification/SoundSettings.java
public class SoundSettings extends DashboardFragment implements OnActivityResultListener {
private static final String TAG = "SoundSettings";
......
}
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dashboard/DashboardFragment.java
@VisibleForTesting
void refreshDashboardTiles(final String TAG) {
final PreferenceScreen screen = getPreferenceScreen();
final DashboardCategory category =
mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
if (category == null) {
Log.d(TAG, "NO dashboard tiles for " + TAG);
return;
}
final List<Tile> tiles = category.getTiles();
if (tiles == null) {
Log.d(TAG, "tile list is empty, skipping category " + category.key);
return;
}
// Create a list to track which tiles are to be removed.
final List<String> remove = new ArrayList<>(mDashboardTilePrefKeys);
// There are dashboard tiles, so we need to install SummaryLoader.
if (mSummaryLoader != null) {
mSummaryLoader.release();
}
final Context context = getContext();
mSummaryLoader = new SummaryLoader(getActivity(), getCategoryKey());
mSummaryLoader.setSummaryConsumer(this);
// Install dashboard tiles.
final boolean forceRoundedIcons = shouldForceRoundedIcon();
for (Tile tile : tiles) {
final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
if (TextUtils.isEmpty(key)) {
Log.d(TAG, "tile does not contain a key, skipping " + tile);
continue;
}
if (!displayTile(tile)) {
continue;
}
if (mDashboardTilePrefKeys.contains(key)) {
// Have the key already, will rebind.
final Preference preference = screen.findPreference(key);
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), forceRoundedIcons,
getMetricsCategory(), preference, tile, key,
mPlaceholderPreferenceController.getOrder());
} else {
// Don‘t have this key, add it.
final Preference pref = new Preference(getPrefContext());
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), forceRoundedIcons,
getMetricsCategory(), pref, tile, key,
mPlaceholderPreferenceController.getOrder());
screen.addPreference(pref);
mDashboardTilePrefKeys.add(key);
}
remove.remove(key);
}
// Finally remove tiles that are gone.
for (String key : remove) {
mDashboardTilePrefKeys.remove(key);
final Preference preference = screen.findPreference(key);
if (preference != null) {
screen.removePreference(preference);
}
}
mSummaryLoader.setListening(true);
}
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dashboard/DashboardFragmentRegistry.java
PARENT_TO_CATEGORY_KEY_MAP.put(AppAndNotificationDashboardFragment.class.getName(),
CategoryKey.CATEGORY_APPS);
PARENT_TO_CATEGORY_KEY_MAP.put(PowerUsageSummary.class.getName(),
CategoryKey.CATEGORY_BATTERY);
PARENT_TO_CATEGORY_KEY_MAP.put(DisplaySettings.class.getName(),
CategoryKey.CATEGORY_DISPLAY);
PARENT_TO_CATEGORY_KEY_MAP.put(SoundSettings.class.getName(),
CategoryKey.CATEGORY_SOUND);
PARENT_TO_CATEGORY_KEY_MAP.put(StorageDashboardFragment.class.getName(),
CategoryKey.CATEGORY_STORAGE);
PARENT_TO_CATEGORY_KEY_MAP.put(SecuritySettings.class.getName(),
CategoryKey.CATEGORY_SECURITY);
PARENT_TO_CATEGORY_KEY_MAP.put(AccountDetailDashboardFragment.class.getName(),
CategoryKey.CATEGORY_ACCOUNT_DETAIL);
PARENT_TO_CATEGORY_KEY_MAP.put(AccountDashboardFragment.class.getName(),
CategoryKey.CATEGORY_ACCOUNT);
frameworks
frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryKey.java
public static final String CATEGORY_STORAGE = "com.android.settings.category.ia.storage";
public static final String CATEGORY_SECURITY = "com.android.settings.category.ia.security";
packages
packages/apps/Car/Settings/res/xml/sound_settings_fragment.xml
<com.android.car.settings.common.LogicalPreferenceGroup
android:key="@string/pk_sounds_extra_settings"
settings:controller="com.android.car.settings.common.ExtraSettingsPreferenceController">
<intent android:action="com.android.settings.action.EXTRA_SETTINGS">
<extra android:name="com.android.settings.category"
android:value="com.android.settings.category.ia.sound"/>
</intent>