diff --git a/internal/platform/device_info.h b/internal/platform/device_info.h index 88254734..c95f592d 100644 --- a/internal/platform/device_info.h +++ b/internal/platform/device_info.h @@ -52,6 +52,9 @@ class DeviceInfo { virtual void UnregisterScreenLockedListener( absl::string_view listener_name) = 0; + virtual bool PreventSleep() = 0; + virtual bool AllowSleep() = 0; + // Returns localized device name depends on device type. std::u16string GetDeviceTypeName() const { // TODO(b/230132370): return localized device name. diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index 702c83a0..6ac551d5 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -108,4 +108,10 @@ void DeviceInfoImpl::UnregisterScreenLockedListener( device_info_impl_->UnregisterScreenLockedListener(listener_name); } +bool DeviceInfoImpl::PreventSleep() { + return device_info_impl_->PreventSleep(); +} + +bool DeviceInfoImpl::AllowSleep() { return device_info_impl_->AllowSleep(); } + } // namespace nearby diff --git a/internal/platform/device_info_impl.h b/internal/platform/device_info_impl.h index 1c44d961..98e6032f 100644 --- a/internal/platform/device_info_impl.h +++ b/internal/platform/device_info_impl.h @@ -53,6 +53,9 @@ class DeviceInfoImpl : public DeviceInfo { std::function callback) override; void UnregisterScreenLockedListener(absl::string_view listener_name) override; + bool PreventSleep() override; + bool AllowSleep() override; + private: std::unique_ptr device_info_impl_; }; diff --git a/internal/platform/implementation/apple/device_info.h b/internal/platform/implementation/apple/device_info.h index 953f54c4..4315040c 100644 --- a/internal/platform/implementation/apple/device_info.h +++ b/internal/platform/implementation/apple/device_info.h @@ -58,6 +58,20 @@ class DeviceInfo : public api::DeviceInfo { std::function callback) override; void UnregisterScreenLockedListener(absl::string_view listener_name) override; + + // Request the system to prevent the device from going to sleep. + // + // To allow the system to sleep again, call @c AllowSleep(). + // + // Returns @c true on success or if the platform does not allow preventing + // sleep. Returns @c false on any other error. + bool PreventSleep() override; + + // Release any requests preventing the device from going to sleep. + // + // Returns @c true on success or if the system is already allowed to sleep. + // Returns @c false if an error occurred. + bool AllowSleep() override; }; } // namespace apple diff --git a/internal/platform/implementation/apple/device_info.mm b/internal/platform/implementation/apple/device_info.mm index b5053759..ee0104dd 100644 --- a/internal/platform/implementation/apple/device_info.mm +++ b/internal/platform/implementation/apple/device_info.mm @@ -168,5 +168,9 @@ void DeviceInfo::RegisterScreenLockedListener( void DeviceInfo::UnregisterScreenLockedListener(absl::string_view listener_name) {} +bool DeviceInfo::PreventSleep() { return true; } + +bool DeviceInfo::AllowSleep() { return true; } + } // namespace apple } // namespace nearby diff --git a/internal/platform/implementation/device_info.h b/internal/platform/implementation/device_info.h index 7247fa85..b4a0ba4b 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -59,6 +59,10 @@ class DeviceInfo { std::function callback) = 0; virtual void UnregisterScreenLockedListener( absl::string_view listener_name) = 0; + + // Control device sleep + virtual bool PreventSleep() = 0; + virtual bool AllowSleep() = 0; }; } // namespace api diff --git a/internal/platform/implementation/g3/device_info.h b/internal/platform/implementation/g3/device_info.h index e9a90ff3..87d6a958 100644 --- a/internal/platform/implementation/g3/device_info.h +++ b/internal/platform/implementation/g3/device_info.h @@ -38,7 +38,7 @@ class DeviceInfo : public api::DeviceInfo { return api::DeviceInfo::DeviceType::kLaptop; } - api::DeviceInfo::OsType GetOsType() const override{ + api::DeviceInfo::OsType GetOsType() const override { return api::DeviceInfo::OsType::kChromeOs; } @@ -92,6 +92,10 @@ class DeviceInfo : public api::DeviceInfo { screen_locked_listeners_.erase(listener_name); } + bool PreventSleep() override { return true; } + + bool AllowSleep() override { return true; } + private: absl::flat_hash_map> diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index eb3220c5..5978f27c 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -353,5 +353,15 @@ void DeviceInfo::UnregisterScreenLockedListener( session_manager_.UnregisterSessionListener(listener_name); } +bool DeviceInfo::PreventSleep() { + absl::MutexLock lock(&mutex_); + return session_manager_.PreventSleep(); +} + +bool DeviceInfo::AllowSleep() { + absl::MutexLock lock(&mutex_); + return session_manager_.AllowSleep(); +} + } // namespace windows } // namespace nearby diff --git a/internal/platform/implementation/windows/device_info.h b/internal/platform/implementation/windows/device_info.h index 9d112d30..79c96771 100644 --- a/internal/platform/implementation/windows/device_info.h +++ b/internal/platform/implementation/windows/device_info.h @@ -52,6 +52,9 @@ class DeviceInfo : public api::DeviceInfo { std::function callback) override; void UnregisterScreenLockedListener(absl::string_view listener_name) override; + bool PreventSleep() override; + bool AllowSleep() override; + private: mutable absl::Mutex mutex_; SessionManager session_manager_ ABSL_GUARDED_BY(mutex_); diff --git a/internal/platform/implementation/windows/device_info_test.cc b/internal/platform/implementation/windows/device_info_test.cc index 4cdbf306..d19c4370 100644 --- a/internal/platform/implementation/windows/device_info_test.cc +++ b/internal/platform/implementation/windows/device_info_test.cc @@ -77,6 +77,14 @@ TEST(DeviceInfo, DISABLED_IsScreenLocked) { EXPECT_FALSE(DeviceInfo().IsScreenLocked()); } +TEST(DeviceInfo, DISABLED_PreventSleep) { + EXPECT_TRUE(DeviceInfo().PreventSleep()); +} + +TEST(DeviceInfo, DISABLED_AllowSleep) { + EXPECT_TRUE(DeviceInfo().AllowSleep()); +} + } // namespace } // namespace windows } // namespace nearby diff --git a/internal/test/fake_device_info.h b/internal/test/fake_device_info.h index 4f296702..b7fbf160 100644 --- a/internal/test/fake_device_info.h +++ b/internal/test/fake_device_info.h @@ -86,6 +86,10 @@ class FakeDeviceInfo : public DeviceInfo { screen_locked_listeners_.erase(listener_name); } + bool PreventSleep() override { return true; } + + bool AllowSleep() override { return true; } + int GetScreenLockedListenerCount() { return screen_locked_listeners_.size(); } // Mock methods. diff --git a/internal/test/fake_device_info_test.cc b/internal/test/fake_device_info_test.cc index 53ffc093..16935c43 100644 --- a/internal/test/fake_device_info_test.cc +++ b/internal/test/fake_device_info_test.cc @@ -188,5 +188,15 @@ TEST(FakeDeviceInfo, UpdateScreenLockedListener) { EXPECT_EQ(screen_locked_tracker_2, api::DeviceInfo::ScreenStatus::kUnlocked); } +TEST(FakeDeviceInfo, PreventSleep) { + FakeDeviceInfo device_info; + EXPECT_TRUE(device_info.PreventSleep()); +} + +TEST(FakeDeviceInfo, AllowSleep) { + FakeDeviceInfo device_info; + EXPECT_TRUE(device_info.AllowSleep()); +} + } // namespace } // namespace nearby