blob: 97d587201f215464ac9d45783c08e886d6ff976c [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 },
47 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
48 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Baoc6dc3252019-04-16 14:22:25 -070049 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070050 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070051};
52
Tom Marshallbe246fe2020-03-29 14:36:57 +020053static std::vector<std::string> g_wipe_header{ "Factory reset" };
54static std::vector<menu_action_t> g_wipe_actions{
55 { "Wipe data/factory reset", Device::WIPE_DATA },
56 { "Wipe cache partition", Device::WIPE_CACHE },
57 { "Wipe system partition", Device::WIPE_SYSTEM },
58};
59
60static std::vector<std::string> g_update_header{ "Apply update" };
61static std::vector<menu_action_t> g_update_actions{
62 { "Apply from ADB", Device::APPLY_ADB_SIDELOAD },
63 { "Choose from internal storage", Device::APPLY_SDCARD },
64};
65
66static std::vector<menu_action_t>* current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070067static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070068
Tao Baoe5d2c252018-05-09 11:05:44 -070069static void PopulateMenuItems() {
70 g_menu_items.clear();
Tom Marshallbe246fe2020-03-29 14:36:57 +020071 std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items),
Tao Baoe5d2c252018-05-09 11:05:44 -070072 [](const auto& entry) { return entry.first; });
73}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070074
Tao Baoe5d2c252018-05-09 11:05:44 -070075Device::Device(RecoveryUI* ui) : ui_(ui) {
76 PopulateMenuItems();
77}
78
Tom Marshallbe246fe2020-03-29 14:36:57 +020079void Device::GoHome() {
80 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070081 PopulateMenuItems();
82}
Tao Bao1fe1afe2018-05-01 15:56:05 -070083
Tom Marshallbe246fe2020-03-29 14:36:57 +020084static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
85 menu.erase(
86 std::remove_if(menu.begin(), menu.end(),
87 [action](const auto& entry) { return entry.second == action; }), menu.end());
88 CHECK(!menu.empty());
89}
90
91void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
92 ::RemoveMenuItemForAction(g_update_actions, action);
93 ::RemoveMenuItemForAction(g_wipe_actions, action);
94 ::RemoveMenuItemForAction(g_advanced_actions, action);
95}
96
Tao Bao1fe1afe2018-05-01 15:56:05 -070097const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070098 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070099}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700100
Tom Marshallbe246fe2020-03-29 14:36:57 +0200101const std::vector<std::string>& Device::GetMenuHeaders() {
102 if (current_menu_ == &g_update_actions)
103 return g_update_header;
104 else if (current_menu_ == &g_wipe_actions)
105 return g_wipe_header;
106 else if (current_menu_ == &g_advanced_actions)
107 return g_advanced_header;
108 return g_main_header;
109}
110
Tao Bao1fe1afe2018-05-01 15:56:05 -0700111Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshallbe246fe2020-03-29 14:36:57 +0200112 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
113
114 if (action > MENU_BASE) {
115 switch (action) {
116 case Device::BuiltinAction::MENU_UPDATE:
117 current_menu_ = &g_update_actions;
118 break;
119 case Device::BuiltinAction::MENU_WIPE:
120 current_menu_ = &g_wipe_actions;
121 break;
122 case Device::BuiltinAction::MENU_ADVANCED:
123 current_menu_ = &g_advanced_actions;
124 break;
125 default:
126 break;
127 }
128 PopulateMenuItems();
129 }
130 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700131}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700132
Tao Baofc5499f2017-02-23 19:06:53 -0800133int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700134 if (!visible) {
135 return kNoAction;
136 }
137
138 switch (key) {
Tom Marshall4b921692017-08-24 13:50:01 +0000139 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700140 case KEY_DOWN:
141 case KEY_VOLUMEDOWN:
Tom Marshall4b921692017-08-24 13:50:01 +0000142 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700143 return kHighlightDown;
144
145 case KEY_UP:
146 case KEY_VOLUMEUP:
Tom Marshall4b921692017-08-24 13:50:01 +0000147 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700148 return kHighlightUp;
149
Alessandro Astone73961412020-10-04 18:11:40 +0200150 case KEY_SCROLLUP:
151 return kScrollUp;
152 case KEY_SCROLLDOWN:
153 return kScrollDown;
154
Elliott Hughes4af215b2015-04-10 15:00:34 -0700155 case KEY_ENTER:
156 case KEY_POWER:
Tom Marshall4b921692017-08-24 13:50:01 +0000157 case BTN_MOUSE:
158 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700159 return kInvokeItem;
160
Tom Marshall4b921692017-08-24 13:50:01 +0000161 case KEY_HOME:
162 case KEY_HOMEPAGE:
163 return kGoHome;
164
165 case KEY_BACKSPACE:
166 case KEY_BACK:
167 return kGoBack;
168
Tom Marshallfdf50332018-12-17 15:57:44 -0800169 case KEY_AGAIN:
170 return kDoSideload;
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}