mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Implement Fast Pair UI broker
PiperOrigin-RevId: 525184851
This commit is contained in:
+4
-1
@@ -5,12 +5,15 @@ cc_library(
|
||||
srcs = [
|
||||
"fast_pair/fast_pair_notification_controller.cc",
|
||||
"fast_pair/fast_pair_presenter_impl.cc",
|
||||
"ui_broker_impl.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"actions.h",
|
||||
"fast_pair/fast_pair_notification_controller.h",
|
||||
"fast_pair/fast_pair_presenter.h",
|
||||
"fast_pair/fast_pair_presenter_impl.h",
|
||||
"ui_broker.h",
|
||||
"ui_broker_impl.h",
|
||||
],
|
||||
compatible_with = ["//buildenv/target:non_prod"],
|
||||
visibility = [
|
||||
@@ -23,7 +26,6 @@ cc_library(
|
||||
"//internal/base",
|
||||
"//internal/platform:logging",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/log:check",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
@@ -48,6 +50,7 @@ cc_test(
|
||||
srcs = [
|
||||
"fast_pair/fast_pair_notification_controller_test.cc",
|
||||
"fast_pair/fast_pair_presenter_impl_test.cc",
|
||||
"ui_broker_impl_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":fake_fast_pair_ui",
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_UI_UI_BROKER_H_
|
||||
#define THIRD_PARTY_NEARBY_FASTPAIR_UI_UI_BROKER_H_
|
||||
|
||||
#include "fastpair/common/fast_pair_device.h"
|
||||
#include "fastpair/ui/actions.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
// The UIBroker is the entry point for the UI component in the FastPair system.
|
||||
// It is responsible for brokering the 'show UI' calls to the correct Presenter
|
||||
// implementation, and exposing user actions taken on that UI.
|
||||
class UIBroker {
|
||||
public:
|
||||
class Observer {
|
||||
public:
|
||||
virtual ~Observer() = default;
|
||||
virtual void OnDiscoveryAction(const FastPairDevice& device,
|
||||
DiscoveryAction action) = 0;
|
||||
};
|
||||
|
||||
virtual ~UIBroker() = default;
|
||||
|
||||
virtual void AddObserver(Observer* observer) = 0;
|
||||
virtual void RemoveObserver(Observer* observer) = 0;
|
||||
|
||||
virtual void ShowDiscovery(
|
||||
const FastPairDevice& device,
|
||||
FastPairNotificationController& notification_controller) = 0;
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_UI_BROKER_H_
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastpair/ui/ui_broker_impl.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "fastpair/common/fast_pair_device.h"
|
||||
#include "fastpair/common/protocol.h"
|
||||
#include "fastpair/ui/actions.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_presenter.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
UIBrokerImpl::UIBrokerImpl()
|
||||
: fast_pair_presenter_(FastPairPresenterImpl::Factory::Create()) {}
|
||||
|
||||
void UIBrokerImpl::AddObserver(Observer* observer) {
|
||||
observers_.AddObserver(observer);
|
||||
}
|
||||
|
||||
void UIBrokerImpl::RemoveObserver(Observer* observer) {
|
||||
observers_.RemoveObserver(observer);
|
||||
}
|
||||
|
||||
void UIBrokerImpl::ShowDiscovery(
|
||||
const FastPairDevice& device,
|
||||
FastPairNotificationController& notification_controller) {
|
||||
switch (device.GetProtocol()) {
|
||||
case Protocol::kFastPairInitialPairing:
|
||||
case Protocol::kFastPairSubsequentPairing:
|
||||
fast_pair_presenter_->ShowDiscovery(
|
||||
device, notification_controller,
|
||||
[&device, this](DiscoveryAction action) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< __func__ << ": Notify discovery action to all observers.";
|
||||
NotifyDiscoveryAction(device, action);
|
||||
});
|
||||
break;
|
||||
case Protocol::kFastPairRetroactivePairing:
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__
|
||||
<< ": Retroactive Pairing should not show discovery Halfsheet.";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UIBrokerImpl::NotifyDiscoveryAction(const FastPairDevice& device,
|
||||
DiscoveryAction action) {
|
||||
for (auto& observer : observers_.GetObservers())
|
||||
observer->OnDiscoveryAction(device, action);
|
||||
}
|
||||
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_UI_BROKER_IMPL_H_
|
||||
#define THIRD_PARTY_NEARBY_FASTPAIR_UI_BROKER_IMPL_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "fastpair/common/fast_pair_device.h"
|
||||
#include "fastpair/ui/actions.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_presenter.h"
|
||||
#include "fastpair/ui/ui_broker.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
class UIBrokerImpl : public UIBroker {
|
||||
public:
|
||||
UIBrokerImpl();
|
||||
UIBrokerImpl(const UIBrokerImpl &) = delete;
|
||||
UIBrokerImpl &operator=(const UIBrokerImpl &) = delete;
|
||||
|
||||
void AddObserver(Observer *observer) override;
|
||||
void RemoveObserver(Observer *observer) override;
|
||||
void ShowDiscovery(
|
||||
const FastPairDevice &device,
|
||||
FastPairNotificationController ¬ification_controller) override;
|
||||
|
||||
private:
|
||||
void NotifyDiscoveryAction(const FastPairDevice &device,
|
||||
DiscoveryAction action);
|
||||
|
||||
std::unique_ptr<FastPairPresenter> fast_pair_presenter_;
|
||||
ObserverList<Observer> observers_;
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_BROKER_IMPL_H_
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastpair/ui/ui_broker_impl.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "fastpair/ui/actions.h"
|
||||
#include "fastpair/ui/fast_pair/fake_fast_pair_presenter.h"
|
||||
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
|
||||
#include "fastpair/ui/ui_broker.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
namespace {
|
||||
|
||||
constexpr absl::string_view kModelId = "718C17";
|
||||
constexpr absl::string_view kAddress = "74:74:46:01:6C:21";
|
||||
|
||||
class UIBrokerImplTest : public ::testing::Test, public UIBroker::Observer {
|
||||
protected:
|
||||
UIBrokerImplTest() {
|
||||
presenter_factory_ = std::make_unique<FakeFastPairPresenterFactory>();
|
||||
FastPairPresenterImpl::Factory::SetFactoryForTesting(
|
||||
presenter_factory_.get());
|
||||
ui_broker_ = std::make_unique<UIBrokerImpl>();
|
||||
ui_broker_->AddObserver(this);
|
||||
}
|
||||
|
||||
void OnDiscoveryAction(const FastPairDevice& device,
|
||||
DiscoveryAction action) override {
|
||||
on_discovery_action_notified_ = true;
|
||||
discovery_action_ = action;
|
||||
}
|
||||
|
||||
std::unique_ptr<UIBroker> ui_broker_;
|
||||
FastPairNotificationController notification_controller_;
|
||||
std::unique_ptr<FakeFastPairPresenterFactory> presenter_factory_;
|
||||
DiscoveryAction discovery_action_;
|
||||
bool on_discovery_action_notified_ = false;
|
||||
};
|
||||
|
||||
TEST_F(UIBrokerImplTest, ShowDiscovery) {
|
||||
FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing);
|
||||
ui_broker_->ShowDiscovery(device, notification_controller_);
|
||||
EXPECT_TRUE(
|
||||
presenter_factory_->fake_fast_pair_presenter()->show_deiscovery());
|
||||
EXPECT_TRUE(on_discovery_action_notified_);
|
||||
EXPECT_EQ(DiscoveryAction::kPairToDevice, discovery_action_);
|
||||
}
|
||||
|
||||
TEST_F(UIBrokerImplTest, ShowDiscoveryWithoutObserver) {
|
||||
FastPairDevice device(kModelId, kAddress, Protocol::kFastPairInitialPairing);
|
||||
ui_broker_->RemoveObserver(this);
|
||||
ui_broker_->ShowDiscovery(device, notification_controller_);
|
||||
EXPECT_TRUE(
|
||||
presenter_factory_->fake_fast_pair_presenter()->show_deiscovery());
|
||||
EXPECT_FALSE(on_discovery_action_notified_);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
Reference in New Issue
Block a user