0

[CrOS Hotspot] Notify users about hotspot inactivity

Hotspot is disabled if there are no active connections for few minutes
and these changes will surface a notification if that happens.

Screenshot: https://screenshot.googleplex.com/7GZiFYjp4TNXhWs
Bug: b/269354043

Change-Id: I2ebea8dde76abbf2cc3ea59478061d06754d4474
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4333777
Reviewed-by: Jason Zhang <jiajunz@google.com>
Commit-Queue: Nikhil Nayunigari <nikhilcn@google.com>
Cr-Commit-Position: refs/heads/main@{#1128104}
This commit is contained in:
Nikhil Nayunigari
2023-04-10 07:47:51 +00:00
committed by Chromium LUCI CQ
parent 3f2782a50d
commit 104e7ca953
6 changed files with 79 additions and 0 deletions

@ -2578,6 +2578,9 @@ Connect your device to power.
<message name="IDS_ASH_HOTSPOT_OFF_TITLE" desc="Title used for the system notification shown when the hotspot is turned off."> <message name="IDS_ASH_HOTSPOT_OFF_TITLE" desc="Title used for the system notification shown when the hotspot is turned off.">
Hotspot is off Hotspot is off
</message> </message>
<message name="IDS_ASH_HOTSPOT_NOTIFICATION_TURN_ON_BUTTON" desc="The label used as the button to turn on hotspot.">
Turn on hotspot
</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>
@ -2587,6 +2590,9 @@ Connect your device to power.
<message name="IDS_ASH_HOTSPOT_ADMIN_RESTRICTED_MESSAGE" desc="Message displayed in the system notification shown when the hotspot is turned off by the administrator."> <message name="IDS_ASH_HOTSPOT_ADMIN_RESTRICTED_MESSAGE" desc="Message displayed in the system notification shown when the hotspot is turned off by the administrator.">
Your administrator has turned Hotspot off. Your administrator has turned Hotspot off.
</message> </message>
<message name="IDS_ASH_HOTSPOT_AUTO_DISABLED_MESSAGE" desc="Message displayed in the system notification shown when the hotspot is turned off due to inactivity.">
Due to inactivity, hotspot has been turned off.
</message>
<message name="IDS_ASH_NETWORK_AUTOCONNECT" desc="Text used for the toast shown when a network has been auto-connected (e.g., when an enterprise policy is applied which initiates a connection to a corporate network like Google-A)."> <message name="IDS_ASH_NETWORK_AUTOCONNECT" desc="Text used for the toast shown when a network has been auto-connected (e.g., when an enterprise policy is applied which initiates a connection to a corporate network like Google-A).">
We've switched you to a better network We've switched you to a better network
</message> </message>

@ -0,0 +1 @@
25aa7efe92368f7fb081bc36b9436b43a4572a4a

@ -0,0 +1 @@
66ca70f41018f3c48777f7f52bfef6ebd6bfd795

@ -22,6 +22,9 @@ const char HotspotNotifier::kAdminRestrictedNotificationId[] =
const char HotspotNotifier::kWiFiTurnedOnNotificationId[] = const char HotspotNotifier::kWiFiTurnedOnNotificationId[] =
"cros_hotspot_notifier_ids.wifi_turned_on"; "cros_hotspot_notifier_ids.wifi_turned_on";
const char HotspotNotifier::kAutoDisabledNotificationId[] =
"cros_hotspot_notifier_ids.auto_disabled";
const char kNotifierHotspot[] = "ash.hotspot"; const char kNotifierHotspot[] = "ash.hotspot";
HotspotNotifier::HotspotNotifier() { HotspotNotifier::HotspotNotifier() {
@ -55,6 +58,7 @@ void HotspotNotifier::OnHotspotTurnedOff(
int title_id; int title_id;
int message_id; int message_id;
const char* notification_id; const char* notification_id;
std::vector<message_center::ButtonInfo> notification_actions;
switch (disable_reason) { switch (disable_reason) {
case hotspot_config::mojom::DisableReason::kProhibitedByPolicy: case hotspot_config::mojom::DisableReason::kProhibitedByPolicy:
title_id = IDS_ASH_HOTSPOT_OFF_TITLE; title_id = IDS_ASH_HOTSPOT_OFF_TITLE;
@ -66,12 +70,29 @@ void HotspotNotifier::OnHotspotTurnedOff(
message_id = IDS_ASH_HOTSPOT_WIFI_TURNED_ON_MESSAGE; message_id = IDS_ASH_HOTSPOT_WIFI_TURNED_ON_MESSAGE;
notification_id = kWiFiTurnedOnNotificationId; notification_id = kWiFiTurnedOnNotificationId;
break; break;
case hotspot_config::mojom::DisableReason::kAutoDisabled:
title_id = IDS_ASH_HOTSPOT_OFF_TITLE;
message_id = IDS_ASH_HOTSPOT_AUTO_DISABLED_MESSAGE;
notification_id = kAutoDisabledNotificationId;
delegate =
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&HotspotNotifier::EnableHotspotHandler,
weak_ptr_factory_.GetWeakPtr(),
notification_id));
notification_actions.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_ASH_HOTSPOT_NOTIFICATION_TURN_ON_BUTTON)));
break;
default: default:
return; return;
} }
std::unique_ptr<message_center::Notification> notification = std::unique_ptr<message_center::Notification> notification =
CreateNotification(title_id, message_id, notification_id, delegate); CreateNotification(title_id, message_id, notification_id, delegate);
if (notification_actions.size() > 0) {
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(notification_id, message_center->RemoveNotification(notification_id,
@ -79,6 +100,27 @@ void HotspotNotifier::OnHotspotTurnedOff(
message_center->AddNotification(std::move(notification)); message_center->AddNotification(std::move(notification));
} }
void HotspotNotifier::EnableHotspotHandler(const char* notification_id,
absl::optional<int> button_index) {
if (!button_index) {
return;
}
if (button_index.value() == 0) {
remote_cros_hotspot_config_->EnableHotspot(
base::BindOnce([](hotspot_config::mojom::HotspotControlResult result) {
if (result == hotspot_config::mojom::HotspotControlResult::kSuccess ||
result == hotspot_config::mojom::HotspotControlResult::
kAlreadyFulfilled) {
message_center::MessageCenter* message_center =
message_center::MessageCenter::Get();
message_center->RemoveNotification(kAutoDisabledNotificationId,
/*by_user=*/false);
}
}));
}
}
std::unique_ptr<message_center::Notification> std::unique_ptr<message_center::Notification>
HotspotNotifier::CreateNotification( HotspotNotifier::CreateNotification(
const int title_id, const int title_id,

@ -34,6 +34,7 @@ class ASH_EXPORT HotspotNotifier
static const char kWiFiTurnedOffNotificationId[]; static const char kWiFiTurnedOffNotificationId[];
static const char kAdminRestrictedNotificationId[]; static const char kAdminRestrictedNotificationId[];
static const char kWiFiTurnedOnNotificationId[]; static const char kWiFiTurnedOnNotificationId[];
static const char kAutoDisabledNotificationId[];
private: private:
friend class HotspotNotifierTest; friend class HotspotNotifierTest;
@ -49,6 +50,9 @@ class ASH_EXPORT HotspotNotifier
const char* notification_id, const char* notification_id,
scoped_refptr<message_center::NotificationDelegate> delegate); scoped_refptr<message_center::NotificationDelegate> delegate);
void EnableHotspotHandler(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::Receiver<hotspot_config::mojom::HotspotEnabledStateObserver> mojo::Receiver<hotspot_config::mojom::HotspotEnabledStateObserver>

@ -168,4 +168,29 @@ TEST_F(HotspotNotifierTest, WiFiTurnedOn) {
HotspotNotifier::kWiFiTurnedOnNotificationId)); HotspotNotifier::kWiFiTurnedOnNotificationId));
} }
TEST_F(HotspotNotifierTest, AutoDisabled) {
SetValidHotspotCapabilities();
SetReadinessCheckResultReady();
AddActiveCellularService();
helper()->manager_test()->SetSimulateTetheringEnableResult(
FakeShillSimulatedResult::kSuccess, shill::kTetheringEnableResultSuccess);
base::RunLoop().RunUntilIdle();
EnableHotspot();
EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
HotspotNotifier::kWiFiTurnedOffNotificationId));
NotifyHotspotTurnedOff(hotspot_config::mojom::DisableReason::kAutoDisabled);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
HotspotNotifier::kAutoDisabledNotificationId));
message_center::MessageCenter::Get()->ClickOnNotificationButton(
HotspotNotifier::kAutoDisabledNotificationId, 0);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(
message_center::MessageCenter::Get()->FindVisibleNotificationById(
HotspotNotifier::kAutoDisabledNotificationId));
}
} // namespace ash } // namespace ash