需求:android10隐藏设置的投射页面
修改前:
修改后:
可以看到隐藏了投射项。
packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragment.java
/**
* Refresh preference items backed by DashboardCategory.
*/
@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());
//通过log把Preference的key打印出来
Log.i(TAG, "placeholder key is:" + key);
Log.e(TAG, "pref.getKey() is:" + pref.getKey());
//向PreferenceScreen添加布局,默认是添加的
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);
}
打出来的log是
2021-01-09 18:52:22.634 816-816/com.android.settings I/AdvancedConnectedDeviceFrag: placeholder key is:dashboard_tile_pref_com.android.settings.Settings$WifiDisplaySettingsActivity
2021-01-09 18:52:22.635 816-816/com.android.settings E/AdvancedConnectedDeviceFrag: pref.getKey() is:dashboard_tile_pref_com.android.settings.Settings$WifiDisplaySettingsActivity
除了投射,settings还有其他项也是这样加载的,比如系统的备份/用户/语言和输入法,还有通知和开发者选项
2021-01-09 20:54:21.481 816-816/com.android.settings I/SystemDashboardFrag: placeholder key is:dashboard_tile_pref_com.android.settings.Settings$UserSettingsActivity
2021-01-09 20:54:21.481 816-816/com.android.settings E/SystemDashboardFrag: pref.getKey() is:dashboard_tile_pref_com.android.settings.Settings$UserSettingsActivity
2021-01-09 20:59:17.439 816-816/com.android.settings I/SystemDashboardFrag: placeholder key is:dashboard_tile_pref_com.android.settings.Settings$LanguageAndInputSettingsActivity
2021-01-09 20:59:17.439 816-816/com.android.settings E/SystemDashboardFrag: pref.getKey() is:dashboard_tile_pref_com.android.settings.Settings$LanguageAndInputSettingsActivity
2021-01-09 20:59:17.433 816-816/com.android.settings I/SystemDashboardFrag: placeholder key is:dashboard_tile_pref_com.android.settings.backup.UserBackupSettingsActivity
2021-01-09 20:59:17.433 816-816/com.android.settings E/SystemDashboardFrag: pref.getKey() is:dashboard_tile_pref_com.android.settings.backup.UserBackupSettingsActivity
默认是addPreference的,把不需要的页面removePreference就好了。
if (key.contains("dashboard_tile_pref_com.android.settings.Settings$WifiDisplaySettingsActivity")){
screen.removePreference(pref);
}else{
screen.addPreference(pref);
mDashboardTilePrefKeys.add(key);
}
结束。
Android10 Frameworks专栏其他文章:
android10 修改媒体音的默认值
android10 隐藏SystemUI锁屏下的多用户图标
android10 禁止下拉状态栏
android10 动态添加和隐藏一级菜单