blob: d46df92d3335fbb62d0285cd077ede2b8e2aed1c [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 Xu8f397302018-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 Xub63a2212019-07-29 14:21:49 -070026#include "otautil/boot_state.h"
Tianjie Xu8f397302018-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 },
37 { "Mount /system", Device::MOUNT_SYSTEM },
38 { "View recovery logs", Device::VIEW_RECOVERY_LOGS },
39 { "Run graphics test", Device::RUN_GRAPHICS_TEST },
40 { "Run locale test", Device::RUN_LOCALE_TEST },
Tao Baoc6dc3252019-04-16 14:22:25 -070041 { "Enter rescue", Device::ENTER_RESCUE },
Tao Baoe5d2c252018-05-09 11:05:44 -070042 { "Power off", Device::SHUTDOWN },
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070043};
44
Tao Baoe5d2c252018-05-09 11:05:44 -070045static std::vector<std::string> g_menu_items;
Elliott Hughes01fcbe12016-05-23 17:43:41 -070046
Tao Baoe5d2c252018-05-09 11:05:44 -070047static void PopulateMenuItems() {
48 g_menu_items.clear();
49 std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items),
50 [](const auto& entry) { return entry.first; });
51}
Elliott Hughes01fcbe12016-05-23 17:43:41 -070052
Tao Baoe5d2c252018-05-09 11:05:44 -070053Device::Device(RecoveryUI* ui) : ui_(ui) {
54 PopulateMenuItems();
55}
56
57void Device::RemoveMenuItemForAction(Device::BuiltinAction action) {
58 g_menu_actions.erase(
59 std::remove_if(g_menu_actions.begin(), g_menu_actions.end(),
60 [action](const auto& entry) { return entry.second == action; }));
61 CHECK(!g_menu_actions.empty());
62
63 // Re-populate the menu items.
64 PopulateMenuItems();
65}
Tao Bao1fe1afe2018-05-01 15:56:05 -070066
67const std::vector<std::string>& Device::GetMenuItems() {
Tao Baoe5d2c252018-05-09 11:05:44 -070068 return g_menu_items;
Elliott Hughes4af215b2015-04-10 15:00:34 -070069}
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070070
Tao Bao1fe1afe2018-05-01 15:56:05 -070071Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) {
Tao Baoe5d2c252018-05-09 11:05:44 -070072 return g_menu_actions[menu_position].second;
Elliott Hughes9e7ae8a2015-04-09 13:40:31 -070073}
Elliott Hughes4af215b2015-04-10 15:00:34 -070074
Tao Baofc5499f2017-02-23 19:06:53 -080075int Device::HandleMenuKey(int key, bool visible) {
Elliott Hughes4af215b2015-04-10 15:00:34 -070076 if (!visible) {
77 return kNoAction;
78 }
79
80 switch (key) {
81 case KEY_DOWN:
82 case KEY_VOLUMEDOWN:
83 return kHighlightDown;
84
85 case KEY_UP:
86 case KEY_VOLUMEUP:
87 return kHighlightUp;
88
89 case KEY_ENTER:
90 case KEY_POWER:
91 return kInvokeItem;
92
93 default:
94 // If you have all of the above buttons, any other buttons
95 // are ignored. Otherwise, any button cycles the highlight.
96 return ui_->HasThreeButtons() ? kNoAction : kHighlightDown;
97 }
98}
Tianjie Xub63a2212019-07-29 14:21:49 -070099
100void Device::SetBootState(const BootState* state) {
101 boot_state_ = state;
102}
103
104std::optional<std::string> Device::GetReason() const {
105 return boot_state_ ? std::make_optional(boot_state_->reason()) : std::nullopt;
106}
107
108std::optional<std::string> Device::GetStage() const {
109 return boot_state_ ? std::make_optional(boot_state_->stage()) : std::nullopt;
110}