blob: 3a422169413dbc95093238689cd0d612b6309868 [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshall4b921692017-08-24 13:50:01 +00003 * Copyright (C) 2019 The LineageOS Project
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Tianjie Xu8f397302018-08-20 13:40:47 -070018#include "recovery_ui/device.h"
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070019
Tao Baoe5d2c252018-05-09 11:05:44 -070020#include <algorithm>
21#include <string>
22#include <utility>
23#include <vector>
24
Tao Bao1fe1afe2018-05-01 15:56:05 -070025#include <android-base/logging.h>
Tao Bao1fe1afe2018-05-01 15:56:05 -070026
Tianjie Xub63a2212019-07-29 14:21:49 -070027#include "otautil/boot_state.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070028#include "recovery_ui/ui.h"
Tao Baoc16fd8a2018-04-30 17:12:03 -070029
Tao Baoe5d2c252018-05-09 11:05:44 -070030static std::vector<std::pair<std::string, Device::BuiltinAction>> g_menu_actions{
31 { "Reboot system now", Device::REBOOT },
32 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Michael Bestasa89d9882019-09-27 20:16:38 +030033 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070034 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070035 { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD },
36 { "Apply update from SD card", Device::APPLY_SDCARD },
37 { "Wipe data/factory reset", Device::WIPE_DATA },
38 { "Wipe cache partition", Device::WIPE_CACHE },
Michael Bestasa9ba2e32019-09-27 19:52:13 +030039 { "Wipe system partition", Device::WIPE_SYSTEM },
Tao Baoe5d2c252018-05-09 11:05:44 -070040 { "Mount /system", Device::MOUNT_SYSTEM },
41 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
42 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
43 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Baoc6dc3252019-04-16 14:22:25 -070044 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070045 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070046};
47
Tao Baoe5d2c252018-05-09 11:05:44 -070048static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070049
Tao Baoe5d2c252018-05-09 11:05:44 -070050static void PopulateMenuItems() {
51 g_menu_items.clear();
52 std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items),
53 [](const auto& entry) { return entry.first; });
54}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070055
Tao Baoe5d2c252018-05-09 11:05:44 -070056Device::Device(RecoveryUI* ui) : ui_(ui) {
57 PopulateMenuItems();
58}
59
60void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
61 g_menu_actions.erase(
62 std::remove_if(g_menu_actions.begin(), g_menu_actions.end(),
63 [action](const auto& entry) { return entry.second == action; }));
64 CHECK(!g_menu_actions.empty());
65
66 // Re-populate the menu items.
67 PopulateMenuItems();
68}
Tao Bao1fe1afe2018-05-01 15:56:05 -070069
70const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070071 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070072}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070073
Tao Bao1fe1afe2018-05-01 15:56:05 -070074Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tao Baoe5d2c252018-05-09 11:05:44 -070075 return g_menu_actions[menu_position].second;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070076}
Elliott Hughes4af215b2015-04-10 15:00:34 -070077
Tao Baofc5499f2017-02-23 19:06:53 -080078int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -070079 if (!visible) {
80 return kNoAction;
81 }
82
83 switch (key) {
Tom Marshall4b921692017-08-24 13:50:01 +000084 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -070085 case KEY_DOWN:
86 case KEY_VOLUMEDOWN:
Tom Marshall4b921692017-08-24 13:50:01 +000087 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -070088 return kHighlightDown;
89
90 case KEY_UP:
91 case KEY_VOLUMEUP:
Tom Marshall4b921692017-08-24 13:50:01 +000092 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -070093 return kHighlightUp;
94
95 case KEY_ENTER:
96 case KEY_POWER:
Tom Marshall4b921692017-08-24 13:50:01 +000097 case BTN_MOUSE:
98 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -070099 return kInvokeItem;
100
Tom Marshall4b921692017-08-24 13:50:01 +0000101 case KEY_HOME:
102 case KEY_HOMEPAGE:
103 return kGoHome;
104
105 case KEY_BACKSPACE:
106 case KEY_BACK:
107 return kGoBack;
108
Tom Marshallfdf50332018-12-17 15:57:44 -0800109 case KEY_AGAIN:
110 return kDoSideload;
111
Elliott Hughes4af215b2015-04-10 15:00:34 -0700112 default:
113 // If you have all of the above buttons, any other buttons
114 // are ignored. Otherwise, any button cycles the highlight.
115 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
116 }
117}
Tianjie Xub63a2212019-07-29 14:21:49 -0700118
119void Device::SetBootState(const BootState* state) {
120 boot_state_ = state;
121}
122
123std::optional<std::string> Device::GetReason() const {
124 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
125}
126
127std::optional<std::string> Device::GetStage() const {
128 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
129}