blob: f5a2d3a2640acf037d4a7cccbe8d61c04ab148ee [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshalleb7a0812017-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
Tom Marshallcb791fa2020-03-29 14:36:57 +020030typedef std::pair<std::string, Device::BuiltinAction> menu_action_t;
31
32static std::vector<std::string> g_main_header{};
33static std::vector<menu_action_t> g_main_actions{
NurKeinNeidaded1b42022-05-07 11:24:50 +020034 { "Reboot to system", Device::REBOOT },
35 { "Install update", Device::APPLY_UPDATE },
Tom Marshallcb791fa2020-03-29 14:36:57 +020036 { "Factory reset", Device::MENU_WIPE },
37 { "Advanced", Device::MENU_ADVANCED },
38};
39
NurKeinNeidaded1b42022-05-07 11:24:50 +020040static std::vector<std::string> g_advanced_header{ "Advanced" };
Tom Marshallcb791fa2020-03-29 14:36:57 +020041static std::vector<menu_action_t> g_advanced_actions{
chandu0787be7bc22022-02-13 07:40:17 +000042 { "Enter Fastboot D", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070043 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Michael Bestasc83ba592019-09-27 20:16:38 +030044 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Michael Bestasb357f8e2021-01-16 18:16:47 +020045 { "Mount/unmount system", Device::MOUNT_SYSTEM },
Tao Baoe5d2c252018-05-09 11:05:44 -070046 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
LuK133720434a92020-04-12 19:04:59 +020047 { "Enable ADB", Device::ENABLE_ADB },
Ethan Yonker521c2c42021-01-03 23:47:09 -080048 { "Switch slot", Device::SWAP_SLOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070049 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
50 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Baoc6dc3252019-04-16 14:22:25 -070051 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070052 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070053};
54
Tom Marshallcb791fa2020-03-29 14:36:57 +020055static std::vector<std::string> g_wipe_header{ "Factory reset" };
56static std::vector<menu_action_t> g_wipe_actions{
Nolen Johnsond3519ab2020-04-12 14:13:31 -040057 { "Format data/factory reset", Device::WIPE_DATA },
58 { "Format cache partition", Device::WIPE_CACHE },
59 { "Format system partition", Device::WIPE_SYSTEM },
Tom Marshallcb791fa2020-03-29 14:36:57 +020060};
61
Tom Marshallcb791fa2020-03-29 14:36:57 +020062static std::vector<menu_action_t>* current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070063static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070064
Tao Baoe5d2c252018-05-09 11:05:44 -070065static void PopulateMenuItems() {
66 g_menu_items.clear();
Tom Marshallcb791fa2020-03-29 14:36:57 +020067 std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items),
Tao Baoe5d2c252018-05-09 11:05:44 -070068 [](const auto& entry) { return entry.first; });
69}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070070
Tao Baoe5d2c252018-05-09 11:05:44 -070071Device::Device(RecoveryUI* ui) : ui_(ui) {
Tom Marshall7cb5d3e2018-06-21 00:57:24 +020072 ui->SetDevice(this);
Tao Baoe5d2c252018-05-09 11:05:44 -070073 PopulateMenuItems();
74}
75
Tom Marshallcb791fa2020-03-29 14:36:57 +020076void Device::GoHome() {
77 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070078 PopulateMenuItems();
79}
Tao Bao1fe1afe2018-05-01 15:56:05 -070080
Tom Marshallcb791fa2020-03-29 14:36:57 +020081static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
82 menu.erase(
83 std::remove_if(menu.begin(), menu.end(),
84 [action](const auto& entry) { return entry.second == action; }), menu.end());
85 CHECK(!menu.empty());
86}
87
88void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
Tom Marshallcb791fa2020-03-29 14:36:57 +020089 ::RemoveMenuItemForAction(g_wipe_actions, action);
90 ::RemoveMenuItemForAction(g_advanced_actions, action);
91}
92
Tao Bao1fe1afe2018-05-01 15:56:05 -070093const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070094 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070095}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070096
Tom Marshallcb791fa2020-03-29 14:36:57 +020097const std::vector<std::string>& Device::GetMenuHeaders() {
Tom Marshall3e101752019-01-04 14:37:31 -080098 if (current_menu_ == &g_wipe_actions)
Tom Marshallcb791fa2020-03-29 14:36:57 +020099 return g_wipe_header;
Tom Marshall3e101752019-01-04 14:37:31 -0800100 if (current_menu_ == &g_advanced_actions)
Tom Marshallcb791fa2020-03-29 14:36:57 +0200101 return g_advanced_header;
102 return g_main_header;
103}
104
Tao Bao1fe1afe2018-05-01 15:56:05 -0700105Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshallcb791fa2020-03-29 14:36:57 +0200106 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
107
108 if (action > MENU_BASE) {
109 switch (action) {
Tom Marshallcb791fa2020-03-29 14:36:57 +0200110 case Device::BuiltinAction::MENU_WIPE:
111 current_menu_ = &g_wipe_actions;
112 break;
113 case Device::BuiltinAction::MENU_ADVANCED:
114 current_menu_ = &g_advanced_actions;
115 break;
116 default:
117 break;
118 }
119 PopulateMenuItems();
120 }
121 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700122}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700123
Tao Baofc5499f2017-02-23 19:06:53 -0800124int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700125 if (!visible) {
126 return kNoAction;
127 }
128
129 switch (key) {
Tom Marshalleb7a0812017-08-24 13:50:01 +0000130 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700131 case KEY_DOWN:
132 case KEY_VOLUMEDOWN:
Tom Marshalleb7a0812017-08-24 13:50:01 +0000133 case KEY_MENU:
Aaron Kling218e5e52019-07-22 14:23:40 -0500134 case BTN_NORTH:
135 case BTN_DPAD_DOWN:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700136 return kHighlightDown;
137
138 case KEY_UP:
139 case KEY_VOLUMEUP:
Tom Marshalleb7a0812017-08-24 13:50:01 +0000140 case KEY_SEARCH:
Aaron Kling218e5e52019-07-22 14:23:40 -0500141 case BTN_WEST:
142 case BTN_DPAD_UP:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700143 return kHighlightUp;
144
Tom Marshall07bcaa32020-03-10 20:17:49 +0100145 case KEY_SCROLLUP:
146 return kScrollUp;
147 case KEY_SCROLLDOWN:
148 return kScrollDown;
149
Elliott Hughes4af215b2015-04-10 15:00:34 -0700150 case KEY_ENTER:
151 case KEY_POWER:
Tom Marshalleb7a0812017-08-24 13:50:01 +0000152 case BTN_MOUSE:
153 case KEY_SEND:
Aaron Kling218e5e52019-07-22 14:23:40 -0500154 case BTN_SOUTH:
155 case BTN_START:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700156 return kInvokeItem;
157
Tom Marshalleb7a0812017-08-24 13:50:01 +0000158 case KEY_HOME:
159 case KEY_HOMEPAGE:
160 return kGoHome;
161
162 case KEY_BACKSPACE:
163 case KEY_BACK:
164 return kGoBack;
165
Tom Marshallcdce2222018-12-17 15:57:44 -0800166 case KEY_AGAIN:
167 return kDoSideload;
168
Tom Marshall3e101752019-01-04 14:37:31 -0800169 case KEY_REFRESH:
170 return kRefresh;
171
Elliott Hughes4af215b2015-04-10 15:00:34 -0700172 default:
173 // If you have all of the above buttons, any other buttons
174 // are ignored. Otherwise, any button cycles the highlight.
175 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
176 }
177}
Tianjie Xub63a2212019-07-29 14:21:49 -0700178
179void Device::SetBootState(const BootState* state) {
180 boot_state_ = state;
181}
182
183std::optional<std::string> Device::GetReason() const {
184 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
185}
186
187std::optional<std::string> Device::GetStage() const {
188 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
189}