Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Tom Marshall | 4b92169 | 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 | be246fe | 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 | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 35 | { "Apply update", Device::MENU_UPDATE }, |
| 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 | a89d988 | 2019-09-27 20:16:38 +0300 | [diff] [blame] | 44 | { "Reboot to recovery", Device::REBOOT_RECOVERY }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 45 | { "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 Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 49 | { "Enter rescue", Device::ENTER_RESCUE }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 50 | { "Power off", Device::SHUTDOWN }, |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 53 | static std::vector<std::string> g_wipe_header{ "Factory reset" }; |
| 54 | static 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 | |
| 60 | static std::vector<std::string> g_update_header{ "Apply update" }; |
| 61 | static 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 | |
| 66 | static std::vector<menu_action_t>* current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 67 | static std::vector<std::string> g_menu_items; |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 68 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 69 | static void PopulateMenuItems() { |
| 70 | g_menu_items.clear(); |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 71 | 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] | 72 | [](const auto& entry) { return entry.first; }); |
| 73 | } |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 74 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 75 | Device::Device(RecoveryUI* ui) : ui_(ui) { |
| 76 | PopulateMenuItems(); |
| 77 | } |
| 78 | |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 79 | void Device::GoHome() { |
| 80 | current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 81 | PopulateMenuItems(); |
| 82 | } |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 83 | |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 84 | static 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 | |
| 91 | void 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 Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 97 | const std::vector<std::string>& Device::GetMenuItems() { |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 98 | return g_menu_items; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 99 | } |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 100 | |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 101 | const 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 Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 111 | Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { |
Tom Marshall | be246fe | 2020-03-29 14:36:57 +0200 | [diff] [blame^] | 112 | 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 Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 131 | } |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 132 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 133 | int Device::HandleMenuKey(int key, bool visible) { |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 134 | if (!visible) { |
| 135 | return kNoAction; |
| 136 | } |
| 137 | |
| 138 | switch (key) { |
Tom Marshall | 4b92169 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 139 | case KEY_RIGHTSHIFT: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 140 | case KEY_DOWN: |
| 141 | case KEY_VOLUMEDOWN: |
Tom Marshall | 4b92169 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 142 | case KEY_MENU: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 143 | return kHighlightDown; |
| 144 | |
| 145 | case KEY_UP: |
| 146 | case KEY_VOLUMEUP: |
Tom Marshall | 4b92169 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 147 | case KEY_SEARCH: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 148 | return kHighlightUp; |
| 149 | |
Alessandro Astone | 7396141 | 2020-10-04 18:11:40 +0200 | [diff] [blame] | 150 | case KEY_SCROLLUP: |
| 151 | return kScrollUp; |
| 152 | case KEY_SCROLLDOWN: |
| 153 | return kScrollDown; |
| 154 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 155 | case KEY_ENTER: |
| 156 | case KEY_POWER: |
Tom Marshall | 4b92169 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 157 | case BTN_MOUSE: |
| 158 | case KEY_SEND: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 159 | return kInvokeItem; |
| 160 | |
Tom Marshall | 4b92169 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 161 | case KEY_HOME: |
| 162 | case KEY_HOMEPAGE: |
| 163 | return kGoHome; |
| 164 | |
| 165 | case KEY_BACKSPACE: |
| 166 | case KEY_BACK: |
| 167 | return kGoBack; |
| 168 | |
Tom Marshall | fdf5033 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 169 | case KEY_AGAIN: |
| 170 | return kDoSideload; |
| 171 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 172 | 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 Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 178 | |
| 179 | void Device::SetBootState(const BootState* state) { |
| 180 | boot_state_ = state; |
| 181 | } |
| 182 | |
| 183 | std::optional<std::string> Device::GetReason() const { |
| 184 | return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt; |
| 185 | } |
| 186 | |
| 187 | std::optional<std::string> Device::GetStage() const { |
| 188 | return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt; |
| 189 | } |