คำบรรยายการตั้งค่า

โดยส่วนใหญ่ การเพิ่มสรุปการตั้งค่านั้นค่อนข้างตรงไปตรงมา เนื่องจากเกี่ยวข้องกับการเพิ่มแอตทริบิวต์ android:summary ให้กับการตั้งค่าตามลำดับด้วยทรัพยากรสตริงที่เหมาะสม อย่างไรก็ตาม หากคำบรรยายควรอัปเดตแบบไดนามิก อาจจำเป็นต้องใช้ตัวควบคุมการตั้งค่าแบบกำหนดเอง

คำบรรยายแบบคงที่

หากต้องการเพิ่มคำบรรยายแบบคงที่ให้กับการตั้งค่า:

  1. เพิ่มแอตทริบิวต์ 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 เพื่อให้มีคำบรรยายที่ระบุว่าตำแหน่งเปิดหรือปิด และหากเปิดอยู่ จะระบุจำนวนแอปที่เข้าถึงตำแหน่งในปัจจุบัน

  1. กำหนดสตริงใหม่:
    <!-- 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>
    
  2. สร้าง 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 พร้อมจำนวนแอปที่ระบุการเข้าถึง
    • เมื่อตัวควบคุมการกำหนดลักษณะเริ่มทำงาน ตัวควบคุมจะลงทะเบียนตัวรับและรีเฟรชสถานะการกำหนดลักษณะเมื่อสถานะตำแหน่งเปลี่ยนแปลง
  3. แก้ไขไฟล์ 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"/>