blob: e950ea65bbc6d14c2a7a02c164443890e666b343 [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshalld13a8c52017-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 Marshall88fe6f52020-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 Marshall55220ba2019-01-04 14:37:31 -080035 { "Apply update", Device::APPLY_UPDATE },
Tom Marshall88fe6f52020-03-29 14:36:57 +020036 { "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 Bestas24928fe2019-09-27 20:16:38 +030044 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Michael Bestasde514502021-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 },
LuK13376422b8f2020-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 Marshall88fe6f52020-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 Johnsonea96a202020-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 Marshall88fe6f52020-03-29 14:36:57 +020059};
60
Tom Marshall88fe6f52020-03-29 14:36:57 +020061static std::vector<menu_action_t>* current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070062static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070063
Tao Baoe5d2c252018-05-09 11:05:44 -070064static void PopulateMenuItems() {
65 g_menu_items.clear();
Tom Marshall88fe6f52020-03-29 14:36:57 +020066 std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items),
Tao Baoe5d2c252018-05-09 11:05:44 -070067 [](const auto& entry) { return entry.first; });
68}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070069
Tao Baoe5d2c252018-05-09 11:05:44 -070070Device::Device(RecoveryUI* ui) : ui_(ui) {
Tom Marshall914603d2018-06-21 00:57:24 +020071 ui->SetDevice(this);
Tao Baoe5d2c252018-05-09 11:05:44 -070072 PopulateMenuItems();
73}
74
Tom Marshall88fe6f52020-03-29 14:36:57 +020075void Device::GoHome() {
76 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070077 PopulateMenuItems();
78}
Tao Bao1fe1afe2018-05-01 15:56:05 -070079
Tom Marshall88fe6f52020-03-29 14:36:57 +020080static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
81 menu.erase(
82 std::remove_if(menu.begin(), menu.end(),
83 [action](const auto& entry) { return entry.second == action; }), menu.end());
84 CHECK(!menu.empty());
85}
86
87void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
Tom Marshall88fe6f52020-03-29 14:36:57 +020088 ::RemoveMenuItemForAction(g_wipe_actions, action);
89 ::RemoveMenuItemForAction(g_advanced_actions, action);
90}
91
Tao Bao1fe1afe2018-05-01 15:56:05 -070092const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070093 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070094}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070095
Tom Marshall88fe6f52020-03-29 14:36:57 +020096const std::vector<std::string>& Device::GetMenuHeaders() {
Tom Marshall55220ba2019-01-04 14:37:31 -080097 if (current_menu_ == &g_wipe_actions)
Tom Marshall88fe6f52020-03-29 14:36:57 +020098 return g_wipe_header;
Tom Marshall55220ba2019-01-04 14:37:31 -080099 if (current_menu_ == &g_advanced_actions)
Tom Marshall88fe6f52020-03-29 14:36:57 +0200100 return g_advanced_header;
101 return g_main_header;
102}
103
Tao Bao1fe1afe2018-05-01 15:56:05 -0700104Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshall88fe6f52020-03-29 14:36:57 +0200105 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
106
107 if (action > MENU_BASE) {
108 switch (action) {
Tom Marshall88fe6f52020-03-29 14:36:57 +0200109 case Device::BuiltinAction::MENU_WIPE:
110 current_menu_ = &g_wipe_actions;
111 break;
112 case Device::BuiltinAction::MENU_ADVANCED:
113 current_menu_ = &g_advanced_actions;
114 break;
115 default:
116 break;
117 }
118 PopulateMenuItems();
119 }
120 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700121}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700122
Tao Baofc5499f2017-02-23 19:06:53 -0800123int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700124 if (!visible) {
125 return kNoAction;
126 }
127
128 switch (key) {
Tom Marshalld13a8c52017-08-24 13:50:01 +0000129 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700130 case KEY_DOWN:
131 case KEY_VOLUMEDOWN:
Tom Marshalld13a8c52017-08-24 13:50:01 +0000132 case KEY_MENU:
Aaron Kling53b26ef2019-07-22 14:23:40 -0500133 case BTN_NORTH:
134 case BTN_DPAD_DOWN:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700135 return kHighlightDown;
136
137 case KEY_UP:
138 case KEY_VOLUMEUP:
Tom Marshalld13a8c52017-08-24 13:50:01 +0000139 case KEY_SEARCH:
Aaron Kling53b26ef2019-07-22 14:23:40 -0500140 case BTN_WEST:
141 case BTN_DPAD_UP:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700142 return kHighlightUp;
143
Tom Marshall53176c72020-03-10 20:17:49 +0100144 case KEY_SCROLLUP:
145 return kScrollUp;
146 case KEY_SCROLLDOWN:
147 return kScrollDown;
148
Elliott Hughes4af215b2015-04-10 15:00:34 -0700149 case KEY_ENTER:
150 case KEY_POWER:
Tom Marshalld13a8c52017-08-24 13:50:01 +0000151 case BTN_MOUSE:
152 case KEY_SEND:
Aaron Kling53b26ef2019-07-22 14:23:40 -0500153 case BTN_SOUTH:
154 case BTN_START:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700155 return kInvokeItem;
156
Tom Marshalld13a8c52017-08-24 13:50:01 +0000157 case KEY_HOME:
158 case KEY_HOMEPAGE:
159 return kGoHome;
160
161 case KEY_BACKSPACE:
162 case KEY_BACK:
163 return kGoBack;
164
Tom Marshall90e09f82018-12-17 15:57:44 -0800165 case KEY_AGAIN:
166 return kDoSideload;
167
Tom Marshall55220ba2019-01-04 14:37:31 -0800168 case KEY_REFRESH:
169 return kRefresh;
170
Elliott Hughes4af215b2015-04-10 15:00:34 -0700171 default:
172 // If you have all of the above buttons, any other buttons
173 // are ignored. Otherwise, any button cycles the highlight.
174 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
175 }
176}
Tianjie Xub63a2212019-07-29 14:21:49 -0700177
178void Device::SetBootState(const BootState* state) {
179 boot_state_ = state;
180}
181
182std::optional<std::string> Device::GetReason() const {
183 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
184}
185
186std::optional<std::string> Device::GetStage() const {
187 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
188}