大多數情況下,新增偏好設定摘要相對簡單明瞭
只要在對應的偏好設定中加入 android:summary
屬性即可
傳回適當的字串資源但如果字幕必須能夠動態更新
那麼您可能需要自訂偏好設定控制器
靜態字幕
如何為偏好設定新增靜態子標題:
- 在偏好設定中加入
android:summary
屬性。舉例來說,如要新增 摘要到 L0 顯示設定偏好設定中,請在 偏好設定屬性:android:summary="@string/display_settings_summary"
以完整的偏好設定程式碼範例為例:
<Preference android:fragment="com.android.car.settings.display.DisplaySettingsFragment" android:icon="@drawable/ic_settings_display" android:key="@string/pk_display_settings_entry" android:title="@string/display_settings" android:summary="@string/display_settings_summary" settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController"/>
動態字幕
使用 android:summary
屬性指定的字幕是靜態的,因此無法顯示
並根據特定條件進行更新如要使用動態字幕,請調整偏好設定
控制器。以下範例將 L0 位置偏好設定改為
副標題,指出位置資訊是否開啟。如已啟用,請說明應用程式數量
目前可存取位置資訊。
- 定義新字串:
<!-- Summary for Location settings when location is off [CHAR LIMIT=NONE] --> <string name="location_settings_summary_location_off">Off</string> <!-- Summary for Location settings when location is on, explaining how many apps have location permission [CHAR LIMIT=NONE]--> <plurals name="location_settings_summary_location_on"> <item quantity="one">On - <xliff:g id="count">%1$d</xliff:g> app has access to location</item> <item quantity="other">On - <xliff:g id="count">%1$d</xliff:g> apps have access to location</item> </plurals> <!-- Location settings, loading the number of apps which have location permission [CHAR LIMIT=30] --> <string name="location_settings_loading_app_permission_stats">Loading\u2026</string>
- 將新的 PreferenceController、
LocationEntryPreferenceController
設為 動態設定並變更位置偏好設定摘要文字:public class LocationEntryPreferenceController extends PreferenceController<Preference> { private static final Logger LOG = new Logger(LocationEntryPreferenceController.class); private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = new IntentFilter(LocationManager.MODE_CHANGED_ACTION); private final Context mContext; private final LocationManager mLocationManager; /** Total number of apps that have location permissions. */ private int mNumTotal = -1; private int mNumTotalLoading = 0; private AtomicInteger mLoadingInProgress = new AtomicInteger(0); private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { refreshUi(); } }; public LocationEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions) { super(context, preferenceKey, fragmentController, uxRestrictions); mContext = context; mLocationManager = (LocationManager) getContext().getSystemService( Service.LOCATION_SERVICE); } @Override protected Class<Preference> getPreferenceType() { return Preference.class; } @Override protected void onStartInternal() { getContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED); } @Override protected void onStopInternal() { getContext().unregisterReceiver(mReceiver); } @Override protected void updateState(Preference preference) { super.updateState(preference); updateSummary(preference); if (!mLocationManager.isLocationEnabled() || mLoadingInProgress.get() != 0) { return; } mNumTotalLoading = 0; // Retrieve a list of users inside the current user profile group. List<UserHandle> users = mContext.getSystemService( UserManager.class).getUserProfiles(); mLoadingInProgress.set(users.size()); for (UserHandle user : users) { Context userContext = createPackageContextAsUser(mContext, user.getIdentifier()); if (userContext == null) { if (mLoadingInProgress.decrementAndGet() == 0) { setLocationAppCount(preference, mNumTotalLoading); } continue; } PermissionControllerManager permController = userContext.getSystemService(PermissionControllerManager.class); permController.countPermissionApps( Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION), PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED, (numApps) -> { mNumTotalLoading += numApps; if (mLoadingInProgress.decrementAndGet() == 0) { setLocationAppCount(preference, mNumTotalLoading); } }, null); } } @VisibleForTesting void setLocationAppCount(Preference preference, int numApps) { mNumTotal = numApps; updateSummary(preference); } private void updateSummary(Preference preference) { String summary = ""; if (mLocationManager.isLocationEnabled()) { if (mNumTotal == -1) { summary = mContext.getString(R.string.location_settings_loading_app_permission_stats); } else { summary = mContext.getResources().getQuantityString( R.plurals.location_settings_summary_location_on, mNumTotal, mNumTotal); } } else { summary = mContext.getString(R.string.location_settings_summary_location_off); } preference.setSummary(summary); } private Context createPackageContextAsUser(Context context, int userId) { try { return context.createPackageContextAsUser( context.getPackageName(), 0 /* flags */, UserHandle.of(userId)); } catch (PackageManager.NameNotFoundException e) { LOG.e("Failed to create user context", e); } return null; } }
這個範例控制器:
- 如果位置資訊已停用,摘要文字會設為
location_settings_summary_location_off
字串。 - 如果已啟用定位功能,系統會新增具備位置存取權的應用程式數量。雖然
載入中,
location_settings_loading_app_permission_stats
字串 。載入資料時,控制器會將摘要設為 「location_settings_summary_location_on
」字串,內含有應用程式數量的應用程式 是否設有存取權 - 偏好設定控制器啟動後,控制器會註冊接收端 在位置狀態變更時重新整理偏好設定狀態。
- 如果位置資訊已停用,摘要文字會設為
- 修改片段 XML 檔案,將新控制器附加至相關偏好設定:
<Preference android:fragment="com.android.car.settings.location.LocationSettingsFragment" android:icon="@drawable/ic_settings_location" android:key="@string/pk_location_settings_entry" android:title="@string/location_settings_title" settings:controller="com.android.car.settings.location.LocationEntryPreferenceController"/>