Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Tom Marshall | 74f4824 | 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 | |
| 18 | #include "device.h" |
| 19 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 20 | #define ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A))) |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 21 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 22 | // *** Main menu *** |
| 23 | static const menu_type_t MAIN_MENU_TYPE = MT_GRID; |
| 24 | static const MenuItem MAIN_MENU_ITEMS[] = { |
| 25 | MenuItem("Reboot", "ic_reboot", "ic_reboot_sel"), |
| 26 | MenuItem("Apply update", "ic_system_update", "ic_system_update_sel"), |
| 27 | MenuItem("Factory reset", "ic_factory_reset", "ic_factory_reset_sel"), |
| 28 | MenuItem("Advanced", "ic_options_advanced", "ic_options_advanced_sel"), |
| 29 | }; |
| 30 | static const MenuItemVector main_menu_items_ = |
| 31 | MenuItemVector(MAIN_MENU_ITEMS, MAIN_MENU_ITEMS + ARRAY_SIZE(MAIN_MENU_ITEMS)); |
| 32 | static const Device::BuiltinAction MAIN_MENU_ACTIONS[] = { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 33 | Device::REBOOT, |
Tom Marshall | a08c6f1 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 34 | Device::APPLY_UPDATE, |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 35 | Device::WIPE_MENU, |
| 36 | Device::ADVANCED_MENU, |
| 37 | }; |
| 38 | static const Device::MenuActionVector main_menu_actions_ = |
| 39 | Device::MenuActionVector(MAIN_MENU_ACTIONS, MAIN_MENU_ACTIONS + ARRAY_SIZE(MAIN_MENU_ACTIONS)); |
| 40 | static_assert(ARRAY_SIZE(MAIN_MENU_ITEMS) == ARRAY_SIZE(MAIN_MENU_ACTIONS), |
| 41 | "MAIN_MENU_ITEMS and MAIN_MENU_ACTIONS should have the same length."); |
| 42 | |
| 43 | // *** Wipe menu *** |
| 44 | static const menu_type_t WIPE_MENU_TYPE = MT_LIST; |
| 45 | static const MenuItem WIPE_MENU_ITEMS[] = { |
| 46 | MenuItem("Wipe data / factory reset"), |
| 47 | #ifndef AB_OTA_UPDATER |
| 48 | MenuItem("Wipe cache"), |
| 49 | #endif |
| 50 | MenuItem("Wipe system"), |
| 51 | }; |
| 52 | static const MenuItemVector wipe_menu_items_ = |
| 53 | MenuItemVector(WIPE_MENU_ITEMS, WIPE_MENU_ITEMS + ARRAY_SIZE(WIPE_MENU_ITEMS)); |
| 54 | static const Device::BuiltinAction WIPE_MENU_ACTIONS[] = { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 55 | Device::WIPE_DATA, |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 56 | #ifndef AB_OTA_UPDATER |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 57 | Device::WIPE_CACHE, |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 58 | #endif |
Dan Pasanen | dc95db8 | 2017-04-04 13:40:19 -0500 | [diff] [blame] | 59 | Device::WIPE_SYSTEM, |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 60 | }; |
| 61 | static const Device::MenuActionVector wipe_menu_actions_ = |
| 62 | Device::MenuActionVector(WIPE_MENU_ACTIONS, WIPE_MENU_ACTIONS + ARRAY_SIZE(WIPE_MENU_ACTIONS)); |
| 63 | static_assert(ARRAY_SIZE(WIPE_MENU_ITEMS) == ARRAY_SIZE(WIPE_MENU_ACTIONS), |
| 64 | "WIPE_MENU_ITEMS and WIPE_MENU_ACTIONS should have the same length."); |
| 65 | |
| 66 | // *** Advanced menu |
| 67 | static const menu_type_t ADVANCED_MENU_TYPE = MT_LIST; |
| 68 | |
| 69 | static const MenuItem ADVANCED_MENU_ITEMS[] = { |
| 70 | #ifdef DOWNLOAD_MODE |
| 71 | MenuItem("Reboot to download mode"), |
| 72 | #else |
| 73 | MenuItem("Reboot to bootloader"), |
| 74 | #endif |
jrior001 | 25f976d | 2019-03-24 17:57:07 -0400 | [diff] [blame] | 75 | MenuItem("Reboot to recovery"), |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 76 | MenuItem("Mount system"), |
| 77 | MenuItem("View logs"), |
Alessandro Astone | b3552c5 | 2019-03-29 17:16:56 +0100 | [diff] [blame] | 78 | #ifdef SHOW_TESTS |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 79 | MenuItem("Run graphics test"), |
| 80 | MenuItem("Run locale test"), |
Alessandro Astone | b3552c5 | 2019-03-29 17:16:56 +0100 | [diff] [blame] | 81 | #endif |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 82 | MenuItem("Power off"), |
| 83 | }; |
| 84 | static const MenuItemVector advanced_menu_items_ = |
| 85 | MenuItemVector(ADVANCED_MENU_ITEMS, ADVANCED_MENU_ITEMS + ARRAY_SIZE(ADVANCED_MENU_ITEMS)); |
| 86 | |
| 87 | static const Device::BuiltinAction ADVANCED_MENU_ACTIONS[] = { |
| 88 | Device::REBOOT_BOOTLOADER, |
jrior001 | 25f976d | 2019-03-24 17:57:07 -0400 | [diff] [blame] | 89 | Device::REBOOT_RECOVERY, |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 90 | Device::MOUNT_SYSTEM, |
| 91 | Device::VIEW_RECOVERY_LOGS, |
Alessandro Astone | b3552c5 | 2019-03-29 17:16:56 +0100 | [diff] [blame] | 92 | #ifdef SHOW_TESTS |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 93 | Device::RUN_GRAPHICS_TEST, |
| 94 | Device::RUN_LOCALE_TEST, |
Alessandro Astone | b3552c5 | 2019-03-29 17:16:56 +0100 | [diff] [blame] | 95 | #endif |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 96 | Device::SHUTDOWN, |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 97 | }; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 98 | static const Device::MenuActionVector advanced_menu_actions_ = Device::MenuActionVector( |
| 99 | ADVANCED_MENU_ACTIONS, ADVANCED_MENU_ACTIONS + ARRAY_SIZE(ADVANCED_MENU_ACTIONS)); |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 100 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 101 | static_assert(ARRAY_SIZE(ADVANCED_MENU_ITEMS) == ARRAY_SIZE(ADVANCED_MENU_ACTIONS), |
| 102 | "ADVANCED_MENU_ITEMS and ADVANCED_MENU_ACTIONS should have the same length."); |
Elliott Hughes | 01fcbe1 | 2016-05-23 17:43:41 -0700 | [diff] [blame] | 103 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 104 | Device::Device(RecoveryUI* ui) : ui_(ui) { |
| 105 | GoHome(); |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 106 | } |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 107 | |
| 108 | Device::BuiltinAction Device::InvokeMenuItem(int menu_position) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 109 | if (menu_position < 0) { |
| 110 | if (menu_position == Device::kGoBack || menu_position == Device::kGoHome) { |
| 111 | // Assume only two menu levels, so back is equivalent to home. |
| 112 | GoHome(); |
| 113 | } |
| 114 | return NO_ACTION; |
| 115 | } |
| 116 | BuiltinAction action = menu_actions_.at(menu_position); |
| 117 | switch (action) { |
| 118 | case WIPE_MENU: |
| 119 | menu_is_main_ = false; |
| 120 | menu_type_ = WIPE_MENU_TYPE; |
| 121 | menu_items_ = wipe_menu_items_; |
| 122 | menu_actions_ = wipe_menu_actions_; |
| 123 | break; |
| 124 | case ADVANCED_MENU: |
| 125 | menu_is_main_ = false; |
| 126 | menu_type_ = ADVANCED_MENU_TYPE; |
| 127 | menu_items_ = advanced_menu_items_; |
| 128 | menu_actions_ = advanced_menu_actions_; |
| 129 | break; |
| 130 | default: |
| 131 | break; // Fall through |
| 132 | } |
| 133 | return action; |
| 134 | } |
| 135 | |
| 136 | void Device::GoHome() { |
| 137 | menu_is_main_ = true; |
| 138 | menu_type_ = MAIN_MENU_TYPE; |
| 139 | menu_items_ = main_menu_items_; |
| 140 | menu_actions_ = main_menu_actions_; |
Elliott Hughes | 9e7ae8a | 2015-04-09 13:40:31 -0700 | [diff] [blame] | 141 | } |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 142 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 143 | int Device::HandleMenuKey(int key, bool visible) { |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 144 | if (!visible) { |
| 145 | return kNoAction; |
| 146 | } |
| 147 | |
| 148 | switch (key) { |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 149 | case KEY_RIGHTSHIFT: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 150 | case KEY_DOWN: |
| 151 | case KEY_VOLUMEDOWN: |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 152 | case KEY_MENU: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 153 | return kHighlightDown; |
| 154 | |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 155 | case KEY_LEFTSHIFT: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 156 | case KEY_UP: |
| 157 | case KEY_VOLUMEUP: |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 158 | case KEY_SEARCH: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 159 | return kHighlightUp; |
| 160 | |
Alessandro Astone | d6eb74e | 2019-03-28 22:02:02 +0100 | [diff] [blame] | 161 | case KEY_SCROLLUP: |
| 162 | return kScrollUp; |
| 163 | case KEY_SCROLLDOWN: |
| 164 | return kScrollDown; |
| 165 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 166 | case KEY_ENTER: |
| 167 | case KEY_POWER: |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 168 | case BTN_MOUSE: |
| 169 | case KEY_SEND: |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 170 | return kInvokeItem; |
| 171 | |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 172 | case KEY_HOME: |
| 173 | case KEY_HOMEPAGE: |
| 174 | return kGoHome; |
| 175 | |
| 176 | case KEY_BACKSPACE: |
| 177 | case KEY_BACK: |
| 178 | return kGoBack; |
| 179 | |
Tom Marshall | a08c6f1 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 180 | case KEY_REFRESH: |
| 181 | return kRefresh; |
| 182 | |
Elliott Hughes | 4af215b | 2015-04-10 15:00:34 -0700 | [diff] [blame] | 183 | default: |
| 184 | // If you have all of the above buttons, any other buttons |
| 185 | // are ignored. Otherwise, any button cycles the highlight. |
| 186 | return ui_->HasThreeButtons() ? kNoAction : kHighlightDown; |
| 187 | } |
| 188 | } |