blob: 3d4f594c01a40f1e26b4d1c262a4e0878ec55f34 [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 },
Michael Bestasb3e3ad62019-09-27 20:16:38 +030032 { "Reboot to recovery", Device::REBOOT_RECOVERY },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070033 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070034 { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD },
35 { "Apply update from SD card", Device::APPLY_SDCARD },
36 { "Wipe data/factory reset", Device::WIPE_DATA },
37 { "Wipe cache partition", Device::WIPE_CACHE },
Michael Bestasc209c072019-09-27 19:52:13 +030038 { "Wipe system partition", Device::WIPE_SYSTEM },
Tao Baoe5d2c252018-05-09 11:05:44 -070039 { "Mount /system", Device::MOUNT_SYSTEM },
40 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
41 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
42 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Bao378bfbf2019-04-16 14:22:25 -070043 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070044 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070045};
46
Tao Baoe5d2c252018-05-09 11:05:44 -070047static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070048
Tao Baoe5d2c252018-05-09 11:05:44 -070049static void PopulateMenuItems() {
50 g_menu_items.clear();
51 std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items),
52 [](const auto& entry) { return entry.first; });
53}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070054
Tao Baoe5d2c252018-05-09 11:05:44 -070055Device::Device(RecoveryUI* ui) : ui_(ui) {
56 PopulateMenuItems();
57}
58
59void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
60 g_menu_actions.erase(
61 std::remove_if(g_menu_actions.begin(), g_menu_actions.end(),
62 [action](const auto& entry) { return entry.second == action; }));
63 CHECK(!g_menu_actions.empty());
64
65 // Re-populate the menu items.
66 PopulateMenuItems();
67}
Tao Bao1fe1afe2018-05-01 15:56:05 -070068
69const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070070 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070071}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070072
Tao Bao1fe1afe2018-05-01 15:56:05 -070073Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tao Baoe5d2c252018-05-09 11:05:44 -070074 return g_menu_actions[menu_position].second;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070075}
Elliott Hughes4af215b2015-04-10 15:00:34 -070076
Tao Baofc5499f2017-02-23 19:06:53 -080077int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -070078 if (!visible) {
79 return kNoAction;
80 }
81
82 switch (key) {
Tom Marshalld23b5102017-08-24 13:50:01 +000083 case KEY_RIGHTSHIFT:
Elliott Hughes4af215b2015-04-10 15:00:34 -070084 case KEY_DOWN:
85 case KEY_VOLUMEDOWN:
Tom Marshalld23b5102017-08-24 13:50:01 +000086 case KEY_MENU:
Elliott Hughes4af215b2015-04-10 15:00:34 -070087 return kHighlightDown;
88
89 case KEY_UP:
90 case KEY_VOLUMEUP:
Tom Marshalld23b5102017-08-24 13:50:01 +000091 case KEY_SEARCH:
Elliott Hughes4af215b2015-04-10 15:00:34 -070092 return kHighlightUp;
93
94 case KEY_ENTER:
95 case KEY_POWER:
Tom Marshalld23b5102017-08-24 13:50:01 +000096 case BTN_MOUSE:
97 case KEY_SEND:
Elliott Hughes4af215b2015-04-10 15:00:34 -070098 return kInvokeItem;
99
Tom Marshalld23b5102017-08-24 13:50:01 +0000100 case KEY_HOME:
101 case KEY_HOMEPAGE:
102 return kGoHome;
103
104 case KEY_BACKSPACE:
105 case KEY_BACK:
106 return kGoBack;
107
Tom Marshalla9ac9552018-12-17 15:57:44 -0800108 case KEY_REFRESH:
109 return kRefresh;
110
Elliott Hughes4af215b2015-04-10 15:00:34 -0700111 default:
112 // If you have all of the above buttons, any other buttons
113 // are ignored. Otherwise, any button cycles the highlight.
114 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
115 }
116}