Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Tom Marshall | d23b510 | 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 | b5108c3 | 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 | b5108c3 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 27 | #include "recovery_ui/ui.h" |
Tao Bao | c16fd8a | 2018-04-30 17:12:03 -0700 | [diff] [blame] | 28 | |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 29 | typedef std::pair<std::string, Device::BuiltinAction> menu_action_t; |
| 30 | |
| 31 | static std::vector<std::string> g_main_header{}; |
| 32 | static std::vector<menu_action_t> g_main_actions{ |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 33 | { "Reboot system now", Device::REBOOT }, |
Tom Marshall | bead261 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 34 | { "Apply update", Device::APPLY_UPDATE }, |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 35 | { "Factory reset", Device::MENU_WIPE }, |
| 36 | { "Advanced", Device::MENU_ADVANCED }, |
| 37 | }; |
| 38 | |
| 39 | static std::vector<std::string> g_advanced_header{ "Advanced options" }; |
| 40 | static std::vector<menu_action_t> g_advanced_actions{ |
| 41 | { "Enter fastboot", Device::ENTER_FASTBOOT }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 42 | { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, |
Michael Bestas | b3e3ad6 | 2019-09-27 20:16:38 +0300 | [diff] [blame] | 43 | { "Reboot to recovery", Device::REBOOT_RECOVERY }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 44 | { "Mount /system", Device::MOUNT_SYSTEM }, |
| 45 | { "View recovery logs", Device::VIEW_RECOVERY_LOGS }, |
LuK1337 | 59e0459 | 2020-04-12 19:04:59 +0200 | [diff] [blame] | 46 | { "Enable ADB", Device::ENABLE_ADB }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 47 | { "Run graphics test", Device::RUN_GRAPHICS_TEST }, |
| 48 | { "Run locale test", Device::RUN_LOCALE_TEST }, |
Tao Bao | 378bfbf | 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 | 645801f | 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 | |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 60 | static std::vector<menu_action_t>* current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 61 | static std::vector<std::string> g_menu_items; |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 62 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 63 | static void PopulateMenuItems() { |
| 64 | g_menu_items.clear(); |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 65 | 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] | 66 | [](const auto& entry) { return entry.first; }); |
| 67 | } |
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 | Device::Device(RecoveryUI* ui) : ui_(ui) { |
| 70 | PopulateMenuItems(); |
| 71 | } |
| 72 | |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 73 | void Device::GoHome() { |
| 74 | current_menu_ = &g_main_actions; |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 75 | PopulateMenuItems(); |
| 76 | } |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 77 | |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 78 | static 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 | |
| 85 | void Device::RemoveMenuItemForAction(Device::BuiltinAction action) { |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 86 | ::RemoveMenuItemForAction(g_wipe_actions, action); |
| 87 | ::RemoveMenuItemForAction(g_advanced_actions, action); |
| 88 | } |
| 89 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 90 | const std::vector<std::string>& Device::GetMenuItems() { |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 91 | return g_menu_items; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 92 | } |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 93 | |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 94 | const std::vector<std::string>& Device::GetMenuHeaders() { |
Tom Marshall | bead261 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 95 | if (current_menu_ == &g_wipe_actions) |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 96 | return g_wipe_header; |
Tom Marshall | bead261 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 97 | if (current_menu_ == &g_advanced_actions) |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 98 | return g_advanced_header; |
| 99 | return g_main_header; |
| 100 | } |
| 101 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 102 | Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 103 | Device::BuiltinAction action = (*current_menu_)[menu_position].second; |
| 104 | |
| 105 | if (action > MENU_BASE) { |
| 106 | switch (action) { |
Tom Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 107 | 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 Marshall | 645801f | 2020-03-29 14:36:57 +0200 | [diff] [blame] | 116 | PopulateMenuItems(); |
| 117 | } |
| 118 | return action; |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 119 | } |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 120 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 121 | int Device::HandleMenuKey(int key, bool visible) { |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 122 | if (!visible) { |
| 123 | return kNoAction; |
| 124 | } |
| 125 | |
| 126 | switch (key) { |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 127 | case KEY_RIGHTSHIFT: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 128 | case KEY_DOWN: |
| 129 | case KEY_VOLUMEDOWN: |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 130 | case KEY_MENU: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 131 | return kHighlightDown; |
| 132 | |
| 133 | case KEY_UP: |
| 134 | case KEY_VOLUMEUP: |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 135 | case KEY_SEARCH: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 136 | return kHighlightUp; |
| 137 | |
Tom Marshall | 7e2c600 | 2020-03-10 20:17:49 +0100 | [diff] [blame] | 138 | case KEY_SCROLLUP: |
| 139 | return kScrollUp; |
| 140 | case KEY_SCROLLDOWN: |
| 141 | return kScrollDown; |
| 142 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 143 | case KEY_ENTER: |
| 144 | case KEY_POWER: |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 145 | case BTN_MOUSE: |
| 146 | case KEY_SEND: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 147 | return kInvokeItem; |
| 148 | |
Tom Marshall | d23b510 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 149 | case KEY_HOME: |
| 150 | case KEY_HOMEPAGE: |
| 151 | return kGoHome; |
| 152 | |
| 153 | case KEY_BACKSPACE: |
| 154 | case KEY_BACK: |
| 155 | return kGoBack; |
| 156 | |
Tom Marshall | a9ac955 | 2018-12-17 15:57:44 -0800 | [diff] [blame] | 157 | case KEY_REFRESH: |
| 158 | return kRefresh; |
| 159 | |
Alessandro | e4c344a | 2020-04-11 12:11:29 +0200 | [diff] [blame] | 160 | case KEY_AGAIN: |
| 161 | return kDoSideload; |
| 162 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 163 | 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 | } |