blob: cf0d3b5631c5adf9628d328c3783159d34dac61e [file] [log] [blame]
Doug Zongker32a0a472011-11-01 11:00:20 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tianjie Xu8f397302018-08-20 13:40:47 -070017#include "recovery_ui/ui.h"
Tao Bao736d59c2017-01-03 10:15:33 -080018
Doug Zongker32a0a472011-11-01 11:00:20 -070019#include <errno.h>
20#include <fcntl.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070024#include <sys/time.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
Tao Bao26ea9592018-05-09 16:32:02 -070029#include <chrono>
Tao Bao9468fc02017-03-17 00:57:37 -070030#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080031#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070032#include <thread>
Tao Bao736d59c2017-01-03 10:15:33 -080033
Tao Bao6278bdf2017-01-16 17:38:18 -080034#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/parseint.h>
Tao Bao0bc88de2018-07-31 14:53:16 -070037#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080038#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070039
Tao Bao26ea9592018-05-09 16:32:02 -070040#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070041#include "otautil/sysutil.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
Tao Bao26ea9592018-05-09 16:32:02 -070043using namespace std::chrono_literals;
44
Tao Bao0bc88de2018-07-31 14:53:16 -070045constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
46constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
48constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness";
49constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
kataoc35c1b02017-12-08 11:02:43 +080050 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Tao Bao0bc88de2018-07-31 14:53:16 -070052constexpr int kDefaultTouchLowThreshold = 50;
53constexpr int kDefaultTouchHighThreshold = 90;
54
Elliott Hughesec283402015-04-10 10:01:53 -070055RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080056 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080057 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080058 brightness_file_(BRIGHTNESS_FILE),
59 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tao Bao5f8dd992017-07-28 00:05:40 -070060 touch_screen_allowed_(false),
David Anderson983e2d52019-01-02 11:35:38 -080061 fastbootd_logo_enabled_(false),
Tao Bao0bc88de2018-07-31 14:53:16 -070062 touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold",
63 kDefaultTouchLowThreshold)),
64 touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold",
65 kDefaultTouchHighThreshold)),
Jerry Zhangb76af932018-05-22 12:08:35 -070066 key_interrupted_(false),
Tao Bao736d59c2017-01-03 10:15:33 -080067 key_queue_len(0),
68 key_last_down(-1),
69 key_long_press(false),
70 key_down_count(0),
71 enable_reboot(true),
72 consecutive_power_keys(0),
73 last_key(-1),
74 has_power_key(false),
75 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080076 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070077 has_touch_screen(false),
78 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070079 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080080 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080081 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070082}
83
Tao Bao26ea9592018-05-09 16:32:02 -070084RecoveryUI::~RecoveryUI() {
85 ev_exit();
86 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070087 if (input_thread_.joinable()) {
88 input_thread_.join();
89 }
Tao Bao26ea9592018-05-09 16:32:02 -070090}
91
Elliott Hughes642aaa72015-04-10 12:47:46 -070092void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070093 if (key_code == KEY_POWER) {
94 has_power_key = true;
95 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
96 has_down_key = true;
97 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
98 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070099 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
100 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700101 }
Elliott Hughes642aaa72015-04-10 12:47:46 -0700102}
103
Tao Bao6278bdf2017-01-16 17:38:18 -0800104bool RecoveryUI::InitScreensaver() {
105 // Disabled.
106 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
107 return false;
108 }
kataoc35c1b02017-12-08 11:02:43 +0800109 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
110 brightness_file_ = BRIGHTNESS_FILE_SDM;
111 }
112 if (access(max_brightness_file_.c_str(), R_OK)) {
113 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
114 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800115 // Set the initial brightness level based on the max brightness. Note that reading the initial
116 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
117 // we don't have a good way to query the default value.
118 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800119 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800120 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800121 return false;
122 }
123
124 unsigned int max_value;
125 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
126 LOG(WARNING) << "Failed to parse max brightness: " << content;
127 return false;
128 }
129
130 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
131 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
132 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800133 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800134 PLOG(WARNING) << "Failed to set brightness";
135 return false;
136 }
137
138 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
139 screensaver_state_ = ScreensaverState::NORMAL;
140 return true;
141}
142
Tao Baoefb49ad2017-01-31 23:03:10 -0800143bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700144 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
145 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700146
Tao Bao736d59c2017-01-03 10:15:33 -0800147 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
148
Tao Bao5f8dd992017-07-28 00:05:40 -0700149 if (touch_screen_allowed_) {
150 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700151
152 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
153 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
154 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
155 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
156 // mode will be turned on automatically on debuggable builds, even without a swipe.
157 std::string cmdline;
158 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
159 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
160 } else {
161 // Non-fatal, and won't affect Init() result.
162 PLOG(WARNING) << "Failed to read /proc/cmdline";
163 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700164 }
165
Tao Bao6278bdf2017-01-16 17:38:18 -0800166 if (!InitScreensaver()) {
167 LOG(INFO) << "Screensaver disabled";
168 }
169
Tao Bao26ea9592018-05-09 16:32:02 -0700170 // Create a separate thread that handles input events.
171 input_thread_ = std::thread([this]() {
172 while (!this->input_thread_stopped_) {
173 if (!ev_wait(500)) {
174 ev_dispatch();
175 }
176 }
177 });
178
Tao Bao736d59c2017-01-03 10:15:33 -0800179 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700180}
181
Tao Bao5f8dd992017-07-28 00:05:40 -0700182void RecoveryUI::OnTouchDetected(int dx, int dy) {
183 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
184
185 // We only consider a valid swipe if:
Tao Bao0bc88de2018-07-31 14:53:16 -0700186 // - the delta along one axis is below touch_low_threshold_;
187 // - and the delta along the other axis is beyond touch_high_threshold_.
188 if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700189 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
Tao Bao0bc88de2018-07-31 14:53:16 -0700190 } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700191 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
192 } else {
Tao Bao0bc88de2018-07-31 14:53:16 -0700193 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_
194 << ", high: " << touch_high_threshold_ << ")";
Tao Bao5f8dd992017-07-28 00:05:40 -0700195 return;
196 }
197
Tao Bao046aae22017-07-31 23:15:09 -0700198 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
199 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
200 ShowText(true);
201 return;
202 }
203
Tao Bao5f8dd992017-07-28 00:05:40 -0700204 LOG(DEBUG) << "Swipe direction=" << direction;
205 switch (direction) {
206 case SwipeDirection::UP:
207 ProcessKey(KEY_UP, 1); // press up key
208 ProcessKey(KEY_UP, 0); // and release it
209 break;
210
211 case SwipeDirection::DOWN:
212 ProcessKey(KEY_DOWN, 1); // press down key
213 ProcessKey(KEY_DOWN, 0); // and release it
214 break;
215
216 case SwipeDirection::LEFT:
217 case SwipeDirection::RIGHT:
218 ProcessKey(KEY_POWER, 1); // press power key
219 ProcessKey(KEY_POWER, 0); // and release it
220 break;
221 };
222}
223
Elliott Hughes985022a2015-04-13 13:04:32 -0700224int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700225 struct input_event ev;
226 if (ev_get_input(fd, epevents, &ev) == -1) {
227 return -1;
228 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700229
Tao Bao5f8dd992017-07-28 00:05:40 -0700230 // Touch inputs handling.
231 //
232 // We handle the touch inputs by tracking the position changes between initial contacting and
233 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
234 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
235 //
236 // Per the doc Multi-touch Protocol at below, there are two protocols.
237 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
238 //
239 // The main difference between the stateless type A protocol and the stateful type B slot protocol
240 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
241 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
242 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
243 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
244 //
245 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
246 // ABS_MT_TRACKING_ID being -1.
247 //
248 // Touch input events will only be available if touch_screen_allowed_ is set.
249
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700250 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700251 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
252 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
253 // contact.
254 if (touch_finger_down_ && !touch_swiping_) {
255 touch_start_X_ = touch_X_;
256 touch_start_Y_ = touch_Y_;
257 touch_swiping_ = true;
258 } else if (!touch_finger_down_ && touch_swiping_) {
259 touch_swiping_ = false;
260 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
261 }
262 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700263 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700264 }
265
266 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267 if (ev.code == REL_Y) {
268 // accumulate the up or down motion reported by
269 // the trackball. When it exceeds a threshold
270 // (positive or negative), fake an up/down
271 // key event.
272 rel_sum += ev.value;
273 if (rel_sum > 3) {
274 ProcessKey(KEY_DOWN, 1); // press down key
275 ProcessKey(KEY_DOWN, 0); // and release it
276 rel_sum = 0;
277 } else if (rel_sum < -3) {
278 ProcessKey(KEY_UP, 1); // press up key
279 ProcessKey(KEY_UP, 0); // and release it
280 rel_sum = 0;
281 }
282 }
283 } else {
284 rel_sum = 0;
285 }
286
Tao Bao5f8dd992017-07-28 00:05:40 -0700287 if (touch_screen_allowed_ && ev.type == EV_ABS) {
288 if (ev.code == ABS_MT_SLOT) {
289 touch_slot_ = ev.value;
290 }
291 // Ignore other fingers.
292 if (touch_slot_ > 0) return 0;
293
294 switch (ev.code) {
295 case ABS_MT_POSITION_X:
296 touch_X_ = ev.value;
297 touch_finger_down_ = true;
298 break;
299
300 case ABS_MT_POSITION_Y:
301 touch_Y_ = ev.value;
302 touch_finger_down_ = true;
303 break;
304
305 case ABS_MT_TRACKING_ID:
306 // Protocol B: -1 marks lifting the contact.
307 if (ev.value < 0) touch_finger_down_ = false;
308 break;
309 }
310 return 0;
311 }
312
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700313 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700314 if (touch_screen_allowed_) {
315 if (ev.code == BTN_TOUCH) {
316 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
317 // lifting the contact.
318 touch_finger_down_ = (ev.value == 1);
319 }
320
321 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
322 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
323 // KEY_POWER and KEY_UP as KEY_DOWN).
324 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
325 return 0;
326 }
327 }
328
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700329 ProcessKey(ev.code, ev.value);
330 }
331
332 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700333}
334
Tao Bao26ea9592018-05-09 16:32:02 -0700335// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
336// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
337// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
338// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700339//
Tao Bao26ea9592018-05-09 16:32:02 -0700340// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
341// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700342//
343// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700344void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700345 bool register_key = false;
346 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800347
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700348 {
349 std::lock_guard<std::mutex> lg(key_queue_mutex);
350 key_pressed[key_code] = updown;
351 if (updown) {
352 ++key_down_count;
353 key_last_down = key_code;
354 key_long_press = false;
355 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
356 time_key_thread.detach();
357 } else {
358 if (key_last_down == key_code) {
359 long_press = key_long_press;
360 register_key = true;
361 }
362 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700363 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700364 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700365
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700366 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700367 if (register_key) {
368 switch (CheckKey(key_code, long_press)) {
369 case RecoveryUI::IGNORE:
370 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800371
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700372 case RecoveryUI::TOGGLE:
373 ShowText(!IsTextVisible());
374 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700375
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700376 case RecoveryUI::REBOOT:
377 if (reboot_enabled) {
Mark Salyzyn488cc052019-05-20 10:36:16 -0700378 Reboot("userrequested,recovery,ui");
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700379 while (true) {
380 pause();
381 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700382 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700383 break;
384
385 case RecoveryUI::ENQUEUE:
386 EnqueueKey(key_code);
387 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700388 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700389 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700390}
391
Tao Bao26ea9592018-05-09 16:32:02 -0700392void RecoveryUI::TimeKey(int key_code, int count) {
393 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700394 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700395 {
396 std::lock_guard<std::mutex> lg(key_queue_mutex);
397 if (key_last_down == key_code && key_down_count == count) {
398 long_press = key_long_press = true;
399 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700400 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700401 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700402}
403
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800404void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700405 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700406 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
407 if (key_queue_len < queue_max) {
408 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700409 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700410 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800411}
412
Jerry Zhangb76af932018-05-22 12:08:35 -0700413void RecoveryUI::SetScreensaverState(ScreensaverState state) {
414 switch (state) {
415 case ScreensaverState::NORMAL:
416 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
417 brightness_file_)) {
418 screensaver_state_ = ScreensaverState::NORMAL;
419 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
420 << "%)";
421 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800422 LOG(WARNING) << "Unable to set brightness to normal";
Jerry Zhangb76af932018-05-22 12:08:35 -0700423 }
424 break;
425 case ScreensaverState::DIMMED:
426 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
427 brightness_file_)) {
428 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
429 << "%)";
430 screensaver_state_ = ScreensaverState::DIMMED;
431 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800432 LOG(WARNING) << "Unable to set brightness to dim";
Jerry Zhangb76af932018-05-22 12:08:35 -0700433 }
434 break;
435 case ScreensaverState::OFF:
436 if (android::base::WriteStringToFile("0", brightness_file_)) {
437 LOG(INFO) << "Brightness: 0 (off)";
438 screensaver_state_ = ScreensaverState::OFF;
439 } else {
Zhang, GaofengX852d9fe2019-06-11 11:16:30 +0800440 LOG(WARNING) << "Unable to set brightness to off";
Jerry Zhangb76af932018-05-22 12:08:35 -0700441 }
442 break;
443 default:
444 LOG(ERROR) << "Invalid screensaver state";
445 }
446}
447
Elliott Hughesec283402015-04-10 10:01:53 -0700448int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700449 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700450
Jerry Zhangb76af932018-05-22 12:08:35 -0700451 // Check for a saved key queue interruption.
452 if (key_interrupted_) {
453 SetScreensaverState(ScreensaverState::NORMAL);
454 return static_cast<int>(KeyError::INTERRUPTED);
455 }
456
Tao Bao53be3322018-08-16 11:48:50 -0700457 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in.
Tao Bao6278bdf2017-01-16 17:38:18 -0800458 do {
Jerry Zhangb76af932018-05-22 12:08:35 -0700459 bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] {
460 return this->key_queue_len != 0 || key_interrupted_;
461 });
462 if (key_interrupted_) {
463 SetScreensaverState(ScreensaverState::NORMAL);
464 return static_cast<int>(KeyError::INTERRUPTED);
Doug Zongker32a0a472011-11-01 11:00:20 -0700465 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800466 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700467 if (!rc) {
Tao Bao53be3322018-08-16 11:48:50 -0700468 // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
Tao Bao6278bdf2017-01-16 17:38:18 -0800469 if (screensaver_state_ == ScreensaverState::NORMAL) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700470 SetScreensaverState(ScreensaverState::DIMMED);
Tao Bao6278bdf2017-01-16 17:38:18 -0800471 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
Jerry Zhangb76af932018-05-22 12:08:35 -0700472 SetScreensaverState(ScreensaverState::OFF);
Tao Bao6278bdf2017-01-16 17:38:18 -0800473 }
Tao Bao53be3322018-08-16 11:48:50 -0700474 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800475 // Drop the first key if it's changing from OFF to NORMAL.
476 if (screensaver_state_ == ScreensaverState::OFF) {
477 if (key_queue_len > 0) {
478 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
479 }
480 }
481
482 // Reset the brightness to normal.
Jerry Zhangb76af932018-05-22 12:08:35 -0700483 SetScreensaverState(ScreensaverState::NORMAL);
Tao Bao6278bdf2017-01-16 17:38:18 -0800484 }
485 }
486 } while (IsUsbConnected() && key_queue_len == 0);
487
Jerry Zhangb76af932018-05-22 12:08:35 -0700488 int key = static_cast<int>(KeyError::TIMED_OUT);
Tao Bao6278bdf2017-01-16 17:38:18 -0800489 if (key_queue_len > 0) {
490 key = key_queue[0];
491 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
492 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800493 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700494}
495
Jerry Zhangb76af932018-05-22 12:08:35 -0700496void RecoveryUI::InterruptKey() {
497 {
498 std::lock_guard<std::mutex> lg(key_queue_mutex);
499 key_interrupted_ = true;
500 }
501 key_queue_cond.notify_one();
502}
503
Elliott Hughes985022a2015-04-13 13:04:32 -0700504bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700505 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
506 if (fd < 0) {
507 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
508 return 0;
509 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700510
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700511 char buf;
512 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
513 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
514 if (close(fd) < 0) {
515 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
516 }
517 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700518}
519
Elliott Hughesec283402015-04-10 10:01:53 -0700520bool RecoveryUI::IsKeyPressed(int key) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700521 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700522 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700523 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700524}
525
Elliott Hughes642aaa72015-04-10 12:47:46 -0700526bool RecoveryUI::IsLongPress() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700527 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700528 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700529 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700530}
531
Elliott Hughes4af215b2015-04-10 15:00:34 -0700532bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700533 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700534}
535
Tao Bao5f8dd992017-07-28 00:05:40 -0700536bool RecoveryUI::HasPowerKey() const {
537 return has_power_key;
538}
539
540bool RecoveryUI::HasTouchScreen() const {
541 return has_touch_screen;
542}
543
Doug Zongker32a0a472011-11-01 11:00:20 -0700544void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700545 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700546 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700547}
548
Elliott Hughes642aaa72015-04-10 12:47:46 -0700549RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700550 {
551 std::lock_guard<std::mutex> lg(key_queue_mutex);
552 key_long_press = false;
553 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700554
555 // If we have power and volume up keys, that chord is the signal to toggle the text display.
Tao Bao5f8dd992017-07-28 00:05:40 -0700556 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
557 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700558 return TOGGLE;
559 }
560 } else {
561 // Otherwise long press of any button toggles to the text display,
562 // and there's no way to toggle back (but that's pretty useless anyway).
563 if (is_long_press && !IsTextVisible()) {
564 return TOGGLE;
565 }
566
567 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
568 if (is_long_press && IsTextVisible()) {
569 EnqueueKey(KEY_ENTER);
570 return IGNORE;
571 }
572 }
573
574 // Press power seven times in a row to reboot.
575 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700576 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700577
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700578 if (reboot_enabled) {
579 ++consecutive_power_keys;
580 if (consecutive_power_keys >= 7) {
581 return REBOOT;
582 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700583 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700584 } else {
585 consecutive_power_keys = 0;
586 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700587
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700588 last_key = key;
589 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700590}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800591
Tianjie Xu8f397302018-08-20 13:40:47 -0700592void RecoveryUI::KeyLongPress(int) {}
Doug Zongkerc704e062014-05-23 08:40:35 -0700593
594void RecoveryUI::SetEnableReboot(bool enabled) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700595 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700596 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700597}