Add Fast Pair saved devices settings preference

Test: Built and flashed on device, verified UI manually.
Bug: 203579197
Change-Id: I8e9563083dd9ed6a8badc6e2536cf94fc635525b
diff --git a/res/values/strings.xml b/res/values/strings.xml
index fc33a8b..019a492 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5846,6 +5846,8 @@
     <string name="fast_pair_settings_summary">Nearby detection of Fast Pair bluetooth devices.</string>
     <!-- Title for Fast Pair main switch preferences. [CHAR LIMIT=50] -->
     <string name="fast_pair_main_switch_title">Scan for nearby devices</string>
+    <!-- Title for Fast Pair saved devices preferences. [CHAR LIMIT=50] -->
+    <string name="fast_pair_saved_devices_title">Saved devices</string>
     <!-- Printing settings -->
     <skip />
 
diff --git a/res/xml/fast_pair_settings.xml b/res/xml/fast_pair_settings.xml
index 3fd306f..95662fc 100644
--- a/res/xml/fast_pair_settings.xml
+++ b/res/xml/fast_pair_settings.xml
@@ -25,4 +25,8 @@
         android:key="fast_pair_scan_switch"
         android:title="@string/fast_pair_main_switch_title" />
 
+    <Preference
+        android:key="saved_devices"
+        android:title="@string/fast_pair_saved_devices_title" />
+
 </PreferenceScreen>
diff --git a/src/com/android/settings/nearby/FastPairSettingsFragment.java b/src/com/android/settings/nearby/FastPairSettingsFragment.java
index 7657bc3..f605cf1 100644
--- a/src/com/android/settings/nearby/FastPairSettingsFragment.java
+++ b/src/com/android/settings/nearby/FastPairSettingsFragment.java
@@ -17,8 +17,17 @@
 package com.android.settings.nearby;
 
 import android.app.settings.SettingsEnums;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.provider.Settings;
+import android.text.TextUtils;
+import android.util.Log;
+
+import androidx.annotation.Nullable;
+import androidx.preference.Preference;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
@@ -34,7 +43,10 @@
 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
 public class FastPairSettingsFragment extends SettingsPreferenceFragment {
 
+    private static final String TAG = "FastPairSettingsFrag";
+
     private static final String SCAN_SWITCH_KEY = "fast_pair_scan_switch";
+    private static final String SAVED_DEVICES_PREF_KEY = "saved_devices";
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -47,6 +59,16 @@
                         Settings.Secure.putInt(getContentResolver(),
                                 Settings.Secure.FAST_PAIR_SCAN_ENABLED, isChecked ? 1 : 0));
         mainSwitchPreference.setChecked(isFastPairScanAvailable());
+
+        Preference savedDevicePref = Objects.requireNonNull(
+                findPreference(SAVED_DEVICES_PREF_KEY));
+        savedDevicePref.setOnPreferenceClickListener(preference -> {
+            Intent savedDevicesIntent = getSavedDevicesIntent();
+            if (savedDevicesIntent != null && getActivity() != null) {
+                getActivity().startActivity(savedDevicesIntent);
+            }
+            return true;
+        });
     }
 
     @Override
@@ -71,4 +93,42 @@
         return Settings.Secure.getInt(getContentResolver(),
                 Settings.Secure.FAST_PAIR_SCAN_ENABLED, 1) != 0;
     }
+
+    @Nullable
+    private ComponentName getSavedDevicesComponent() {
+        String savedDevicesComponent = Settings.Secure.getString(
+                getContentResolver(),
+                Settings.Secure.NEARBY_FAST_PAIR_SETTINGS_DEVICES_COMPONENT);
+        if (TextUtils.isEmpty(savedDevicesComponent)) {
+            savedDevicesComponent = getString(
+                com.android.internal.R.string.config_defaultNearbyFastPairSettingsDevicesComponent);
+        }
+
+        if (TextUtils.isEmpty(savedDevicesComponent)) {
+            return null;
+        }
+
+        return ComponentName.unflattenFromString(savedDevicesComponent);
+    }
+
+    @Nullable
+    private Intent getSavedDevicesIntent() {
+        ComponentName componentName = getSavedDevicesComponent();
+        if (componentName == null) {
+            return null;
+        }
+
+        PackageManager pm = getPackageManager();
+        Intent intent = getIntent();
+        intent.setAction(Intent.ACTION_VIEW);
+        intent.setComponent(componentName);
+
+        final ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.GET_META_DATA);
+        if (resolveInfo == null || resolveInfo.activityInfo == null) {
+            Log.e(TAG, "Device-specified fast pair component (" + componentName
+                    + ") not available");
+            return null;
+        }
+        return intent;
+    }
 }