Add Fast Pair saved devices settings preference

Test: Built and flashed on device, verified UI manually.
Bug: 203579197
Change-Id: I8e9563083dd9ed6a8badc6e2536cf94fc635525b
This commit is contained in:
Xinyi Zhou
2022-01-27 01:33:02 -08:00
parent 3b8ad48c63
commit 3adae773b6
3 changed files with 66 additions and 0 deletions

View File

@@ -5846,6 +5846,8 @@
<string name="fast_pair_settings_summary">Nearby detection of Fast Pair bluetooth devices.</string> <string name="fast_pair_settings_summary">Nearby detection of Fast Pair bluetooth devices.</string>
<!-- Title for Fast Pair main switch preferences. [CHAR LIMIT=50] --> <!-- Title for Fast Pair main switch preferences. [CHAR LIMIT=50] -->
<string name="fast_pair_main_switch_title">Scan for nearby devices</string> <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 --> <!-- Printing settings -->
<skip /> <skip />

View File

@@ -25,4 +25,8 @@
android:key="fast_pair_scan_switch" android:key="fast_pair_scan_switch"
android:title="@string/fast_pair_main_switch_title" /> android:title="@string/fast_pair_main_switch_title" />
<Preference
android:key="saved_devices"
android:title="@string/fast_pair_saved_devices_title" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -17,8 +17,17 @@
package com.android.settings.nearby; package com.android.settings.nearby;
import android.app.settings.SettingsEnums; 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.os.Bundle;
import android.provider.Settings; 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.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
@@ -34,7 +43,10 @@ import java.util.Objects;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class FastPairSettingsFragment extends SettingsPreferenceFragment { 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 SCAN_SWITCH_KEY = "fast_pair_scan_switch";
private static final String SAVED_DEVICES_PREF_KEY = "saved_devices";
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@@ -47,6 +59,16 @@ public class FastPairSettingsFragment extends SettingsPreferenceFragment {
Settings.Secure.putInt(getContentResolver(), Settings.Secure.putInt(getContentResolver(),
Settings.Secure.FAST_PAIR_SCAN_ENABLED, isChecked ? 1 : 0)); Settings.Secure.FAST_PAIR_SCAN_ENABLED, isChecked ? 1 : 0));
mainSwitchPreference.setChecked(isFastPairScanAvailable()); 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 @Override
@@ -71,4 +93,42 @@ public class FastPairSettingsFragment extends SettingsPreferenceFragment {
return Settings.Secure.getInt(getContentResolver(), return Settings.Secure.getInt(getContentResolver(),
Settings.Secure.FAST_PAIR_SCAN_ENABLED, 1) != 0; 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;
}
} }