Added APIs to control device sleep

These APIs can be used to prevent device into sleep when sending supper big files.

PiperOrigin-RevId: 556809236
This commit is contained in:
Guogang Li
2023-08-14 09:05:11 -07:00
committed by Copybara-Service
parent 06942caeb3
commit fabeff0d57
12 changed files with 74 additions and 1 deletions
@@ -58,6 +58,20 @@ class DeviceInfo : public api::DeviceInfo {
std::function<void(api::DeviceInfo::ScreenStatus)> 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
@@ -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
@@ -59,6 +59,10 @@ class DeviceInfo {
std::function<void(ScreenStatus)> callback) = 0;
virtual void UnregisterScreenLockedListener(
absl::string_view listener_name) = 0;
// Control device sleep
virtual bool PreventSleep() = 0;
virtual bool AllowSleep() = 0;
};
} // namespace api
@@ -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<std::string,
std::function<void(api::DeviceInfo::ScreenStatus)>>
@@ -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
@@ -52,6 +52,9 @@ class DeviceInfo : public api::DeviceInfo {
std::function<void(api::DeviceInfo::ScreenStatus)> 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_);
@@ -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