-
Notifications
You must be signed in to change notification settings - Fork 230
/
session_environment.cc
93 lines (79 loc) · 3.21 KB
/
session_environment.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (C) 2022 The Android Open Source Project
*
* 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
*
* http://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 "session_environment.h"
#include "accessors/surface_control.h"
#include "agent.h"
#include "log.h"
#include "num_to_string.h"
#include "settings.h"
namespace screensharing {
using namespace std;
namespace {
// Constants from android.os.BatteryManager.
constexpr int BATTERY_PLUGGED_AC = 1;
constexpr int BATTERY_PLUGGED_USB = 2;
constexpr int BATTERY_PLUGGED_WIRELESS = 4;
// Names an location of the screen sharing agent's files.
#define SCREEN_SHARING_AGENT_JAR_NAME "screen-sharing-agent.jar"
#define SCREEN_SHARING_AGENT_SO_NAME "libscreen-sharing-agent.so"
#define DEVICE_PATH_BASE "/data/local/tmp/.studio"
// Removes files of the screen sharing agent from the persistent storage.
void RemoveAgentFiles() {
remove(DEVICE_PATH_BASE "/" SCREEN_SHARING_AGENT_JAR_NAME);
remove(DEVICE_PATH_BASE "/" SCREEN_SHARING_AGENT_SO_NAME);
}
} // namespace
SessionEnvironment::SessionEnvironment(bool turn_off_display)
: accelerometer_rotation_(Settings::Table::SYSTEM, "accelerometer_rotation"),
stay_on_(Settings::Table::GLOBAL, "stay_on_while_plugged_in"),
restore_normal_display_power_mode_(false) {
// Keep the screen on as long as the device has power.
stay_on_.Set(num_to_string<BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS>::value);
if (turn_off_display) {
// Turn off display.
Jni jni = Jvm::GetJni();
if (Agent::feature_level() >= 35) {
DisplayManager::RequestDisplayPower(jni, PRIMARY_DISPLAY_ID, false);
restore_normal_display_power_mode_ = true;
} else if (Agent::feature_level() <= 33) {
JObject display_token = SurfaceControl::GetInternalDisplayToken(jni);
if (display_token.IsNotNull()) {
SurfaceControl::SetDisplayPowerMode(jni, display_token, DisplayPowerMode::POWER_MODE_OFF);
restore_normal_display_power_mode_ = true;
} else {
Log::W(jni.GetAndClearException(), "Unable to get display token to turn off display");
}
}
}
RemoveAgentFiles();
}
SessionEnvironment::~SessionEnvironment() {
if (restore_normal_display_power_mode_) {
Jni jni = Jvm::GetJni();
if (Agent::feature_level() >= 35) {
DisplayManager::RequestDisplayPower(jni, PRIMARY_DISPLAY_ID, true);
} else if (Agent::feature_level() <= 33) {
JObject display_token = SurfaceControl::GetInternalDisplayToken(jni);
if (display_token.IsNotNull()) {
SurfaceControl::SetDisplayPowerMode(jni, display_token, DisplayPowerMode::POWER_MODE_NORMAL);
}
}
}
stay_on_.Restore();
accelerometer_rotation_.Restore();
Log::D("Restored original system settings");
}
} // namespace screensharing