blob: 4408c9f7f2cb7fbd2e7159be800bba0126e67ab4 [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
Tom Marshallbe246fe2020-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{
Tao Baoe5d2c252018-05-09 11:05:44 -070034 { "Reboot system now", Device::REBOOT },
Tom Marshallbe246fe2020-03-29 14:36:57 +020035 { "Apply update", Device::MENU_UPDATE },
36 { "Factory reset", Device::MENU_WIPE },
37 { "Advanced", Device::MENU_ADVANCED },
38};
39
40static std::vector<std::string> g_advanced_header{ "Advanced options" };
41static std::vector<menu_action_t> g_advanced_actions{
42 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070043 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Michael Bestasa89d9882019-09-27 20:16:38 +030044 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Tao Baoe5d2c252018-05-09 11:05:44 -070045 { "Mount /system", Device::MOUNT_SYSTEM },
46 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
LuK1337b3190ae2020-04-12 19:04:59 +020047 { "Enable ADB", Device::ENABLE_ADB },
Tao Baoe5d2c252018-05-09 11:05:44 -070048 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
49 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Baoc6dc3252019-04-16 14:22:25 -070050 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070051 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070052};
53
Tom Marshallbe246fe2020-03-29 14:36:57 +020054static std::vector<std::string> g_wipe_header{ "Factory reset" };
55static std::vector<menu_action_t> g_wipe_actions{
Nolen Johnson754d4f42020-04-12 14:13:31 -040056 { "Format data/factory reset", Device::WIPE_DATA },
57 { "Format cache partition", Device::WIPE_CACHE },
58 { "Format system partition", Device::WIPE_SYSTEM },
Tom Marshallbe246fe2020-03-29 14:36:57 +020059};
60
61static std::vector<std::string> g_update_header{ "Apply update" };
62static std::vector<menu_action_t> g_update_actions{
63 { "Apply from ADB", Device::APPLY_ADB_SIDELOAD },
64 { "Choose from internal storage", Device::APPLY_SDCARD },
65};
66
67static std::vector<menu_action_t>* current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070068static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070069
Tao Baoe5d2c252018-05-09 11:05:44 -070070static void PopulateMenuItems() {
71 g_menu_items.clear();
Tom Marshallbe246fe2020-03-29 14:36:57 +020072 std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items),
Tao Baoe5d2c252018-05-09 11:05:44 -070073 [](const auto& entry) { return entry.first; });
74}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070075
Tao Baoe5d2c252018-05-09 11:05:44 -070076Device::Device(RecoveryUI* ui) : ui_(ui) {
Tom Marshalle976b082018-06-21 00:57:24 +020077 ui->SetDevice(this);
Tao Baoe5d2c252018-05-09 11:05:44 -070078 PopulateMenuItems();
79}
80
Tom Marshallbe246fe2020-03-29 14:36:57 +020081void Device::GoHome() {
82 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070083 PopulateMenuItems();
84}
Tao Bao1fe1afe2018-05-01 15:56:05 -070085
Tom Marshallbe246fe2020-03-29 14:36:57 +020086static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
87 menu.erase(
88 std::remove_if(menu.begin(), menu.end(),
89 [action](const auto& entry) { return entry.second == action; }), menu.end());
90 CHECK(!menu.empty());
91}
92
93void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
94 ::RemoveMenuItemForAction(g_update_actions, action);
95 ::RemoveMenuItemForAction(g_wipe_actions, action);
96 ::RemoveMenuItemForAction(g_advanced_actions, action);
97}
98
Tao Bao1fe1afe2018-05-01 15:56:05 -070099const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -0700100 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700101}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700102
Tom Marshallbe246fe2020-03-29 14:36:57 +0200103const std::vector<std::string>& Device::GetMenuHeaders() {
104 if (current_menu_ == &g_update_actions)
105 return g_update_header;
106 else if (current_menu_ == &g_wipe_actions)
107 return g_wipe_header;
108 else if (current_menu_ == &g_advanced_actions)
109 return g_advanced_header;
110 return g_main_header;
111}
112
Tao Bao1fe1afe2018-05-01 15:56:05 -0700113Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshallbe246fe2020-03-29 14:36:57 +0200114 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
115
116 if (action > MENU_BASE) {
117 switch (action) {
118 case Device::BuiltinAction::MENU_UPDATE:
119 current_menu_ = &g_update_actions;
120 break;
121 case Device::BuiltinAction::MENU_WIPE:
122 current_menu_ = &g_wipe_actions;
123 break;
124 case Device::BuiltinAction::MENU_ADVANCED:
125 current_menu_ = &g_advanced_actions;
126 break;
127 default:
128 break;
129 }
130 PopulateMenuItems();
131 }
132 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700133}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700134
Tao Baofc5499f2017-02-23 19:06:53 -0800135int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700136 if (!visible) {
137 return kNoAction;
138 }
139
140 switch (key) {
Tom Marshall4b921692017-08-24 13:50:01 +0000141 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700142 case KEY_DOWN:
143 case KEY_VOLUMEDOWN:
Tom Marshall4b921692017-08-24 13:50:01 +0000144 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700145 return kHighlightDown;
146
147 case KEY_UP:
148 case KEY_VOLUMEUP:
Tom Marshall4b921692017-08-24 13:50:01 +0000149 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700150 return kHighlightUp;
151
Alessandro Astone73961412020-10-04 18:11:40 +0200152 case KEY_SCROLLUP:
153 return kScrollUp;
154 case KEY_SCROLLDOWN:
155 return kScrollDown;
156
Elliott Hughes4af215b2015-04-10 15:00:34 -0700157 case KEY_ENTER:
158 case KEY_POWER:
Tom Marshall4b921692017-08-24 13:50:01 +0000159 case BTN_MOUSE:
160 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700161 return kInvokeItem;
162
Tom Marshall4b921692017-08-24 13:50:01 +0000163 case KEY_HOME:
164 case KEY_HOMEPAGE:
165 return kGoHome;
166
167 case KEY_BACKSPACE:
168 case KEY_BACK:
169 return kGoBack;
170
Tom Marshallfdf50332018-12-17 15:57:44 -0800171 case KEY_AGAIN:
172 return kDoSideload;
173
Elliott Hughes4af215b2015-04-10 15:00:34 -0700174 default:
175 // If you have all of the above buttons, any other buttons
176 // are ignored. Otherwise, any button cycles the highlight.
177 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
178 }
179}
Tianjie Xub63a2212019-07-29 14:21:49 -0700180
181void Device::SetBootState(const BootState* state) {
182 boot_state_ = state;
183}
184
185std::optional<std::string> Device::GetReason() const {
186 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
187}
188
189std::optional<std::string> Device::GetStage() const {
190 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
191}