blob: e7ae1a3e190e8b20a774f99006223793b7202f0b [file] [log] [blame]
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -07001/*
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
Tianjie Xub5108c32018-08-20 13:40:47 -070017#include "recovery_ui/device.h"
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070018
Tao Baoe5d2c252018-05-09 11:05:44 -070019#include <algorithm>
20#include <string>
21#include <utility>
22#include <vector>
23
Tao Bao1fe1afe2018-05-01 15:56:05 -070024#include <android-base/logging.h>
Tao Bao1fe1afe2018-05-01 15:56:05 -070025
Tianjie Xub5108c32018-08-20 13:40:47 -070026#include "recovery_ui/ui.h"
Tao Baoc16fd8a2018-04-30 17:12:03 -070027
Tao Baoe5d2c252018-05-09 11:05:44 -070028static std::vector<std::pair<std::string, Device::BuiltinAction>> g_menu_actions{
29 { "Reboot system now", Device::REBOOT },
30 { "Reboot to bootloader", Device::REBOOT_BOOTLOADER },
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070031 { "Enter fastboot", Device::ENTER_FASTBOOT },
Tao Baoe5d2c252018-05-09 11:05:44 -070032 { "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 },
Tao Bao378bfbf2019-04-16 14:22:25 -070040 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070041 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070042};
43
Tao Baoe5d2c252018-05-09 11:05:44 -070044static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070045
Tao Baoe5d2c252018-05-09 11:05:44 -070046static 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}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070051
Tao Baoe5d2c252018-05-09 11:05:44 -070052Device::Device(RecoveryUI* ui) : ui_(ui) {
53 PopulateMenuItems();
54}
55
56void 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}
Tao Bao1fe1afe2018-05-01 15:56:05 -070065
66const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070067 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070068}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070069
Tao Bao1fe1afe2018-05-01 15:56:05 -070070Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tao Baoe5d2c252018-05-09 11:05:44 -070071 return g_menu_actions[menu_position].second;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070072}
Elliott Hughes4af215b2015-04-10 15:00:34 -070073
Tao Baofc5499f2017-02-23 19:06:53 -080074int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -070075 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}