Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 17 | #include "recovery_ui/device.h" |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 18 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 25 | |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 26 | #include "recovery_ui/ui.h" |
Tao Bao | c16fd8a | 2018-04-30 17:12:03 -0700 | [diff] [blame] | 27 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 28 | static std::vector<std::pair<std::string, Device::BuiltinAction>> g_menu_actions{ |
| 29 | { "Reboot system now", Device::REBOOT }, |
| 30 | { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 31 | { "Enter fastboot", Device::ENTER_FASTBOOT }, |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 32 | { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD }, |
| 33 | { "Apply update from SD card", Device::APPLY_SDCARD }, |
| 34 | { "Wipe data/factory reset", Device::WIPE_DATA }, |
| 35 | { "Wipe cache partition", Device::WIPE_CACHE }, |
| 36 | { "Mount /system", Device::MOUNT_SYSTEM }, |
| 37 | { "View recovery logs", Device::VIEW_RECOVERY_LOGS }, |
| 38 | { "Run graphics test", Device::RUN_GRAPHICS_TEST }, |
| 39 | { "Run locale test", Device::RUN_LOCALE_TEST }, |
| 40 | { "Power off", Device::SHUTDOWN }, |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 43 | static std::vector<std::string> g_menu_items; |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 44 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 45 | static void PopulateMenuItems() { |
| 46 | g_menu_items.clear(); |
| 47 | std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items), |
| 48 | [](const auto& entry) { return entry.first; }); |
| 49 | } |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 50 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 51 | Device::Device(RecoveryUI* ui) : ui_(ui) { |
| 52 | PopulateMenuItems(); |
| 53 | } |
| 54 | |
| 55 | void Device::RemoveMenuItemForAction(Device::BuiltinAction action) { |
| 56 | g_menu_actions.erase( |
| 57 | std::remove_if(g_menu_actions.begin(), g_menu_actions.end(), |
| 58 | [action](const auto& entry) { return entry.second == action; })); |
| 59 | CHECK(!g_menu_actions.empty()); |
| 60 | |
| 61 | // Re-populate the menu items. |
| 62 | PopulateMenuItems(); |
| 63 | } |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 64 | |
| 65 | const std::vector<std::string>& Device::GetMenuItems() { |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 66 | return g_menu_items; |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 67 | } |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 68 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 69 | Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 70 | return g_menu_actions[menu_position].second; |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 71 | } |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 72 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 73 | int Device::HandleMenuKey(int key, bool visible) { |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 74 | if (!visible) { |
| 75 | return kNoAction; |
| 76 | } |
| 77 | |
| 78 | switch (key) { |
| 79 | case KEY_DOWN: |
| 80 | case KEY_VOLUMEDOWN: |
| 81 | return kHighlightDown; |
| 82 | |
| 83 | case KEY_UP: |
| 84 | case KEY_VOLUMEUP: |
| 85 | return kHighlightUp; |
| 86 | |
| 87 | case KEY_ENTER: |
| 88 | case KEY_POWER: |
| 89 | return kInvokeItem; |
| 90 | |
| 91 | default: |
| 92 | // If you have all of the above buttons, any other buttons |
| 93 | // are ignored. Otherwise, any button cycles the highlight. |
| 94 | return ui_->HasThreeButtons() ? kNoAction : kHighlightDown; |
| 95 | } |
| 96 | } |