blob: 38a2551d8056b126dadc096f597972b831492c2a [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{
56 { "Wipe data/factory reset", Device::WIPE_DATA },
57 { "Wipe cache partition", Device::WIPE_CACHE },
58 { "Wipe system partition", Device::WIPE_SYSTEM },
59};
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) {
77 PopulateMenuItems();
78}
79
Tom Marshallbe246fe2020-03-29 14:36:57 +020080void Device::GoHome() {
81 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070082 PopulateMenuItems();
83}
Tao Bao1fe1afe2018-05-01 15:56:05 -070084
Tom Marshallbe246fe2020-03-29 14:36:57 +020085static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
86 menu.erase(
87 std::remove_if(menu.begin(), menu.end(),
88 [action](const auto& entry) { return entry.second == action; }), menu.end());
89 CHECK(!menu.empty());
90}
91
92void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
93 ::RemoveMenuItemForAction(g_update_actions, action);
94 ::RemoveMenuItemForAction(g_wipe_actions, action);
95 ::RemoveMenuItemForAction(g_advanced_actions, action);
96}
97
Tao Bao1fe1afe2018-05-01 15:56:05 -070098const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070099 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -0700100}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700101
Tom Marshallbe246fe2020-03-29 14:36:57 +0200102const std::vector<std::string>& Device::GetMenuHeaders() {
103 if (current_menu_ == &g_update_actions)
104 return g_update_header;
105 else if (current_menu_ == &g_wipe_actions)
106 return g_wipe_header;
107 else if (current_menu_ == &g_advanced_actions)
108 return g_advanced_header;
109 return g_main_header;
110}
111
Tao Bao1fe1afe2018-05-01 15:56:05 -0700112Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshallbe246fe2020-03-29 14:36:57 +0200113 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
114
115 if (action > MENU_BASE) {
116 switch (action) {
117 case Device::BuiltinAction::MENU_UPDATE:
118 current_menu_ = &g_update_actions;
119 break;
120 case Device::BuiltinAction::MENU_WIPE:
121 current_menu_ = &g_wipe_actions;
122 break;
123 case Device::BuiltinAction::MENU_ADVANCED:
124 current_menu_ = &g_advanced_actions;
125 break;
126 default:
127 break;
128 }
129 PopulateMenuItems();
130 }
131 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700132}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700133
Tao Baofc5499f2017-02-23 19:06:53 -0800134int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700135 if (!visible) {
136 return kNoAction;
137 }
138
139 switch (key) {
Tom Marshall4b921692017-08-24 13:50:01 +0000140 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700141 case KEY_DOWN:
142 case KEY_VOLUMEDOWN:
Tom Marshall4b921692017-08-24 13:50:01 +0000143 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700144 return kHighlightDown;
145
146 case KEY_UP:
147 case KEY_VOLUMEUP:
Tom Marshall4b921692017-08-24 13:50:01 +0000148 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700149 return kHighlightUp;
150
Alessandro Astone73961412020-10-04 18:11:40 +0200151 case KEY_SCROLLUP:
152 return kScrollUp;
153 case KEY_SCROLLDOWN:
154 return kScrollDown;
155
Elliott Hughes4af215b2015-04-10 15:00:34 -0700156 case KEY_ENTER:
157 case KEY_POWER:
Tom Marshall4b921692017-08-24 13:50:01 +0000158 case BTN_MOUSE:
159 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700160 return kInvokeItem;
161
Tom Marshall4b921692017-08-24 13:50:01 +0000162 case KEY_HOME:
163 case KEY_HOMEPAGE:
164 return kGoHome;
165
166 case KEY_BACKSPACE:
167 case KEY_BACK:
168 return kGoBack;
169
Tom Marshallfdf50332018-12-17 15:57:44 -0800170 case KEY_AGAIN:
171 return kDoSideload;
172
Elliott Hughes4af215b2015-04-10 15:00:34 -0700173 default:
174 // If you have all of the above buttons, any other buttons
175 // are ignored. Otherwise, any button cycles the highlight.
176 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
177 }
178}
Tianjie Xub63a2212019-07-29 14:21:49 -0700179
180void Device::SetBootState(const BootState* state) {
181 boot_state_ = state;
182}
183
184std::optional<std::string> Device::GetReason() const {
185 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
186}
187
188std::optional<std::string> Device::GetStage() const {
189 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
190}