0

[CrOS Hotspot] Add the 'Turn Wifi On' button to hotspot notification

A notification is displayed to the user when they turn on the hotspot
while WiFi is enabled. This notification provides a button to the user
to turn the WiFi on which will disable the hotspot as well.

Screenshot: https://screenshot.googleplex.com/5zxNrqngmcez3dj
Bug: b/269353987
Change-Id: Ic45e4d6687d47c15905859885dee698c0d1136d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4408388
Reviewed-by: Jason Zhang <jiajunz@google.com>
Commit-Queue: Nikhil Nayunigari <nikhilcn@google.com>
Cr-Commit-Position: refs/heads/main@{#1128234}
This commit is contained in:
Nikhil Nayunigari
2023-04-10 18:16:20 +00:00
committed by Chromium LUCI CQ
parent e6be88bf31
commit c8c9a42e75
5 changed files with 85 additions and 4 deletions

@ -2581,6 +2581,9 @@ Connect your device to power.
<message name="IDS_ASH_HOTSPOT_NOTIFICATION_TURN_ON_BUTTON" desc="The label used as the button to turn on hotspot."> <message name="IDS_ASH_HOTSPOT_NOTIFICATION_TURN_ON_BUTTON" desc="The label used as the button to turn on hotspot.">
Turn on hotspot Turn on hotspot
</message> </message>
<message name="IDS_ASH_HOTSPOT_NOTIFICATION_WIFI_TURN_ON_BUTTON" desc="The label for button to turn WiFi on in the hotspot notification.">
Turn on WiFi instead
</message>
<message name="IDS_ASH_HOTSPOT_WIFI_TURNED_OFF_MESSAGE" desc="Message displayed in the system notification shown when WiFi is turned off upon enabling hotspot."> <message name="IDS_ASH_HOTSPOT_WIFI_TURNED_OFF_MESSAGE" desc="Message displayed in the system notification shown when WiFi is turned off upon enabling hotspot.">
We've turned off the WiFi to start using Hotspot through Mobile data. This may incur data costs. We've turned off the WiFi to start using Hotspot through Mobile data. This may incur data costs.
</message> </message>

@ -0,0 +1 @@
c702fbf5ed2c91d143dc713285e3c1440a8b9eda

@ -4,6 +4,7 @@
#include "ash/system/network/hotspot_notifier.h" #include "ash/system/network/hotspot_notifier.h"
#include "ash/public/cpp/hotspot_config_service.h" #include "ash/public/cpp/hotspot_config_service.h"
#include "ash/public/cpp/network_config_service.h"
#include "ash/public/cpp/notification_utils.h" #include "ash/public/cpp/notification_utils.h"
#include "ash/strings/grit/ash_strings.h" #include "ash/strings/grit/ash_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
@ -35,18 +36,32 @@ HotspotNotifier::HotspotNotifier() {
remote_cros_hotspot_config_.BindNewPipeAndPassReceiver()); remote_cros_hotspot_config_.BindNewPipeAndPassReceiver());
remote_cros_hotspot_config_->ObserveEnabledStateChanges( remote_cros_hotspot_config_->ObserveEnabledStateChanges(
hotspot_enabled_state_observer_receiver_.BindNewPipeAndPassRemote()); hotspot_enabled_state_observer_receiver_.BindNewPipeAndPassRemote());
GetNetworkConfigService(
remote_cros_network_config_.BindNewPipeAndPassReceiver());
remote_cros_network_config_->AddObserver(
cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
} }
HotspotNotifier::~HotspotNotifier() = default; HotspotNotifier::~HotspotNotifier() = default;
void HotspotNotifier::OnHotspotTurnedOn(bool wifi_turned_off) { void HotspotNotifier::OnHotspotTurnedOn(bool wifi_turned_off) {
if (wifi_turned_off) { if (wifi_turned_off) {
scoped_refptr<message_center::NotificationDelegate> delegate =
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&HotspotNotifier::EnableWiFiHandler,
weak_ptr_factory_.GetWeakPtr(),
kWiFiTurnedOffNotificationId));
std::vector<message_center::ButtonInfo> notification_actions;
std::unique_ptr<message_center::Notification> notification = std::unique_ptr<message_center::Notification> notification =
CreateNotification(IDS_ASH_HOTSPOT_ON_TITLE, CreateNotification(IDS_ASH_HOTSPOT_ON_TITLE,
IDS_ASH_HOTSPOT_WIFI_TURNED_OFF_MESSAGE, IDS_ASH_HOTSPOT_WIFI_TURNED_OFF_MESSAGE,
kWiFiTurnedOffNotificationId, kWiFiTurnedOffNotificationId, delegate);
/*delegate=*/nullptr); notification_actions.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_ASH_HOTSPOT_NOTIFICATION_WIFI_TURN_ON_BUTTON)));
notification->set_buttons(notification_actions);
message_center::MessageCenter* message_center = message_center::MessageCenter* message_center =
message_center::MessageCenter::Get(); message_center::MessageCenter::Get();
message_center->RemoveNotification(kWiFiTurnedOffNotificationId, message_center->RemoveNotification(kWiFiTurnedOffNotificationId,
@ -139,6 +154,40 @@ void HotspotNotifier::EnableHotspotHandler(const char* notification_id,
} }
} }
void HotspotNotifier::EnableWiFiHandler(const char* notification_id,
absl::optional<int> button_index) {
if (!button_index) {
return;
}
if (button_index.value() == 0) {
remote_cros_network_config_->SetNetworkTypeEnabledState(
chromeos::network_config::mojom::NetworkType::kWiFi, /*enabled=*/true,
base::DoNothing());
}
}
void HotspotNotifier::OnDeviceStateListChanged() {
remote_cros_network_config_->GetDeviceStateList(base::BindOnce(
&HotspotNotifier::OnGetDeviceStateList, weak_ptr_factory_.GetWeakPtr()));
}
void HotspotNotifier::OnGetDeviceStateList(
std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
devices) {
for (auto& device : devices) {
if (device->type == chromeos::network_config::mojom::NetworkType::kWiFi &&
device->device_state ==
chromeos::network_config::mojom::DeviceStateType::kEnabled) {
message_center::MessageCenter* message_center =
message_center::MessageCenter::Get();
message_center->RemoveNotification(kWiFiTurnedOffNotificationId,
/*by_user=*/false);
return;
}
}
}
std::unique_ptr<message_center::Notification> std::unique_ptr<message_center::Notification>
HotspotNotifier::CreateNotification( HotspotNotifier::CreateNotification(
const int title_id, const int title_id,

@ -9,6 +9,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h" #include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"
#include "chromeos/services/network_config/public/cpp/cros_network_config_observer.h" #include "chromeos/services/network_config/public/cpp/cros_network_config_observer.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "ui/message_center/message_center.h" #include "ui/message_center/message_center.h"
@ -24,7 +25,8 @@ namespace ash {
// - 4. In activity // - 4. In activity
// - Hotspot is turned on and has 'n' active connections // - Hotspot is turned on and has 'n' active connections
class ASH_EXPORT HotspotNotifier class ASH_EXPORT HotspotNotifier
: public hotspot_config::mojom::HotspotEnabledStateObserver { : public hotspot_config::mojom::HotspotEnabledStateObserver,
public chromeos::network_config::CrosNetworkConfigObserver {
public: public:
HotspotNotifier(); HotspotNotifier();
HotspotNotifier(const HotspotNotifier&) = delete; HotspotNotifier(const HotspotNotifier&) = delete;
@ -45,6 +47,13 @@ class ASH_EXPORT HotspotNotifier
void OnHotspotTurnedOff( void OnHotspotTurnedOff(
hotspot_config::mojom::DisableReason disable_reason) override; hotspot_config::mojom::DisableReason disable_reason) override;
// CrosNetworkConfigObserver:
void OnDeviceStateListChanged() override;
void OnGetDeviceStateList(
std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
devices);
std::unique_ptr<message_center::Notification> CreateNotification( std::unique_ptr<message_center::Notification> CreateNotification(
const int title_id, const int title_id,
const int message_id, const int message_id,
@ -53,9 +62,15 @@ class ASH_EXPORT HotspotNotifier
void EnableHotspotHandler(const char* notification_id, void EnableHotspotHandler(const char* notification_id,
absl::optional<int> index); absl::optional<int> index);
void EnableWiFiHandler(const char* notification_id,
absl::optional<int> index);
mojo::Remote<hotspot_config::mojom::CrosHotspotConfig> mojo::Remote<hotspot_config::mojom::CrosHotspotConfig>
remote_cros_hotspot_config_; remote_cros_hotspot_config_;
mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
remote_cros_network_config_;
mojo::Receiver<chromeos::network_config::mojom::CrosNetworkConfigObserver>
cros_network_config_observer_receiver_{this};
mojo::Receiver<hotspot_config::mojom::HotspotEnabledStateObserver> mojo::Receiver<hotspot_config::mojom::HotspotEnabledStateObserver>
hotspot_enabled_state_observer_receiver_{this}; hotspot_enabled_state_observer_receiver_{this};

@ -11,8 +11,12 @@
#include "chromeos/ash/components/network/hotspot_state_handler.h" #include "chromeos/ash/components/network/hotspot_state_handler.h"
#include "chromeos/ash/components/network/network_handler.h" #include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h" #include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/services/hotspot_config/cros_hotspot_config.h"
#include "chromeos/ash/services/hotspot_config/public/cpp/cros_hotspot_config_test_helper.h" #include "chromeos/ash/services/hotspot_config/public/cpp/cros_hotspot_config_test_helper.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h" #include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
namespace ash { namespace ash {
@ -116,6 +120,8 @@ class HotspotNotifierTest : public NoSessionAshTestBase {
cros_network_config_test_helper_; cros_network_config_test_helper_;
std::unique_ptr<hotspot_config::CrosHotspotConfigTestHelper> std::unique_ptr<hotspot_config::CrosHotspotConfigTestHelper>
cros_hotspot_config_test_helper_; cros_hotspot_config_test_helper_;
std::unique_ptr<HotspotNotifier> hotspot_notifier_;
mojo::Remote<hotspot_config::mojom::CrosHotspotConfig> cros_hotspot_config_;
}; };
TEST_F(HotspotNotifierTest, WiFiTurnedOff) { TEST_F(HotspotNotifierTest, WiFiTurnedOff) {
@ -129,6 +135,13 @@ TEST_F(HotspotNotifierTest, WiFiTurnedOff) {
EnableHotspot(); EnableHotspot();
EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById( EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
HotspotNotifier::kWiFiTurnedOffNotificationId)); HotspotNotifier::kWiFiTurnedOffNotificationId));
message_center::MessageCenter::Get()->ClickOnNotificationButton(
HotspotNotifier::kWiFiTurnedOffNotificationId, 0);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(
message_center::MessageCenter::Get()->FindVisibleNotificationById(
HotspotNotifier::kWiFiTurnedOffNotificationId));
} }
TEST_F(HotspotNotifierTest, AdminRestricted) { TEST_F(HotspotNotifierTest, AdminRestricted) {