blob: eadcdd43379e707cb71f5e4b95707ce538f1a720 [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>
Elliott Hughescb220402016-09-23 15:30:55 -070039#include <android-base/properties.h>
Tao Bao6278bdf2017-01-16 17:38:18 -080040#include <android-base/strings.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070041#include <cutils/android_reboot.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080042#include <minui/minui.h>
Doug Zongker32a0a472011-11-01 11:00:20 -070043
44#include "common.h"
Doug Zongker9e805d62013-09-04 13:44:38 -070045#include "roots.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070046#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070047
Tao Bao6278bdf2017-01-16 17:38:18 -080048static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120;
49static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness";
50static constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness";
Doug Zongker32a0a472011-11-01 11:00:20 -070051
Elliott Hughesec283402015-04-10 10:01:53 -070052RecoveryUI::RecoveryUI()
Tao Bao736d59c2017-01-03 10:15:33 -080053 : locale_(""),
54 rtl_locale_(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080055 brightness_normal_(50),
56 brightness_dimmed_(25),
Tao Baoaf9f8b42017-07-28 00:05:40 -070057 touch_screen_allowed_(false),
58 kTouchLowThreshold(RECOVERY_UI_TOUCH_LOW_THRESHOLD),
59 kTouchHighThreshold(RECOVERY_UI_TOUCH_HIGH_THRESHOLD),
Tao Bao736d59c2017-01-03 10:15:33 -080060 key_queue_len(0),
61 key_last_down(-1),
62 key_long_press(false),
63 key_down_count(0),
64 enable_reboot(true),
65 consecutive_power_keys(0),
66 last_key(-1),
67 has_power_key(false),
68 has_up_key(false),
Tao Bao6278bdf2017-01-16 17:38:18 -080069 has_down_key(false),
Tao Baoaf9f8b42017-07-28 00:05:40 -070070 has_touch_screen(false),
71 touch_slot_(0),
Tao Bao6278bdf2017-01-16 17:38:18 -080072 screensaver_state_(ScreensaverState::DISABLED) {
Tao Bao736d59c2017-01-03 10:15:33 -080073 pthread_mutex_init(&key_queue_mutex, nullptr);
74 pthread_cond_init(&key_queue_cond, nullptr);
75 memset(key_pressed, 0, sizeof(key_pressed));
Doug Zongker32a0a472011-11-01 11:00:20 -070076}
77
Elliott Hughes642aaa72015-04-10 12:47:46 -070078void RecoveryUI::OnKeyDetected(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070079 if (key_code == KEY_POWER) {
80 has_power_key = true;
81 } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) {
82 has_down_key = true;
83 } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) {
84 has_up_key = true;
Tao Baoaf9f8b42017-07-28 00:05:40 -070085 } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) {
86 has_touch_screen = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070087 }
Elliott Hughes642aaa72015-04-10 12:47:46 -070088}
89
Elliott Hughes985022a2015-04-13 13:04:32 -070090// Reads input events, handles special hot keys, and adds to the key queue.
91static void* InputThreadLoop(void*) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070092 while (true) {
93 if (!ev_wait(-1)) {
94 ev_dispatch();
Elliott Hughes985022a2015-04-13 13:04:32 -070095 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070096 }
97 return nullptr;
Elliott Hughes985022a2015-04-13 13:04:32 -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 }
105
106 // Set the initial brightness level based on the max brightness. Note that reading the initial
107 // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so
108 // we don't have a good way to query the default value.
109 std::string content;
110 if (!android::base::ReadFileToString(MAX_BRIGHTNESS_FILE, &content)) {
Tao Bao8eec3732017-01-31 21:24:16 -0800111 PLOG(WARNING) << "Failed to read max brightness";
Tao Bao6278bdf2017-01-16 17:38:18 -0800112 return false;
113 }
114
115 unsigned int max_value;
116 if (!android::base::ParseUint(android::base::Trim(content), &max_value)) {
117 LOG(WARNING) << "Failed to parse max brightness: " << content;
118 return false;
119 }
120
121 brightness_normal_value_ = max_value * brightness_normal_ / 100.0;
122 brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0;
123 if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
124 BRIGHTNESS_FILE)) {
125 PLOG(WARNING) << "Failed to set brightness";
126 return false;
127 }
128
129 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)";
130 screensaver_state_ = ScreensaverState::NORMAL;
131 return true;
132}
133
Tao Bao736d59c2017-01-03 10:15:33 -0800134bool RecoveryUI::Init(const std::string& locale) {
135 // Set up the locale info.
136 SetLocale(locale);
Elliott Hughes985022a2015-04-13 13:04:32 -0700137
Tao Baoaf9f8b42017-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 Baoaf9f8b42017-07-28 00:05:40 -0700143 if (touch_screen_allowed_) {
144 ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1));
145 }
146
Tao Bao6278bdf2017-01-16 17:38:18 -0800147 if (!InitScreensaver()) {
148 LOG(INFO) << "Screensaver disabled";
149 }
150
Tao Bao736d59c2017-01-03 10:15:33 -0800151 pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr);
152 return true;
Elliott Hughes985022a2015-04-13 13:04:32 -0700153}
154
Tao Baoaf9f8b42017-07-28 00:05:40 -0700155void RecoveryUI::OnTouchDetected(int dx, int dy) {
156 enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction;
157
158 // We only consider a valid swipe if:
159 // - the delta along one axis is below kTouchLowThreshold;
160 // - and the delta along the other axis is beyond kTouchHighThreshold.
161 if (abs(dy) < kTouchLowThreshold && abs(dx) > kTouchHighThreshold) {
162 direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT;
163 } else if (abs(dx) < kTouchLowThreshold && abs(dy) > kTouchHighThreshold) {
164 direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN;
165 } else {
166 LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << kTouchLowThreshold
167 << ", high: " << kTouchHighThreshold << ")";
168 return;
169 }
170
171 LOG(DEBUG) << "Swipe direction=" << direction;
172 switch (direction) {
173 case SwipeDirection::UP:
174 ProcessKey(KEY_UP, 1); // press up key
175 ProcessKey(KEY_UP, 0); // and release it
176 break;
177
178 case SwipeDirection::DOWN:
179 ProcessKey(KEY_DOWN, 1); // press down key
180 ProcessKey(KEY_DOWN, 0); // and release it
181 break;
182
183 case SwipeDirection::LEFT:
184 case SwipeDirection::RIGHT:
185 ProcessKey(KEY_POWER, 1); // press power key
186 ProcessKey(KEY_POWER, 0); // and release it
187 break;
188 };
189}
190
Elliott Hughes985022a2015-04-13 13:04:32 -0700191int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700192 struct input_event ev;
193 if (ev_get_input(fd, epevents, &ev) == -1) {
194 return -1;
195 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700196
Tao Baoaf9f8b42017-07-28 00:05:40 -0700197 // Touch inputs handling.
198 //
199 // We handle the touch inputs by tracking the position changes between initial contacting and
200 // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon
201 // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes.
202 //
203 // Per the doc Multi-touch Protocol at below, there are two protocols.
204 // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
205 //
206 // The main difference between the stateless type A protocol and the stateful type B slot protocol
207 // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The
208 // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and
209 // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send
210 // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event.
211 //
212 // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for
213 // ABS_MT_TRACKING_ID being -1.
214 //
215 // Touch input events will only be available if touch_screen_allowed_ is set.
216
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700217 if (ev.type == EV_SYN) {
Tao Baoaf9f8b42017-07-28 00:05:40 -0700218 if (touch_screen_allowed_ && ev.code == SYN_REPORT) {
219 // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the
220 // contact.
221 if (touch_finger_down_ && !touch_swiping_) {
222 touch_start_X_ = touch_X_;
223 touch_start_Y_ = touch_Y_;
224 touch_swiping_ = true;
225 } else if (!touch_finger_down_ && touch_swiping_) {
226 touch_swiping_ = false;
227 OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_);
228 }
229 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700230 return 0;
Tao Baoaf9f8b42017-07-28 00:05:40 -0700231 }
232
233 if (ev.type == EV_REL) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700234 if (ev.code == REL_Y) {
235 // accumulate the up or down motion reported by
236 // the trackball. When it exceeds a threshold
237 // (positive or negative), fake an up/down
238 // key event.
239 rel_sum += ev.value;
240 if (rel_sum > 3) {
241 ProcessKey(KEY_DOWN, 1); // press down key
242 ProcessKey(KEY_DOWN, 0); // and release it
243 rel_sum = 0;
244 } else if (rel_sum < -3) {
245 ProcessKey(KEY_UP, 1); // press up key
246 ProcessKey(KEY_UP, 0); // and release it
247 rel_sum = 0;
248 }
249 }
250 } else {
251 rel_sum = 0;
252 }
253
Tao Baoaf9f8b42017-07-28 00:05:40 -0700254 if (touch_screen_allowed_ && ev.type == EV_ABS) {
255 if (ev.code == ABS_MT_SLOT) {
256 touch_slot_ = ev.value;
257 }
258 // Ignore other fingers.
259 if (touch_slot_ > 0) return 0;
260
261 switch (ev.code) {
262 case ABS_MT_POSITION_X:
263 touch_X_ = ev.value;
264 touch_finger_down_ = true;
265 break;
266
267 case ABS_MT_POSITION_Y:
268 touch_Y_ = ev.value;
269 touch_finger_down_ = true;
270 break;
271
272 case ABS_MT_TRACKING_ID:
273 // Protocol B: -1 marks lifting the contact.
274 if (ev.value < 0) touch_finger_down_ = false;
275 break;
276 }
277 return 0;
278 }
279
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700280 if (ev.type == EV_KEY && ev.code <= KEY_MAX) {
Tao Baoaf9f8b42017-07-28 00:05:40 -0700281 if (touch_screen_allowed_) {
282 if (ev.code == BTN_TOUCH) {
283 // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means
284 // lifting the contact.
285 touch_finger_down_ = (ev.value == 1);
286 }
287
288 // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger
289 // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than
290 // KEY_POWER and KEY_UP as KEY_DOWN).
291 if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) {
292 return 0;
293 }
294 }
295
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700296 ProcessKey(ev.code, ev.value);
297 }
298
299 return 0;
Doug Zongker32a0a472011-11-01 11:00:20 -0700300}
301
302// Process a key-up or -down event. A key is "registered" when it is
303// pressed and then released, with no other keypresses or releases in
304// between. Registered keys are passed to CheckKey() to see if it
305// should trigger a visibility toggle, an immediate reboot, or be
306// queued to be processed next time the foreground thread wants a key
307// (eg, for the menu).
308//
309// We also keep track of which keys are currently down so that
310// CheckKey can call IsKeyPressed to see what other keys are held when
311// a key is registered.
312//
313// updown == 1 for key down events; 0 for key up events
Elliott Hughes985022a2015-04-13 13:04:32 -0700314void RecoveryUI::ProcessKey(int key_code, int updown) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700315 bool register_key = false;
316 bool long_press = false;
317 bool reboot_enabled;
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800318
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700319 pthread_mutex_lock(&key_queue_mutex);
320 key_pressed[key_code] = updown;
321 if (updown) {
322 ++key_down_count;
323 key_last_down = key_code;
324 key_long_press = false;
325 key_timer_t* info = new key_timer_t;
326 info->ui = this;
327 info->key_code = key_code;
328 info->count = key_down_count;
329 pthread_t thread;
330 pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info);
331 pthread_detach(thread);
332 } else {
333 if (key_last_down == key_code) {
334 long_press = key_long_press;
335 register_key = true;
Doug Zongker32a0a472011-11-01 11:00:20 -0700336 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700337 key_last_down = -1;
338 }
339 reboot_enabled = enable_reboot;
340 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700341
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700342 if (register_key) {
343 switch (CheckKey(key_code, long_press)) {
344 case RecoveryUI::IGNORE:
345 break;
Doug Zongker48b5b072012-01-18 13:46:26 -0800346
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700347 case RecoveryUI::TOGGLE:
348 ShowText(!IsTextVisible());
349 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700350
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700351 case RecoveryUI::REBOOT:
352 if (reboot_enabled) {
353 reboot("reboot,");
354 while (true) {
355 pause();
356 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700357 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700358 break;
359
360 case RecoveryUI::ENQUEUE:
361 EnqueueKey(key_code);
362 break;
Doug Zongker32a0a472011-11-01 11:00:20 -0700363 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700364 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700365}
366
Doug Zongkerc0441d12013-07-31 11:28:24 -0700367void* RecoveryUI::time_key_helper(void* cookie) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700368 key_timer_t* info = static_cast<key_timer_t*>(cookie);
369 info->ui->time_key(info->key_code, info->count);
370 delete info;
371 return nullptr;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700372}
373
374void RecoveryUI::time_key(int key_code, int count) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700375 usleep(750000); // 750 ms == "long"
376 bool long_press = false;
377 pthread_mutex_lock(&key_queue_mutex);
378 if (key_last_down == key_code && key_down_count == count) {
379 long_press = key_long_press = true;
380 }
381 pthread_mutex_unlock(&key_queue_mutex);
382 if (long_press) KeyLongPress(key_code);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700383}
384
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800385void RecoveryUI::EnqueueKey(int key_code) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700386 pthread_mutex_lock(&key_queue_mutex);
387 const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]);
388 if (key_queue_len < queue_max) {
389 key_queue[key_queue_len++] = key_code;
390 pthread_cond_signal(&key_queue_cond);
391 }
392 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerbb01d0c2012-12-17 10:52:58 -0800393}
394
Elliott Hughesec283402015-04-10 10:01:53 -0700395int RecoveryUI::WaitKey() {
Tao Bao6278bdf2017-01-16 17:38:18 -0800396 pthread_mutex_lock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700397
Tao Bao6278bdf2017-01-16 17:38:18 -0800398 // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is
399 // plugged in.
400 do {
401 struct timeval now;
402 struct timespec timeout;
403 gettimeofday(&now, nullptr);
404 timeout.tv_sec = now.tv_sec;
405 timeout.tv_nsec = now.tv_usec * 1000;
406 timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC;
Doug Zongker32a0a472011-11-01 11:00:20 -0700407
Tao Bao6278bdf2017-01-16 17:38:18 -0800408 int rc = 0;
409 while (key_queue_len == 0 && rc != ETIMEDOUT) {
410 rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout);
Doug Zongker32a0a472011-11-01 11:00:20 -0700411 }
Tao Bao6278bdf2017-01-16 17:38:18 -0800412
413 if (screensaver_state_ != ScreensaverState::DISABLED) {
414 if (rc == ETIMEDOUT) {
415 // Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF.
416 if (screensaver_state_ == ScreensaverState::NORMAL) {
417 if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_),
418 BRIGHTNESS_FILE)) {
419 LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_
420 << "%)";
421 screensaver_state_ = ScreensaverState::DIMMED;
422 }
423 } else if (screensaver_state_ == ScreensaverState::DIMMED) {
424 if (android::base::WriteStringToFile("0", BRIGHTNESS_FILE)) {
425 LOG(INFO) << "Brightness: 0 (off)";
426 screensaver_state_ = ScreensaverState::OFF;
427 }
428 }
429 } else if (screensaver_state_ != ScreensaverState::NORMAL) {
430 // Drop the first key if it's changing from OFF to NORMAL.
431 if (screensaver_state_ == ScreensaverState::OFF) {
432 if (key_queue_len > 0) {
433 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
434 }
435 }
436
437 // Reset the brightness to normal.
438 if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_),
439 BRIGHTNESS_FILE)) {
440 screensaver_state_ = ScreensaverState::NORMAL;
441 LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_
442 << "%)";
443 }
444 }
445 }
446 } while (IsUsbConnected() && key_queue_len == 0);
447
448 int key = -1;
449 if (key_queue_len > 0) {
450 key = key_queue[0];
451 memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len);
452 }
453 pthread_mutex_unlock(&key_queue_mutex);
454 return key;
Doug Zongker32a0a472011-11-01 11:00:20 -0700455}
456
Elliott Hughes985022a2015-04-13 13:04:32 -0700457bool RecoveryUI::IsUsbConnected() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700458 int fd = open("/sys/class/android_usb/android0/state", O_RDONLY);
459 if (fd < 0) {
460 printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno));
461 return 0;
462 }
Doug Zongker32a0a472011-11-01 11:00:20 -0700463
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700464 char buf;
465 // USB is connected if android_usb state is CONNECTED or CONFIGURED.
466 int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
467 if (close(fd) < 0) {
468 printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno));
469 }
470 return connected;
Doug Zongker32a0a472011-11-01 11:00:20 -0700471}
472
Elliott Hughesec283402015-04-10 10:01:53 -0700473bool RecoveryUI::IsKeyPressed(int key) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700474 pthread_mutex_lock(&key_queue_mutex);
475 int pressed = key_pressed[key];
476 pthread_mutex_unlock(&key_queue_mutex);
477 return pressed;
Doug Zongker32a0a472011-11-01 11:00:20 -0700478}
479
Elliott Hughes642aaa72015-04-10 12:47:46 -0700480bool RecoveryUI::IsLongPress() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700481 pthread_mutex_lock(&key_queue_mutex);
482 bool result = key_long_press;
483 pthread_mutex_unlock(&key_queue_mutex);
484 return result;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700485}
486
Elliott Hughes4af215b2015-04-10 15:00:34 -0700487bool RecoveryUI::HasThreeButtons() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700488 return has_power_key && has_up_key && has_down_key;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700489}
490
Tao Baoaf9f8b42017-07-28 00:05:40 -0700491bool RecoveryUI::HasPowerKey() const {
492 return has_power_key;
493}
494
495bool RecoveryUI::HasTouchScreen() const {
496 return has_touch_screen;
497}
498
Doug Zongker32a0a472011-11-01 11:00:20 -0700499void RecoveryUI::FlushKeys() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700500 pthread_mutex_lock(&key_queue_mutex);
501 key_queue_len = 0;
502 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongker32a0a472011-11-01 11:00:20 -0700503}
504
Elliott Hughes642aaa72015-04-10 12:47:46 -0700505RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700506 pthread_mutex_lock(&key_queue_mutex);
507 key_long_press = false;
508 pthread_mutex_unlock(&key_queue_mutex);
509
510 // If we have power and volume up keys, that chord is the signal to toggle the text display.
Tao Baoaf9f8b42017-07-28 00:05:40 -0700511 if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) {
512 if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700513 return TOGGLE;
514 }
515 } else {
516 // Otherwise long press of any button toggles to the text display,
517 // and there's no way to toggle back (but that's pretty useless anyway).
518 if (is_long_press && !IsTextVisible()) {
519 return TOGGLE;
520 }
521
522 // Also, for button-limited devices, a long press is translated to KEY_ENTER.
523 if (is_long_press && IsTextVisible()) {
524 EnqueueKey(KEY_ENTER);
525 return IGNORE;
526 }
527 }
528
529 // Press power seven times in a row to reboot.
530 if (key == KEY_POWER) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700531 pthread_mutex_lock(&key_queue_mutex);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700532 bool reboot_enabled = enable_reboot;
Elliott Hughes642aaa72015-04-10 12:47:46 -0700533 pthread_mutex_unlock(&key_queue_mutex);
534
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) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700553 pthread_mutex_lock(&key_queue_mutex);
554 enable_reboot = enabled;
555 pthread_mutex_unlock(&key_queue_mutex);
Doug Zongkerc704e062014-05-23 08:40:35 -0700556}
Tao Bao736d59c2017-01-03 10:15:33 -0800557
558void RecoveryUI::SetLocale(const std::string& new_locale) {
559 this->locale_ = new_locale;
560 this->rtl_locale_ = false;
561
562 if (!new_locale.empty()) {
563 size_t underscore = new_locale.find('_');
564 // lang has the language prefix prior to '_', or full string if '_' doesn't exist.
565 std::string lang = new_locale.substr(0, underscore);
566
567 // A bit cheesy: keep an explicit list of supported RTL languages.
568 if (lang == "ar" || // Arabic
569 lang == "fa" || // Persian (Farsi)
570 lang == "he" || // Hebrew (new language code)
571 lang == "iw" || // Hebrew (old language code)
572 lang == "ur") { // Urdu
573 rtl_locale_ = true;
574 }
575 }
576}