blob: 7d3e11f1ffdbff86e58a569b7471a789619d73d9 [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshall74f48242017-08-24 13:50:01 +00003 * Copyright (C) 2019 The LineageOS Project
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07004 *
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 Marshall820517b2019-01-14 14:28:55 -080020#define ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A)))
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070021
Tom Marshall820517b2019-01-14 14:28:55 -080022// *** Main menu ***
23static const menu_type_t MAIN_MENU_TYPE = MT_GRID;
24static 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};
30static const MenuItemVector main_menu_items_ =
31 MenuItemVector(MAIN_MENU_ITEMS, MAIN_MENU_ITEMS + ARRAY_SIZE(MAIN_MENU_ITEMS));
32static const Device::BuiltinAction MAIN_MENU_ACTIONS[] = {
Tianjie Xu29d55752017-09-20 17:53:46 -070033 Device::REBOOT,
Tom Marshalla08c6f12019-01-04 14:37:31 -080034 Device::APPLY_UPDATE,
Tom Marshall820517b2019-01-14 14:28:55 -080035 Device::WIPE_MENU,
36 Device::ADVANCED_MENU,
37};
38static const Device::MenuActionVector main_menu_actions_ =
39 Device::MenuActionVector(MAIN_MENU_ACTIONS, MAIN_MENU_ACTIONS + ARRAY_SIZE(MAIN_MENU_ACTIONS));
40static_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 ***
44static const menu_type_t WIPE_MENU_TYPE = MT_LIST;
45static 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};
52static const MenuItemVector wipe_menu_items_ =
53 MenuItemVector(WIPE_MENU_ITEMS, WIPE_MENU_ITEMS + ARRAY_SIZE(WIPE_MENU_ITEMS));
54static const Device::BuiltinAction WIPE_MENU_ACTIONS[] = {
Tianjie Xu29d55752017-09-20 17:53:46 -070055 Device::WIPE_DATA,
Alex Deymo4344d632016-08-03 21:03:53 -070056#ifndef AB_OTA_UPDATER
Tianjie Xu29d55752017-09-20 17:53:46 -070057 Device::WIPE_CACHE,
Tom Marshall820517b2019-01-14 14:28:55 -080058#endif
Dan Pasanendc95db82017-04-04 13:40:19 -050059 Device::WIPE_SYSTEM,
Tom Marshall820517b2019-01-14 14:28:55 -080060};
61static const Device::MenuActionVector wipe_menu_actions_ =
62 Device::MenuActionVector(WIPE_MENU_ACTIONS, WIPE_MENU_ACTIONS + ARRAY_SIZE(WIPE_MENU_ACTIONS));
63static_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
67static const menu_type_t ADVANCED_MENU_TYPE = MT_LIST;
68
69static const MenuItem ADVANCED_MENU_ITEMS[] = {
70#ifdef DOWNLOAD_MODE
71 MenuItem("Reboot to download mode"),
72#else
73 MenuItem("Reboot to bootloader"),
74#endif
jrior00125f976d2019-03-24 17:57:07 -040075 MenuItem("Reboot to recovery"),
Tom Marshall820517b2019-01-14 14:28:55 -080076 MenuItem("Mount system"),
77 MenuItem("View logs"),
Alessandro Astoneb3552c52019-03-29 17:16:56 +010078#ifdef SHOW_TESTS
Tom Marshall820517b2019-01-14 14:28:55 -080079 MenuItem("Run graphics test"),
80 MenuItem("Run locale test"),
Alessandro Astoneb3552c52019-03-29 17:16:56 +010081#endif
Tom Marshall820517b2019-01-14 14:28:55 -080082 MenuItem("Power off"),
83};
84static const MenuItemVector advanced_menu_items_ =
85 MenuItemVector(ADVANCED_MENU_ITEMS, ADVANCED_MENU_ITEMS + ARRAY_SIZE(ADVANCED_MENU_ITEMS));
86
87static const Device::BuiltinAction ADVANCED_MENU_ACTIONS[] = {
88 Device::REBOOT_BOOTLOADER,
jrior00125f976d2019-03-24 17:57:07 -040089 Device::REBOOT_RECOVERY,
Tianjie Xu29d55752017-09-20 17:53:46 -070090 Device::MOUNT_SYSTEM,
91 Device::VIEW_RECOVERY_LOGS,
Alessandro Astoneb3552c52019-03-29 17:16:56 +010092#ifdef SHOW_TESTS
Tianjie Xu29d55752017-09-20 17:53:46 -070093 Device::RUN_GRAPHICS_TEST,
94 Device::RUN_LOCALE_TEST,
Alessandro Astoneb3552c52019-03-29 17:16:56 +010095#endif
Tianjie Xu29d55752017-09-20 17:53:46 -070096 Device::SHUTDOWN,
Elliott Hughes01fcbe12016-05-23 17:43:41 -070097};
Tom Marshall820517b2019-01-14 14:28:55 -080098static const Device::MenuActionVector advanced_menu_actions_ = Device::MenuActionVector(
99 ADVANCED_MENU_ACTIONS, ADVANCED_MENU_ACTIONS + ARRAY_SIZE(ADVANCED_MENU_ACTIONS));
Elliott Hughes01fcbe12016-05-23 17:43:41 -0700100
Tom Marshall820517b2019-01-14 14:28:55 -0800101static_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 Hughes01fcbe12016-05-23 17:43:41 -0700103
Tom Marshall820517b2019-01-14 14:28:55 -0800104Device::Device(RecoveryUI* ui) : ui_(ui) {
105 GoHome();
Elliott Hughes4af215b2015-04-10 15:00:34 -0700106}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -0700107
108Device::BuiltinAction Device::InvokeMenuItem(int menu_position) {
Tom Marshall820517b2019-01-14 14:28:55 -0800109 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
136void 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 Hughes9e7ae8a2015-04-09 13:40:31 -0700141}
Elliott Hughes4af215b2015-04-10 15:00:34 -0700142
Tao Baofc5499f2017-02-23 19:06:53 -0800143int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -0700144 if (!visible) {
145 return kNoAction;
146 }
147
148 switch (key) {
Tom Marshall74f48242017-08-24 13:50:01 +0000149 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700150 case KEY_DOWN:
151 case KEY_VOLUMEDOWN:
Tom Marshall74f48242017-08-24 13:50:01 +0000152 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700153 return kHighlightDown;
154
Tom Marshall74f48242017-08-24 13:50:01 +0000155 case KEY_LEFTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700156 case KEY_UP:
157 case KEY_VOLUMEUP:
Tom Marshall74f48242017-08-24 13:50:01 +0000158 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700159 return kHighlightUp;
160
Alessandro Astoned6eb74e2019-03-28 22:02:02 +0100161 case KEY_SCROLLUP:
162 return kScrollUp;
163 case KEY_SCROLLDOWN:
164 return kScrollDown;
165
Elliott Hughes4af215b2015-04-10 15:00:34 -0700166 case KEY_ENTER:
167 case KEY_POWER:
Tom Marshall74f48242017-08-24 13:50:01 +0000168 case BTN_MOUSE:
169 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -0700170 return kInvokeItem;
171
Tom Marshall74f48242017-08-24 13:50:01 +0000172 case KEY_HOME:
173 case KEY_HOMEPAGE:
174 return kGoHome;
175
176 case KEY_BACKSPACE:
177 case KEY_BACK:
178 return kGoBack;
179
Tom Marshalla08c6f12019-01-04 14:37:31 -0800180 case KEY_REFRESH:
181 return kRefresh;
182
Elliott Hughes4af215b2015-04-10 15:00:34 -0700183 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}