blob: 8983d76928dfa86226f7920f0532c1bff8ff8ecc [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>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
Tao Bao9468fc02017-03-17 00:57:37 -070033#include <functional>
Tao Bao736d59c2017-01-03 10:15:33 -080034#include <string>
35
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>
Tao Bao0b1118d2017-01-14 07:46:10 -080040#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070041
Doug Zongker32a0a472011-11-01 11:00:20 -070042#include "device.h"
Tao Bao2c526392018-05-03 23:01:13 -070043#include "otautil/sysutil.h"
44#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070045
Tao Bao6278bdf2017-01-16 17:38:18 -080046static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
47static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
48static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
kataoc35c1b02017-12-08 11:02:43 +080049static constexpr const char* BRIGHTNESS_FILE_SDM =
50 "/sys/class/backlight/panel0-backlight/brightness";
51static constexpr const char* MAX_BRIGHTNESS_FILE_SDM =
52 "/sys/class/backlight/panel0-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070053
Elliott Hughesec283402015-04-10 10:01:53 -070054RecoveryUI::RecoveryUI()
Tao Baoefb49ad2017-01-31 23:03:10 -080055 : brightness_normal_(50),
Tao Bao6278bdf2017-01-16 17:38:18 -080056 brightness_dimmed_(25),
kataoc35c1b02017-12-08 11:02:43 +080057 brightness_file_(BRIGHTNESS_FILE),
58 max_brightness_file_(MAX_BRIGHTNESS_FILE),
Tao Bao5f8dd992017-07-28 00:05:40 -070059 touch_screen_allowed_(false),
60 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
61 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Tao Bao736d59c2017-01-03 10:15:33 -080062 key_queue_len(0),
63 key_last_down(-1),
64 key_long_press(false),
65 key_down_count(0),
66 enable_reboot(true),
67 consecutive_power_keys(0),
68 last_key(-1),
69 has_power_key(false),
70 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080071 has_down_key(false),
Tao Bao5f8dd992017-07-28 00:05:40 -070072 has_touch_screen(false),
73 touch_slot_(0),
Tao Bao046aae22017-07-31 23:15:09 -070074 is_bootreason_recovery_ui_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080075 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080076 pthread_mutex_init(&key_queue_mutex, nullptr);
77 pthread_cond_init(&key_queue_cond, nullptr);
78 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070079}
80
Elliott Hughes642aaa72015-04-10 12:47:46 -070081void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070082 if (key_code == KEY_POWER) {
83 has_power_key = true;
84 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
85 has_down_key = true;
86 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
87 has_up_key = true;
Tao Bao5f8dd992017-07-28 00:05:40 -070088 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
89 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070090 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070091}
92
Elliott Hughes985022a2015-04-13 13:04:32 -070093// Reads input events, handles special hot keys, and adds to the key queue.
94static void* InputThreadLoop(void*) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095 while (true) {
96 if (!ev_wait(-1)) {
97 ev_dispatch();
Elliott Hughes985022a2015-04-13 13:04:32 -070098 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070099 }
100 return nullptr;
Elliott Hughes985022a2015-04-13 13:04:32 -0700101}
102
Tao Bao6278bdf2017-01-16 17:38:18 -0800103bool RecoveryUI::InitScreensaver() {
104 // Disabled.
105 if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) {
106 return false;
107 }
kataoc35c1b02017-12-08 11:02:43 +0800108 if (access(brightness_file_.c_str(), R_OK | W_OK)) {
109 brightness_file_ = BRIGHTNESS_FILE_SDM;
110 }
111 if (access(max_brightness_file_.c_str(), R_OK)) {
112 max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM;
113 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800114 // Set the initial brightness level based on the max brightness. Note that reading the initial
115 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
116 // we don't have a good way to query the default value.
117 std::string content;
kataoc35c1b02017-12-08 11:02:43 +0800118 if (!android::base::ReadFileToString(max_brightness_file_, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800119 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800120 return false;
121 }
122
123 unsigned int max_value;
124 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
125 LOG(WARNING) << "Failed to parse max brightness: " << content;
126 return false;
127 }
128
129 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
130 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
131 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800132 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800133 PLOG(WARNING) << "Failed to set brightness";
134 return false;
135 }
136
137 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
138 screensaver_state_ = ScreensaverState::NORMAL;
139 return true;
140}
141
Tao Baoefb49ad2017-01-31 23:03:10 -0800142bool RecoveryUI::Init(const std::string& /* locale */) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700143 ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2),
144 touch_screen_allowed_);
Elliott Hughes985022a2015-04-13 13:04:32 -0700145
Tao Bao736d59c2017-01-03 10:15:33 -0800146 ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
147
Tao Bao5f8dd992017-07-28 00:05:40 -0700148 if (touch_screen_allowed_) {
149 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
Tao Bao046aae22017-07-31 23:15:09 -0700150
151 // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of
152 // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way
153 // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or
154 // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text
155 // mode will be turned on automatically on debuggable builds, even without a swipe.
156 std::string cmdline;
157 if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
158 is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos;
159 } else {
160 // Non-fatal, and won't affect Init() result.
161 PLOG(WARNING) << "Failed to read /proc/cmdline";
162 }
Tao Bao5f8dd992017-07-28 00:05:40 -0700163 }
164
Tao Bao6278bdf2017-01-16 17:38:18 -0800165 if (!InitScreensaver()) {
166 LOG(INFO) << "Screensaver disabled";
167 }
168
Tao Bao736d59c2017-01-03 10:15:33 -0800169 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
170 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700171}
172
Tao Bao5f8dd992017-07-28 00:05:40 -0700173void RecoveryUI::OnTouchDetected(int dx, int dy) {
174 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
175
176 // We only consider a valid swipe if:
177 // - the delta along one axis is below kTouchLowThreshold;
178 // - and the delta along the other axis is beyond kTouchHighThreshold.
179 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
180 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
181 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
182 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
183 } else {
184 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
185 << ", high: " << kTouchHighThreshold << ")";
186 return;
187 }
188
Tao Bao046aae22017-07-31 23:15:09 -0700189 // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui.
190 if (is_bootreason_recovery_ui_ && !IsTextVisible()) {
191 ShowText(true);
192 return;
193 }
194
Tao Bao5f8dd992017-07-28 00:05:40 -0700195 LOG(DEBUG) << "Swipe direction=" << direction;
196 switch (direction) {
197 case SwipeDirection::UP:
198 ProcessKey(KEY_UP, 1); // press up key
199 ProcessKey(KEY_UP, 0); // and release it
200 break;
201
202 case SwipeDirection::DOWN:
203 ProcessKey(KEY_DOWN, 1); // press down key
204 ProcessKey(KEY_DOWN, 0); // and release it
205 break;
206
207 case SwipeDirection::LEFT:
208 case SwipeDirection::RIGHT:
209 ProcessKey(KEY_POWER, 1); // press power key
210 ProcessKey(KEY_POWER, 0); // and release it
211 break;
212 };
213}
214
Elliott Hughes985022a2015-04-13 13:04:32 -0700215int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700216 struct input_event ev;
217 if (ev_get_input(fd, epevents, &ev) == -1) {
218 return -1;
219 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700220
Tao Bao5f8dd992017-07-28 00:05:40 -0700221 // Touch inputs handling.
222 //
223 // We handle the touch inputs by tracking the position changes between initial contacting and
224 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
225 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
226 //
227 // Per the doc Multi-touch Protocol at below, there are two protocols.
228 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
229 //
230 // The main difference between the stateless type A protocol and the stateful type B slot protocol
231 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
232 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
233 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
234 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
235 //
236 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
237 // ABS_MT_TRACKING_ID being -1.
238 //
239 // Touch input events will only be available if touch_screen_allowed_ is set.
240
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700241 if (ev.type == EV_SYN) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700242 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
243 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
244 // contact.
245 if (touch_finger_down_ && !touch_swiping_) {
246 touch_start_X_ = touch_X_;
247 touch_start_Y_ = touch_Y_;
248 touch_swiping_ = true;
249 } else if (!touch_finger_down_ && touch_swiping_) {
250 touch_swiping_ = false;
251 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
252 }
253 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700254 return 0;
Tao Bao5f8dd992017-07-28 00:05:40 -0700255 }
256
257 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700258 if (ev.code == REL_Y) {
259 // accumulate the up or down motion reported by
260 // the trackball. When it exceeds a threshold
261 // (positive or negative), fake an up/down
262 // key event.
263 rel_sum += ev.value;
264 if (rel_sum > 3) {
265 ProcessKey(KEY_DOWN, 1); // press down key
266 ProcessKey(KEY_DOWN, 0); // and release it
267 rel_sum = 0;
268 } else if (rel_sum < -3) {
269 ProcessKey(KEY_UP, 1); // press up key
270 ProcessKey(KEY_UP, 0); // and release it
271 rel_sum = 0;
272 }
273 }
274 } else {
275 rel_sum = 0;
276 }
277
Tao Bao5f8dd992017-07-28 00:05:40 -0700278 if (touch_screen_allowed_ && ev.type == EV_ABS) {
279 if (ev.code == ABS_MT_SLOT) {
280 touch_slot_ = ev.value;
281 }
282 // Ignore other fingers.
283 if (touch_slot_ > 0) return 0;
284
285 switch (ev.code) {
286 case ABS_MT_POSITION_X:
287 touch_X_ = ev.value;
288 touch_finger_down_ = true;
289 break;
290
291 case ABS_MT_POSITION_Y:
292 touch_Y_ = ev.value;
293 touch_finger_down_ = true;
294 break;
295
296 case ABS_MT_TRACKING_ID:
297 // Protocol B: -1 marks lifting the contact.
298 if (ev.value < 0) touch_finger_down_ = false;
299 break;
300 }
301 return 0;
302 }
303
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700304 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700305 if (touch_screen_allowed_) {
306 if (ev.code == BTN_TOUCH) {
307 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
308 // lifting the contact.
309 touch_finger_down_ = (ev.value == 1);
310 }
311
312 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
313 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
314 // KEY_POWER and KEY_UP as KEY_DOWN).
315 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
316 return 0;
317 }
318 }
319
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700320 ProcessKey(ev.code, ev.value);
321 }
322
323 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700324}
325
326// Process a key-up or -down event. A key is "registered" when it is
327// pressed and then released, with no other keypresses or releases in
328// between. Registered keys are passed to CheckKey() to see if it
329// should trigger a visibility toggle, an immediate reboot, or be
330// queued to be processed next time the foreground thread wants a key
331// (eg, for the menu).
332//
333// We also keep track of which keys are currently down so that
334// CheckKey can call IsKeyPressed to see what other keys are held when
335// a key is registered.
336//
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;
341 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800342
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700343 pthread_mutex_lock(&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 key_timer_t* info = new key_timer_t;
350 info->ui = this;
351 info->key_code = key_code;
352 info->count = key_down_count;
353 pthread_t thread;
354 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
355 pthread_detach(thread);
356 } else {
357 if (key_last_down == key_code) {
358 long_press = key_long_press;
359 register_key = true;
Doug Zongker32a0a472011-11-01 11:00:20 -0700360 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700361 key_last_down = -1;
362 }
363 reboot_enabled = enable_reboot;
364 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700365
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700366 if (register_key) {
367 switch (CheckKey(key_code, long_press)) {
368 case RecoveryUI::IGNORE:
369 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800370
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700371 case RecoveryUI::TOGGLE:
372 ShowText(!IsTextVisible());
373 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700374
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700375 case RecoveryUI::REBOOT:
376 if (reboot_enabled) {
377 reboot("reboot,");
378 while (true) {
379 pause();
380 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700381 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700382 break;
383
384 case RecoveryUI::ENQUEUE:
385 EnqueueKey(key_code);
386 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700387 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700388 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700389}
390
Doug Zongkerc0441d12013-07-31 11:28:24 -0700391void* RecoveryUI::time_key_helper(void* cookie) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700392 key_timer_t* info = static_cast<key_timer_t*>(cookie);
393 info->ui->time_key(info->key_code, info->count);
394 delete info;
395 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700396}
397
398void RecoveryUI::time_key(int key_code, int count) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700399 usleep(750000); // 750 ms == "long"
400 bool long_press = false;
401 pthread_mutex_lock(&key_queue_mutex);
402 if (key_last_down == key_code && key_down_count == count) {
403 long_press = key_long_press = true;
404 }
405 pthread_mutex_unlock(&key_queue_mutex);
406 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700407}
408
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800409void RecoveryUI::EnqueueKey(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700410 pthread_mutex_lock(&key_queue_mutex);
411 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
412 if (key_queue_len < queue_max) {
413 key_queue[key_queue_len++] = key_code;
414 pthread_cond_signal(&key_queue_cond);
415 }
416 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800417}
418
Elliott Hughesec283402015-04-10 10:01:53 -0700419int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800420 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700421
Tao Bao6278bdf2017-01-16 17:38:18 -0800422 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
423 // plugged in.
424 do {
425 struct timeval now;
426 struct timespec timeout;
427 gettimeofday(&now, nullptr);
428 timeout.tv_sec = now.tv_sec;
429 timeout.tv_nsec = now.tv_usec * 1000;
430 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700431
Tao Bao6278bdf2017-01-16 17:38:18 -0800432 int rc = 0;
433 while (key_queue_len == 0 && rc != ETIMEDOUT) {
434 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700435 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800436
437 if (screensaver_state_ != ScreensaverState::DISABLED) {
438 if (rc == ETIMEDOUT) {
439 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
440 if (screensaver_state_ == ScreensaverState::NORMAL) {
441 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
kataoc35c1b02017-12-08 11:02:43 +0800442 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800443 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
444 << "%)";
445 screensaver_state_ = ScreensaverState::DIMMED;
446 }
447 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
kataoc35c1b02017-12-08 11:02:43 +0800448 if (android::base::WriteStringToFile("0", brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800449 LOG(INFO) << "Brightness: 0 (off)";
450 screensaver_state_ = ScreensaverState::OFF;
451 }
452 }
453 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
454 // Drop the first key if it's changing from OFF to NORMAL.
455 if (screensaver_state_ == ScreensaverState::OFF) {
456 if (key_queue_len > 0) {
457 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
458 }
459 }
460
461 // Reset the brightness to normal.
462 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
kataoc35c1b02017-12-08 11:02:43 +0800463 brightness_file_)) {
Tao Bao6278bdf2017-01-16 17:38:18 -0800464 screensaver_state_ = ScreensaverState::NORMAL;
465 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
466 << "%)";
467 }
468 }
469 }
470 } while (IsUsbConnected() && key_queue_len == 0);
471
472 int key = -1;
473 if (key_queue_len > 0) {
474 key = key_queue[0];
475 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
476 }
477 pthread_mutex_unlock(&key_queue_mutex);
478 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700479}
480
Elliott Hughes985022a2015-04-13 13:04:32 -0700481bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700482 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
483 if (fd < 0) {
484 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
485 return 0;
486 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700487
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 char buf;
489 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
490 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
491 if (close(fd) < 0) {
492 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
493 }
494 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700495}
496
Elliott Hughesec283402015-04-10 10:01:53 -0700497bool RecoveryUI::IsKeyPressed(int key) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700498 pthread_mutex_lock(&key_queue_mutex);
499 int pressed = key_pressed[key];
500 pthread_mutex_unlock(&key_queue_mutex);
501 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700502}
503
Elliott Hughes642aaa72015-04-10 12:47:46 -0700504bool RecoveryUI::IsLongPress() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700505 pthread_mutex_lock(&key_queue_mutex);
506 bool result = key_long_press;
507 pthread_mutex_unlock(&key_queue_mutex);
508 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700509}
510
Elliott Hughes4af215b2015-04-10 15:00:34 -0700511bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700512 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700513}
514
Tao Bao5f8dd992017-07-28 00:05:40 -0700515bool RecoveryUI::HasPowerKey() const {
516 return has_power_key;
517}
518
519bool RecoveryUI::HasTouchScreen() const {
520 return has_touch_screen;
521}
522
Doug Zongker32a0a472011-11-01 11:00:20 -0700523void RecoveryUI::FlushKeys() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700524 pthread_mutex_lock(&key_queue_mutex);
525 key_queue_len = 0;
526 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700527}
528
Elliott Hughes642aaa72015-04-10 12:47:46 -0700529RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700530 pthread_mutex_lock(&key_queue_mutex);
531 key_long_press = false;
532 pthread_mutex_unlock(&key_queue_mutex);
533
534 // 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 -0700535 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
536 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700537 return TOGGLE;
538 }
539 } else {
540 // Otherwise long press of any button toggles to the text display,
541 // and there's no way to toggle back (but that's pretty useless anyway).
542 if (is_long_press && !IsTextVisible()) {
543 return TOGGLE;
544 }
545
546 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
547 if (is_long_press && IsTextVisible()) {
548 EnqueueKey(KEY_ENTER);
549 return IGNORE;
550 }
551 }
552
553 // Press power seven times in a row to reboot.
554 if (key == KEY_POWER) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700555 pthread_mutex_lock(&key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700556 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700557 pthread_mutex_unlock(&key_queue_mutex);
558
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700559 if (reboot_enabled) {
560 ++consecutive_power_keys;
561 if (consecutive_power_keys >= 7) {
562 return REBOOT;
563 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700564 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700565 } else {
566 consecutive_power_keys = 0;
567 }
Doug Zongker9e805d62013-09-04 13:44:38 -0700568
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700569 last_key = key;
570 return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
Doug Zongker32a0a472011-11-01 11:00:20 -0700571}
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800572
Elliott Hughes642aaa72015-04-10 12:47:46 -0700573void RecoveryUI::KeyLongPress(int) {
Doug Zongkerc0441d12013-07-31 11:28:24 -0700574}
Doug Zongkerc704e062014-05-23 08:40:35 -0700575
576void RecoveryUI::SetEnableReboot(bool enabled) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700577 pthread_mutex_lock(&key_queue_mutex);
578 enable_reboot = enabled;
579 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerc704e062014-05-23 08:40:35 -0700580}