blob: dbe77ae8c817f0797a61d932ac01176fcfa6a23c [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>
21#include <linux/input.h>
22#include <pthread.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070026#include <sys/time.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
Tao Bao26ea9592018-05-09 16:32:02 -070031#include <chrono>
Tao Bao9468fc02017-03-17 00:57:37 -070032#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080033#include <string>
Tao Bao26ea9592018-05-09 16:32:02 -070034#include <thread>
Tao Bao736d59c2017-01-03 10:15:33 -080035
Tao Bao6278bdf2017-01-16 17:38:18 -080036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/parseint.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080039#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070040
Tao Bao26ea9592018-05-09 16:32:02 -070041#include "minui/minui.h"
Tao Bao2c526392018-05-03 23:01:13 -070042#include "otautil/sysutil.h"
43#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070044
Tao Bao26ea9592018-05-09 16:32:02 -070045using namespace std::chrono_literals;
46
Tao Bao6278bdf2017-01-16 17:38:18 -080047static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
48static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
49static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
kataoc35c1b02017-12-08 11:02:43 +080050static constexpr const char* BRIGHTNESS_FILE_SDM =
51 "/sys/class/backlight/panel0-backlight/brightness";
52static constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
53 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070054
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),
61 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
62 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Tao Bao736d59c2017-01-03 10:15:33 -080063 key_queue_len(0),
64 key_last_down(-1),
65 key_long_press(false),
66 key_down_count(0),
67 enable_reboot(true),
68 consecutive_power_keys(0),
69 last_key(-1),
70 has_power_key(false),
71 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080072 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070073 has_touch_screen(false),
74 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070075 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080076 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080077 pthread_mutex_init(&key_queue_mutex, nullptr);
78 pthread_cond_init(&key_queue_cond, nullptr);
79 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070080}
81
Tao Bao26ea9592018-05-09 16:32:02 -070082RecoveryUI::~RecoveryUI() {
83 ev_exit();
84 input_thread_stopped_ = true;
85 input_thread_.join();
86}
87
Elliott Hughes642aaa72015-04-10 12:47:46 -070088void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070089 if (key_code == KEY_POWER) {
90 has_power_key = true;
91 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
92 has_down_key = true;
93 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
94 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070095 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
96 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070097 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070098}
99
Tao Bao6278bdf2017-01-16 17:38:18 -0800100bool RecoveryUI::InitScreensaver() {
101 // Disabled.
102 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
103 return false;
104 }
kataoc35c1b02017-12-08 11:02:43 +0800105 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
106 brightness_file_ = BRIGHTNESS_FILE_SDM;
107 }
108 if (access(max_brightness_file_.c_str(), R_OK)) {
109 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
110 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800111 // Set the initial brightness level based on the max brightness. Note that reading the initial
112 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
113 // we don't have a good way to query the default value.
114 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800115 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800116 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800117 return false;
118 }
119
120 unsigned int max_value;
121 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
122 LOG(WARNING) << "Failed to parse max brightness: " << content;
123 return false;
124 }
125
126 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
127 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
128 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800129 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800130 PLOG(WARNING) << "Failed to set brightness";
131 return false;
132 }
133
134 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
135 screensaver_state_ = ScreensaverState::NORMAL;
136 return true;
137}
138
Tao Baoefb49ad2017-01-31 23:03:10 -0800139bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700140 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
141 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700142
Tao Bao736d59c2017-01-03 10:15:33 -0800143 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
144
Tao Bao5f8dd992017-07-28 00:05:40 -0700145 if (touch_screen_allowed_) {
146 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700147
148 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
149 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
150 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
151 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
152 // mode will be turned on automatically on debuggable builds, even without a swipe.
153 std::string cmdline;
154 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
155 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
156 } else {
157 // Non-fatal, and won't affect Init() result.
158 PLOG(WARNING) << "Failed to read /proc/cmdline";
159 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700160 }
161
Tao Bao6278bdf2017-01-16 17:38:18 -0800162 if (!InitScreensaver()) {
163 LOG(INFO) << "Screensaver disabled";
164 }
165
Tao Bao26ea9592018-05-09 16:32:02 -0700166 // Create a separate thread that handles input events.
167 input_thread_ = std::thread([this]() {
168 while (!this->input_thread_stopped_) {
169 if (!ev_wait(500)) {
170 ev_dispatch();
171 }
172 }
173 });
174
Tao Bao736d59c2017-01-03 10:15:33 -0800175 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700176}
177
Tao Bao5f8dd992017-07-28 00:05:40 -0700178void RecoveryUI::OnTouchDetected(int dx, int dy) {
179 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
180
181 // We only consider a valid swipe if:
182 // - the delta along one axis is below kTouchLowThreshold;
183 // - and the delta along the other axis is beyond kTouchHighThreshold.
184 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
185 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
186 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
187 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
188 } else {
189 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
190 << ", high: " << kTouchHighThreshold << ")";
191 return;
192 }
193
Tao Bao046aae22017-07-31 23:15:09 -0700194 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
195 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
196 ShowText(true);
197 return;
198 }
199
Tao Bao5f8dd992017-07-28 00:05:40 -0700200 LOG(DEBUG) << "Swipe direction=" << direction;
201 switch (direction) {
202 case SwipeDirection::UP:
203 ProcessKey(KEY_UP, 1); // press up key
204 ProcessKey(KEY_UP, 0); // and release it
205 break;
206
207 case SwipeDirection::DOWN:
208 ProcessKey(KEY_DOWN, 1); // press down key
209 ProcessKey(KEY_DOWN, 0); // and release it
210 break;
211
212 case SwipeDirection::LEFT:
213 case SwipeDirection::RIGHT:
214 ProcessKey(KEY_POWER, 1); // press power key
215 ProcessKey(KEY_POWER, 0); // and release it
216 break;
217 };
218}
219
Elliott Hughes985022a2015-04-13 13:04:32 -0700220int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700221 struct input_event ev;
222 if (ev_get_input(fd, epevents, &ev) == -1) {
223 return -1;
224 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700225
Tao Bao5f8dd992017-07-28 00:05:40 -0700226 // Touch inputs handling.
227 //
228 // We handle the touch inputs by tracking the position changes between initial contacting and
229 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
230 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
231 //
232 // Per the doc Multi-touch Protocol at below, there are two protocols.
233 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
234 //
235 // The main difference between the stateless type A protocol and the stateful type B slot protocol
236 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
237 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
238 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
239 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
240 //
241 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
242 // ABS_MT_TRACKING_ID being -1.
243 //
244 // Touch input events will only be available if touch_screen_allowed_ is set.
245
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700246 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700247 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
248 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
249 // contact.
250 if (touch_finger_down_ && !touch_swiping_) {
251 touch_start_X_ = touch_X_;
252 touch_start_Y_ = touch_Y_;
253 touch_swiping_ = true;
254 } else if (!touch_finger_down_ && touch_swiping_) {
255 touch_swiping_ = false;
256 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
257 }
258 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700259 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700260 }
261
262 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700263 if (ev.code == REL_Y) {
264 // accumulate the up or down motion reported by
265 // the trackball. When it exceeds a threshold
266 // (positive or negative), fake an up/down
267 // key event.
268 rel_sum += ev.value;
269 if (rel_sum > 3) {
270 ProcessKey(KEY_DOWN, 1); // press down key
271 ProcessKey(KEY_DOWN, 0); // and release it
272 rel_sum = 0;
273 } else if (rel_sum < -3) {
274 ProcessKey(KEY_UP, 1); // press up key
275 ProcessKey(KEY_UP, 0); // and release it
276 rel_sum = 0;
277 }
278 }
279 } else {
280 rel_sum = 0;
281 }
282
Tao Bao5f8dd992017-07-28 00:05:40 -0700283 if (touch_screen_allowed_ && ev.type == EV_ABS) {
284 if (ev.code == ABS_MT_SLOT) {
285 touch_slot_ = ev.value;
286 }
287 // Ignore other fingers.
288 if (touch_slot_ > 0) return 0;
289
290 switch (ev.code) {
291 case ABS_MT_POSITION_X:
292 touch_X_ = ev.value;
293 touch_finger_down_ = true;
294 break;
295
296 case ABS_MT_POSITION_Y:
297 touch_Y_ = ev.value;
298 touch_finger_down_ = true;
299 break;
300
301 case ABS_MT_TRACKING_ID:
302 // Protocol B: -1 marks lifting the contact.
303 if (ev.value < 0) touch_finger_down_ = false;
304 break;
305 }
306 return 0;
307 }
308
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700309 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700310 if (touch_screen_allowed_) {
311 if (ev.code == BTN_TOUCH) {
312 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
313 // lifting the contact.
314 touch_finger_down_ = (ev.value == 1);
315 }
316
317 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
318 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
319 // KEY_POWER and KEY_UP as KEY_DOWN).
320 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
321 return 0;
322 }
323 }
324
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700325 ProcessKey(ev.code, ev.value);
326 }
327
328 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700329}
330
Tao Bao26ea9592018-05-09 16:32:02 -0700331// Processes a key-up or -down event. A key is "registered" when it is pressed and then released,
332// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to
333// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed
334// next time the foreground thread wants a key (eg, for the menu).
Doug Zongker32a0a472011-11-01 11:00:20 -0700335//
Tao Bao26ea9592018-05-09 16:32:02 -0700336// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed()
337// to see what other keys are held when a key is registered.
Doug Zongker32a0a472011-11-01 11:00:20 -0700338//
339// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700340void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700341 bool register_key = false;
342 bool long_press = false;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800343
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700344 pthread_mutex_lock(&key_queue_mutex);
345 key_pressed[key_code] = updown;
346 if (updown) {
347 ++key_down_count;
348 key_last_down = key_code;
349 key_long_press = false;
Tao Bao26ea9592018-05-09 16:32:02 -0700350
351 std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count);
352 time_key_thread.detach();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700353 } else {
354 if (key_last_down == key_code) {
355 long_press = key_long_press;
356 register_key = true;
Doug Zongker32a0a472011-11-01 11:00:20 -0700357 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700358 key_last_down = -1;
359 }
Tao Bao26ea9592018-05-09 16:32:02 -0700360 bool reboot_enabled = enable_reboot;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700361 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700362
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700363 if (register_key) {
364 switch (CheckKey(key_code, long_press)) {
365 case RecoveryUI::IGNORE:
366 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800367
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700368 case RecoveryUI::TOGGLE:
369 ShowText(!IsTextVisible());
370 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700371
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700372 case RecoveryUI::REBOOT:
373 if (reboot_enabled) {
374 reboot("reboot,");
375 while (true) {
376 pause();
377 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700378 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700379 break;
380
381 case RecoveryUI::ENQUEUE:
382 EnqueueKey(key_code);
383 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700384 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700385 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700386}
387
Tao Bao26ea9592018-05-09 16:32:02 -0700388void RecoveryUI::TimeKey(int key_code, int count) {
389 std::this_thread::sleep_for(750ms); // 750 ms == "long"
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700390 bool long_press = false;
391 pthread_mutex_lock(&key_queue_mutex);
392 if (key_last_down == key_code && key_down_count == count) {
393 long_press = key_long_press = true;
394 }
395 pthread_mutex_unlock(&key_queue_mutex);
396 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700397}
398
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800399void RecoveryUI::EnqueueKey(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700400 pthread_mutex_lock(&key_queue_mutex);
401 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
402 if (key_queue_len < queue_max) {
403 key_queue[key_queue_len++] = key_code;
404 pthread_cond_signal(&key_queue_cond);
405 }
406 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800407}
408
Elliott Hughesec283402015-04-10 10:01:53 -0700409int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800410 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700411
Tao Bao6278bdf2017-01-16 17:38:18 -0800412 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
413 // plugged in.
414 do {
415 struct timeval now;
416 struct timespec timeout;
417 gettimeofday(&now, nullptr);
418 timeout.tv_sec = now.tv_sec;
419 timeout.tv_nsec = now.tv_usec * 1000;
420 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700421
Tao Bao6278bdf2017-01-16 17:38:18 -0800422 int rc = 0;
423 while (key_queue_len == 0 && rc != ETIMEDOUT) {
424 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700425 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800426
427 if (screensaver_state_ != ScreensaverState::DISABLED) {
428 if (rc == ETIMEDOUT) {
429 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
430 if (screensaver_state_ == ScreensaverState::NORMAL) {
431 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
kataoc35c1b02017-12-08 11:02:43 +0800432 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800433 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
434 << "%)";
435 screensaver_state_ = ScreensaverState::DIMMED;
436 }
437 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
kataoc35c1b02017-12-08 11:02:43 +0800438 if (android::base::WriteStringToFile("0", brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800439 LOG(INFO) << "Brightness: 0 (off)";
440 screensaver_state_ = ScreensaverState::OFF;
441 }
442 }
443 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
444 // Drop the first key if it's changing from OFF to NORMAL.
445 if (screensaver_state_ == ScreensaverState::OFF) {
446 if (key_queue_len > 0) {
447 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
448 }
449 }
450
451 // Reset the brightness to normal.
452 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800453 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800454 screensaver_state_ = ScreensaverState::NORMAL;
455 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
456 << "%)";
457 }
458 }
459 }
460 } while (IsUsbConnected() && key_queue_len == 0);
461
462 int key = -1;
463 if (key_queue_len > 0) {
464 key = key_queue[0];
465 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
466 }
467 pthread_mutex_unlock(&key_queue_mutex);
468 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700469}
470
Elliott Hughes985022a2015-04-13 13:04:32 -0700471bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700472 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
473 if (fd < 0) {
474 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
475 return 0;
476 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700477
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700478 char buf;
479 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
480 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
481 if (close(fd) < 0) {
482 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
483 }
484 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700485}
486
Elliott Hughesec283402015-04-10 10:01:53 -0700487bool RecoveryUI::IsKeyPressed(int key) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 pthread_mutex_lock(&key_queue_mutex);
489 int pressed = key_pressed[key];
490 pthread_mutex_unlock(&key_queue_mutex);
491 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700492}
493
Elliott Hughes642aaa72015-04-10 12:47:46 -0700494bool RecoveryUI::IsLongPress() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700495 pthread_mutex_lock(&key_queue_mutex);
496 bool result = key_long_press;
497 pthread_mutex_unlock(&key_queue_mutex);
498 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700499}
500
Elliott Hughes4af215b2015-04-10 15:00:34 -0700501bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700502 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700503}
504
Tao Bao5f8dd992017-07-28 00:05:40 -0700505bool RecoveryUI::HasPowerKey() const {
506 return has_power_key;
507}
508
509bool RecoveryUI::HasTouchScreen() const {
510 return has_touch_screen;
511}
512
Doug Zongker32a0a472011-11-01 11:00:20 -0700513void RecoveryUI::FlushKeys() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700514 pthread_mutex_lock(&key_queue_mutex);
515 key_queue_len = 0;
516 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700517}
518
Elliott Hughes642aaa72015-04-10 12:47:46 -0700519RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700520 pthread_mutex_lock(&key_queue_mutex);
521 key_long_press = false;
522 pthread_mutex_unlock(&key_queue_mutex);
523
524 // 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 -0700525 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
526 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700527 return TOGGLE;
528 }
529 } else {
530 // Otherwise long press of any button toggles to the text display,
531 // and there's no way to toggle back (but that's pretty useless anyway).
532 if (is_long_press && !IsTextVisible()) {
533 return TOGGLE;
534 }
535
536 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
537 if (is_long_press && IsTextVisible()) {
538 EnqueueKey(KEY_ENTER);
539 return IGNORE;
540 }
541 }
542
543 // Press power seven times in a row to reboot.
544 if (key == KEY_POWER) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700545 pthread_mutex_lock(&key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700546 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700547 pthread_mutex_unlock(&key_queue_mutex);
548
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700549 if (reboot_enabled) {
550 ++consecutive_power_keys;
551 if (consecutive_power_keys >= 7) {
552 return REBOOT;
553 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700554 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700555 } else {
556 consecutive_power_keys = 0;
557 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700558
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700559 last_key = key;
560 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700561}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800562
Elliott Hughes642aaa72015-04-10 12:47:46 -0700563void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700564}
Doug Zongkerc704e062014-05-23 08:40:35 -0700565
566void RecoveryUI::SetEnableReboot(bool enabled) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700567 pthread_mutex_lock(&key_queue_mutex);
568 enable_reboot = enabled;
569 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerc704e062014-05-23 08:40:35 -0700570}