blob: 6c91d01b87c556593166e0d1f47567f32d26d327 [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
Tao Bao736d59c2017-01-03 10:15:33 -080017#include "ui.h"
18
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 Bao6278bdf2017-01-16 17:38:18 -080037#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070038
Tao Bao26ea9592018-05-09 16:32:02 -070039#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070040#include "otautil/sysutil.h"
41#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070042
Tao Bao26ea9592018-05-09 16:32:02 -070043using namespace std::chrono_literals;
44
Tao Bao6278bdf2017-01-16 17:38:18 -080045static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
46static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
47static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
kataoc35c1b02017-12-08 11:02:43 +080048static constexpr const char* BRIGHTNESS_FILE_SDM =
49 "/sys/class/backlight/panel0-backlight/brightness";
50static constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
51 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070052
Elliott Hughesec283402015-04-10 10:01:53 -070053RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080054 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080055 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080056 brightness_file_(BRIGHTNESS_FILE),
57 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tao Bao5f8dd992017-07-28 00:05:40 -070058 touch_screen_allowed_(false),
59 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
60 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Tao Bao736d59c2017-01-03 10:15:33 -080061 key_queue_len(0),
62 key_last_down(-1),
63 key_long_press(false),
64 key_down_count(0),
65 enable_reboot(true),
66 consecutive_power_keys(0),
67 last_key(-1),
68 has_power_key(false),
69 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080070 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070071 has_touch_screen(false),
72 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070073 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080074 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080075 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070076}
77
Tao Bao26ea9592018-05-09 16:32:02 -070078RecoveryUI::~RecoveryUI() {
79 ev_exit();
80 input_thread_stopped_ = true;
Tao Bao94371fd2018-06-06 07:38:54 -070081 if (input_thread_.joinable()) {
82 input_thread_.join();
83 }
Tao Bao26ea9592018-05-09 16:32:02 -070084}
85
Elliott Hughes642aaa72015-04-10 12:47:46 -070086void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070087 if (key_code == KEY_POWER) {
88 has_power_key = true;
89 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
90 has_down_key = true;
91 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
92 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070093 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
94 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070096}
97
Tao Bao6278bdf2017-01-16 17:38:18 -080098bool RecoveryUI::InitScreensaver() {
99 // Disabled.
100 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
101 return false;
102 }
kataoc35c1b02017-12-08 11:02:43 +0800103 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
104 brightness_file_ = BRIGHTNESS_FILE_SDM;
105 }
106 if (access(max_brightness_file_.c_str(), R_OK)) {
107 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
108 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800109 // Set the initial brightness level based on the max brightness. Note that reading the initial
110 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
111 // we don't have a good way to query the default value.
112 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800113 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800114 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800115 return false;
116 }
117
118 unsigned int max_value;
119 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
120 LOG(WARNING) << "Failed to parse max brightness: " << content;
121 return false;
122 }
123
124 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
125 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
126 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800127 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800128 PLOG(WARNING) << "Failed to set brightness";
129 return false;
130 }
131
132 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
133 screensaver_state_ = ScreensaverState::NORMAL;
134 return true;
135}
136
Tao Baoefb49ad2017-01-31 23:03:10 -0800137bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700138 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
139 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700140
Tao Bao736d59c2017-01-03 10:15:33 -0800141 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
142
Tao Bao5f8dd992017-07-28 00:05:40 -0700143 if (touch_screen_allowed_) {
144 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700145
146 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
147 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
148 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
149 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
150 // mode will be turned on automatically on debuggable builds, even without a swipe.
151 std::string cmdline;
152 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
153 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
154 } else {
155 // Non-fatal, and won't affect Init() result.
156 PLOG(WARNING) << "Failed to read /proc/cmdline";
157 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700158 }
159
Tao Bao6278bdf2017-01-16 17:38:18 -0800160 if (!InitScreensaver()) {
161 LOG(INFO) << "Screensaver disabled";
162 }
163
Tao Bao26ea9592018-05-09 16:32:02 -0700164 // Create a separate thread that handles input events.
165 input_thread_ = std::thread([this]() {
166 while (!this->input_thread_stopped_) {
167 if (!ev_wait(500)) {
168 ev_dispatch();
169 }
170 }
171 });
172
Tao Bao736d59c2017-01-03 10:15:33 -0800173 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700174}
175
Tao Bao5f8dd992017-07-28 00:05:40 -0700176void RecoveryUI::OnTouchDetected(int dx, int dy) {
177 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
178
179 // We only consider a valid swipe if:
180 // - the delta along one axis is below kTouchLowThreshold;
181 // - and the delta along the other axis is beyond kTouchHighThreshold.
182 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
183 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
184 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
185 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
186 } else {
187 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
188 << ", high: " << kTouchHighThreshold << ")";
189 return;
190 }
191
Tao Bao046aae22017-07-31 23:15:09 -0700192 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
193 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
194 ShowText(true);
195 return;
196 }
197
Tao Bao5f8dd992017-07-28 00:05:40 -0700198 LOG(DEBUG) << "Swipe direction=" << direction;
199 switch (direction) {
200 case SwipeDirection::UP:
201 ProcessKey(KEY_UP, 1); // press up key
202 ProcessKey(KEY_UP, 0); // and release it
203 break;
204
205 case SwipeDirection::DOWN:
206 ProcessKey(KEY_DOWN, 1); // press down key
207 ProcessKey(KEY_DOWN, 0); // and release it
208 break;
209
210 case SwipeDirection::LEFT:
211 case SwipeDirection::RIGHT:
212 ProcessKey(KEY_POWER, 1); // press power key
213 ProcessKey(KEY_POWER, 0); // and release it
214 break;
215 };
216}
217
Elliott Hughes985022a2015-04-13 13:04:32 -0700218int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700219 struct input_event ev;
220 if (ev_get_input(fd, epevents, &ev) == -1) {
221 return -1;
222 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700223
Tao Bao5f8dd992017-07-28 00:05:40 -0700224 // Touch inputs handling.
225 //
226 // We handle the touch inputs by tracking the position changes between initial contacting and
227 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
228 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
229 //
230 // Per the doc Multi-touch Protocol at below, there are two protocols.
231 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
232 //
233 // The main difference between the stateless type A protocol and the stateful type B slot protocol
234 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
235 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
236 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
237 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
238 //
239 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
240 // ABS_MT_TRACKING_ID being -1.
241 //
242 // Touch input events will only be available if touch_screen_allowed_ is set.
243
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700244 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700245 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
246 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
247 // contact.
248 if (touch_finger_down_ && !touch_swiping_) {
249 touch_start_X_ = touch_X_;
250 touch_start_Y_ = touch_Y_;
251 touch_swiping_ = true;
252 } else if (!touch_finger_down_ && touch_swiping_) {
253 touch_swiping_ = false;
254 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
255 }
256 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700257 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700258 }
259
260 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700261 if (ev.code == REL_Y) {
262 // accumulate the up or down motion reported by
263 // the trackball. When it exceeds a threshold
264 // (positive or negative), fake an up/down
265 // key event.
266 rel_sum += ev.value;
267 if (rel_sum > 3) {
268 ProcessKey(KEY_DOWN, 1); // press down key
269 ProcessKey(KEY_DOWN, 0); // and release it
270 rel_sum = 0;
271 } else if (rel_sum < -3) {
272 ProcessKey(KEY_UP, 1); // press up key
273 ProcessKey(KEY_UP, 0); // and release it
274 rel_sum = 0;
275 }
276 }
277 } else {
278 rel_sum = 0;
279 }
280
Tao Bao5f8dd992017-07-28 00:05:40 -0700281 if (touch_screen_allowed_ && ev.type == EV_ABS) {
282 if (ev.code == ABS_MT_SLOT) {
283 touch_slot_ = ev.value;
284 }
285 // Ignore other fingers.
286 if (touch_slot_ > 0) return 0;
287
288 switch (ev.code) {
289 case ABS_MT_POSITION_X:
290 touch_X_ = ev.value;
291 touch_finger_down_ = true;
292 break;
293
294 case ABS_MT_POSITION_Y:
295 touch_Y_ = ev.value;
296 touch_finger_down_ = true;
297 break;
298
299 case ABS_MT_TRACKING_ID:
300 // Protocol B: -1 marks lifting the contact.
301 if (ev.value < 0) touch_finger_down_ = false;
302 break;
303 }
304 return 0;
305 }
306
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700307 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700308 if (touch_screen_allowed_) {
309 if (ev.code == BTN_TOUCH) {
310 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
311 // lifting the contact.
312 touch_finger_down_ = (ev.value == 1);
313 }
314
315 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
316 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
317 // KEY_POWER and KEY_UP as KEY_DOWN).
318 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
319 return 0;
320 }
321 }
322
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700323 ProcessKey(ev.code, ev.value);
324 }
325
326 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700327}
328
Tao Bao26ea9592018-05-09 16:32:02 -0700329// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
330// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
331// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
332// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700333//
Tao Bao26ea9592018-05-09 16:32:02 -0700334// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
335// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700336//
337// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700338void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700339 bool register_key = false;
340 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800341
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700342 {
343 std::lock_guard<std::mutex> lg(key_queue_mutex);
344 key_pressed[key_code] = updown;
345 if (updown) {
346 ++key_down_count;
347 key_last_down = key_code;
348 key_long_press = false;
349 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
350 time_key_thread.detach();
351 } else {
352 if (key_last_down == key_code) {
353 long_press = key_long_press;
354 register_key = true;
355 }
356 key_last_down = -1;
Doug Zongker32a0a472011-11-01 11:00:20 -0700357 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700358 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700359
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700360 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700361 if (register_key) {
362 switch (CheckKey(key_code, long_press)) {
363 case RecoveryUI::IGNORE:
364 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800365
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700366 case RecoveryUI::TOGGLE:
367 ShowText(!IsTextVisible());
368 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700369
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700370 case RecoveryUI::REBOOT:
371 if (reboot_enabled) {
372 reboot("reboot,");
373 while (true) {
374 pause();
375 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700376 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700377 break;
378
379 case RecoveryUI::ENQUEUE:
380 EnqueueKey(key_code);
381 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700382 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700383 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700384}
385
Tao Bao26ea9592018-05-09 16:32:02 -0700386void RecoveryUI::TimeKey(int key_code, int count) {
387 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700388 bool long_press = false;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700389 {
390 std::lock_guard<std::mutex> lg(key_queue_mutex);
391 if (key_last_down == key_code && key_down_count == count) {
392 long_press = key_long_press = true;
393 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700394 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700395 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700396}
397
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800398void RecoveryUI::EnqueueKey(int key_code) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700399 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700400 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
401 if (key_queue_len < queue_max) {
402 key_queue[key_queue_len++] = key_code;
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700403 key_queue_cond.notify_one();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700404 }
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800405}
406
Elliott Hughesec283402015-04-10 10:01:53 -0700407int RecoveryUI::WaitKey() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700408 std::unique_lock<std::mutex> lk(key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700409
Tao Bao6278bdf2017-01-16 17:38:18 -0800410 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
411 // plugged in.
412 do {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700413 std::cv_status rc = std::cv_status::no_timeout;
414 while (key_queue_len == 0 && rc != std::cv_status::timeout) {
415 rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC));
Doug Zongker32a0a472011-11-01 11:00:20 -0700416 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800417
418 if (screensaver_state_ != ScreensaverState::DISABLED) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700419 if (rc == std::cv_status::timeout) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800420 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
421 if (screensaver_state_ == ScreensaverState::NORMAL) {
422 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
kataoc35c1b02017-12-08 11:02:43 +0800423 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800424 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
425 << "%)";
426 screensaver_state_ = ScreensaverState::DIMMED;
427 }
428 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
kataoc35c1b02017-12-08 11:02:43 +0800429 if (android::base::WriteStringToFile("0", brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800430 LOG(INFO) << "Brightness: 0 (off)";
431 screensaver_state_ = ScreensaverState::OFF;
432 }
433 }
434 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
435 // Drop the first key if it's changing from OFF to NORMAL.
436 if (screensaver_state_ == ScreensaverState::OFF) {
437 if (key_queue_len > 0) {
438 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
439 }
440 }
441
442 // Reset the brightness to normal.
443 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800444 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800445 screensaver_state_ = ScreensaverState::NORMAL;
446 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
447 << "%)";
448 }
449 }
450 }
451 } while (IsUsbConnected() && key_queue_len == 0);
452
453 int key = -1;
454 if (key_queue_len > 0) {
455 key = key_queue[0];
456 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
457 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800458 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700459}
460
Elliott Hughes985022a2015-04-13 13:04:32 -0700461bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700462 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
463 if (fd < 0) {
464 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
465 return 0;
466 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700467
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700468 char buf;
469 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
470 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
471 if (close(fd) < 0) {
472 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
473 }
474 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700475}
476
Elliott Hughesec283402015-04-10 10:01:53 -0700477bool RecoveryUI::IsKeyPressed(int key) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700478 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700479 int pressed = key_pressed[key];
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700480 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700481}
482
Elliott Hughes642aaa72015-04-10 12:47:46 -0700483bool RecoveryUI::IsLongPress() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700484 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700485 bool result = key_long_press;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700486 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700487}
488
Elliott Hughes4af215b2015-04-10 15:00:34 -0700489bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700490 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700491}
492
Tao Bao5f8dd992017-07-28 00:05:40 -0700493bool RecoveryUI::HasPowerKey() const {
494 return has_power_key;
495}
496
497bool RecoveryUI::HasTouchScreen() const {
498 return has_touch_screen;
499}
500
Doug Zongker32a0a472011-11-01 11:00:20 -0700501void RecoveryUI::FlushKeys() {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700502 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700503 key_queue_len = 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700504}
505
Elliott Hughes642aaa72015-04-10 12:47:46 -0700506RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700507 {
508 std::lock_guard<std::mutex> lg(key_queue_mutex);
509 key_long_press = false;
510 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700511
512 // 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 -0700513 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
514 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700515 return TOGGLE;
516 }
517 } else {
518 // Otherwise long press of any button toggles to the text display,
519 // and there's no way to toggle back (but that's pretty useless anyway).
520 if (is_long_press && !IsTextVisible()) {
521 return TOGGLE;
522 }
523
524 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
525 if (is_long_press && IsTextVisible()) {
526 EnqueueKey(KEY_ENTER);
527 return IGNORE;
528 }
529 }
530
531 // Press power seven times in a row to reboot.
532 if (key == KEY_POWER) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700533 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700534
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700535 if (reboot_enabled) {
536 ++consecutive_power_keys;
537 if (consecutive_power_keys >= 7) {
538 return REBOOT;
539 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700540 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700541 } else {
542 consecutive_power_keys = 0;
543 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700544
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700545 last_key = key;
546 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700547}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800548
Elliott Hughes642aaa72015-04-10 12:47:46 -0700549void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700550}
Doug Zongkerc704e062014-05-23 08:40:35 -0700551
552void RecoveryUI::SetEnableReboot(bool enabled) {
Jerry Zhangb31f9ce2018-05-21 16:04:57 -0700553 std::lock_guard<std::mutex> lg(key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700554 enable_reboot = enabled;
Doug Zongkerc704e062014-05-23 08:40:35 -0700555}