From b89eeb1ac12f3ded506f7b43d897ab6a97b661c8 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Mon, 26 Jun 2023 12:36:24 -0700 Subject: [PATCH] Pass screen locked event to plugins PiperOrigin-RevId: 543512467 --- fastpair/fast_pair_events.h | 5 ++- fastpair/fast_pair_service.cc | 17 +++++++--- fastpair/fast_pair_service.h | 2 +- fastpair/fast_pair_service_test.cc | 32 +++++++++++++++++++ fastpair/internal/BUILD | 1 + fastpair/internal/fast_pair_seeker_impl.cc | 12 +++---- fastpair/internal/fast_pair_seeker_impl.h | 10 +++--- .../internal/fast_pair_seeker_impl_test.cc | 19 ++++++----- fastpair/plugins/BUILD | 1 + fastpair/plugins/fake_fast_pair_plugin.cc | 6 ++++ fastpair/plugins/fake_fast_pair_plugin.h | 3 ++ 11 files changed, 80 insertions(+), 28 deletions(-) diff --git a/fastpair/fast_pair_events.h b/fastpair/fast_pair_events.h index 65249fab..aa8c93a4 100644 --- a/fastpair/fast_pair_events.h +++ b/fastpair/fast_pair_events.h @@ -15,6 +15,7 @@ #ifndef THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_EVENTS_H_ #define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_EVENTS_H_ +#include namespace nearby { namespace fastpair { @@ -27,7 +28,9 @@ struct SubsequentDiscoveryEvent {}; struct PairEvent {}; -struct ScreenEvent {}; +struct ScreenEvent { + std::optional is_locked; +}; struct BatteryEvent {}; diff --git a/fastpair/fast_pair_service.cc b/fastpair/fast_pair_service.cc index c67cb9e4..3482c78e 100644 --- a/fastpair/fast_pair_service.cc +++ b/fastpair/fast_pair_service.cc @@ -64,9 +64,7 @@ FastPairService::FastPairService(std::unique_ptr repository) OnPairEvent(device, std::move(event)); }, .on_screen_event = - [this](const FastPairDevice& device, ScreenEvent event) { - OnScreenEvent(device, std::move(event)); - }, + [this](ScreenEvent event) { OnScreenEvent(std::move(event)); }, .on_battery_event = [this](const FastPairDevice& device, BatteryEvent event) { OnBatteryEvent(device, std::move(event)); @@ -125,12 +123,21 @@ void FastPairService::OnInitialDiscoveryEvent(const FastPairDevice& device, } }); } + void FastPairService::OnSubsequentDiscoveryEvent( const FastPairDevice& device, SubsequentDiscoveryEvent event) {} void FastPairService::OnPairEvent(const FastPairDevice& device, PairEvent event) {} -void FastPairService::OnScreenEvent(const FastPairDevice& device, - ScreenEvent event) {} +void FastPairService::OnScreenEvent(ScreenEvent event) { + executor_.Execute("on-screen-event", [this, event = std::move(event)]() { + NEARBY_LOGS(INFO) << "OnScreenEvent "; + for (auto& entry : plugin_states_) { + for (auto& plugin : entry.second.plugins) { + plugin.second->OnScreenEvent(event); + } + } + }); +} void FastPairService::OnBatteryEvent(const FastPairDevice& device, BatteryEvent event) {} void FastPairService::OnRingEvent(const FastPairDevice& device, diff --git a/fastpair/fast_pair_service.h b/fastpair/fast_pair_service.h index 4b771a73..bcd53a98 100644 --- a/fastpair/fast_pair_service.h +++ b/fastpair/fast_pair_service.h @@ -70,7 +70,7 @@ class FastPairService { void OnSubsequentDiscoveryEvent(const FastPairDevice& device, SubsequentDiscoveryEvent event); void OnPairEvent(const FastPairDevice& device, PairEvent event); - void OnScreenEvent(const FastPairDevice& device, ScreenEvent event); + void OnScreenEvent(ScreenEvent event); void OnBatteryEvent(const FastPairDevice& device, BatteryEvent event); void OnRingEvent(const FastPairDevice& device, RingEvent event); void OnDeviceDestroyed(const FastPairDevice& device); diff --git a/fastpair/fast_pair_service_test.cc b/fastpair/fast_pair_service_test.cc index 6011ea18..e480b2a0 100644 --- a/fastpair/fast_pair_service_test.cc +++ b/fastpair/fast_pair_service_test.cc @@ -99,6 +99,38 @@ TEST(FastPairService, InitialDiscoveryEvent) { MediumEnvironment::Instance().Stop(); } +TEST(FastPairService, ScreenEvent) { + MediumEnvironment::Instance().Start(); + constexpr absl::string_view kPluginName = "my plugin"; + auto repository = FakeFastPairRepository::Create(kModelId, kPublicAntiSpoof); + FakeProvider provider; + FastPairService service(std::move(repository)); + CountDownLatch latch(1); + auto plugin_provider = std::make_unique(); + plugin_provider->on_initial_discovery_event_ = + [&](const FastPairDevice* device, const InitialDiscoveryEvent& event) { + NEARBY_LOGS(INFO) << "Initial discovery: " << device; + EXPECT_EQ(device->GetModelId(), kModelId); + latch.CountDown(); + }; + plugin_provider->on_screen_event_ = [&](ScreenEvent event) { + EXPECT_TRUE(event.is_locked); + latch.CountDown(); + }; + EXPECT_OK( + service.RegisterPluginProvider(kPluginName, std::move(plugin_provider))); + FastPairSeekerExt* seeker = + static_cast(service.GetSeeker()); + EXPECT_OK(seeker->StartFastPairScan()); + provider.StartDiscoverableAdvertisement(kModelId); + seeker->SetIsScreenLocked(true); + latch.Await(); + + EXPECT_OK(seeker->StopFastPairScan()); + EXPECT_OK(service.UnregisterPluginProvider(kPluginName)); + MediumEnvironment::Instance().Stop(); +} + } // namespace } // namespace fastpair } // namespace nearby diff --git a/fastpair/internal/BUILD b/fastpair/internal/BUILD index a6b1e965..d02168ef 100644 --- a/fastpair/internal/BUILD +++ b/fastpair/internal/BUILD @@ -32,6 +32,7 @@ cc_test( ], deps = [ ":internal", + "//fastpair:fast_pair_events", "//fastpair/message_stream:fake_gatt_callbacks", "//fastpair/message_stream:fake_provider", "//fastpair/server_access:test_support", diff --git a/fastpair/internal/fast_pair_seeker_impl.cc b/fastpair/internal/fast_pair_seeker_impl.cc index 9957e1c2..dd903fe1 100644 --- a/fastpair/internal/fast_pair_seeker_impl.cc +++ b/fastpair/internal/fast_pair_seeker_impl.cc @@ -161,14 +161,10 @@ void FastPairSeekerImpl::FinishPairing(absl::Status result) { } void FastPairSeekerImpl::SetIsScreenLocked(bool locked) { - executor_->Execute( - "on_lock_state_changed", - [this, locked]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) { - NEARBY_LOGS(INFO) << __func__ << ": Screen lock state changed. ( " - << std::boolalpha << locked << ")"; - is_screen_locked_ = locked; - InvalidateScanningState(); - }); + NEARBY_LOGS(INFO) << __func__ << ": Screen lock state changed. ( " + << std::boolalpha << locked << ")"; + is_screen_locked_ = locked; + callbacks_.on_screen_event(ScreenEvent{.is_locked = locked}); } void FastPairSeekerImpl::InvalidateScanningState() { diff --git a/fastpair/internal/fast_pair_seeker_impl.h b/fastpair/internal/fast_pair_seeker_impl.h index 5cbfda19..c029bac8 100644 --- a/fastpair/internal/fast_pair_seeker_impl.h +++ b/fastpair/internal/fast_pair_seeker_impl.h @@ -36,6 +36,9 @@ class FastPairSeekerExt : public FastPairSeeker { public: virtual absl::Status StartFastPairScan() = 0; virtual absl::Status StopFastPairScan() = 0; + + // Handle the state changes of screen lock. + virtual void SetIsScreenLocked(bool is_locked) = 0; }; class FastPairSeekerImpl : public FastPairSeekerExt, @@ -49,8 +52,7 @@ class FastPairSeekerImpl : public FastPairSeekerExt, absl::AnyInvocable on_subsequent_discovery; absl::AnyInvocable on_pair_event; - absl::AnyInvocable - on_screen_event; + absl::AnyInvocable on_screen_event; absl::AnyInvocable on_battery_event; absl::AnyInvocable on_ring_event; @@ -77,6 +79,7 @@ class FastPairSeekerImpl : public FastPairSeekerExt, // From FastPairSeekerExt. absl::Status StartFastPairScan() override; absl::Status StopFastPairScan() override; + void SetIsScreenLocked(bool is_locked) override; // From BluetoothClassicMedium::Observer. void DeviceAdded(BluetoothDevice& device) override; @@ -88,9 +91,6 @@ class FastPairSeekerImpl : public FastPairSeekerExt, void DeviceConnectedStateChanged(BluetoothDevice& device, bool connected) override; - // Handle the state changes of screen lock. - void SetIsScreenLocked(bool is_locked); - // Internal methods, not exported to plugins. private: // From ScannerBrokerImpl::Observer. diff --git a/fastpair/internal/fast_pair_seeker_impl_test.cc b/fastpair/internal/fast_pair_seeker_impl_test.cc index 16288d80..cc2fe7b0 100644 --- a/fastpair/internal/fast_pair_seeker_impl_test.cc +++ b/fastpair/internal/fast_pair_seeker_impl_test.cc @@ -24,6 +24,7 @@ #include "gtest/gtest.h" #include "absl/time/clock.h" #include "absl/time/time.h" +#include "fastpair/fast_pair_events.h" #include "fastpair/message_stream/fake_gatt_callbacks.h" #include "fastpair/message_stream/fake_provider.h" #include "fastpair/server_access/fake_fast_pair_repository.h" @@ -119,20 +120,19 @@ TEST_F(FastPairSeekerImplTest, StopFastPairScanTwiceFails) { } TEST_F(FastPairSeekerImplTest, ScreenLocksDuringAdvertising) { - CountDownLatch latch(1); + CountDownLatch latch(2); fast_pair_seeker_ = std::make_unique( FastPairSeekerImpl::ServiceCallbacks{ .on_initial_discovery = [&](const FastPairDevice& device, InitialDiscoveryEvent event) { latch.CountDown(); + }, + .on_screen_event = + [&](ScreenEvent event) { + EXPECT_TRUE(event.is_locked); + latch.CountDown(); }}, &executor_, &devices_); - - EXPECT_OK(fast_pair_seeker_->StartFastPairScan()); - EXPECT_THAT(fast_pair_seeker_->StartFastPairScan(), - StatusIs(absl::StatusCode::kAlreadyExists)); - - fast_pair_seeker_->SetIsScreenLocked(true); // Create Advertiser and startAdvertising Mediums mediums_2; std::string service_id(kServiceID); @@ -140,8 +140,11 @@ TEST_F(FastPairSeekerImplTest, ScreenLocksDuringAdvertising) { std::string fast_pair_service_uuid(kFastPairServiceUuid); mediums_2.GetBle().GetMedium().StartAdvertising( service_id, advertisement_bytes, fast_pair_service_uuid); + EXPECT_OK(fast_pair_seeker_->StartFastPairScan()); - EXPECT_FALSE(latch.Await(kTaskWaitTimeout).result()); + fast_pair_seeker_->SetIsScreenLocked(true); + + EXPECT_TRUE(latch.Await().Ok()); } TEST_F(FastPairSeekerImplTest, InitialPairing) { diff --git a/fastpair/plugins/BUILD b/fastpair/plugins/BUILD index 707b7eaa..9c5aadbc 100644 --- a/fastpair/plugins/BUILD +++ b/fastpair/plugins/BUILD @@ -65,5 +65,6 @@ cc_library( "//fastpair:fast_pair_plugin", "//fastpair:fast_pair_seeker", "//fastpair/common", + "@com_google_absl//absl/functional:any_invocable", ], ) diff --git a/fastpair/plugins/fake_fast_pair_plugin.cc b/fastpair/plugins/fake_fast_pair_plugin.cc index d04f3cbd..9069a0e9 100644 --- a/fastpair/plugins/fake_fast_pair_plugin.cc +++ b/fastpair/plugins/fake_fast_pair_plugin.cc @@ -24,5 +24,11 @@ void FakeFastPairPlugin::OnInitialDiscoveryEvent( } } +void FakeFastPairPlugin::OnScreenEvent(const ScreenEvent& event) { + if (provider_->on_screen_event_ != nullptr) { + provider_->on_screen_event_(event); + } +} + } // namespace fastpair } // namespace nearby diff --git a/fastpair/plugins/fake_fast_pair_plugin.h b/fastpair/plugins/fake_fast_pair_plugin.h index 88673992..10831e11 100644 --- a/fastpair/plugins/fake_fast_pair_plugin.h +++ b/fastpair/plugins/fake_fast_pair_plugin.h @@ -17,6 +17,7 @@ #include +#include "absl/functional/any_invocable.h" #include "fastpair/common/fast_pair_device.h" #include "fastpair/fast_pair_events.h" #include "fastpair/fast_pair_plugin.h" @@ -33,6 +34,7 @@ class FakeFastPairPlugin : public FastPairPlugin { : seeker_(seeker), device_(device), provider_(provider) {} void OnInitialDiscoveryEvent(const InitialDiscoveryEvent& event) override; + void OnScreenEvent(const ScreenEvent& event) override; private: FastPairSeeker* seeker_; @@ -50,6 +52,7 @@ class FakeFastPairPluginProvider : public FastPairPluginProvider { absl::AnyInvocable on_initial_discovery_event_ = nullptr; + absl::AnyInvocable on_screen_event_ = nullptr; }; } // namespace fastpair