Pass screen locked event to plugins

PiperOrigin-RevId: 543512467
This commit is contained in:
Janusz Sobczak
2023-06-26 12:37:45 -07:00
committed by Copybara-Service
parent 1ddd4d91ea
commit b89eeb1ac1
11 changed files with 80 additions and 28 deletions
+4 -1
View File
@@ -15,6 +15,7 @@
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_EVENTS_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_EVENTS_H_
#include <optional>
namespace nearby {
namespace fastpair {
@@ -27,7 +28,9 @@ struct SubsequentDiscoveryEvent {};
struct PairEvent {};
struct ScreenEvent {};
struct ScreenEvent {
std::optional<bool> is_locked;
};
struct BatteryEvent {};
+12 -5
View File
@@ -64,9 +64,7 @@ FastPairService::FastPairService(std::unique_ptr<FastPairRepository> 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,
+1 -1
View File
@@ -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);
+32
View File
@@ -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<FakeFastPairPluginProvider>();
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<FastPairSeekerExt*>(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
+1
View File
@@ -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",
+4 -8
View File
@@ -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() {
+5 -5
View File
@@ -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<void(const FastPairDevice&, SubsequentDiscoveryEvent)>
on_subsequent_discovery;
absl::AnyInvocable<void(const FastPairDevice&, PairEvent)> on_pair_event;
absl::AnyInvocable<void(const FastPairDevice&, ScreenEvent)>
on_screen_event;
absl::AnyInvocable<void(ScreenEvent)> on_screen_event;
absl::AnyInvocable<void(const FastPairDevice&, BatteryEvent)>
on_battery_event;
absl::AnyInvocable<void(const FastPairDevice&, RingEvent)> 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.
@@ -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>(
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) {
+1
View File
@@ -65,5 +65,6 @@ cc_library(
"//fastpair:fast_pair_plugin",
"//fastpair:fast_pair_seeker",
"//fastpair/common",
"@com_google_absl//absl/functional:any_invocable",
],
)
@@ -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
+3
View File
@@ -17,6 +17,7 @@
#include <memory>
#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<void(const FastPairDevice* device,
const InitialDiscoveryEvent& event)>
on_initial_discovery_event_ = nullptr;
absl::AnyInvocable<void(const ScreenEvent& event)> on_screen_event_ = nullptr;
};
} // namespace fastpair