blob: c09411d66213b3b1b930b42dac5b0b4c9a47eff8 [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Tom Marshalld23b5102017-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
Tianjie Xub5108c32018-08-20 13:40:47 -070018#include "recovery_ui/device.h"
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070019
Tao Baoe5d2c252018-05-09 11:05:44 -070020#include <algorithm>
21#include <string>
22#include <utility>
23#include <vector>
24
Tao Bao1fe1afe2018-05-01 15:56:05 -070025#include <android-base/logging.h>
Tao Bao1fe1afe2018-05-01 15:56:05 -070026
Tianjie Xub5108c32018-08-20 13:40:47 -070027#include "recovery_ui/ui.h"
Tao Baoc16fd8a2018-04-30 17:12:03 -070028
Tao Baoe5d2c252018-05-09 11:05:44 -070029static std::vector<std::pair<std::string, Device::BuiltinAction>> g_menu_actions{
30 { "Reboot system now", Device::REBOOT },
31 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070032 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070033 { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD },
34 { "Apply update from SD card", Device::APPLY_SDCARD },
35 { "Wipe data/factory reset", Device::WIPE_DATA },
36 { "Wipe cache partition", Device::WIPE_CACHE },
Michael Bestasc209c072019-09-27 19:52:13 +030037 { "Wipe system partition", Device::WIPE_SYSTEM },
Tao Baoe5d2c252018-05-09 11:05:44 -070038 { "Mount /system", Device::MOUNT_SYSTEM },
39 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
40 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
41 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Bao378bfbf2019-04-16 14:22:25 -070042 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070043 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070044};
45
Tao Baoe5d2c252018-05-09 11:05:44 -070046static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070047
Tao Baoe5d2c252018-05-09 11:05:44 -070048static void PopulateMenuItems() {
49 g_menu_items.clear();
50 std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items),
51 [](const auto& entry) { return entry.first; });
52}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070053
Tao Baoe5d2c252018-05-09 11:05:44 -070054Device::Device(RecoveryUI* ui) : ui_(ui) {
55 PopulateMenuItems();
56}
57
58void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
59 g_menu_actions.erase(
60 std::remove_if(g_menu_actions.begin(), g_menu_actions.end(),
61 [action](const auto& entry) { return entry.second == action; }));
62 CHECK(!g_menu_actions.empty());
63
64 // Re-populate the menu items.
65 PopulateMenuItems();
66}
Tao Bao1fe1afe2018-05-01 15:56:05 -070067
68const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070069 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070070}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070071
Tao Bao1fe1afe2018-05-01 15:56:05 -070072Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tao Baoe5d2c252018-05-09 11:05:44 -070073 return g_menu_actions[menu_position].second;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070074}
Elliott Hughes4af215b2015-04-10 15:00:34 -070075
Tao Baofc5499f2017-02-23 19:06:53 -080076int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -070077 if (!visible) {
78 return kNoAction;
79 }
80
81 switch (key) {
Tom Marshalld23b5102017-08-24 13:50:01 +000082 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -070083 case KEY_DOWN:
84 case KEY_VOLUMEDOWN:
Tom Marshalld23b5102017-08-24 13:50:01 +000085 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -070086 return kHighlightDown;
87
88 case KEY_UP:
89 case KEY_VOLUMEUP:
Tom Marshalld23b5102017-08-24 13:50:01 +000090 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -070091 return kHighlightUp;
92
93 case KEY_ENTER:
94 case KEY_POWER:
Tom Marshalld23b5102017-08-24 13:50:01 +000095 case BTN_MOUSE:
96 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -070097 return kInvokeItem;
98
Tom Marshalld23b5102017-08-24 13:50:01 +000099 case KEY_HOME:
100 case KEY_HOMEPAGE:
101 return kGoHome;
102
103 case KEY_BACKSPACE:
104 case KEY_BACK:
105 return kGoBack;
106
Tom Marshalla9ac9552018-12-17 15:57:44 -0800107 case KEY_REFRESH:
108 return kRefresh;
109
Elliott Hughes4af215b2015-04-10 15:00:34 -0700110 default:
111 // If you have all of the above buttons, any other buttons
112 // are ignored. Otherwise, any button cycles the highlight.
113 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
114 }
115}