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
17 #include "recovery_ui/device.h"
18
19 #include <algorithm>
20 #include <string>
21 #include <utility>
22 #include <vector>
23
24 #include <android-base/logging.h>
25
26 #include "recovery_ui/ui.h"
27
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 },
31 { "Enter fastboot", Device::ENTER_FASTBOOT },
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 { "Enter rescue", Device::ENTER_RESCUE },
41 { "Power off", Device::SHUTDOWN },
42 };
43
44 static std::vector<std::string> g_menu_items;
45
PopulateMenuItems()46 static void PopulateMenuItems() {
47 g_menu_items.clear();
48 std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items),
49 [](const auto& entry) { return entry.first; });
50 }
51
Device(RecoveryUI * ui)52 Device::Device(RecoveryUI* ui) : ui_(ui) {
53 PopulateMenuItems();
54 }
55
RemoveMenuItemForAction(Device::BuiltinAction action)56 void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
57 g_menu_actions.erase(
58 std::remove_if(g_menu_actions.begin(), g_menu_actions.end(),
59 [action](const auto& entry) { return entry.second == action; }));
60 CHECK(!g_menu_actions.empty());
61
62 // Re-populate the menu items.
63 PopulateMenuItems();
64 }
65
GetMenuItems()66 const std::vector<std::string>& Device::GetMenuItems() {
67 return g_menu_items;
68 }
69
InvokeMenuItem(size_t menu_position)70 Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
71 return g_menu_actions[menu_position].second;
72 }
73
HandleMenuKey(int key,bool visible)74 int Device::HandleMenuKey(int key, bool visible) {
75 if (!visible) {
76 return kNoAction;
77 }
78
79 switch (key) {
80 case KEY_DOWN:
81 case KEY_VOLUMEDOWN:
82 return kHighlightDown;
83
84 case KEY_UP:
85 case KEY_VOLUMEUP:
86 return kHighlightUp;
87
88 case KEY_ENTER:
89 case KEY_POWER:
90 return kInvokeItem;
91
92 default:
93 // If you have all of the above buttons, any other buttons
94 // are ignored. Otherwise, any button cycles the highlight.
95 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
96 }
97 }
98