Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 3 | * Copyright (C) 2019 The LineageOS Project |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 4 | * |
| 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 Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 18 | #include "recovery_ui/device.h" |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 19 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <string> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 26 | |
Tianjie Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 27 | #include "otautil/boot_state.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 28 | #include "recovery_ui/ui.h" |
Tao Bao | c16fd8a | 2018-04-30 17:12:03 -0700 | [diff] [blame] | 29 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 30 | typedef std::pair<std::string, Device::BuiltinAction> menu_action_t; |
| 31 | |
| 32 | static std::vector<std::string> g_main_header{}; |
| 33 | static std::vector<menu_action_t> g_main_actions{ |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 34 | { "Reboot system now", Device::REBOOT }, |
Tom Marshall | 55220ba | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 35 | { "Apply update", Device::APPLY_UPDATE }, |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 36 | { "Factory reset", Device::MENU_WIPE }, |
| 37 | { "Advanced", Device::MENU_ADVANCED }, |
| 38 | }; |
| 39 | |
| 40 | static std::vector<std::string> g_advanced_header{ "Advanced options" }; |
| 41 | static std::vector<menu_action_t> g_advanced_actions{ |
| 42 | { "Enter fastboot", Device::ENTER_FASTBOOT }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 43 | { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, |
Michael Bestas | 24928fe | 2019-09-27 20:16:38 +0300 | [diff] [blame] | 44 | { "Reboot to recovery", Device::REBOOT_RECOVERY }, |
Michael Bestas | de51450 | 2021-01-16 18:16:47 +0200 | [diff] [blame] | 45 | { "Mount/unmount system", Device::MOUNT_SYSTEM }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 46 | { "View recovery logs", Device::VIEW_RECOVERY_LOGS }, |
LuK1337 | 6422b8f | 2020-04-12 19:04:59 +0200 | [diff] [blame] | 47 | { "Enable ADB", Device::ENABLE_ADB }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 48 | { "Run graphics test", Device::RUN_GRAPHICS_TEST }, |
| 49 | { "Run locale test", Device::RUN_LOCALE_TEST }, |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 50 | { "Enter rescue", Device::ENTER_RESCUE }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 51 | { "Power off", Device::SHUTDOWN }, |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 54 | static std::vector<std::string> g_wipe_header{ "Factory reset" }; |
| 55 | static std::vector<menu_action_t> g_wipe_actions{ |
Nolen Johnson | ea96a20 | 2020-04-12 14:13:31 -0400 | [diff] [blame] | 56 | { "Format data/factory reset", Device::WIPE_DATA }, |
| 57 | { "Format cache partition", Device::WIPE_CACHE }, |
| 58 | { "Format system partition", Device::WIPE_SYSTEM }, |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 61 | static std::vector<menu_action_t>* current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 62 | static std::vector<std::string> g_menu_items; |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 63 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 64 | static void PopulateMenuItems() { |
| 65 | g_menu_items.clear(); |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 66 | std::transform(current_menu_->cbegin(), current_menu_->cend(), std::back_inserter(g_menu_items), |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 67 | [](const auto& entry) { return entry.first; }); |
| 68 | } |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 69 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 70 | Device::Device(RecoveryUI* ui) : ui_(ui) { |
Tom Marshall | 914603d | 2018-06-21 00:57:24 +0200 | [diff] [blame] | 71 | ui->SetDevice(this); |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 72 | PopulateMenuItems(); |
| 73 | } |
| 74 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 75 | void Device::GoHome() { |
| 76 | current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 77 | PopulateMenuItems(); |
| 78 | } |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 79 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 80 | static 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 | |
| 87 | void Device::RemoveMenuItemForAction(Device::BuiltinAction action) { |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 88 | ::RemoveMenuItemForAction(g_wipe_actions, action); |
| 89 | ::RemoveMenuItemForAction(g_advanced_actions, action); |
| 90 | } |
| 91 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 92 | const std::vector<std::string>& Device::GetMenuItems() { |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 93 | return g_menu_items; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 94 | } |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 95 | |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 96 | const std::vector<std::string>& Device::GetMenuHeaders() { |
Tom Marshall | 55220ba | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 97 | if (current_menu_ == &g_wipe_actions) |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 98 | return g_wipe_header; |
Tom Marshall | 55220ba | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 99 | if (current_menu_ == &g_advanced_actions) |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 100 | return g_advanced_header; |
| 101 | return g_main_header; |
| 102 | } |
| 103 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 104 | Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 105 | Device::BuiltinAction action = (*current_menu_)[menu_position].second; |
| 106 | |
| 107 | if (action > MENU_BASE) { |
| 108 | switch (action) { |
Tom Marshall | 88fe6f5 | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 109 | 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 Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 121 | } |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 122 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 123 | int Device::HandleMenuKey(int key, bool visible) { |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 124 | if (!visible) { |
| 125 | return kNoAction; |
| 126 | } |
| 127 | |
| 128 | switch (key) { |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 129 | case KEY_RIGHTSHIFT: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 130 | case KEY_DOWN: |
| 131 | case KEY_VOLUMEDOWN: |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 132 | case KEY_MENU: |
Aaron Kling | 53b26ef | 2019-07-22 14:23:40 -0500 | [diff] [blame] | 133 | case BTN_NORTH: |
| 134 | case BTN_DPAD_DOWN: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 135 | return kHighlightDown; |
| 136 | |
| 137 | case KEY_UP: |
| 138 | case KEY_VOLUMEUP: |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 139 | case KEY_SEARCH: |
Aaron Kling | 53b26ef | 2019-07-22 14:23:40 -0500 | [diff] [blame] | 140 | case BTN_WEST: |
| 141 | case BTN_DPAD_UP: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 142 | return kHighlightUp; |
| 143 | |
Tom Marshall | 53176c7 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 144 | case KEY_SCROLLUP: |
| 145 | return kScrollUp; |
| 146 | case KEY_SCROLLDOWN: |
| 147 | return kScrollDown; |
| 148 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 149 | case KEY_ENTER: |
| 150 | case KEY_POWER: |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 151 | case BTN_MOUSE: |
| 152 | case KEY_SEND: |
Aaron Kling | 53b26ef | 2019-07-22 14:23:40 -0500 | [diff] [blame] | 153 | case BTN_SOUTH: |
| 154 | case BTN_START: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 155 | return kInvokeItem; |
| 156 | |
Tom Marshall | d13a8c5 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 157 | case KEY_HOME: |
| 158 | case KEY_HOMEPAGE: |
| 159 | return kGoHome; |
| 160 | |
| 161 | case KEY_BACKSPACE: |
| 162 | case KEY_BACK: |
| 163 | return kGoBack; |
| 164 | |
Tom Marshall | 90e09f8 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 165 | case KEY_AGAIN: |
| 166 | return kDoSideload; |
| 167 | |
Tom Marshall | 55220ba | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 168 | case KEY_REFRESH: |
| 169 | return kRefresh; |
| 170 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 171 | 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 Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 177 | |
| 178 | void Device::SetBootState(const BootState* state) { |
| 179 | boot_state_ = state; |
| 180 | } |
| 181 | |
| 182 | std::optional<std::string> Device::GetReason() const { |
| 183 | return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt; |
| 184 | } |
| 185 | |
| 186 | std::optional<std::string> Device::GetStage() const { |
| 187 | return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt; |
| 188 | } |