blob: fcb319e259e7b6b61fcead1ce3e6e8ab929d5d84 [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshalld23b5102017-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 Xub5108c32018-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 Xub5108c32018-08-20 13:40:47 -070027#include "recovery_ui/ui.h"
Tao Baoc16fd8a2018-04-30 17:12:03 -070028
Tom Marshall645801f2020-03-29 14:36:57 +020029typedef std::pair<std::string, Device::BuiltinAction> menu_action_t;
30
31static std::vector<std::string> g_main_header{};
32static std::vector<menu_action_t> g_main_actions{
Tao Baoe5d2c252018-05-09 11:05:44 -070033 { "Reboot system now", Device::REBOOT },
Tom Marshallbead2612019-01-04 14:37:31 -080034 { "Apply update", Device::APPLY_UPDATE },
Tom Marshall645801f2020-03-29 14:36:57 +020035 { "Factory reset", Device::MENU_WIPE },
36 { "Advanced", Device::MENU_ADVANCED },
37};
38
39static std::vector<std::string> g_advanced_header{ "Advanced options" };
40static std::vector<menu_action_t> g_advanced_actions{
41 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070042 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Michael Bestasb3e3ad62019-09-27 20:16:38 +030043 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Tao Baoe5d2c252018-05-09 11:05:44 -070044 { "Mount /system", Device::MOUNT_SYSTEM },
45 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
LuK133759e04592020-04-12 19:04:59 +020046 { "Enable ADB", Device::ENABLE_ADB },
Tao Baoe5d2c252018-05-09 11:05:44 -070047 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
48 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Bao378bfbf2019-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 Marshall645801f2020-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
Tom Marshall645801f2020-03-29 14:36:57 +020060static std::vector<menu_action_t>* current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070061static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070062
Tao Baoe5d2c252018-05-09 11:05:44 -070063static void PopulateMenuItems() {
64 g_menu_items.clear();
Tom Marshall645801f2020-03-29 14:36:57 +020065 std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items),
Tao Baoe5d2c252018-05-09 11:05:44 -070066 [](const auto& entry) { return entry.first; });
67}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070068
Tao Baoe5d2c252018-05-09 11:05:44 -070069Device::Device(RecoveryUI* ui) : ui_(ui) {
70 PopulateMenuItems();
71}
72
Tom Marshall645801f2020-03-29 14:36:57 +020073void Device::GoHome() {
74 current_menu_ = &g_main_actions;
Tao Baoe5d2c252018-05-09 11:05:44 -070075 PopulateMenuItems();
76}
Tao Bao1fe1afe2018-05-01 15:56:05 -070077
Tom Marshall645801f2020-03-29 14:36:57 +020078static void RemoveMenuItemForAction(std::vector<menu_action_t>& menu, Device::BuiltinAction action) {
79 menu.erase(
80 std::remove_if(menu.begin(), menu.end(),
81 [action](const auto& entry) { return entry.second == action; }), menu.end());
82 CHECK(!menu.empty());
83}
84
85void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
Tom Marshall645801f2020-03-29 14:36:57 +020086 ::RemoveMenuItemForAction(g_wipe_actions, action);
87 ::RemoveMenuItemForAction(g_advanced_actions, action);
88}
89
Tao Bao1fe1afe2018-05-01 15:56:05 -070090const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070091 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070092}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070093
Tom Marshall645801f2020-03-29 14:36:57 +020094const std::vector<std::string>& Device::GetMenuHeaders() {
Tom Marshallbead2612019-01-04 14:37:31 -080095 if (current_menu_ == &g_wipe_actions)
Tom Marshall645801f2020-03-29 14:36:57 +020096 return g_wipe_header;
Tom Marshallbead2612019-01-04 14:37:31 -080097 if (current_menu_ == &g_advanced_actions)
Tom Marshall645801f2020-03-29 14:36:57 +020098 return g_advanced_header;
99 return g_main_header;
100}
101
Tao Bao1fe1afe2018-05-01 15:56:05 -0700102Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tom Marshall645801f2020-03-29 14:36:57 +0200103 Device::BuiltinAction action = (*current_menu_)[menu_position].second;
104
105 if (action > MENU_BASE) {
106 switch (action) {
Tom Marshall645801f2020-03-29 14:36:57 +0200107 case Device::BuiltinAction::MENU_WIPE:
108 current_menu_ = &g_wipe_actions;
109 break;
110 case Device::BuiltinAction::MENU_ADVANCED:
111 current_menu_ = &g_advanced_actions;
112 break;
113 default:
114 break;
115 }
Tom Marshall645801f2020-03-29 14:36:57 +0200116 PopulateMenuItems();
117 }
118 return action;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700119}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700120
Tao Baofc5499f2017-02-23 19:06:53 -0800121int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700122 if (!visible) {
123 return kNoAction;
124 }
125
126 switch (key) {
Tom Marshalld23b5102017-08-24 13:50:01 +0000127 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700128 case KEY_DOWN:
129 case KEY_VOLUMEDOWN:
Tom Marshalld23b5102017-08-24 13:50:01 +0000130 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700131 return kHighlightDown;
132
133 case KEY_UP:
134 case KEY_VOLUMEUP:
Tom Marshalld23b5102017-08-24 13:50:01 +0000135 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700136 return kHighlightUp;
137
Tom Marshall7e2c6002020-03-10 20:17:49 +0100138 case KEY_SCROLLUP:
139 return kScrollUp;
140 case KEY_SCROLLDOWN:
141 return kScrollDown;
142
Elliott Hughes4af215b2015-04-10 15:00:34 -0700143 case KEY_ENTER:
144 case KEY_POWER:
Tom Marshalld23b5102017-08-24 13:50:01 +0000145 case BTN_MOUSE:
146 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700147 return kInvokeItem;
148
Tom Marshalld23b5102017-08-24 13:50:01 +0000149 case KEY_HOME:
150 case KEY_HOMEPAGE:
151 return kGoHome;
152
153 case KEY_BACKSPACE:
154 case KEY_BACK:
155 return kGoBack;
156
Tom Marshalla9ac9552018-12-17 15:57:44 -0800157 case KEY_REFRESH:
158 return kRefresh;
159
Alessandroe4c344a2020-04-11 12:11:29 +0200160 case KEY_AGAIN:
161 return kDoSideload;
162
Elliott Hughes4af215b2015-04-10 15:00:34 -0700163 default:
164 // If you have all of the above buttons, any other buttons
165 // are ignored. Otherwise, any button cycles the highlight.
166 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
167 }
168}