diff --git a/fastpair/internal/fast_pair_seeker_impl.cc b/fastpair/internal/fast_pair_seeker_impl.cc index 41c1e9ad..d0496bd4 100644 --- a/fastpair/internal/fast_pair_seeker_impl.cc +++ b/fastpair/internal/fast_pair_seeker_impl.cc @@ -51,7 +51,7 @@ FastPairSeekerImpl::FastPairSeekerImpl(ServiceCallbacks callbacks, pairer_broker_->AddObserver(this); mediums_.GetBluetoothClassic().AddObserver(this); retro_detector_ = std::make_unique( - mediums_, devices, executor); + mediums_, devices, account_manager_, executor); retro_detector_->AddObserver(this); } diff --git a/fastpair/internal/fast_pair_seeker_impl_test.cc b/fastpair/internal/fast_pair_seeker_impl_test.cc index d77c6cf4..9957265c 100644 --- a/fastpair/internal/fast_pair_seeker_impl_test.cc +++ b/fastpair/internal/fast_pair_seeker_impl_test.cc @@ -22,8 +22,6 @@ #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" -#include "absl/time/clock.h" -#include "absl/time/time.h" #include "fastpair/common/fast_pair_prefs.h" #include "fastpair/fast_pair_events.h" #include "fastpair/fast_pair_seeker.h" @@ -76,6 +74,8 @@ class FastPairSeekerImplTest : public testing::Test { NEARBY_LOG_SET_SEVERITY(VERBOSE); repository_ = FakeFastPairRepository::Create( kModelId, absl::HexStringToBytes(kBobPublicKey)); + repository_->SetResultOfIsDeviceSavedToAccount( + absl::NotFoundError("not found")); account_manager_ = std::make_unique( preferences_manager_.get(), prefs::kNearbyFastPairUsersName, authentication_manager_.get(), task_runner_.get()); diff --git a/fastpair/retroactive/BUILD b/fastpair/retroactive/BUILD index ec1f012c..33884bf9 100644 --- a/fastpair/retroactive/BUILD +++ b/fastpair/retroactive/BUILD @@ -36,7 +36,9 @@ cc_library( "//fastpair/internal/mediums", "//fastpair/message_stream", "//fastpair/pairing", + "//fastpair/repository", "//fastpair/repository:device_repository", + "//internal/account", "//internal/base", "//internal/platform:comm", "//internal/platform:types", diff --git a/fastpair/retroactive/retroactive_pairing_detector_impl.cc b/fastpair/retroactive/retroactive_pairing_detector_impl.cc index 4aae90c5..7a104b94 100644 --- a/fastpair/retroactive/retroactive_pairing_detector_impl.cc +++ b/fastpair/retroactive/retroactive_pairing_detector_impl.cc @@ -17,18 +17,23 @@ #include #include #include +#include #include #include "fastpair/internal/mediums/mediums.h" -#include "fastpair/pairing/pairer_broker.h" +#include "fastpair/repository/fast_pair_repository.h" +#include "internal/account/account_manager.h" namespace nearby { namespace fastpair { RetroactivePairingDetectorImpl::RetroactivePairingDetectorImpl( Mediums& mediums, FastPairDeviceRepository* repository, - SingleThreadExecutor* executor) - : mediums_(mediums), repository_(repository), executor_(executor) { + AccountManager* account_manager, SingleThreadExecutor* executor) + : mediums_(mediums), + repository_(repository), + account_manager_(account_manager), + executor_(executor) { mediums_.GetBluetoothClassic().AddObserver(this); mediums_.GetBluetoothClassic().StartDiscovery(); } @@ -75,15 +80,33 @@ void RetroactivePairingDetectorImpl::DevicePairedChanged( // first check if it has already been saved to the user's account. If it has // already been saved, we don't want to prompt the user to save a device // again. - // TODO(b/285047010): check if device has already been saved to the user's - // account + if (!account_manager_->GetCurrentAccount().has_value()) { + NEARBY_LOGS(INFO) << __func__ << ": Ignoring because no logged in user."; + return; + } + FastPairRepository::Get()->IsDeviceSavedToAccount( + device.GetMacAddress(), + [this, mac_address = device.GetMacAddress()](absl::Status status) { + if (status.ok()) { + NEARBY_LOGS(VERBOSE) << __func__ + << ": Ignoring because device is already saved " + "to the current account."; + return; + } + NotifyRetroactiveDeviceFound(mac_address); + }); +} + +void RetroactivePairingDetectorImpl::NotifyRetroactiveDeviceFound( + absl::string_view mac_address) { + NEARBY_LOGS(VERBOSE) << __func__ << ": mac_address = " << mac_address; auto fast_pair_device = std::make_unique(Protocol::kFastPairRetroactivePairing); - fast_pair_device->SetPublicAddress(device.GetMacAddress()); + fast_pair_device->SetPublicAddress(mac_address); repository_->AddDevice(std::move(fast_pair_device)); executor_->Execute("notify-retro-candidate", - [this, address = device.GetMacAddress()]() { + [this, address = std::string(mac_address)]() { std::optional fast_pair_device = repository_->FindDevice(address); if (!fast_pair_device) return; diff --git a/fastpair/retroactive/retroactive_pairing_detector_impl.h b/fastpair/retroactive/retroactive_pairing_detector_impl.h index a24d0617..7c876f86 100644 --- a/fastpair/retroactive/retroactive_pairing_detector_impl.h +++ b/fastpair/retroactive/retroactive_pairing_detector_impl.h @@ -14,13 +14,12 @@ #ifndef THIRD_PARTY_NEARBY_FASTPAIR_RETROACTIVE_RETROACTIVE_PAIRING_DETECTOR_IMPL_H_ #define THIRD_PARTY_NEARBY_FASTPAIR_RETROACTIVE_RETROACTIVE_PAIRING_DETECTOR_IMPL_H_ -#include -#include "absl/container/flat_hash_set.h" +#include "absl/strings/string_view.h" #include "fastpair/internal/mediums/mediums.h" -#include "fastpair/pairing/pairer_broker.h" #include "fastpair/repository/fast_pair_device_repository.h" #include "fastpair/retroactive/retroactive_pairing_detector.h" +#include "internal/account/account_manager.h" #include "internal/base/observer_list.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/single_thread_executor.h" @@ -34,6 +33,7 @@ class RetroactivePairingDetectorImpl public: RetroactivePairingDetectorImpl(Mediums& mediums, FastPairDeviceRepository* repository, + AccountManager* account_manager, SingleThreadExecutor* executor); RetroactivePairingDetectorImpl(const RetroactivePairingDetectorImpl&) = delete; @@ -50,9 +50,11 @@ class RetroactivePairingDetectorImpl bool new_paired_status) override; private: + void NotifyRetroactiveDeviceFound(absl::string_view mac_address); Mediums& mediums_; ObserverList observers_; FastPairDeviceRepository* repository_; + AccountManager* account_manager_; SingleThreadExecutor* executor_; };