Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3adae773 authored by Xinyi Zhou's avatar Xinyi Zhou
Browse files

Add Fast Pair saved devices settings preference

Test: Built and flashed on device, verified UI manually.
Bug: 203579197
Change-Id: I8e9563083dd9ed6a8badc6e2536cf94fc635525b
parent 3b8ad48c
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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 />
+4 −0
Original line number Diff line number Diff line
@@ -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>
+60 −0
Original line number Diff line number Diff line
@@ -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 @@ import java.util.Objects;
@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 @@ public class FastPairSettingsFragment extends SettingsPreferenceFragment {
                        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 @@ public class FastPairSettingsFragment extends SettingsPreferenceFragment {
        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;
    }
}