From 8f397309b41814205c8d32f1c10f6b056c13e8c3 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 20 Aug 2018 13:40:47 -0700 Subject: Move librecovery_ui to a sub-directory This helps to expose librecovery_ui for device specific RecoveryUi. Bug: 76436783 Test: mma, unit tests pass Change-Id: Ic6c3d301d5833e4a592e6ea9d9d059bc4e4919be (cherry picked from commit b5108c372c8b92671ea5ebb4eeff00757fcee187) --- Android.bp | 76 +- adb_install.cpp | 2 +- default_device.cpp | 22 - device.cpp | 96 -- device.h | 132 --- fastboot/fastboot.cpp | 3 +- fastboot/fastboot.h | 2 +- fuse_sdcard_install.h | 4 +- install.cpp | 2 +- recovery.cpp | 5 +- recovery.h | 2 +- recovery_main.cpp | 6 +- recovery_ui/Android.bp | 91 ++ recovery_ui/default_device.cpp | 22 + recovery_ui/device.cpp | 96 ++ recovery_ui/include/recovery_ui/device.h | 132 +++ recovery_ui/include/recovery_ui/screen_ui.h | 412 +++++++++ recovery_ui/include/recovery_ui/stub_ui.h | 87 ++ recovery_ui/include/recovery_ui/ui.h | 272 ++++++ recovery_ui/include/recovery_ui/vr_ui.h | 45 + recovery_ui/include/recovery_ui/wear_ui.h | 52 ++ recovery_ui/screen_ui.cpp | 1334 +++++++++++++++++++++++++++ recovery_ui/ui.cpp | 597 ++++++++++++ recovery_ui/vr_device.cpp | 22 + recovery_ui/vr_ui.cpp | 72 ++ recovery_ui/wear_device.cpp | 22 + recovery_ui/wear_ui.cpp | 108 +++ screen_ui.cpp | 1334 --------------------------- screen_ui.h | 412 --------- stub_ui.h | 87 -- tests/unit/screen_ui_test.cpp | 4 +- ui.cpp | 599 ------------ ui.h | 272 ------ vr_device.cpp | 23 - vr_ui.cpp | 72 -- vr_ui.h | 45 - wear_device.cpp | 23 - wear_ui.cpp | 108 --- wear_ui.h | 52 -- 39 files changed, 3383 insertions(+), 3364 deletions(-) delete mode 100644 default_device.cpp delete mode 100644 device.cpp delete mode 100644 device.h create mode 100644 recovery_ui/Android.bp create mode 100644 recovery_ui/default_device.cpp create mode 100644 recovery_ui/device.cpp create mode 100644 recovery_ui/include/recovery_ui/device.h create mode 100644 recovery_ui/include/recovery_ui/screen_ui.h create mode 100644 recovery_ui/include/recovery_ui/stub_ui.h create mode 100644 recovery_ui/include/recovery_ui/ui.h create mode 100644 recovery_ui/include/recovery_ui/vr_ui.h create mode 100644 recovery_ui/include/recovery_ui/wear_ui.h create mode 100644 recovery_ui/screen_ui.cpp create mode 100644 recovery_ui/ui.cpp create mode 100644 recovery_ui/vr_device.cpp create mode 100644 recovery_ui/vr_ui.cpp create mode 100644 recovery_ui/wear_device.cpp create mode 100644 recovery_ui/wear_ui.cpp delete mode 100644 screen_ui.cpp delete mode 100644 screen_ui.h delete mode 100644 stub_ui.h delete mode 100644 ui.cpp delete mode 100644 ui.h delete mode 100644 vr_device.cpp delete mode 100644 vr_ui.cpp delete mode 100644 vr_ui.h delete mode 100644 wear_device.cpp delete mode 100644 wear_ui.cpp delete mode 100644 wear_ui.h diff --git a/Android.bp b/Android.bp index a6986bb2c..7b67f4077 100644 --- a/Android.bp +++ b/Android.bp @@ -26,77 +26,6 @@ cc_defaults { ], } -cc_library { - name: "librecovery_ui", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "device.cpp", - "screen_ui.cpp", - "ui.cpp", - "vr_ui.cpp", - "wear_ui.cpp" - ], - - static_libs: [ - "libminui", - "libotautil", - "libfstab", - ], - - shared_libs: [ - "libbase", - "libpng", - "libz", - ], -} - -// Generic device that uses ScreenRecoveryUI. -cc_library_static { - name: "librecovery_ui_default", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "default_device.cpp", - ], -} - -// The default wear device that uses WearRecoveryUI. -cc_library_static { - name: "librecovery_ui_wear", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "wear_device.cpp", - ], -} - -// The default VR device that uses VrRecoveryUI. -cc_library_static { - name: "librecovery_ui_vr", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "vr_device.cpp", - ], -} - cc_library_static { name: "librecovery_fastboot", recovery_available: true, @@ -113,6 +42,7 @@ cc_library_static { "libbootloader_message", "libcutils", "liblog", + "librecovery_ui", ], static_libs: [ @@ -180,6 +110,10 @@ cc_library_static { "roots.cpp", ], + shared_libs: [ + "librecovery_ui", + ], + include_dirs: [ "system/vold", ], diff --git a/adb_install.cpp b/adb_install.cpp index 3f2843fef..1d19fd3a1 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -32,7 +32,7 @@ #include "common.h" #include "fuse_sideload.h" #include "install.h" -#include "ui.h" +#include "recovery_ui/ui.h" int apply_from_adb(bool* wipe_cache) { // Save the usb state to restore after the sideload operation. diff --git a/default_device.cpp b/default_device.cpp deleted file mode 100644 index a9718668d..000000000 --- a/default_device.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device.h" -#include "screen_ui.h" - -Device* make_device() { - return new Device(new ScreenRecoveryUI); -} diff --git a/device.cpp b/device.cpp deleted file mode 100644 index eec1932c2..000000000 --- a/device.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device.h" - -#include -#include -#include -#include - -#include - -#include "ui.h" - -static std::vector> g_menu_actions{ - { "Reboot system now", Device::REBOOT }, - { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, - { "Enter fastboot", Device::ENTER_FASTBOOT }, - { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD }, - { "Apply update from SD card", Device::APPLY_SDCARD }, - { "Wipe data/factory reset", Device::WIPE_DATA }, - { "Wipe cache partition", Device::WIPE_CACHE }, - { "Mount /system", Device::MOUNT_SYSTEM }, - { "View recovery logs", Device::VIEW_RECOVERY_LOGS }, - { "Run graphics test", Device::RUN_GRAPHICS_TEST }, - { "Run locale test", Device::RUN_LOCALE_TEST }, - { "Power off", Device::SHUTDOWN }, -}; - -static std::vector g_menu_items; - -static void PopulateMenuItems() { - g_menu_items.clear(); - std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items), - [](const auto& entry) { return entry.first; }); -} - -Device::Device(RecoveryUI* ui) : ui_(ui) { - PopulateMenuItems(); -} - -void Device::RemoveMenuItemForAction(Device::BuiltinAction action) { - g_menu_actions.erase( - std::remove_if(g_menu_actions.begin(), g_menu_actions.end(), - [action](const auto& entry) { return entry.second == action; })); - CHECK(!g_menu_actions.empty()); - - // Re-populate the menu items. - PopulateMenuItems(); -} - -const std::vector& Device::GetMenuItems() { - return g_menu_items; -} - -Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { - return g_menu_actions[menu_position].second; -} - -int Device::HandleMenuKey(int key, bool visible) { - if (!visible) { - return kNoAction; - } - - switch (key) { - case KEY_DOWN: - case KEY_VOLUMEDOWN: - return kHighlightDown; - - case KEY_UP: - case KEY_VOLUMEUP: - return kHighlightUp; - - case KEY_ENTER: - case KEY_POWER: - return kInvokeItem; - - default: - // If you have all of the above buttons, any other buttons - // are ignored. Otherwise, any button cycles the highlight. - return ui_->HasThreeButtons() ? kNoAction : kHighlightDown; - } -} diff --git a/device.h b/device.h deleted file mode 100644 index 6a8daf83e..000000000 --- a/device.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _RECOVERY_DEVICE_H -#define _RECOVERY_DEVICE_H - -#include - -#include -#include -#include - -// Forward declaration to avoid including "ui.h". -class RecoveryUI; - -class Device { - public: - static constexpr const int kNoAction = -1; - static constexpr const int kHighlightUp = -2; - static constexpr const int kHighlightDown = -3; - static constexpr const int kInvokeItem = -4; - - enum BuiltinAction { - NO_ACTION = 0, - REBOOT = 1, - APPLY_SDCARD = 2, - // APPLY_CACHE was 3. - APPLY_ADB_SIDELOAD = 4, - WIPE_DATA = 5, - WIPE_CACHE = 6, - REBOOT_BOOTLOADER = 7, - SHUTDOWN = 8, - VIEW_RECOVERY_LOGS = 9, - MOUNT_SYSTEM = 10, - RUN_GRAPHICS_TEST = 11, - RUN_LOCALE_TEST = 12, - KEY_INTERRUPTED = 13, - ENTER_FASTBOOT = 14, - ENTER_RECOVERY = 15, - }; - - explicit Device(RecoveryUI* ui); - virtual ~Device() {} - - // Returns a raw pointer to the RecoveryUI object. - virtual RecoveryUI* GetUI() { - return ui_.get(); - } - - // Resets the UI object to the given UI. Used to override the default UI in case initialization - // failed, or we want a different UI for some reason. The device object will take the ownership. - virtual void ResetUI(RecoveryUI* ui) { - ui_.reset(ui); - } - - // Called when recovery starts up (after the UI has been obtained and initialized and after the - // arguments have been parsed, but before anything else). - virtual void StartRecovery() {}; - - // Called from the main thread when recovery is at the main menu and waiting for input, and a key - // is pressed. (Note that "at" the main menu does not necessarily mean the menu is visible; - // recovery will be at the main menu with it invisible after an unsuccessful operation, such as - // failed to install an OTA package, or if recovery is started with no command.) - // - // 'key' is the code of the key just pressed. (You can call IsKeyPressed() on the RecoveryUI - // object you returned from GetUI() if you want to find out if other keys are held down.) - // - // 'visible' is true if the menu is visible. - // - // Returns one of the defined constants below in order to: - // - move the menu highlight (kHighlight{Up,Down}: negative value) - // - invoke the highlighted item (kInvokeItem: negative value) - // - do nothing (kNoAction: negative value) - // - invoke a specific action (a menu position: non-negative value) - virtual int HandleMenuKey(int key, bool visible); - - // Returns the list of menu items (a vector of strings). The menu_position passed to - // InvokeMenuItem() will correspond to the indexes into this array. - virtual const std::vector& GetMenuItems(); - - // Performs a recovery action selected from the menu. 'menu_position' will be the index of the - // selected menu item, or a non-negative value returned from HandleMenuKey(). The menu will be - // hidden when this is called; implementations can call ui_print() to print information to the - // screen. If the menu position is one of the builtin actions, you can just return the - // corresponding enum value. If it is an action specific to your device, you actually perform it - // here and return NO_ACTION. - virtual BuiltinAction InvokeMenuItem(size_t menu_position); - - // Removes the menu item for the given action. This allows tailoring the menu based on the - // runtime info, such as the availability of /cache or /sdcard. - virtual void RemoveMenuItemForAction(Device::BuiltinAction action); - - // Called before and after we do a wipe data/factory reset operation, either via a reboot from the - // main system with the --wipe_data flag, or when the user boots into recovery image manually and - // selects the option from the menu, to perform whatever device-specific wiping actions as needed. - // Returns true on success; returning false from PreWipeData will prevent the regular wipe, and - // returning false from PostWipeData will cause the wipe to be considered a failure. - virtual bool PreWipeData() { - return true; - } - - virtual bool PostWipeData() { - return true; - } - - private: - // The RecoveryUI object that should be used to display the user interface for this device. - std::unique_ptr ui_; -}; - -// Disable name mangling, as this function will be loaded via dlsym(3). -extern "C" { - -// The device-specific library must define this function (or the default one will be used, if there -// is no device-specific library). It returns the Device object that recovery should use. -Device* make_device(); -} - -#endif // _DEVICE_H diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 8458c99dd..14f5e4bdc 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -27,8 +27,7 @@ #include #include -#include "device.h" -#include "ui.h" +#include "recovery_ui/ui.h" static const std::vector> kFastbootMenuActions{ { "Reboot system now", Device::REBOOT }, diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h index 53a2adcae..1aa7de66e 100644 --- a/fastboot/fastboot.h +++ b/fastboot/fastboot.h @@ -19,6 +19,6 @@ #include #include -#include "device.h" +#include "recovery_ui/device.h" Device::BuiltinAction StartFastboot(Device* device, const std::vector& args); diff --git a/fuse_sdcard_install.h b/fuse_sdcard_install.h index 5f0d64acf..345aea45b 100644 --- a/fuse_sdcard_install.h +++ b/fuse_sdcard_install.h @@ -16,7 +16,7 @@ #pragma once -#include "device.h" -#include "ui.h" +#include "recovery_ui/device.h" +#include "recovery_ui/ui.h" int ApplyFromSdcard(Device* device, bool* wipe_cache, RecoveryUI* ui); diff --git a/install.cpp b/install.cpp index dbc815d47..b7fb78878 100644 --- a/install.cpp +++ b/install.cpp @@ -53,8 +53,8 @@ #include "otautil/thermalutil.h" #include "package.h" #include "private/install.h" +#include "recovery_ui/ui.h" #include "roots.h" -#include "ui.h" #include "verifier.h" using namespace std::chrono_literals; diff --git a/recovery.cpp b/recovery.cpp index 90ca3f0ec..d9c1f22f5 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -52,7 +52,6 @@ #include "adb_install.h" #include "common.h" -#include "device.h" #include "fsck_unshare_blocks.h" #include "fuse_sdcard_install.h" #include "install.h" @@ -62,9 +61,9 @@ #include "otautil/paths.h" #include "otautil/sysutil.h" #include "package.h" +#include "recovery_ui/screen_ui.h" +#include "recovery_ui/ui.h" #include "roots.h" -#include "screen_ui.h" -#include "ui.h" static constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; diff --git a/recovery.h b/recovery.h index 00e22daa6..f050549cc 100644 --- a/recovery.h +++ b/recovery.h @@ -19,6 +19,6 @@ #include #include -#include "device.h" +#include "recovery_ui/device.h" Device::BuiltinAction start_recovery(Device* device, const std::vector& args); diff --git a/recovery_main.cpp b/recovery_main.cpp index 935d69815..2f5a1845b 100644 --- a/recovery_main.cpp +++ b/recovery_main.cpp @@ -49,16 +49,16 @@ #include #include "common.h" -#include "device.h" #include "fastboot/fastboot.h" #include "logging.h" #include "minadbd/minadbd.h" #include "otautil/paths.h" #include "otautil/sysutil.h" #include "recovery.h" +#include "recovery_ui/device.h" +#include "recovery_ui/stub_ui.h" +#include "recovery_ui/ui.h" #include "roots.h" -#include "stub_ui.h" -#include "ui.h" static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; diff --git a/recovery_ui/Android.bp b/recovery_ui/Android.bp new file mode 100644 index 000000000..ee3149d5e --- /dev/null +++ b/recovery_ui/Android.bp @@ -0,0 +1,91 @@ +// Copyright (C) 2019 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_library { + name: "librecovery_ui", + recovery_available: true, + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "device.cpp", + "screen_ui.cpp", + "ui.cpp", + "vr_ui.cpp", + "wear_ui.cpp", + ], + + export_include_dirs: ["include"], + + static_libs: [ + "libminui", + "libotautil", + ], + + shared_libs: [ + "libbase", + "libpng", + "libz", + ], +} + +// Generic device that uses ScreenRecoveryUI. +cc_library_static { + name: "librecovery_ui_default", + recovery_available: true, + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "default_device.cpp", + ], + + export_include_dirs: ["include"], +} + +// The default wear device that uses WearRecoveryUI. +cc_library_static { + name: "librecovery_ui_wear", + recovery_available: true, + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "wear_device.cpp", + ], + + export_include_dirs: ["include"], +} + +// The default VR device that uses VrRecoveryUI. +cc_library_static { + name: "librecovery_ui_vr", + recovery_available: true, + + defaults: [ + "recovery_defaults", + ], + + srcs: [ + "vr_device.cpp", + ], + + export_include_dirs: ["include"], +} diff --git a/recovery_ui/default_device.cpp b/recovery_ui/default_device.cpp new file mode 100644 index 000000000..4db461af6 --- /dev/null +++ b/recovery_ui/default_device.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/device.h" +#include "recovery_ui/screen_ui.h" + +Device* make_device() { + return new Device(new ScreenRecoveryUI); +} diff --git a/recovery_ui/device.cpp b/recovery_ui/device.cpp new file mode 100644 index 000000000..ddb0118db --- /dev/null +++ b/recovery_ui/device.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/device.h" + +#include +#include +#include +#include + +#include + +#include "recovery_ui/ui.h" + +static std::vector> g_menu_actions{ + { "Reboot system now", Device::REBOOT }, + { "Reboot to bootloader", Device::REBOOT_BOOTLOADER }, + { "Enter fastboot", Device::ENTER_FASTBOOT }, + { "Apply update from ADB", Device::APPLY_ADB_SIDELOAD }, + { "Apply update from SD card", Device::APPLY_SDCARD }, + { "Wipe data/factory reset", Device::WIPE_DATA }, + { "Wipe cache partition", Device::WIPE_CACHE }, + { "Mount /system", Device::MOUNT_SYSTEM }, + { "View recovery logs", Device::VIEW_RECOVERY_LOGS }, + { "Run graphics test", Device::RUN_GRAPHICS_TEST }, + { "Run locale test", Device::RUN_LOCALE_TEST }, + { "Power off", Device::SHUTDOWN }, +}; + +static std::vector g_menu_items; + +static void PopulateMenuItems() { + g_menu_items.clear(); + std::transform(g_menu_actions.cbegin(), g_menu_actions.cend(), std::back_inserter(g_menu_items), + [](const auto& entry) { return entry.first; }); +} + +Device::Device(RecoveryUI* ui) : ui_(ui) { + PopulateMenuItems(); +} + +void Device::RemoveMenuItemForAction(Device::BuiltinAction action) { + g_menu_actions.erase( + std::remove_if(g_menu_actions.begin(), g_menu_actions.end(), + [action](const auto& entry) { return entry.second == action; })); + CHECK(!g_menu_actions.empty()); + + // Re-populate the menu items. + PopulateMenuItems(); +} + +const std::vector& Device::GetMenuItems() { + return g_menu_items; +} + +Device::BuiltinAction Device::InvokeMenuItem(size_t menu_position) { + return g_menu_actions[menu_position].second; +} + +int Device::HandleMenuKey(int key, bool visible) { + if (!visible) { + return kNoAction; + } + + switch (key) { + case KEY_DOWN: + case KEY_VOLUMEDOWN: + return kHighlightDown; + + case KEY_UP: + case KEY_VOLUMEUP: + return kHighlightUp; + + case KEY_ENTER: + case KEY_POWER: + return kInvokeItem; + + default: + // If you have all of the above buttons, any other buttons + // are ignored. Otherwise, any button cycles the highlight. + return ui_->HasThreeButtons() ? kNoAction : kHighlightDown; + } +} diff --git a/recovery_ui/include/recovery_ui/device.h b/recovery_ui/include/recovery_ui/device.h new file mode 100644 index 000000000..cfa914e77 --- /dev/null +++ b/recovery_ui/include/recovery_ui/device.h @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _RECOVERY_DEVICE_H +#define _RECOVERY_DEVICE_H + +#include + +#include +#include +#include + +// Forward declaration to avoid including "ui.h". +class RecoveryUI; + +class Device { + public: + static constexpr const int kNoAction = -1; + static constexpr const int kHighlightUp = -2; + static constexpr const int kHighlightDown = -3; + static constexpr const int kInvokeItem = -4; + + enum BuiltinAction { + NO_ACTION = 0, + REBOOT = 1, + APPLY_SDCARD = 2, + // APPLY_CACHE was 3. + APPLY_ADB_SIDELOAD = 4, + WIPE_DATA = 5, + WIPE_CACHE = 6, + REBOOT_BOOTLOADER = 7, + SHUTDOWN = 8, + VIEW_RECOVERY_LOGS = 9, + MOUNT_SYSTEM = 10, + RUN_GRAPHICS_TEST = 11, + RUN_LOCALE_TEST = 12, + KEY_INTERRUPTED = 13, + ENTER_FASTBOOT = 14, + ENTER_RECOVERY = 15, + }; + + explicit Device(RecoveryUI* ui); + virtual ~Device() {} + + // Returns a raw pointer to the RecoveryUI object. + virtual RecoveryUI* GetUI() { + return ui_.get(); + } + + // Resets the UI object to the given UI. Used to override the default UI in case initialization + // failed, or we want a different UI for some reason. The device object will take the ownership. + virtual void ResetUI(RecoveryUI* ui) { + ui_.reset(ui); + } + + // Called when recovery starts up (after the UI has been obtained and initialized and after the + // arguments have been parsed, but before anything else). + virtual void StartRecovery() {} + + // Called from the main thread when recovery is at the main menu and waiting for input, and a key + // is pressed. (Note that "at" the main menu does not necessarily mean the menu is visible; + // recovery will be at the main menu with it invisible after an unsuccessful operation, such as + // failed to install an OTA package, or if recovery is started with no command.) + // + // 'key' is the code of the key just pressed. (You can call IsKeyPressed() on the RecoveryUI + // object you returned from GetUI() if you want to find out if other keys are held down.) + // + // 'visible' is true if the menu is visible. + // + // Returns one of the defined constants below in order to: + // - move the menu highlight (kHighlight{Up,Down}: negative value) + // - invoke the highlighted item (kInvokeItem: negative value) + // - do nothing (kNoAction: negative value) + // - invoke a specific action (a menu position: non-negative value) + virtual int HandleMenuKey(int key, bool visible); + + // Returns the list of menu items (a vector of strings). The menu_position passed to + // InvokeMenuItem() will correspond to the indexes into this array. + virtual const std::vector& GetMenuItems(); + + // Performs a recovery action selected from the menu. 'menu_position' will be the index of the + // selected menu item, or a non-negative value returned from HandleMenuKey(). The menu will be + // hidden when this is called; implementations can call ui_print() to print information to the + // screen. If the menu position is one of the builtin actions, you can just return the + // corresponding enum value. If it is an action specific to your device, you actually perform it + // here and return NO_ACTION. + virtual BuiltinAction InvokeMenuItem(size_t menu_position); + + // Removes the menu item for the given action. This allows tailoring the menu based on the + // runtime info, such as the availability of /cache or /sdcard. + virtual void RemoveMenuItemForAction(Device::BuiltinAction action); + + // Called before and after we do a wipe data/factory reset operation, either via a reboot from the + // main system with the --wipe_data flag, or when the user boots into recovery image manually and + // selects the option from the menu, to perform whatever device-specific wiping actions as needed. + // Returns true on success; returning false from PreWipeData will prevent the regular wipe, and + // returning false from PostWipeData will cause the wipe to be considered a failure. + virtual bool PreWipeData() { + return true; + } + + virtual bool PostWipeData() { + return true; + } + + private: + // The RecoveryUI object that should be used to display the user interface for this device. + std::unique_ptr ui_; +}; + +// Disable name mangling, as this function will be loaded via dlsym(3). +extern "C" { + +// The device-specific library must define this function (or the default one will be used, if there +// is no device-specific library). It returns the Device object that recovery should use. +Device* make_device(); +} + +#endif // _DEVICE_H diff --git a/recovery_ui/include/recovery_ui/screen_ui.h b/recovery_ui/include/recovery_ui/screen_ui.h new file mode 100644 index 000000000..5cda2a2e5 --- /dev/null +++ b/recovery_ui/include/recovery_ui/screen_ui.h @@ -0,0 +1,412 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_SCREEN_UI_H +#define RECOVERY_SCREEN_UI_H + +#include + +#include +#include +#include +#include +#include +#include + +#include "ui.h" + +// From minui/minui.h. +class GRSurface; + +enum class UIElement { + HEADER, + MENU, + MENU_SEL_BG, + MENU_SEL_BG_ACTIVE, + MENU_SEL_FG, + LOG, + TEXT_FILL, + INFO +}; + +// Interface to draw the UI elements on the screen. +class DrawInterface { + public: + virtual ~DrawInterface() = default; + + // Sets the color to the predefined value for |element|. + virtual void SetColor(UIElement element) const = 0; + + // Draws a highlight bar at (x, y) - (x + width, y + height). + virtual void DrawHighlightBar(int x, int y, int width, int height) const = 0; + + // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis. + virtual int DrawHorizontalRule(int y) const = 0; + + // Draws a line of text. Returns the offset it should be moving along Y-axis. + virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const = 0; + + // Draws surface portion (sx, sy, w, h) at screen location (dx, dy). + virtual void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, + int dy) const = 0; + + // Draws rectangle at (x, y) - (x + w, y + h). + virtual void DrawFill(int x, int y, int w, int h) const = 0; + + // Draws given surface (surface->pixel_bytes = 1) as text at (x, y). + virtual void DrawTextIcon(int x, int y, const GRSurface* surface) const = 0; + + // Draws multiple text lines. Returns the offset it should be moving along Y-axis. + virtual int DrawTextLines(int x, int y, const std::vector& lines) const = 0; + + // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines. It + // keeps symmetrical margins of 'x' at each end of a line. Returns the offset it should be moving + // along Y-axis. + virtual int DrawWrappedTextLines(int x, int y, const std::vector& lines) const = 0; +}; + +// Interface for classes that maintain the menu selection and display. +class Menu { + public: + virtual ~Menu() = default; + // Returns the current menu selection. + size_t selection() const; + // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is + // scrollable. + virtual int Select(int sel) = 0; + // Displays the menu headers on the screen at offset x, y + virtual int DrawHeader(int x, int y) const = 0; + // Iterates over the menu items and displays each of them at offset x, y. + virtual int DrawItems(int x, int y, int screen_width, bool long_press) const = 0; + + protected: + Menu(size_t initial_selection, const DrawInterface& draw_func); + // Current menu selection. + size_t selection_; + // Reference to the class that implements all the draw functions. + const DrawInterface& draw_funcs_; +}; + +// This class uses strings as the menu header and items. +class TextMenu : public Menu { + public: + // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial + // selection to |initial_selection|. + TextMenu(bool scrollable, size_t max_items, size_t max_length, + const std::vector& headers, const std::vector& items, + size_t initial_selection, int char_height, const DrawInterface& draw_funcs); + + int Select(int sel) override; + int DrawHeader(int x, int y) const override; + int DrawItems(int x, int y, int screen_width, bool long_press) const override; + + bool scrollable() const { + return scrollable_; + } + + // Returns count of menu items. + size_t ItemsCount() const; + + // Returns the index of the first menu item. + size_t MenuStart() const; + + // Returns the index of the last menu item + 1. + size_t MenuEnd() const; + + // Menu example: + // info: Android Recovery + // .... + // help messages: Swipe up/down to move + // Swipe left/right to select + // empty line (horizontal rule): + // menu headers: Select file to view + // menu items: /cache/recovery/last_log + // /cache/recovery/last_log.1 + // /cache/recovery/last_log.2 + // ... + const std::vector& text_headers() const; + std::string TextItem(size_t index) const; + + // Checks if the menu items fit vertically on the screen. Returns true and set the + // |cur_selection_str| if the items exceed the screen limit. + bool ItemsOverflow(std::string* cur_selection_str) const; + + private: + // The menu is scrollable to display more items. Used on wear devices who have smaller screens. + const bool scrollable_; + // The max number of menu items to fit vertically on a screen. + const size_t max_display_items_; + // The length of each item to fit horizontally on a screen. + const size_t max_item_length_; + // The menu headers. + std::vector text_headers_; + // The actual menu items trimmed to fit the given properties. + std::vector text_items_; + // The first item to display on the screen. + size_t menu_start_; + + // Height in pixels of each character. + int char_height_; +}; + +// This class uses GRSurface's as the menu header and items. +class GraphicMenu : public Menu { + public: + // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial + // selection to |initial_selection|. |headers| and |items| will be made local copies. + GraphicMenu(const GRSurface* graphic_headers, const std::vector& graphic_items, + size_t initial_selection, const DrawInterface& draw_funcs); + + int Select(int sel) override; + int DrawHeader(int x, int y) const override; + int DrawItems(int x, int y, int screen_width, bool long_press) const override; + + // Checks if all the header and items are valid GRSurface's; and that they can fit in the area + // defined by |max_width| and |max_height|. + static bool Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, + const std::vector& graphic_items); + + // Returns true if |surface| fits on the screen with a vertical offset |y|. + static bool ValidateGraphicSurface(size_t max_width, size_t max_height, int y, + const GRSurface* surface); + + private: + // Menu headers and items in graphic icons. These are the copies owned by the class instance. + std::unique_ptr graphic_headers_; + std::vector> graphic_items_; +}; + +// Implementation of RecoveryUI appropriate for devices with a screen +// (shows an icon + a progress bar, text logging, menu, etc.) +class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { + public: + ScreenRecoveryUI(); + explicit ScreenRecoveryUI(bool scrollable_menu); + ~ScreenRecoveryUI() override; + + bool Init(const std::string& locale) override; + std::string GetLocale() const override; + + // overall recovery state ("background image") + void SetBackground(Icon icon) override; + void SetSystemUpdateText(bool security_update) override; + + // progress indicator + void SetProgressType(ProgressType type) override; + void ShowProgress(float portion, float seconds) override; + void SetProgress(float fraction) override; + + void SetStage(int current, int max) override; + + // text log + void ShowText(bool visible) override; + bool IsTextVisible() override; + bool WasTextEverVisible() override; + + // printing messages + void Print(const char* fmt, ...) override __printflike(2, 3); + void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3); + void ShowFile(const std::string& filename) override; + + // menu display + size_t ShowMenu(const std::vector& headers, const std::vector& items, + size_t initial_selection, bool menu_only, + const std::function& key_handler) override; + void SetTitle(const std::vector& lines) override; + + void KeyLongPress(int) override; + + void Redraw(); + + // Checks the background text image, for debugging purpose. It iterates the locales embedded in + // the on-device resource files and shows the localized text, for manual inspection. + void CheckBackgroundTextImages(); + + // Displays the localized wipe data menu. + size_t ShowPromptWipeDataMenu(const std::vector& backup_headers, + const std::vector& backup_items, + const std::function& key_handler) override; + + // Displays the localized wipe data confirmation menu. + size_t ShowPromptWipeDataConfirmationMenu( + const std::vector& backup_headers, const std::vector& backup_items, + const std::function& key_handler) override; + + protected: + static constexpr int kMenuIndent = 4; + + // The margin that we don't want to use for showing texts (e.g. round screen, or screen with + // rounded corners). + const int margin_width_; + const int margin_height_; + + // Number of frames per sec (default: 30) for both parts of the animation. + const int animation_fps_; + + // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi. + const float density_; + + virtual bool InitTextParams(); + + virtual bool LoadWipeDataMenuText(); + + // Creates a GraphicMenu with |graphic_header| and |graphic_items|. If the GraphicMenu isn't + // valid or it doesn't fit on the screen; falls back to create a TextMenu instead. If succeeds, + // returns a unique pointer to the created menu; otherwise returns nullptr. + virtual std::unique_ptr CreateMenu(const GRSurface* graphic_header, + const std::vector& graphic_items, + const std::vector& text_headers, + const std::vector& text_items, + size_t initial_selection) const; + + // Creates a TextMenu with |text_headers| and |text_items|; and sets the menu selection to + // |initial_selection|. + virtual std::unique_ptr CreateMenu(const std::vector& text_headers, + const std::vector& text_items, + size_t initial_selection) const; + + // Takes the ownership of |menu| and displays it. + virtual size_t ShowMenu(std::unique_ptr&& menu, bool menu_only, + const std::function& key_handler); + + // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item + // selected. + virtual int SelectMenu(int sel); + + virtual void draw_background_locked(); + virtual void draw_foreground_locked(); + virtual void draw_screen_locked(); + virtual void draw_menu_and_text_buffer_locked(const std::vector& help_message); + virtual void update_screen_locked(); + virtual void update_progress_locked(); + + const GRSurface* GetCurrentFrame() const; + const GRSurface* GetCurrentText() const; + + void ProgressThreadLoop(); + + virtual void ShowFile(FILE*); + virtual void PrintV(const char*, bool, va_list); + void PutChar(char); + void ClearText(); + + void LoadAnimation(); + std::unique_ptr LoadBitmap(const std::string& filename); + std::unique_ptr LoadLocalizedBitmap(const std::string& filename); + + int PixelsFromDp(int dp) const; + virtual int GetAnimationBaseline() const; + virtual int GetProgressBaseline() const; + virtual int GetTextBaseline() const; + + // Returns pixel width of draw buffer. + virtual int ScreenWidth() const; + // Returns pixel height of draw buffer. + virtual int ScreenHeight() const; + + // Implementation of the draw functions in DrawInterface. + void SetColor(UIElement e) const override; + void DrawHighlightBar(int x, int y, int width, int height) const override; + int DrawHorizontalRule(int y) const override; + void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, + int dy) const override; + void DrawFill(int x, int y, int w, int h) const override; + void DrawTextIcon(int x, int y, const GRSurface* surface) const override; + int DrawTextLine(int x, int y, const std::string& line, bool bold) const override; + int DrawTextLines(int x, int y, const std::vector& lines) const override; + int DrawWrappedTextLines(int x, int y, const std::vector& lines) const override; + + // The layout to use. + int layout_; + + // The images that contain localized texts. + std::unique_ptr erasing_text_; + std::unique_ptr error_text_; + std::unique_ptr installing_text_; + std::unique_ptr no_command_text_; + + // Localized text images for the wipe data menu. + std::unique_ptr cancel_wipe_data_text_; + std::unique_ptr factory_data_reset_text_; + std::unique_ptr try_again_text_; + std::unique_ptr wipe_data_confirmation_text_; + std::unique_ptr wipe_data_menu_header_text_; + + std::unique_ptr fastbootd_logo_; + + // current_icon_ points to one of the frames in intro_frames_ or loop_frames_, indexed by + // current_frame_, or error_icon_. + Icon current_icon_; + std::unique_ptr error_icon_; + std::vector> intro_frames_; + std::vector> loop_frames_; + size_t current_frame_; + bool intro_done_; + + // progress_bar and stage_marker images. + std::unique_ptr progress_bar_empty_; + std::unique_ptr progress_bar_fill_; + std::unique_ptr stage_marker_empty_; + std::unique_ptr stage_marker_fill_; + + ProgressType progressBarType; + + float progressScopeStart, progressScopeSize, progress; + double progressScopeTime, progressScopeDuration; + + // true when both graphics pages are the same (except for the progress bar). + bool pagesIdentical; + + size_t text_cols_, text_rows_; + + // Log text overlay, displayed when a magic key is pressed. + char** text_; + size_t text_col_, text_row_; + + bool show_text; + bool show_text_ever; // has show_text ever been true? + + std::vector title_lines_; + + bool scrollable_menu_; + std::unique_ptr menu_; + + // An alternate text screen, swapped with 'text_' when we're viewing a log file. + char** file_viewer_text_; + + std::thread progress_thread_; + std::atomic progress_thread_stopped_{ false }; + + int stage, max_stage; + + int char_width_; + int char_height_; + + // The locale that's used to show the rendered texts. + std::string locale_; + bool rtl_locale_; + + std::mutex updateMutex; + + private: + void SetLocale(const std::string&); + + // Display the background texts for "erasing", "error", "no_command" and "installing" for the + // selected locale. + void SelectAndShowBackgroundText(const std::vector& locales_entries, size_t sel); +}; + +#endif // RECOVERY_UI_H diff --git a/recovery_ui/include/recovery_ui/stub_ui.h b/recovery_ui/include/recovery_ui/stub_ui.h new file mode 100644 index 000000000..fb1d8c7a6 --- /dev/null +++ b/recovery_ui/include/recovery_ui/stub_ui.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_STUB_UI_H +#define RECOVERY_STUB_UI_H + +#include +#include +#include + +#include "ui.h" + +// Stub implementation of RecoveryUI for devices without screen. +class StubRecoveryUI : public RecoveryUI { + public: + StubRecoveryUI() = default; + + std::string GetLocale() const override { + return ""; + } + void SetBackground(Icon /* icon */) override {} + void SetSystemUpdateText(bool /* security_update */) override {} + + // progress indicator + void SetProgressType(ProgressType /* type */) override {} + void ShowProgress(float /* portion */, float /* seconds */) override {} + void SetProgress(float /* fraction */) override {} + + void SetStage(int /* current */, int /* max */) override {} + + // text log + void ShowText(bool /* visible */) override {} + bool IsTextVisible() override { + return false; + } + bool WasTextEverVisible() override { + return false; + } + + // printing messages + void Print(const char* fmt, ...) override { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + } + void PrintOnScreenOnly(const char* /* fmt */, ...) override {} + void ShowFile(const std::string& /* filename */) override {} + + // menu display + size_t ShowMenu(const std::vector& /* headers */, + const std::vector& /* items */, size_t initial_selection, + bool /* menu_only */, + const std::function& /* key_handler */) override { + return initial_selection; + } + + size_t ShowPromptWipeDataMenu(const std::vector& /* backup_headers */, + const std::vector& /* backup_items */, + const std::function& /* key_handle */) override { + return 0; + } + + size_t ShowPromptWipeDataConfirmationMenu( + const std::vector& /* backup_headers */, + const std::vector& /* backup_items */, + const std::function& /* key_handle */) override { + return 0; + } + + void SetTitle(const std::vector& /* lines */) override {} +}; + +#endif // RECOVERY_STUB_UI_H diff --git a/recovery_ui/include/recovery_ui/ui.h b/recovery_ui/include/recovery_ui/ui.h new file mode 100644 index 000000000..d55322cf0 --- /dev/null +++ b/recovery_ui/include/recovery_ui/ui.h @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_UI_H +#define RECOVERY_UI_H + +#include // KEY_MAX + +#include +#include +#include +#include +#include +#include +#include + +// Abstract class for controlling the user interface during recovery. +class RecoveryUI { + public: + enum Icon { + NONE, + INSTALLING_UPDATE, + ERASING, + NO_COMMAND, + ERROR, + }; + + enum ProgressType { + EMPTY, + INDETERMINATE, + DETERMINATE, + }; + + enum KeyAction { + ENQUEUE, + TOGGLE, + REBOOT, + IGNORE, + }; + + enum class KeyError : int { + TIMED_OUT = -1, + INTERRUPTED = -2, + }; + + RecoveryUI(); + + virtual ~RecoveryUI(); + + // Initializes the object; called before anything else. UI texts will be initialized according + // to the given locale. Returns true on success. + virtual bool Init(const std::string& locale); + + virtual std::string GetLocale() const = 0; + + // Shows a stage indicator. Called immediately after Init(). + virtual void SetStage(int current, int max) = 0; + + // Sets the overall recovery state ("background image"). + virtual void SetBackground(Icon icon) = 0; + virtual void SetSystemUpdateText(bool security_update) = 0; + + // --- progress indicator --- + virtual void SetProgressType(ProgressType determinate) = 0; + + // Shows a progress bar and define the scope of the next operation: + // portion - fraction of the progress bar the next operation will use + // seconds - expected time interval (progress bar moves at this minimum rate) + virtual void ShowProgress(float portion, float seconds) = 0; + + // Sets progress bar position (0.0 - 1.0 within the scope defined by the last call to + // ShowProgress). + virtual void SetProgress(float fraction) = 0; + + // --- text log --- + + virtual void ShowText(bool visible) = 0; + + virtual bool IsTextVisible() = 0; + + virtual bool WasTextEverVisible() = 0; + + // Writes a message to the on-screen log (shown if the user has toggled on the text display). + // Print() will also dump the message to stdout / log file, while PrintOnScreenOnly() not. + virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0; + virtual void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3) = 0; + + // Shows the contents of the given file. Caller ensures the patition that contains the file has + // been mounted. + virtual void ShowFile(const std::string& filename) = 0; + + // --- key handling --- + + // Waits for a key and return it. May return TIMED_OUT after timeout and + // KeyError::INTERRUPTED on a key interrupt. + virtual int WaitKey(); + + // Wakes up the UI if it is waiting on key input, causing WaitKey to return KeyError::INTERRUPTED. + virtual void InterruptKey(); + + virtual bool IsKeyPressed(int key); + virtual bool IsLongPress(); + + // Returns true if you have the volume up/down and power trio typical of phones and tablets, false + // otherwise. + virtual bool HasThreeButtons(); + + // Returns true if it has a power key. + virtual bool HasPowerKey() const; + + // Returns true if it supports touch inputs. + virtual bool HasTouchScreen() const; + + // Erases any queued-up keys. + virtual void FlushKeys(); + + // Called on each key press, even while operations are in progress. Return value indicates whether + // an immediate operation should be triggered (toggling the display, rebooting the device), or if + // the key should be enqueued for use by the main thread. + virtual KeyAction CheckKey(int key, bool is_long_press); + + // Called when a key is held down long enough to have been a long-press (but before the key is + // released). This means that if the key is eventually registered (released without any other keys + // being pressed in the meantime), CheckKey will be called with 'is_long_press' true. + virtual void KeyLongPress(int key); + + // Normally in recovery there's a key sequence that triggers immediate reboot of the device, + // regardless of what recovery is doing (with the default CheckKey implementation, it's pressing + // the power button 7 times in row). Call this to enable or disable that feature. It is enabled by + // default. + virtual void SetEnableReboot(bool enabled); + + // --- menu display --- + + virtual void SetTitle(const std::vector& lines) = 0; + + // Displays a menu with the given 'headers' and 'items'. The supplied 'key_handler' callback, + // which is typically bound to Device::HandleMenuKey(), should return the expected action for the + // given key code and menu visibility (e.g. to move the cursor or to select an item). Caller sets + // 'menu_only' to true to ensure only a menu item gets selected and returned. Otherwise if + // 'menu_only' is false, ShowMenu() will forward any non-negative value returned from the + // key_handler, which may be beyond the range of menu items. This could be used to trigger a + // device-specific action, even without that being listed in the menu. Caller needs to handle + // such a case accordingly (e.g. by calling Device::InvokeMenuItem() to process the action). + // Returns a non-negative value (the chosen item number or device-specific action code), or + // static_cast(TIMED_OUT) if timed out waiting for input or + // static_cast(ERR_KEY_INTERTUPT) if interrupted, such as by InterruptKey(). + virtual size_t ShowMenu(const std::vector& headers, + const std::vector& items, size_t initial_selection, + bool menu_only, const std::function& key_handler) = 0; + + // Displays the localized wipe data menu with pre-generated graphs. If there's an issue + // with the graphs, falls back to use the backup string headers and items instead. The initial + // selection is the 0th item in the menu, which is expected to reboot the device without a wipe. + virtual size_t ShowPromptWipeDataMenu(const std::vector& backup_headers, + const std::vector& backup_items, + const std::function& key_handler) = 0; + // Displays the localized wipe data confirmation menu with pre-generated images. Falls back to + // the text strings upon failures. The initial selection is the 0th item, which returns to the + // upper level menu. + virtual size_t ShowPromptWipeDataConfirmationMenu( + const std::vector& backup_headers, const std::vector& backup_items, + const std::function& key_handler) = 0; + + // Set whether or not the fastbootd logo is displayed. + void SetEnableFastbootdLogo(bool enable) { + fastbootd_logo_enabled_ = enable; + } + + // Resets the key interrupt status. + void ResetKeyInterruptStatus() { + key_interrupted_ = false; + } + + // Returns the key interrupt status. + bool IsKeyInterrupted() const { + return key_interrupted_; + } + + protected: + void EnqueueKey(int key_code); + + // The normal and dimmed brightness percentages (default: 50 and 25, which means 50% and 25% of + // the max_brightness). Because the absolute values may vary across devices. These two values can + // be configured via subclassing. Setting brightness_normal_ to 0 to disable screensaver. + unsigned int brightness_normal_; + unsigned int brightness_dimmed_; + std::string brightness_file_; + std::string max_brightness_file_; + + // Whether we should listen for touch inputs (default: false). + bool touch_screen_allowed_; + + bool fastbootd_logo_enabled_; + + private: + enum class ScreensaverState { + DISABLED, + NORMAL, + DIMMED, + OFF, + }; + + // The sensitivity when detecting a swipe. + const int touch_low_threshold_; + const int touch_high_threshold_; + + void OnKeyDetected(int key_code); + void OnTouchDetected(int dx, int dy); + int OnInputEvent(int fd, uint32_t epevents); + void ProcessKey(int key_code, int updown); + void TimeKey(int key_code, int count); + + bool IsUsbConnected(); + + bool InitScreensaver(); + void SetScreensaverState(ScreensaverState state); + // Key event input queue + std::mutex key_queue_mutex; + std::condition_variable key_queue_cond; + bool key_interrupted_; + int key_queue[256], key_queue_len; + char key_pressed[KEY_MAX + 1]; // under key_queue_mutex + int key_last_down; // under key_queue_mutex + bool key_long_press; // under key_queue_mutex + int key_down_count; // under key_queue_mutex + bool enable_reboot; // under key_queue_mutex + int rel_sum; + + int consecutive_power_keys; + int last_key; + + bool has_power_key; + bool has_up_key; + bool has_down_key; + bool has_touch_screen; + + // Touch event related variables. See the comments in RecoveryUI::OnInputEvent(). + int touch_slot_; + int touch_X_; + int touch_Y_; + int touch_start_X_; + int touch_start_Y_; + bool touch_finger_down_; + bool touch_swiping_; + bool is_bootreason_recovery_ui_; + + std::thread input_thread_; + std::atomic input_thread_stopped_{ false }; + + ScreensaverState screensaver_state_; + + // The following two contain the absolute values computed from brightness_normal_ and + // brightness_dimmed_ respectively. + unsigned int brightness_normal_value_; + unsigned int brightness_dimmed_value_; +}; + +#endif // RECOVERY_UI_H diff --git a/recovery_ui/include/recovery_ui/vr_ui.h b/recovery_ui/include/recovery_ui/vr_ui.h new file mode 100644 index 000000000..2e8ac5921 --- /dev/null +++ b/recovery_ui/include/recovery_ui/vr_ui.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_VR_UI_H +#define RECOVERY_VR_UI_H + +#include + +#include "screen_ui.h" + +class VrRecoveryUI : public ScreenRecoveryUI { + public: + VrRecoveryUI(); + + protected: + // Pixel offsets to move drawing functions to visible range. + // Can vary per device depending on screen size and lens distortion. + const int stereo_offset_; + + int ScreenWidth() const override; + int ScreenHeight() const override; + + void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, + int dy) const override; + int DrawHorizontalRule(int y) const override; + void DrawHighlightBar(int x, int y, int width, int height) const override; + void DrawFill(int x, int y, int w, int h) const override; + void DrawTextIcon(int x, int y, const GRSurface* surface) const override; + int DrawTextLine(int x, int y, const std::string& line, bool bold) const override; +}; + +#endif // RECOVERY_VR_UI_H diff --git a/recovery_ui/include/recovery_ui/wear_ui.h b/recovery_ui/include/recovery_ui/wear_ui.h new file mode 100644 index 000000000..429af69d2 --- /dev/null +++ b/recovery_ui/include/recovery_ui/wear_ui.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_WEAR_UI_H +#define RECOVERY_WEAR_UI_H + +#include +#include + +#include "screen_ui.h" + +class WearRecoveryUI : public ScreenRecoveryUI { + public: + WearRecoveryUI(); + + void SetStage(int current, int max) override; + + protected: + // progress bar vertical position, it's centered horizontally + const int progress_bar_baseline_; + + // Unusable rows when displaying the recovery menu, including the lines for headers (Android + // Recovery, build id and etc) and the bottom lines that may otherwise go out of the screen. + const int menu_unusable_rows_; + + std::unique_ptr CreateMenu(const std::vector& text_headers, + const std::vector& text_items, + size_t initial_selection) const override; + + int GetProgressBaseline() const override; + + void update_progress_locked() override; + + private: + void draw_background_locked() override; + void draw_screen_locked() override; +}; + +#endif // RECOVERY_WEAR_UI_H diff --git a/recovery_ui/screen_ui.cpp b/recovery_ui/screen_ui.cpp new file mode 100644 index 000000000..870db621c --- /dev/null +++ b/recovery_ui/screen_ui.cpp @@ -0,0 +1,1334 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/screen_ui.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "minui/minui.h" +#include "otautil/paths.h" +#include "recovery_ui/device.h" +#include "recovery_ui/ui.h" + +// Return the current time as a double (including fractions of a second). +static double now() { + struct timeval tv; + gettimeofday(&tv, nullptr); + return tv.tv_sec + tv.tv_usec / 1000000.0; +} + +Menu::Menu(size_t initial_selection, const DrawInterface& draw_func) + : selection_(initial_selection), draw_funcs_(draw_func) {} + +size_t Menu::selection() const { + return selection_; +} + +TextMenu::TextMenu(bool scrollable, size_t max_items, size_t max_length, + const std::vector& headers, const std::vector& items, + size_t initial_selection, int char_height, const DrawInterface& draw_funcs) + : Menu(initial_selection, draw_funcs), + scrollable_(scrollable), + max_display_items_(max_items), + max_item_length_(max_length), + text_headers_(headers), + menu_start_(0), + char_height_(char_height) { + CHECK_LE(max_items, static_cast(std::numeric_limits::max())); + + // It's fine to have more entries than text_rows_ if scrollable menu is supported. + size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_); + for (size_t i = 0; i < items_count; ++i) { + text_items_.emplace_back(items[i].substr(0, max_item_length_)); + } + + CHECK(!text_items_.empty()); +} + +const std::vector& TextMenu::text_headers() const { + return text_headers_; +} + +std::string TextMenu::TextItem(size_t index) const { + CHECK_LT(index, text_items_.size()); + + return text_items_[index]; +} + +size_t TextMenu::MenuStart() const { + return menu_start_; +} + +size_t TextMenu::MenuEnd() const { + return std::min(ItemsCount(), menu_start_ + max_display_items_); +} + +size_t TextMenu::ItemsCount() const { + return text_items_.size(); +} + +bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const { + if (!scrollable_ || ItemsCount() <= max_display_items_) { + return false; + } + + *cur_selection_str = + android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount()); + return true; +} + +// TODO(xunchang) modify the function parameters to button up & down. +int TextMenu::Select(int sel) { + CHECK_LE(ItemsCount(), static_cast(std::numeric_limits::max())); + int count = ItemsCount(); + + // Wraps the selection at boundary if the menu is not scrollable. + if (!scrollable_) { + if (sel < 0) { + selection_ = count - 1; + } else if (sel >= count) { + selection_ = 0; + } else { + selection_ = sel; + } + + return selection_; + } + + if (sel < 0) { + selection_ = 0; + } else if (sel >= count) { + selection_ = count - 1; + } else { + if (static_cast(sel) < menu_start_) { + menu_start_--; + } else if (static_cast(sel) >= MenuEnd()) { + menu_start_++; + } + selection_ = sel; + } + + return selection_; +} + +int TextMenu::DrawHeader(int x, int y) const { + int offset = 0; + + draw_funcs_.SetColor(UIElement::HEADER); + if (!scrollable()) { + offset += draw_funcs_.DrawWrappedTextLines(x, y + offset, text_headers()); + } else { + offset += draw_funcs_.DrawTextLines(x, y + offset, text_headers()); + // Show the current menu item number in relation to total number if items don't fit on the + // screen. + std::string cur_selection_str; + if (ItemsOverflow(&cur_selection_str)) { + offset += draw_funcs_.DrawTextLine(x, y + offset, cur_selection_str, true); + } + } + + return offset; +} + +int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { + int offset = 0; + + draw_funcs_.SetColor(UIElement::MENU); + // Do not draw the horizontal rule for wear devices. + if (!scrollable()) { + offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; + } + for (size_t i = MenuStart(); i < MenuEnd(); ++i) { + bool bold = false; + if (i == selection()) { + // Draw the highlight bar. + draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); + + int bar_height = char_height_ + 4; + draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); + + // Bold white text for the selected item. + draw_funcs_.SetColor(UIElement::MENU_SEL_FG); + bold = true; + } + offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold); + + draw_funcs_.SetColor(UIElement::MENU); + } + offset += draw_funcs_.DrawHorizontalRule(y + offset); + + return offset; +} + +GraphicMenu::GraphicMenu(const GRSurface* graphic_headers, + const std::vector& graphic_items, + size_t initial_selection, const DrawInterface& draw_funcs) + : Menu(initial_selection, draw_funcs) { + graphic_headers_ = graphic_headers->Clone(); + graphic_items_.reserve(graphic_items.size()); + for (const auto& item : graphic_items) { + graphic_items_.emplace_back(item->Clone()); + } +} + +int GraphicMenu::Select(int sel) { + CHECK_LE(graphic_items_.size(), static_cast(std::numeric_limits::max())); + int count = graphic_items_.size(); + + // Wraps the selection at boundary if the menu is not scrollable. + if (sel < 0) { + selection_ = count - 1; + } else if (sel >= count) { + selection_ = 0; + } else { + selection_ = sel; + } + + return selection_; +} + +int GraphicMenu::DrawHeader(int x, int y) const { + draw_funcs_.SetColor(UIElement::HEADER); + draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get()); + return graphic_headers_->height; +} + +int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { + int offset = 0; + + draw_funcs_.SetColor(UIElement::MENU); + offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; + + for (size_t i = 0; i < graphic_items_.size(); i++) { + auto& item = graphic_items_[i]; + if (i == selection_) { + draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); + + int bar_height = item->height + 4; + draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); + + // Bold white text for the selected item. + draw_funcs_.SetColor(UIElement::MENU_SEL_FG); + } + draw_funcs_.DrawTextIcon(x, y + offset, item.get()); + offset += item->height; + + draw_funcs_.SetColor(UIElement::MENU); + } + offset += draw_funcs_.DrawHorizontalRule(y + offset); + + return offset; +} + +bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, + const std::vector& graphic_items) { + int offset = 0; + if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) { + return false; + } + offset += graphic_headers->height; + + for (const auto& item : graphic_items) { + if (!ValidateGraphicSurface(max_width, max_height, offset, item)) { + return false; + } + offset += item->height; + } + + return true; +} + +bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y, + const GRSurface* surface) { + if (!surface) { + fprintf(stderr, "Graphic surface can not be null\n"); + return false; + } + + if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) { + fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu\n", + surface->pixel_bytes, surface->width, surface->row_bytes); + return false; + } + + if (surface->width > max_width || surface->height > max_height - y) { + fprintf(stderr, + "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu," + " max_height: %zu, vertical offset: %d\n", + surface->width, surface->height, max_width, max_height, y); + return false; + } + + return true; +} + +ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} + +constexpr int kDefaultMarginHeight = 0; +constexpr int kDefaultMarginWidth = 0; +constexpr int kDefaultAnimationFps = 30; + +ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) + : margin_width_( + android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)), + margin_height_( + android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)), + animation_fps_( + android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)), + density_(static_cast(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f), + current_icon_(NONE), + current_frame_(0), + intro_done_(false), + progressBarType(EMPTY), + progressScopeStart(0), + progressScopeSize(0), + progress(0), + pagesIdentical(false), + text_cols_(0), + text_rows_(0), + text_(nullptr), + text_col_(0), + text_row_(0), + show_text(false), + show_text_ever(false), + scrollable_menu_(scrollable_menu), + file_viewer_text_(nullptr), + stage(-1), + max_stage(-1), + locale_(""), + rtl_locale_(false) {} + +ScreenRecoveryUI::~ScreenRecoveryUI() { + progress_thread_stopped_ = true; + if (progress_thread_.joinable()) { + progress_thread_.join(); + } + // No-op if gr_init() (via Init()) was not called or had failed. + gr_exit(); +} + +const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { + if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) { + return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get(); + } + return error_icon_.get(); +} + +const GRSurface* ScreenRecoveryUI::GetCurrentText() const { + switch (current_icon_) { + case ERASING: + return erasing_text_.get(); + case ERROR: + return error_text_.get(); + case INSTALLING_UPDATE: + return installing_text_.get(); + case NO_COMMAND: + return no_command_text_.get(); + case NONE: + abort(); + } +} + +int ScreenRecoveryUI::PixelsFromDp(int dp) const { + return dp * density_; +} + +// Here's the intended layout: + +// | portrait large landscape large +// ---------+------------------------------------------------- +// gap | +// icon | (200dp) +// gap | 68dp 68dp 56dp 112dp +// text | (14sp) +// gap | 32dp 32dp 26dp 52dp +// progress | (2dp) +// gap | + +// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines +// work), so that's the more useful measurement for calling code. We use even top and bottom gaps. + +enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; +enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; +static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { + { 32, 68 }, // PORTRAIT + { 32, 68 }, // PORTRAIT_LARGE + { 26, 56 }, // LANDSCAPE + { 52, 112 }, // LANDSCAPE_LARGE +}; + +int ScreenRecoveryUI::GetAnimationBaseline() const { + return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - + gr_get_height(loop_frames_[0].get()); +} + +int ScreenRecoveryUI::GetTextBaseline() const { + return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - + gr_get_height(installing_text_.get()); +} + +int ScreenRecoveryUI::GetProgressBaseline() const { + int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) + + gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) + + gr_get_height(progress_bar_fill_.get()); + int bottom_gap = (ScreenHeight() - elements_sum) / 2; + return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get()); +} + +// Clear the screen and draw the currently selected background icon (if any). +// Should only be called with updateMutex locked. +void ScreenRecoveryUI::draw_background_locked() { + pagesIdentical = false; + gr_color(0, 0, 0, 255); + gr_clear(); + if (current_icon_ != NONE) { + if (max_stage != -1) { + int stage_height = gr_get_height(stage_marker_empty_.get()); + int stage_width = gr_get_width(stage_marker_empty_.get()); + int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2; + int y = ScreenHeight() - stage_height - margin_height_; + for (int i = 0; i < max_stage; ++i) { + const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_; + DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y); + x += stage_width; + } + } + + const auto& text_surface = GetCurrentText(); + int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2; + int text_y = GetTextBaseline(); + gr_color(255, 255, 255, 255); + DrawTextIcon(text_x, text_y, text_surface); + } +} + +// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be +// called with updateMutex locked. +void ScreenRecoveryUI::draw_foreground_locked() { + if (current_icon_ != NONE) { + const auto& frame = GetCurrentFrame(); + int frame_width = gr_get_width(frame); + int frame_height = gr_get_height(frame); + int frame_x = (ScreenWidth() - frame_width) / 2; + int frame_y = GetAnimationBaseline(); + DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); + } + + if (progressBarType != EMPTY) { + int width = gr_get_width(progress_bar_empty_.get()); + int height = gr_get_height(progress_bar_empty_.get()); + + int progress_x = (ScreenWidth() - width) / 2; + int progress_y = GetProgressBaseline(); + + // Erase behind the progress bar (in case this was a progress-only update) + gr_color(0, 0, 0, 255); + DrawFill(progress_x, progress_y, width, height); + + if (progressBarType == DETERMINATE) { + float p = progressScopeStart + progress * progressScopeSize; + int pos = static_cast(p * width); + + if (rtl_locale_) { + // Fill the progress bar from right to left. + if (pos > 0) { + DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height, + progress_x + width - pos, progress_y); + } + if (pos < width - 1) { + DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y); + } + } else { + // Fill the progress bar from left to right. + if (pos > 0) { + DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y); + } + if (pos < width - 1) { + DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos, + progress_y); + } + } + } + } +} + +void ScreenRecoveryUI::SetColor(UIElement e) const { + switch (e) { + case UIElement::INFO: + gr_color(249, 194, 0, 255); + break; + case UIElement::HEADER: + gr_color(247, 0, 6, 255); + break; + case UIElement::MENU: + case UIElement::MENU_SEL_BG: + gr_color(0, 106, 157, 255); + break; + case UIElement::MENU_SEL_BG_ACTIVE: + gr_color(0, 156, 100, 255); + break; + case UIElement::MENU_SEL_FG: + gr_color(255, 255, 255, 255); + break; + case UIElement::LOG: + gr_color(196, 196, 196, 255); + break; + case UIElement::TEXT_FILL: + gr_color(0, 0, 0, 160); + break; + default: + gr_color(255, 255, 255, 255); + break; + } +} + +void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector& locales_entries, + size_t sel) { + SetLocale(locales_entries[sel]); + std::vector text_name = { "erasing_text", "error_text", "installing_text", + "installing_security_text", "no_command_text" }; + std::unordered_map> surfaces; + for (const auto& name : text_name) { + auto text_image = LoadLocalizedBitmap(name); + if (!text_image) { + Print("Failed to load %s\n", name.c_str()); + return; + } + surfaces.emplace(name, std::move(text_image)); + } + + std::lock_guard lg(updateMutex); + gr_color(0, 0, 0, 255); + gr_clear(); + + int text_y = margin_height_; + int text_x = margin_width_; + int line_spacing = gr_sys_font()->char_height; // Put some extra space between images. + // Write the header and descriptive texts. + SetColor(UIElement::INFO); + std::string header = "Show background text image"; + text_y += DrawTextLine(text_x, text_y, header, true); + std::string locale_selection = android::base::StringPrintf( + "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size()); + // clang-format off + std::vector instruction = { + locale_selection, + "Use volume up/down to switch locales and power to exit." + }; + // clang-format on + text_y += DrawWrappedTextLines(text_x, text_y, instruction); + + // Iterate through the text images and display them in order for the current locale. + for (const auto& p : surfaces) { + text_y += line_spacing; + SetColor(UIElement::LOG); + text_y += DrawTextLine(text_x, text_y, p.first, false); + gr_color(255, 255, 255, 255); + gr_texticon(text_x, text_y, p.second.get()); + text_y += gr_get_height(p.second.get()); + } + // Update the whole screen. + gr_flip(); +} + +void ScreenRecoveryUI::CheckBackgroundTextImages() { + // Load a list of locales embedded in one of the resource files. + std::vector locales_entries = get_locales_in_png("installing_text"); + if (locales_entries.empty()) { + Print("Failed to load locales from the resource files\n"); + return; + } + std::string saved_locale = locale_; + size_t selected = 0; + SelectAndShowBackgroundText(locales_entries, selected); + + FlushKeys(); + while (true) { + int key = WaitKey(); + if (key == static_cast(KeyError::INTERRUPTED)) break; + if (key == KEY_POWER || key == KEY_ENTER) { + break; + } else if (key == KEY_UP || key == KEY_VOLUMEUP) { + selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1; + SelectAndShowBackgroundText(locales_entries, selected); + } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) { + selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1; + SelectAndShowBackgroundText(locales_entries, selected); + } + } + + SetLocale(saved_locale); +} + +int ScreenRecoveryUI::ScreenWidth() const { + return gr_fb_width(); +} + +int ScreenRecoveryUI::ScreenHeight() const { + return gr_fb_height(); +} + +void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, + int dy) const { + gr_blit(surface, sx, sy, w, h, dx, dy); +} + +int ScreenRecoveryUI::DrawHorizontalRule(int y) const { + gr_fill(0, y + 4, ScreenWidth(), y + 6); + return 8; +} + +void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { + gr_fill(x, y, x + width, y + height); +} + +void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const { + gr_fill(x, y, w, h); +} + +void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const { + gr_texticon(x, y, surface); +} + +int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const { + gr_text(gr_sys_font(), x, y, line.c_str(), bold); + return char_height_ + 4; +} + +int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector& lines) const { + int offset = 0; + for (const auto& line : lines) { + offset += DrawTextLine(x, y + offset, line, false); + } + return offset; +} + +int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, + const std::vector& lines) const { + // Keep symmetrical margins based on the given offset (i.e. x). + size_t text_cols = (ScreenWidth() - x * 2) / char_width_; + int offset = 0; + for (const auto& line : lines) { + size_t next_start = 0; + while (next_start < line.size()) { + std::string sub = line.substr(next_start, text_cols + 1); + if (sub.size() <= text_cols) { + next_start += sub.size(); + } else { + // Line too long and must be wrapped to text_cols columns. + size_t last_space = sub.find_last_of(" \t\n"); + if (last_space == std::string::npos) { + // No space found, just draw as much as we can. + sub.resize(text_cols); + next_start += text_cols; + } else { + sub.resize(last_space); + next_start += last_space + 1; + } + } + offset += DrawTextLine(x, y + offset, sub, false); + } + } + return offset; +} + +void ScreenRecoveryUI::SetTitle(const std::vector& lines) { + title_lines_ = lines; +} + +// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex +// locked. +void ScreenRecoveryUI::draw_screen_locked() { + if (!show_text) { + draw_background_locked(); + draw_foreground_locked(); + return; + } + + gr_color(0, 0, 0, 255); + gr_clear(); + + // clang-format off + static std::vector REGULAR_HELP{ + "Use volume up/down and power.", + }; + static std::vector LONG_PRESS_HELP{ + "Any button cycles highlight.", + "Long-press activates.", + }; + // clang-format on + draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); +} + +// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked. +void ScreenRecoveryUI::draw_menu_and_text_buffer_locked( + const std::vector& help_message) { + int y = margin_height_; + + if (fastbootd_logo_ && fastbootd_logo_enabled_) { + // Try to get this centered on screen. + auto width = gr_get_width(fastbootd_logo_.get()); + auto height = gr_get_height(fastbootd_logo_.get()); + auto centered_x = ScreenWidth() / 2 - width / 2; + DrawSurface(fastbootd_logo_.get(), 0, 0, width, height, centered_x, y); + y += height; + } + + if (menu_) { + int x = margin_width_ + kMenuIndent; + + SetColor(UIElement::INFO); + + for (size_t i = 0; i < title_lines_.size(); i++) { + y += DrawTextLine(x, y, title_lines_[i], i == 0); + } + + y += DrawTextLines(x, y, help_message); + + y += menu_->DrawHeader(x, y); + y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress()); + } + + // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or + // we've displayed the entire text buffer. + SetColor(UIElement::LOG); + int row = text_row_; + size_t count = 0; + for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_; + ty -= char_height_, ++count) { + DrawTextLine(margin_width_, ty, text_[row], false); + --row; + if (row < 0) row = text_rows_ - 1; + } +} + +// Redraw everything on the screen and flip the screen (make it visible). +// Should only be called with updateMutex locked. +void ScreenRecoveryUI::update_screen_locked() { + draw_screen_locked(); + gr_flip(); +} + +// Updates only the progress bar, if possible, otherwise redraws the screen. +// Should only be called with updateMutex locked. +void ScreenRecoveryUI::update_progress_locked() { + if (show_text || !pagesIdentical) { + draw_screen_locked(); // Must redraw the whole screen + pagesIdentical = true; + } else { + draw_foreground_locked(); // Draw only the progress bar and overlays + } + gr_flip(); +} + +void ScreenRecoveryUI::ProgressThreadLoop() { + double interval = 1.0 / animation_fps_; + while (!progress_thread_stopped_) { + double start = now(); + bool redraw = false; + { + std::lock_guard lg(updateMutex); + + // update the installation animation, if active + // skip this if we have a text overlay (too expensive to update) + if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) { + if (!intro_done_) { + if (current_frame_ == intro_frames_.size() - 1) { + intro_done_ = true; + current_frame_ = 0; + } else { + ++current_frame_; + } + } else { + current_frame_ = (current_frame_ + 1) % loop_frames_.size(); + } + + redraw = true; + } + + // move the progress bar forward on timed intervals, if configured + int duration = progressScopeDuration; + if (progressBarType == DETERMINATE && duration > 0) { + double elapsed = now() - progressScopeTime; + float p = 1.0 * elapsed / duration; + if (p > 1.0) p = 1.0; + if (p > progress) { + progress = p; + redraw = true; + } + } + + if (redraw) update_progress_locked(); + } + + double end = now(); + // minimum of 20ms delay between frames + double delay = interval - (end - start); + if (delay < 0.02) delay = 0.02; + usleep(static_cast(delay * 1000000)); + } +} + +std::unique_ptr ScreenRecoveryUI::LoadBitmap(const std::string& filename) { + GRSurface* surface; + if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) { + LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")"; + return nullptr; + } + return std::unique_ptr(surface); +} + +std::unique_ptr ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) { + GRSurface* surface; + if (auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface); + result < 0) { + LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")"; + return nullptr; + } + return std::unique_ptr(surface); +} + +static char** Alloc2d(size_t rows, size_t cols) { + char** result = new char*[rows]; + for (size_t i = 0; i < rows; ++i) { + result[i] = new char[cols]; + memset(result[i], 0, cols); + } + return result; +} + +// Choose the right background string to display during update. +void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { + if (security_update) { + installing_text_ = LoadLocalizedBitmap("installing_security_text"); + } else { + installing_text_ = LoadLocalizedBitmap("installing_text"); + } + Redraw(); +} + +bool ScreenRecoveryUI::InitTextParams() { + // gr_init() would return successfully on font initialization failure. + if (gr_sys_font() == nullptr) { + return false; + } + gr_font_size(gr_sys_font(), &char_width_, &char_height_); + text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_; + text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_; + return true; +} + +bool ScreenRecoveryUI::LoadWipeDataMenuText() { + // Ignores the errors since the member variables will stay as nullptr. + cancel_wipe_data_text_ = LoadLocalizedBitmap("cancel_wipe_data_text"); + factory_data_reset_text_ = LoadLocalizedBitmap("factory_data_reset_text"); + try_again_text_ = LoadLocalizedBitmap("try_again_text"); + wipe_data_confirmation_text_ = LoadLocalizedBitmap("wipe_data_confirmation_text"); + wipe_data_menu_header_text_ = LoadLocalizedBitmap("wipe_data_menu_header_text"); + return true; +} + +bool ScreenRecoveryUI::Init(const std::string& locale) { + RecoveryUI::Init(locale); + + if (gr_init() == -1) { + return false; + } + + if (!InitTextParams()) { + return false; + } + + // Are we portrait or landscape? + layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; + // Are we the large variant of our base layout? + if (gr_fb_height() > PixelsFromDp(800)) ++layout_; + + text_ = Alloc2d(text_rows_, text_cols_ + 1); + file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); + + text_col_ = text_row_ = 0; + + // Set up the locale info. + SetLocale(locale); + + error_icon_ = LoadBitmap("icon_error"); + + progress_bar_empty_ = LoadBitmap("progress_empty"); + progress_bar_fill_ = LoadBitmap("progress_fill"); + stage_marker_empty_ = LoadBitmap("stage_empty"); + stage_marker_fill_ = LoadBitmap("stage_fill"); + + erasing_text_ = LoadLocalizedBitmap("erasing_text"); + no_command_text_ = LoadLocalizedBitmap("no_command_text"); + error_text_ = LoadLocalizedBitmap("error_text"); + + if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { + fastbootd_logo_ = LoadBitmap("fastbootd"); + } + + // Background text for "installing_update" could be "installing update" or + // "installing security update". It will be set after Init() according to the commands in BCB. + installing_text_.reset(); + + LoadWipeDataMenuText(); + + LoadAnimation(); + + // Keep the progress bar updated, even when the process is otherwise busy. + progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this); + + return true; +} + +std::string ScreenRecoveryUI::GetLocale() const { + return locale_; +} + +void ScreenRecoveryUI::LoadAnimation() { + std::unique_ptr dir(opendir(Paths::Get().resource_dir().c_str()), + closedir); + dirent* de; + std::vector intro_frame_names; + std::vector loop_frame_names; + + while ((de = readdir(dir.get())) != nullptr) { + int value, num_chars; + if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { + intro_frame_names.emplace_back(de->d_name, num_chars); + } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { + loop_frame_names.emplace_back(de->d_name, num_chars); + } + } + + size_t intro_frames = intro_frame_names.size(); + size_t loop_frames = loop_frame_names.size(); + + // It's okay to not have an intro. + if (intro_frames == 0) intro_done_ = true; + // But you must have an animation. + if (loop_frames == 0) abort(); + + std::sort(intro_frame_names.begin(), intro_frame_names.end()); + std::sort(loop_frame_names.begin(), loop_frame_names.end()); + + intro_frames_.clear(); + intro_frames_.reserve(intro_frames); + for (const auto& frame_name : intro_frame_names) { + intro_frames_.emplace_back(LoadBitmap(frame_name)); + } + + loop_frames_.clear(); + loop_frames_.reserve(loop_frames); + for (const auto& frame_name : loop_frame_names) { + loop_frames_.emplace_back(LoadBitmap(frame_name)); + } +} + +void ScreenRecoveryUI::SetBackground(Icon icon) { + std::lock_guard lg(updateMutex); + + current_icon_ = icon; + update_screen_locked(); +} + +void ScreenRecoveryUI::SetProgressType(ProgressType type) { + std::lock_guard lg(updateMutex); + if (progressBarType != type) { + progressBarType = type; + } + progressScopeStart = 0; + progressScopeSize = 0; + progress = 0; + update_progress_locked(); +} + +void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { + std::lock_guard lg(updateMutex); + progressBarType = DETERMINATE; + progressScopeStart += progressScopeSize; + progressScopeSize = portion; + progressScopeTime = now(); + progressScopeDuration = seconds; + progress = 0; + update_progress_locked(); +} + +void ScreenRecoveryUI::SetProgress(float fraction) { + std::lock_guard lg(updateMutex); + if (fraction < 0.0) fraction = 0.0; + if (fraction > 1.0) fraction = 1.0; + if (progressBarType == DETERMINATE && fraction > progress) { + // Skip updates that aren't visibly different. + int width = gr_get_width(progress_bar_empty_.get()); + float scale = width * progressScopeSize; + if ((int)(progress * scale) != (int)(fraction * scale)) { + progress = fraction; + update_progress_locked(); + } + } +} + +void ScreenRecoveryUI::SetStage(int current, int max) { + std::lock_guard lg(updateMutex); + stage = current; + max_stage = max; +} + +void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { + std::string str; + android::base::StringAppendV(&str, fmt, ap); + + if (copy_to_stdout) { + fputs(str.c_str(), stdout); + } + + std::lock_guard lg(updateMutex); + if (text_rows_ > 0 && text_cols_ > 0) { + for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { + if (*ptr == '\n' || text_col_ >= text_cols_) { + text_[text_row_][text_col_] = '\0'; + text_col_ = 0; + text_row_ = (text_row_ + 1) % text_rows_; + } + if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; + } + text_[text_row_][text_col_] = '\0'; + update_screen_locked(); + } +} + +void ScreenRecoveryUI::Print(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + PrintV(fmt, true, ap); + va_end(ap); +} + +void ScreenRecoveryUI::PrintOnScreenOnly(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + PrintV(fmt, false, ap); + va_end(ap); +} + +void ScreenRecoveryUI::PutChar(char ch) { + std::lock_guard lg(updateMutex); + if (ch != '\n') text_[text_row_][text_col_++] = ch; + if (ch == '\n' || text_col_ >= text_cols_) { + text_col_ = 0; + ++text_row_; + } +} + +void ScreenRecoveryUI::ClearText() { + std::lock_guard lg(updateMutex); + text_col_ = 0; + text_row_ = 0; + for (size_t i = 0; i < text_rows_; ++i) { + memset(text_[i], 0, text_cols_ + 1); + } +} + +void ScreenRecoveryUI::ShowFile(FILE* fp) { + std::vector offsets; + offsets.push_back(ftello(fp)); + ClearText(); + + struct stat sb; + fstat(fileno(fp), &sb); + + bool show_prompt = false; + while (true) { + if (show_prompt) { + PrintOnScreenOnly("--(%d%% of %d bytes)--", + static_cast(100 * (double(ftello(fp)) / double(sb.st_size))), + static_cast(sb.st_size)); + Redraw(); + while (show_prompt) { + show_prompt = false; + int key = WaitKey(); + if (key == static_cast(KeyError::INTERRUPTED)) return; + if (key == KEY_POWER || key == KEY_ENTER) { + return; + } else if (key == KEY_UP || key == KEY_VOLUMEUP) { + if (offsets.size() <= 1) { + show_prompt = true; + } else { + offsets.pop_back(); + fseek(fp, offsets.back(), SEEK_SET); + } + } else { + if (feof(fp)) { + return; + } + offsets.push_back(ftello(fp)); + } + } + ClearText(); + } + + int ch = getc(fp); + if (ch == EOF) { + while (text_row_ < text_rows_ - 1) PutChar('\n'); + show_prompt = true; + } else { + PutChar(ch); + if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { + show_prompt = true; + } + } + } +} + +void ScreenRecoveryUI::ShowFile(const std::string& filename) { + std::unique_ptr fp(fopen(filename.c_str(), "re"), fclose); + if (!fp) { + Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno)); + return; + } + + char** old_text = text_; + size_t old_text_col = text_col_; + size_t old_text_row = text_row_; + + // Swap in the alternate screen and clear it. + text_ = file_viewer_text_; + ClearText(); + + ShowFile(fp.get()); + + text_ = old_text; + text_col_ = old_text_col; + text_row_ = old_text_row; +} + +std::unique_ptr ScreenRecoveryUI::CreateMenu( + const GRSurface* graphic_header, const std::vector& graphic_items, + const std::vector& text_headers, const std::vector& text_items, + size_t initial_selection) const { + // horizontal unusable area: margin width + menu indent + size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent; + // vertical unusable area: margin height + title lines + helper message + high light bar. + // It is safe to reserve more space. + size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3); + if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) { + return std::make_unique(graphic_header, graphic_items, initial_selection, *this); + } + + fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n"); + + return CreateMenu(text_headers, text_items, initial_selection); +} + +std::unique_ptr ScreenRecoveryUI::CreateMenu(const std::vector& text_headers, + const std::vector& text_items, + size_t initial_selection) const { + if (text_rows_ > 0 && text_cols_ > 1) { + return std::make_unique(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers, + text_items, initial_selection, char_height_, *this); + } + + fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_, + text_cols_); + return nullptr; +} + +int ScreenRecoveryUI::SelectMenu(int sel) { + std::lock_guard lg(updateMutex); + if (menu_) { + int old_sel = menu_->selection(); + sel = menu_->Select(sel); + + if (sel != old_sel) { + update_screen_locked(); + } + } + return sel; +} + +size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr&& menu, bool menu_only, + const std::function& key_handler) { + // Throw away keys pressed previously, so user doesn't accidentally trigger menu items. + FlushKeys(); + + // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the + // menu. + if (IsKeyInterrupted()) return static_cast(KeyError::INTERRUPTED); + + CHECK(menu != nullptr); + + // Starts and displays the menu + menu_ = std::move(menu); + Redraw(); + + int selected = menu_->selection(); + int chosen_item = -1; + while (chosen_item < 0) { + int key = WaitKey(); + if (key == static_cast(KeyError::INTERRUPTED)) { // WaitKey() was interrupted. + return static_cast(KeyError::INTERRUPTED); + } + if (key == static_cast(KeyError::TIMED_OUT)) { // WaitKey() timed out. + if (WasTextEverVisible()) { + continue; + } else { + LOG(INFO) << "Timed out waiting for key input; rebooting."; + menu_.reset(); + Redraw(); + return static_cast(KeyError::TIMED_OUT); + } + } + + bool visible = IsTextVisible(); + int action = key_handler(key, visible); + if (action < 0) { + switch (action) { + case Device::kHighlightUp: + selected = SelectMenu(--selected); + break; + case Device::kHighlightDown: + selected = SelectMenu(++selected); + break; + case Device::kInvokeItem: + chosen_item = selected; + break; + case Device::kNoAction: + break; + } + } else if (!menu_only) { + chosen_item = action; + } + } + + menu_.reset(); + Redraw(); + + return chosen_item; +} + +size_t ScreenRecoveryUI::ShowMenu(const std::vector& headers, + const std::vector& items, size_t initial_selection, + bool menu_only, + const std::function& key_handler) { + auto menu = CreateMenu(headers, items, initial_selection); + if (menu == nullptr) { + return initial_selection; + } + + return ShowMenu(CreateMenu(headers, items, initial_selection), menu_only, key_handler); +} + +size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector& backup_headers, + const std::vector& backup_items, + const std::function& key_handler) { + auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(), + { try_again_text_.get(), factory_data_reset_text_.get() }, + backup_headers, backup_items, 0); + if (wipe_data_menu == nullptr) { + return 0; + } + + return ShowMenu(std::move(wipe_data_menu), true, key_handler); +} + +size_t ScreenRecoveryUI::ShowPromptWipeDataConfirmationMenu( + const std::vector& backup_headers, const std::vector& backup_items, + const std::function& key_handler) { + auto confirmation_menu = + CreateMenu(wipe_data_confirmation_text_.get(), + { cancel_wipe_data_text_.get(), factory_data_reset_text_.get() }, backup_headers, + backup_items, 0); + if (confirmation_menu == nullptr) { + return 0; + } + + return ShowMenu(std::move(confirmation_menu), true, key_handler); +} + +bool ScreenRecoveryUI::IsTextVisible() { + std::lock_guard lg(updateMutex); + int visible = show_text; + return visible; +} + +bool ScreenRecoveryUI::WasTextEverVisible() { + std::lock_guard lg(updateMutex); + int ever_visible = show_text_ever; + return ever_visible; +} + +void ScreenRecoveryUI::ShowText(bool visible) { + std::lock_guard lg(updateMutex); + show_text = visible; + if (show_text) show_text_ever = true; + update_screen_locked(); +} + +void ScreenRecoveryUI::Redraw() { + std::lock_guard lg(updateMutex); + update_screen_locked(); +} + +void ScreenRecoveryUI::KeyLongPress(int) { + // Redraw so that if we're in the menu, the highlight + // will change color to indicate a successful long press. + Redraw(); +} + +void ScreenRecoveryUI::SetLocale(const std::string& new_locale) { + locale_ = new_locale; + rtl_locale_ = false; + + if (!new_locale.empty()) { + size_t separator = new_locale.find('-'); + // lang has the language prefix prior to the separator, or full string if none exists. + std::string lang = new_locale.substr(0, separator); + + // A bit cheesy: keep an explicit list of supported RTL languages. + if (lang == "ar" || // Arabic + lang == "fa" || // Persian (Farsi) + lang == "he" || // Hebrew (new language code) + lang == "iw" || // Hebrew (old language code) + lang == "ur") { // Urdu + rtl_locale_ = true; + } + } +} diff --git a/recovery_ui/ui.cpp b/recovery_ui/ui.cpp new file mode 100644 index 000000000..b7107ff21 --- /dev/null +++ b/recovery_ui/ui.cpp @@ -0,0 +1,597 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/ui.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "minui/minui.h" +#include "otautil/sysutil.h" + +using namespace std::chrono_literals; + +constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; +constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; +constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness"; +constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness"; +constexpr const char* MAX_BRIGHTNESS_FILE_SDM = + "/sys/class/backlight/panel0-backlight/max_brightness"; + +constexpr int kDefaultTouchLowThreshold = 50; +constexpr int kDefaultTouchHighThreshold = 90; + +RecoveryUI::RecoveryUI() + : brightness_normal_(50), + brightness_dimmed_(25), + brightness_file_(BRIGHTNESS_FILE), + max_brightness_file_(MAX_BRIGHTNESS_FILE), + touch_screen_allowed_(false), + fastbootd_logo_enabled_(false), + touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold", + kDefaultTouchLowThreshold)), + touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold", + kDefaultTouchHighThreshold)), + key_interrupted_(false), + key_queue_len(0), + key_last_down(-1), + key_long_press(false), + key_down_count(0), + enable_reboot(true), + consecutive_power_keys(0), + last_key(-1), + has_power_key(false), + has_up_key(false), + has_down_key(false), + has_touch_screen(false), + touch_slot_(0), + is_bootreason_recovery_ui_(false), + screensaver_state_(ScreensaverState::DISABLED) { + memset(key_pressed, 0, sizeof(key_pressed)); +} + +RecoveryUI::~RecoveryUI() { + ev_exit(); + input_thread_stopped_ = true; + if (input_thread_.joinable()) { + input_thread_.join(); + } +} + +void RecoveryUI::OnKeyDetected(int key_code) { + if (key_code == KEY_POWER) { + has_power_key = true; + } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { + has_down_key = true; + } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { + has_up_key = true; + } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) { + has_touch_screen = true; + } +} + +bool RecoveryUI::InitScreensaver() { + // Disabled. + if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) { + return false; + } + if (access(brightness_file_.c_str(), R_OK | W_OK)) { + brightness_file_ = BRIGHTNESS_FILE_SDM; + } + if (access(max_brightness_file_.c_str(), R_OK)) { + max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM; + } + // Set the initial brightness level based on the max brightness. Note that reading the initial + // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so + // we don't have a good way to query the default value. + std::string content; + if (!android::base::ReadFileToString(max_brightness_file_, &content)) { + PLOG(WARNING) << "Failed to read max brightness"; + return false; + } + + unsigned int max_value; + if (!android::base::ParseUint(android::base::Trim(content), &max_value)) { + LOG(WARNING) << "Failed to parse max brightness: " << content; + return false; + } + + brightness_normal_value_ = max_value * brightness_normal_ / 100.0; + brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0; + if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_), + brightness_file_)) { + PLOG(WARNING) << "Failed to set brightness"; + return false; + } + + LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)"; + screensaver_state_ = ScreensaverState::NORMAL; + return true; +} + +bool RecoveryUI::Init(const std::string& /* locale */) { + ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2), + touch_screen_allowed_); + + ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); + + if (touch_screen_allowed_) { + ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); + + // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of + // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way + // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or + // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text + // mode will be turned on automatically on debuggable builds, even without a swipe. + std::string cmdline; + if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) { + is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos; + } else { + // Non-fatal, and won't affect Init() result. + PLOG(WARNING) << "Failed to read /proc/cmdline"; + } + } + + if (!InitScreensaver()) { + LOG(INFO) << "Screensaver disabled"; + } + + // Create a separate thread that handles input events. + input_thread_ = std::thread([this]() { + while (!this->input_thread_stopped_) { + if (!ev_wait(500)) { + ev_dispatch(); + } + } + }); + + return true; +} + +void RecoveryUI::OnTouchDetected(int dx, int dy) { + enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction; + + // We only consider a valid swipe if: + // - the delta along one axis is below touch_low_threshold_; + // - and the delta along the other axis is beyond touch_high_threshold_. + if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) { + direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT; + } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) { + direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN; + } else { + LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_ + << ", high: " << touch_high_threshold_ << ")"; + return; + } + + // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui. + if (is_bootreason_recovery_ui_ && !IsTextVisible()) { + ShowText(true); + return; + } + + LOG(DEBUG) << "Swipe direction=" << direction; + switch (direction) { + case SwipeDirection::UP: + ProcessKey(KEY_UP, 1); // press up key + ProcessKey(KEY_UP, 0); // and release it + break; + + case SwipeDirection::DOWN: + ProcessKey(KEY_DOWN, 1); // press down key + ProcessKey(KEY_DOWN, 0); // and release it + break; + + case SwipeDirection::LEFT: + case SwipeDirection::RIGHT: + ProcessKey(KEY_POWER, 1); // press power key + ProcessKey(KEY_POWER, 0); // and release it + break; + }; +} + +int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { + struct input_event ev; + if (ev_get_input(fd, epevents, &ev) == -1) { + return -1; + } + + // Touch inputs handling. + // + // We handle the touch inputs by tracking the position changes between initial contacting and + // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon + // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes. + // + // Per the doc Multi-touch Protocol at below, there are two protocols. + // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt + // + // The main difference between the stateless type A protocol and the stateful type B slot protocol + // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The + // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and + // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send + // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event. + // + // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for + // ABS_MT_TRACKING_ID being -1. + // + // Touch input events will only be available if touch_screen_allowed_ is set. + + if (ev.type == EV_SYN) { + if (touch_screen_allowed_ && ev.code == SYN_REPORT) { + // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the + // contact. + if (touch_finger_down_ && !touch_swiping_) { + touch_start_X_ = touch_X_; + touch_start_Y_ = touch_Y_; + touch_swiping_ = true; + } else if (!touch_finger_down_ && touch_swiping_) { + touch_swiping_ = false; + OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_); + } + } + return 0; + } + + if (ev.type == EV_REL) { + if (ev.code == REL_Y) { + // accumulate the up or down motion reported by + // the trackball. When it exceeds a threshold + // (positive or negative), fake an up/down + // key event. + rel_sum += ev.value; + if (rel_sum > 3) { + ProcessKey(KEY_DOWN, 1); // press down key + ProcessKey(KEY_DOWN, 0); // and release it + rel_sum = 0; + } else if (rel_sum < -3) { + ProcessKey(KEY_UP, 1); // press up key + ProcessKey(KEY_UP, 0); // and release it + rel_sum = 0; + } + } + } else { + rel_sum = 0; + } + + if (touch_screen_allowed_ && ev.type == EV_ABS) { + if (ev.code == ABS_MT_SLOT) { + touch_slot_ = ev.value; + } + // Ignore other fingers. + if (touch_slot_ > 0) return 0; + + switch (ev.code) { + case ABS_MT_POSITION_X: + touch_X_ = ev.value; + touch_finger_down_ = true; + break; + + case ABS_MT_POSITION_Y: + touch_Y_ = ev.value; + touch_finger_down_ = true; + break; + + case ABS_MT_TRACKING_ID: + // Protocol B: -1 marks lifting the contact. + if (ev.value < 0) touch_finger_down_ = false; + break; + } + return 0; + } + + if (ev.type == EV_KEY && ev.code <= KEY_MAX) { + if (touch_screen_allowed_) { + if (ev.code == BTN_TOUCH) { + // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means + // lifting the contact. + touch_finger_down_ = (ev.value == 1); + } + + // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger + // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than + // KEY_POWER and KEY_UP as KEY_DOWN). + if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) { + return 0; + } + } + + ProcessKey(ev.code, ev.value); + } + + return 0; +} + +// Processes a key-up or -down event. A key is "registered" when it is pressed and then released, +// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to +// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed +// next time the foreground thread wants a key (eg, for the menu). +// +// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed() +// to see what other keys are held when a key is registered. +// +// updown == 1 for key down events; 0 for key up events +void RecoveryUI::ProcessKey(int key_code, int updown) { + bool register_key = false; + bool long_press = false; + + { + std::lock_guard lg(key_queue_mutex); + key_pressed[key_code] = updown; + if (updown) { + ++key_down_count; + key_last_down = key_code; + key_long_press = false; + std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count); + time_key_thread.detach(); + } else { + if (key_last_down == key_code) { + long_press = key_long_press; + register_key = true; + } + key_last_down = -1; + } + } + + bool reboot_enabled = enable_reboot; + if (register_key) { + switch (CheckKey(key_code, long_press)) { + case RecoveryUI::IGNORE: + break; + + case RecoveryUI::TOGGLE: + ShowText(!IsTextVisible()); + break; + + case RecoveryUI::REBOOT: + if (reboot_enabled) { + reboot("reboot,"); + while (true) { + pause(); + } + } + break; + + case RecoveryUI::ENQUEUE: + EnqueueKey(key_code); + break; + } + } +} + +void RecoveryUI::TimeKey(int key_code, int count) { + std::this_thread::sleep_for(750ms); // 750 ms == "long" + bool long_press = false; + { + std::lock_guard lg(key_queue_mutex); + if (key_last_down == key_code && key_down_count == count) { + long_press = key_long_press = true; + } + } + if (long_press) KeyLongPress(key_code); +} + +void RecoveryUI::EnqueueKey(int key_code) { + std::lock_guard lg(key_queue_mutex); + const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); + if (key_queue_len < queue_max) { + key_queue[key_queue_len++] = key_code; + key_queue_cond.notify_one(); + } +} + +void RecoveryUI::SetScreensaverState(ScreensaverState state) { + switch (state) { + case ScreensaverState::NORMAL: + if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_), + brightness_file_)) { + screensaver_state_ = ScreensaverState::NORMAL; + LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ + << "%)"; + } else { + LOG(ERROR) << "Unable to set brightness to normal"; + } + break; + case ScreensaverState::DIMMED: + if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_), + brightness_file_)) { + LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_ + << "%)"; + screensaver_state_ = ScreensaverState::DIMMED; + } else { + LOG(ERROR) << "Unable to set brightness to dim"; + } + break; + case ScreensaverState::OFF: + if (android::base::WriteStringToFile("0", brightness_file_)) { + LOG(INFO) << "Brightness: 0 (off)"; + screensaver_state_ = ScreensaverState::OFF; + } else { + LOG(ERROR) << "Unable to set brightness to off"; + } + break; + default: + LOG(ERROR) << "Invalid screensaver state"; + } +} + +int RecoveryUI::WaitKey() { + std::unique_lock lk(key_queue_mutex); + + // Check for a saved key queue interruption. + if (key_interrupted_) { + SetScreensaverState(ScreensaverState::NORMAL); + return static_cast(KeyError::INTERRUPTED); + } + + // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in. + do { + bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] { + return this->key_queue_len != 0 || key_interrupted_; + }); + if (key_interrupted_) { + SetScreensaverState(ScreensaverState::NORMAL); + return static_cast(KeyError::INTERRUPTED); + } + if (screensaver_state_ != ScreensaverState::DISABLED) { + if (!rc) { + // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. + if (screensaver_state_ == ScreensaverState::NORMAL) { + SetScreensaverState(ScreensaverState::DIMMED); + } else if (screensaver_state_ == ScreensaverState::DIMMED) { + SetScreensaverState(ScreensaverState::OFF); + } + } else if (screensaver_state_ != ScreensaverState::NORMAL) { + // Drop the first key if it's changing from OFF to NORMAL. + if (screensaver_state_ == ScreensaverState::OFF) { + if (key_queue_len > 0) { + memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); + } + } + + // Reset the brightness to normal. + SetScreensaverState(ScreensaverState::NORMAL); + } + } + } while (IsUsbConnected() && key_queue_len == 0); + + int key = static_cast(KeyError::TIMED_OUT); + if (key_queue_len > 0) { + key = key_queue[0]; + memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); + } + return key; +} + +void RecoveryUI::InterruptKey() { + { + std::lock_guard lg(key_queue_mutex); + key_interrupted_ = true; + } + key_queue_cond.notify_one(); +} + +bool RecoveryUI::IsUsbConnected() { + int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); + if (fd < 0) { + printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno)); + return 0; + } + + char buf; + // USB is connected if android_usb state is CONNECTED or CONFIGURED. + int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); + if (close(fd) < 0) { + printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno)); + } + return connected; +} + +bool RecoveryUI::IsKeyPressed(int key) { + std::lock_guard lg(key_queue_mutex); + int pressed = key_pressed[key]; + return pressed; +} + +bool RecoveryUI::IsLongPress() { + std::lock_guard lg(key_queue_mutex); + bool result = key_long_press; + return result; +} + +bool RecoveryUI::HasThreeButtons() { + return has_power_key && has_up_key && has_down_key; +} + +bool RecoveryUI::HasPowerKey() const { + return has_power_key; +} + +bool RecoveryUI::HasTouchScreen() const { + return has_touch_screen; +} + +void RecoveryUI::FlushKeys() { + std::lock_guard lg(key_queue_mutex); + key_queue_len = 0; +} + +RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { + { + std::lock_guard lg(key_queue_mutex); + key_long_press = false; + } + + // If we have power and volume up keys, that chord is the signal to toggle the text display. + if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) { + if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) { + return TOGGLE; + } + } else { + // Otherwise long press of any button toggles to the text display, + // and there's no way to toggle back (but that's pretty useless anyway). + if (is_long_press && !IsTextVisible()) { + return TOGGLE; + } + + // Also, for button-limited devices, a long press is translated to KEY_ENTER. + if (is_long_press && IsTextVisible()) { + EnqueueKey(KEY_ENTER); + return IGNORE; + } + } + + // Press power seven times in a row to reboot. + if (key == KEY_POWER) { + bool reboot_enabled = enable_reboot; + + if (reboot_enabled) { + ++consecutive_power_keys; + if (consecutive_power_keys >= 7) { + return REBOOT; + } + } + } else { + consecutive_power_keys = 0; + } + + last_key = key; + return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; +} + +void RecoveryUI::KeyLongPress(int) {} + +void RecoveryUI::SetEnableReboot(bool enabled) { + std::lock_guard lg(key_queue_mutex); + enable_reboot = enabled; +} diff --git a/recovery_ui/vr_device.cpp b/recovery_ui/vr_device.cpp new file mode 100644 index 000000000..fd7613307 --- /dev/null +++ b/recovery_ui/vr_device.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/device.h" +#include "recovery_ui/vr_ui.h" + +Device* make_device() { + return new Device(new VrRecoveryUI); +} diff --git a/recovery_ui/vr_ui.cpp b/recovery_ui/vr_ui.cpp new file mode 100644 index 000000000..5b9b1b4e5 --- /dev/null +++ b/recovery_ui/vr_ui.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/vr_ui.h" + +#include + +#include "minui/minui.h" + +constexpr int kDefaultStereoOffset = 0; + +VrRecoveryUI::VrRecoveryUI() + : stereo_offset_( + android::base::GetIntProperty("ro.recovery.ui.stereo_offset", kDefaultStereoOffset)) {} + +int VrRecoveryUI::ScreenWidth() const { + return gr_fb_width() / 2; +} + +int VrRecoveryUI::ScreenHeight() const { + return gr_fb_height(); +} + +void VrRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, + int dy) const { + gr_blit(surface, sx, sy, w, h, dx + stereo_offset_, dy); + gr_blit(surface, sx, sy, w, h, dx - stereo_offset_ + ScreenWidth(), dy); +} + +void VrRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const { + gr_texticon(x + stereo_offset_, y, surface); + gr_texticon(x - stereo_offset_ + ScreenWidth(), y, surface); +} + +int VrRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const { + gr_text(gr_sys_font(), x + stereo_offset_, y, line.c_str(), bold); + gr_text(gr_sys_font(), x - stereo_offset_ + ScreenWidth(), y, line.c_str(), bold); + return char_height_ + 4; +} + +int VrRecoveryUI::DrawHorizontalRule(int y) const { + y += 4; + gr_fill(margin_width_ + stereo_offset_, y, ScreenWidth() - margin_width_ + stereo_offset_, y + 2); + gr_fill(ScreenWidth() + margin_width_ - stereo_offset_, y, + gr_fb_width() - margin_width_ - stereo_offset_, y + 2); + return y + 4; +} + +void VrRecoveryUI::DrawHighlightBar(int /* x */, int y, int /* width */, int height) const { + gr_fill(margin_width_ + stereo_offset_, y, ScreenWidth() - margin_width_ + stereo_offset_, + y + height); + gr_fill(ScreenWidth() + margin_width_ - stereo_offset_, y, + gr_fb_width() - margin_width_ - stereo_offset_, y + height); +} + +void VrRecoveryUI::DrawFill(int x, int y, int w, int h) const { + gr_fill(x + stereo_offset_, y, w, h); + gr_fill(x - stereo_offset_ + ScreenWidth(), y, w, h); +} diff --git a/recovery_ui/wear_device.cpp b/recovery_ui/wear_device.cpp new file mode 100644 index 000000000..bf21bc962 --- /dev/null +++ b/recovery_ui/wear_device.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/device.h" +#include "recovery_ui/wear_ui.h" + +Device* make_device() { + return new Device(new WearRecoveryUI); +} diff --git a/recovery_ui/wear_ui.cpp b/recovery_ui/wear_ui.cpp new file mode 100644 index 000000000..8d8108f14 --- /dev/null +++ b/recovery_ui/wear_ui.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "recovery_ui/wear_ui.h" + +#include + +#include +#include + +#include +#include +#include + +constexpr int kDefaultProgressBarBaseline = 259; +constexpr int kDefaultMenuUnusableRows = 9; + +WearRecoveryUI::WearRecoveryUI() + : ScreenRecoveryUI(true), + progress_bar_baseline_(android::base::GetIntProperty("ro.recovery.ui.progress_bar_baseline", + kDefaultProgressBarBaseline)), + menu_unusable_rows_(android::base::GetIntProperty("ro.recovery.ui.menu_unusable_rows", + kDefaultMenuUnusableRows)) { + // TODO: menu_unusable_rows_ should be computed based on the lines in draw_screen_locked(). + + touch_screen_allowed_ = true; +} + +int WearRecoveryUI::GetProgressBaseline() const { + return progress_bar_baseline_; +} + +// Draw background frame on the screen. Does not flip pages. +// Should only be called with updateMutex locked. +// TODO merge drawing routines with screen_ui +void WearRecoveryUI::draw_background_locked() { + pagesIdentical = false; + gr_color(0, 0, 0, 255); + gr_fill(0, 0, gr_fb_width(), gr_fb_height()); + + if (current_icon_ != NONE) { + const auto& frame = GetCurrentFrame(); + int frame_width = gr_get_width(frame); + int frame_height = gr_get_height(frame); + int frame_x = (gr_fb_width() - frame_width) / 2; + int frame_y = (gr_fb_height() - frame_height) / 2; + gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); + + // Draw recovery text on screen above progress bar. + const auto& text = GetCurrentText(); + int text_x = (ScreenWidth() - gr_get_width(text)) / 2; + int text_y = GetProgressBaseline() - gr_get_height(text) - 10; + gr_color(255, 255, 255, 255); + gr_texticon(text_x, text_y, text); + } +} + +void WearRecoveryUI::draw_screen_locked() { + draw_background_locked(); + if (!show_text) { + draw_foreground_locked(); + } else { + SetColor(UIElement::TEXT_FILL); + gr_fill(0, 0, gr_fb_width(), gr_fb_height()); + + // clang-format off + static std::vector SWIPE_HELP = { + "Swipe up/down to move.", + "Swipe left/right to select.", + "", + }; + // clang-format on + draw_menu_and_text_buffer_locked(SWIPE_HELP); + } +} + +// TODO merge drawing routines with screen_ui +void WearRecoveryUI::update_progress_locked() { + draw_screen_locked(); + gr_flip(); +} + +void WearRecoveryUI::SetStage(int /* current */, int /* max */) {} + +std::unique_ptr WearRecoveryUI::CreateMenu(const std::vector& text_headers, + const std::vector& text_items, + size_t initial_selection) const { + if (text_rows_ > 0 && text_cols_ > 0) { + return std::make_unique(scrollable_menu_, text_rows_ - menu_unusable_rows_ - 1, + text_cols_ - 1, text_headers, text_items, initial_selection, + char_height_, *this); + } + + return nullptr; +} diff --git a/screen_ui.cpp b/screen_ui.cpp deleted file mode 100644 index 6f2b68b41..000000000 --- a/screen_ui.cpp +++ /dev/null @@ -1,1334 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "screen_ui.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "device.h" -#include "minui/minui.h" -#include "otautil/paths.h" -#include "ui.h" - -// Return the current time as a double (including fractions of a second). -static double now() { - struct timeval tv; - gettimeofday(&tv, nullptr); - return tv.tv_sec + tv.tv_usec / 1000000.0; -} - -Menu::Menu(size_t initial_selection, const DrawInterface& draw_func) - : selection_(initial_selection), draw_funcs_(draw_func) {} - -size_t Menu::selection() const { - return selection_; -} - -TextMenu::TextMenu(bool scrollable, size_t max_items, size_t max_length, - const std::vector& headers, const std::vector& items, - size_t initial_selection, int char_height, const DrawInterface& draw_funcs) - : Menu(initial_selection, draw_funcs), - scrollable_(scrollable), - max_display_items_(max_items), - max_item_length_(max_length), - text_headers_(headers), - menu_start_(0), - char_height_(char_height) { - CHECK_LE(max_items, static_cast(std::numeric_limits::max())); - - // It's fine to have more entries than text_rows_ if scrollable menu is supported. - size_t items_count = scrollable_ ? items.size() : std::min(items.size(), max_display_items_); - for (size_t i = 0; i < items_count; ++i) { - text_items_.emplace_back(items[i].substr(0, max_item_length_)); - } - - CHECK(!text_items_.empty()); -} - -const std::vector& TextMenu::text_headers() const { - return text_headers_; -} - -std::string TextMenu::TextItem(size_t index) const { - CHECK_LT(index, text_items_.size()); - - return text_items_[index]; -} - -size_t TextMenu::MenuStart() const { - return menu_start_; -} - -size_t TextMenu::MenuEnd() const { - return std::min(ItemsCount(), menu_start_ + max_display_items_); -} - -size_t TextMenu::ItemsCount() const { - return text_items_.size(); -} - -bool TextMenu::ItemsOverflow(std::string* cur_selection_str) const { - if (!scrollable_ || ItemsCount() <= max_display_items_) { - return false; - } - - *cur_selection_str = - android::base::StringPrintf("Current item: %zu/%zu", selection_ + 1, ItemsCount()); - return true; -} - -// TODO(xunchang) modify the function parameters to button up & down. -int TextMenu::Select(int sel) { - CHECK_LE(ItemsCount(), static_cast(std::numeric_limits::max())); - int count = ItemsCount(); - - // Wraps the selection at boundary if the menu is not scrollable. - if (!scrollable_) { - if (sel < 0) { - selection_ = count - 1; - } else if (sel >= count) { - selection_ = 0; - } else { - selection_ = sel; - } - - return selection_; - } - - if (sel < 0) { - selection_ = 0; - } else if (sel >= count) { - selection_ = count - 1; - } else { - if (static_cast(sel) < menu_start_) { - menu_start_--; - } else if (static_cast(sel) >= MenuEnd()) { - menu_start_++; - } - selection_ = sel; - } - - return selection_; -} - -int TextMenu::DrawHeader(int x, int y) const { - int offset = 0; - - draw_funcs_.SetColor(UIElement::HEADER); - if (!scrollable()) { - offset += draw_funcs_.DrawWrappedTextLines(x, y + offset, text_headers()); - } else { - offset += draw_funcs_.DrawTextLines(x, y + offset, text_headers()); - // Show the current menu item number in relation to total number if items don't fit on the - // screen. - std::string cur_selection_str; - if (ItemsOverflow(&cur_selection_str)) { - offset += draw_funcs_.DrawTextLine(x, y + offset, cur_selection_str, true); - } - } - - return offset; -} - -int TextMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { - int offset = 0; - - draw_funcs_.SetColor(UIElement::MENU); - // Do not draw the horizontal rule for wear devices. - if (!scrollable()) { - offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; - } - for (size_t i = MenuStart(); i < MenuEnd(); ++i) { - bool bold = false; - if (i == selection()) { - // Draw the highlight bar. - draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); - - int bar_height = char_height_ + 4; - draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); - - // Bold white text for the selected item. - draw_funcs_.SetColor(UIElement::MENU_SEL_FG); - bold = true; - } - offset += draw_funcs_.DrawTextLine(x, y + offset, TextItem(i), bold); - - draw_funcs_.SetColor(UIElement::MENU); - } - offset += draw_funcs_.DrawHorizontalRule(y + offset); - - return offset; -} - -GraphicMenu::GraphicMenu(const GRSurface* graphic_headers, - const std::vector& graphic_items, - size_t initial_selection, const DrawInterface& draw_funcs) - : Menu(initial_selection, draw_funcs) { - graphic_headers_ = graphic_headers->Clone(); - graphic_items_.reserve(graphic_items.size()); - for (const auto& item : graphic_items) { - graphic_items_.emplace_back(item->Clone()); - } -} - -int GraphicMenu::Select(int sel) { - CHECK_LE(graphic_items_.size(), static_cast(std::numeric_limits::max())); - int count = graphic_items_.size(); - - // Wraps the selection at boundary if the menu is not scrollable. - if (sel < 0) { - selection_ = count - 1; - } else if (sel >= count) { - selection_ = 0; - } else { - selection_ = sel; - } - - return selection_; -} - -int GraphicMenu::DrawHeader(int x, int y) const { - draw_funcs_.SetColor(UIElement::HEADER); - draw_funcs_.DrawTextIcon(x, y, graphic_headers_.get()); - return graphic_headers_->height; -} - -int GraphicMenu::DrawItems(int x, int y, int screen_width, bool long_press) const { - int offset = 0; - - draw_funcs_.SetColor(UIElement::MENU); - offset += draw_funcs_.DrawHorizontalRule(y + offset) + 4; - - for (size_t i = 0; i < graphic_items_.size(); i++) { - auto& item = graphic_items_[i]; - if (i == selection_) { - draw_funcs_.SetColor(long_press ? UIElement::MENU_SEL_BG_ACTIVE : UIElement::MENU_SEL_BG); - - int bar_height = item->height + 4; - draw_funcs_.DrawHighlightBar(0, y + offset - 2, screen_width, bar_height); - - // Bold white text for the selected item. - draw_funcs_.SetColor(UIElement::MENU_SEL_FG); - } - draw_funcs_.DrawTextIcon(x, y + offset, item.get()); - offset += item->height; - - draw_funcs_.SetColor(UIElement::MENU); - } - offset += draw_funcs_.DrawHorizontalRule(y + offset); - - return offset; -} - -bool GraphicMenu::Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, - const std::vector& graphic_items) { - int offset = 0; - if (!ValidateGraphicSurface(max_width, max_height, offset, graphic_headers)) { - return false; - } - offset += graphic_headers->height; - - for (const auto& item : graphic_items) { - if (!ValidateGraphicSurface(max_width, max_height, offset, item)) { - return false; - } - offset += item->height; - } - - return true; -} - -bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, int y, - const GRSurface* surface) { - if (!surface) { - fprintf(stderr, "Graphic surface can not be null\n"); - return false; - } - - if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) { - fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu\n", - surface->pixel_bytes, surface->width, surface->row_bytes); - return false; - } - - if (surface->width > max_width || surface->height > max_height - y) { - fprintf(stderr, - "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu," - " max_height: %zu, vertical offset: %d\n", - surface->width, surface->height, max_width, max_height, y); - return false; - } - - return true; -} - -ScreenRecoveryUI::ScreenRecoveryUI() : ScreenRecoveryUI(false) {} - -constexpr int kDefaultMarginHeight = 0; -constexpr int kDefaultMarginWidth = 0; -constexpr int kDefaultAnimationFps = 30; - -ScreenRecoveryUI::ScreenRecoveryUI(bool scrollable_menu) - : margin_width_( - android::base::GetIntProperty("ro.recovery.ui.margin_width", kDefaultMarginWidth)), - margin_height_( - android::base::GetIntProperty("ro.recovery.ui.margin_height", kDefaultMarginHeight)), - animation_fps_( - android::base::GetIntProperty("ro.recovery.ui.animation_fps", kDefaultAnimationFps)), - density_(static_cast(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f), - current_icon_(NONE), - current_frame_(0), - intro_done_(false), - progressBarType(EMPTY), - progressScopeStart(0), - progressScopeSize(0), - progress(0), - pagesIdentical(false), - text_cols_(0), - text_rows_(0), - text_(nullptr), - text_col_(0), - text_row_(0), - show_text(false), - show_text_ever(false), - scrollable_menu_(scrollable_menu), - file_viewer_text_(nullptr), - stage(-1), - max_stage(-1), - locale_(""), - rtl_locale_(false) {} - -ScreenRecoveryUI::~ScreenRecoveryUI() { - progress_thread_stopped_ = true; - if (progress_thread_.joinable()) { - progress_thread_.join(); - } - // No-op if gr_init() (via Init()) was not called or had failed. - gr_exit(); -} - -const GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { - if (current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) { - return intro_done_ ? loop_frames_[current_frame_].get() : intro_frames_[current_frame_].get(); - } - return error_icon_.get(); -} - -const GRSurface* ScreenRecoveryUI::GetCurrentText() const { - switch (current_icon_) { - case ERASING: - return erasing_text_.get(); - case ERROR: - return error_text_.get(); - case INSTALLING_UPDATE: - return installing_text_.get(); - case NO_COMMAND: - return no_command_text_.get(); - case NONE: - abort(); - } -} - -int ScreenRecoveryUI::PixelsFromDp(int dp) const { - return dp * density_; -} - -// Here's the intended layout: - -// | portrait large landscape large -// ---------+------------------------------------------------- -// gap | -// icon | (200dp) -// gap | 68dp 68dp 56dp 112dp -// text | (14sp) -// gap | 32dp 32dp 26dp 52dp -// progress | (2dp) -// gap | - -// Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines -// work), so that's the more useful measurement for calling code. We use even top and bottom gaps. - -enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; -enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; -static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { - { 32, 68, }, // PORTRAIT - { 32, 68, }, // PORTRAIT_LARGE - { 26, 56, }, // LANDSCAPE - { 52, 112, }, // LANDSCAPE_LARGE -}; - -int ScreenRecoveryUI::GetAnimationBaseline() const { - return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - - gr_get_height(loop_frames_[0].get()); -} - -int ScreenRecoveryUI::GetTextBaseline() const { - return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - - gr_get_height(installing_text_.get()); -} - -int ScreenRecoveryUI::GetProgressBaseline() const { - int elements_sum = gr_get_height(loop_frames_[0].get()) + PixelsFromDp(kLayouts[layout_][ICON]) + - gr_get_height(installing_text_.get()) + PixelsFromDp(kLayouts[layout_][TEXT]) + - gr_get_height(progress_bar_fill_.get()); - int bottom_gap = (ScreenHeight() - elements_sum) / 2; - return ScreenHeight() - bottom_gap - gr_get_height(progress_bar_fill_.get()); -} - -// Clear the screen and draw the currently selected background icon (if any). -// Should only be called with updateMutex locked. -void ScreenRecoveryUI::draw_background_locked() { - pagesIdentical = false; - gr_color(0, 0, 0, 255); - gr_clear(); - if (current_icon_ != NONE) { - if (max_stage != -1) { - int stage_height = gr_get_height(stage_marker_empty_.get()); - int stage_width = gr_get_width(stage_marker_empty_.get()); - int x = (ScreenWidth() - max_stage * gr_get_width(stage_marker_empty_.get())) / 2; - int y = ScreenHeight() - stage_height - margin_height_; - for (int i = 0; i < max_stage; ++i) { - const auto& stage_surface = (i < stage) ? stage_marker_fill_ : stage_marker_empty_; - DrawSurface(stage_surface.get(), 0, 0, stage_width, stage_height, x, y); - x += stage_width; - } - } - - const auto& text_surface = GetCurrentText(); - int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2; - int text_y = GetTextBaseline(); - gr_color(255, 255, 255, 255); - DrawTextIcon(text_x, text_y, text_surface); - } -} - -// Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be -// called with updateMutex locked. -void ScreenRecoveryUI::draw_foreground_locked() { - if (current_icon_ != NONE) { - const auto& frame = GetCurrentFrame(); - int frame_width = gr_get_width(frame); - int frame_height = gr_get_height(frame); - int frame_x = (ScreenWidth() - frame_width) / 2; - int frame_y = GetAnimationBaseline(); - DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); - } - - if (progressBarType != EMPTY) { - int width = gr_get_width(progress_bar_empty_.get()); - int height = gr_get_height(progress_bar_empty_.get()); - - int progress_x = (ScreenWidth() - width) / 2; - int progress_y = GetProgressBaseline(); - - // Erase behind the progress bar (in case this was a progress-only update) - gr_color(0, 0, 0, 255); - DrawFill(progress_x, progress_y, width, height); - - if (progressBarType == DETERMINATE) { - float p = progressScopeStart + progress * progressScopeSize; - int pos = static_cast(p * width); - - if (rtl_locale_) { - // Fill the progress bar from right to left. - if (pos > 0) { - DrawSurface(progress_bar_fill_.get(), width - pos, 0, pos, height, - progress_x + width - pos, progress_y); - } - if (pos < width - 1) { - DrawSurface(progress_bar_empty_.get(), 0, 0, width - pos, height, progress_x, progress_y); - } - } else { - // Fill the progress bar from left to right. - if (pos > 0) { - DrawSurface(progress_bar_fill_.get(), 0, 0, pos, height, progress_x, progress_y); - } - if (pos < width - 1) { - DrawSurface(progress_bar_empty_.get(), pos, 0, width - pos, height, progress_x + pos, - progress_y); - } - } - } - } -} - -void ScreenRecoveryUI::SetColor(UIElement e) const { - switch (e) { - case UIElement::INFO: - gr_color(249, 194, 0, 255); - break; - case UIElement::HEADER: - gr_color(247, 0, 6, 255); - break; - case UIElement::MENU: - case UIElement::MENU_SEL_BG: - gr_color(0, 106, 157, 255); - break; - case UIElement::MENU_SEL_BG_ACTIVE: - gr_color(0, 156, 100, 255); - break; - case UIElement::MENU_SEL_FG: - gr_color(255, 255, 255, 255); - break; - case UIElement::LOG: - gr_color(196, 196, 196, 255); - break; - case UIElement::TEXT_FILL: - gr_color(0, 0, 0, 160); - break; - default: - gr_color(255, 255, 255, 255); - break; - } -} - -void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector& locales_entries, - size_t sel) { - SetLocale(locales_entries[sel]); - std::vector text_name = { "erasing_text", "error_text", "installing_text", - "installing_security_text", "no_command_text" }; - std::unordered_map> surfaces; - for (const auto& name : text_name) { - auto text_image = LoadLocalizedBitmap(name); - if (!text_image) { - Print("Failed to load %s\n", name.c_str()); - return; - } - surfaces.emplace(name, std::move(text_image)); - } - - std::lock_guard lg(updateMutex); - gr_color(0, 0, 0, 255); - gr_clear(); - - int text_y = margin_height_; - int text_x = margin_width_; - int line_spacing = gr_sys_font()->char_height; // Put some extra space between images. - // Write the header and descriptive texts. - SetColor(UIElement::INFO); - std::string header = "Show background text image"; - text_y += DrawTextLine(text_x, text_y, header, true); - std::string locale_selection = android::base::StringPrintf( - "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel + 1, locales_entries.size()); - // clang-format off - std::vector instruction = { - locale_selection, - "Use volume up/down to switch locales and power to exit." - }; - // clang-format on - text_y += DrawWrappedTextLines(text_x, text_y, instruction); - - // Iterate through the text images and display them in order for the current locale. - for (const auto& p : surfaces) { - text_y += line_spacing; - SetColor(UIElement::LOG); - text_y += DrawTextLine(text_x, text_y, p.first, false); - gr_color(255, 255, 255, 255); - gr_texticon(text_x, text_y, p.second.get()); - text_y += gr_get_height(p.second.get()); - } - // Update the whole screen. - gr_flip(); -} - -void ScreenRecoveryUI::CheckBackgroundTextImages() { - // Load a list of locales embedded in one of the resource files. - std::vector locales_entries = get_locales_in_png("installing_text"); - if (locales_entries.empty()) { - Print("Failed to load locales from the resource files\n"); - return; - } - std::string saved_locale = locale_; - size_t selected = 0; - SelectAndShowBackgroundText(locales_entries, selected); - - FlushKeys(); - while (true) { - int key = WaitKey(); - if (key == static_cast(KeyError::INTERRUPTED)) break; - if (key == KEY_POWER || key == KEY_ENTER) { - break; - } else if (key == KEY_UP || key == KEY_VOLUMEUP) { - selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1; - SelectAndShowBackgroundText(locales_entries, selected); - } else if (key == KEY_DOWN || key == KEY_VOLUMEDOWN) { - selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1; - SelectAndShowBackgroundText(locales_entries, selected); - } - } - - SetLocale(saved_locale); -} - -int ScreenRecoveryUI::ScreenWidth() const { - return gr_fb_width(); -} - -int ScreenRecoveryUI::ScreenHeight() const { - return gr_fb_height(); -} - -void ScreenRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, - int dy) const { - gr_blit(surface, sx, sy, w, h, dx, dy); -} - -int ScreenRecoveryUI::DrawHorizontalRule(int y) const { - gr_fill(0, y + 4, ScreenWidth(), y + 6); - return 8; -} - -void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { - gr_fill(x, y, x + width, y + height); -} - -void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const { - gr_fill(x, y, w, h); -} - -void ScreenRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const { - gr_texticon(x, y, surface); -} - -int ScreenRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const { - gr_text(gr_sys_font(), x, y, line.c_str(), bold); - return char_height_ + 4; -} - -int ScreenRecoveryUI::DrawTextLines(int x, int y, const std::vector& lines) const { - int offset = 0; - for (const auto& line : lines) { - offset += DrawTextLine(x, y + offset, line, false); - } - return offset; -} - -int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, - const std::vector& lines) const { - // Keep symmetrical margins based on the given offset (i.e. x). - size_t text_cols = (ScreenWidth() - x * 2) / char_width_; - int offset = 0; - for (const auto& line : lines) { - size_t next_start = 0; - while (next_start < line.size()) { - std::string sub = line.substr(next_start, text_cols + 1); - if (sub.size() <= text_cols) { - next_start += sub.size(); - } else { - // Line too long and must be wrapped to text_cols columns. - size_t last_space = sub.find_last_of(" \t\n"); - if (last_space == std::string::npos) { - // No space found, just draw as much as we can. - sub.resize(text_cols); - next_start += text_cols; - } else { - sub.resize(last_space); - next_start += last_space + 1; - } - } - offset += DrawTextLine(x, y + offset, sub, false); - } - } - return offset; -} - -void ScreenRecoveryUI::SetTitle(const std::vector& lines) { - title_lines_ = lines; -} - -// Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex -// locked. -void ScreenRecoveryUI::draw_screen_locked() { - if (!show_text) { - draw_background_locked(); - draw_foreground_locked(); - return; - } - - gr_color(0, 0, 0, 255); - gr_clear(); - - // clang-format off - static std::vector REGULAR_HELP{ - "Use volume up/down and power.", - }; - static std::vector LONG_PRESS_HELP{ - "Any button cycles highlight.", - "Long-press activates.", - }; - // clang-format on - draw_menu_and_text_buffer_locked(HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); -} - -// Draws the menu and text buffer on the screen. Should only be called with updateMutex locked. -void ScreenRecoveryUI::draw_menu_and_text_buffer_locked( - const std::vector& help_message) { - int y = margin_height_; - - if (fastbootd_logo_ && fastbootd_logo_enabled_) { - // Try to get this centered on screen. - auto width = gr_get_width(fastbootd_logo_.get()); - auto height = gr_get_height(fastbootd_logo_.get()); - auto centered_x = ScreenWidth() / 2 - width / 2; - DrawSurface(fastbootd_logo_.get(), 0, 0, width, height, centered_x, y); - y += height; - } - - if (menu_) { - int x = margin_width_ + kMenuIndent; - - SetColor(UIElement::INFO); - - for (size_t i = 0; i < title_lines_.size(); i++) { - y += DrawTextLine(x, y, title_lines_[i], i == 0); - } - - y += DrawTextLines(x, y, help_message); - - y += menu_->DrawHeader(x, y); - y += menu_->DrawItems(x, y, ScreenWidth(), IsLongPress()); - } - - // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or - // we've displayed the entire text buffer. - SetColor(UIElement::LOG); - int row = text_row_; - size_t count = 0; - for (int ty = ScreenHeight() - margin_height_ - char_height_; ty >= y && count < text_rows_; - ty -= char_height_, ++count) { - DrawTextLine(margin_width_, ty, text_[row], false); - --row; - if (row < 0) row = text_rows_ - 1; - } -} - -// Redraw everything on the screen and flip the screen (make it visible). -// Should only be called with updateMutex locked. -void ScreenRecoveryUI::update_screen_locked() { - draw_screen_locked(); - gr_flip(); -} - -// Updates only the progress bar, if possible, otherwise redraws the screen. -// Should only be called with updateMutex locked. -void ScreenRecoveryUI::update_progress_locked() { - if (show_text || !pagesIdentical) { - draw_screen_locked(); // Must redraw the whole screen - pagesIdentical = true; - } else { - draw_foreground_locked(); // Draw only the progress bar and overlays - } - gr_flip(); -} - -void ScreenRecoveryUI::ProgressThreadLoop() { - double interval = 1.0 / animation_fps_; - while (!progress_thread_stopped_) { - double start = now(); - bool redraw = false; - { - std::lock_guard lg(updateMutex); - - // update the installation animation, if active - // skip this if we have a text overlay (too expensive to update) - if ((current_icon_ == INSTALLING_UPDATE || current_icon_ == ERASING) && !show_text) { - if (!intro_done_) { - if (current_frame_ == intro_frames_.size() - 1) { - intro_done_ = true; - current_frame_ = 0; - } else { - ++current_frame_; - } - } else { - current_frame_ = (current_frame_ + 1) % loop_frames_.size(); - } - - redraw = true; - } - - // move the progress bar forward on timed intervals, if configured - int duration = progressScopeDuration; - if (progressBarType == DETERMINATE && duration > 0) { - double elapsed = now() - progressScopeTime; - float p = 1.0 * elapsed / duration; - if (p > 1.0) p = 1.0; - if (p > progress) { - progress = p; - redraw = true; - } - } - - if (redraw) update_progress_locked(); - } - - double end = now(); - // minimum of 20ms delay between frames - double delay = interval - (end - start); - if (delay < 0.02) delay = 0.02; - usleep(static_cast(delay * 1000000)); - } -} - -std::unique_ptr ScreenRecoveryUI::LoadBitmap(const std::string& filename) { - GRSurface* surface; - if (auto result = res_create_display_surface(filename.c_str(), &surface); result < 0) { - LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")"; - return nullptr; - } - return std::unique_ptr(surface); -} - -std::unique_ptr ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) { - GRSurface* surface; - if (auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface); - result < 0) { - LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")"; - return nullptr; - } - return std::unique_ptr(surface); -} - -static char** Alloc2d(size_t rows, size_t cols) { - char** result = new char*[rows]; - for (size_t i = 0; i < rows; ++i) { - result[i] = new char[cols]; - memset(result[i], 0, cols); - } - return result; -} - -// Choose the right background string to display during update. -void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { - if (security_update) { - installing_text_ = LoadLocalizedBitmap("installing_security_text"); - } else { - installing_text_ = LoadLocalizedBitmap("installing_text"); - } - Redraw(); -} - -bool ScreenRecoveryUI::InitTextParams() { - // gr_init() would return successfully on font initialization failure. - if (gr_sys_font() == nullptr) { - return false; - } - gr_font_size(gr_sys_font(), &char_width_, &char_height_); - text_rows_ = (ScreenHeight() - margin_height_ * 2) / char_height_; - text_cols_ = (ScreenWidth() - margin_width_ * 2) / char_width_; - return true; -} - -bool ScreenRecoveryUI::LoadWipeDataMenuText() { - // Ignores the errors since the member variables will stay as nullptr. - cancel_wipe_data_text_ = LoadLocalizedBitmap("cancel_wipe_data_text"); - factory_data_reset_text_ = LoadLocalizedBitmap("factory_data_reset_text"); - try_again_text_ = LoadLocalizedBitmap("try_again_text"); - wipe_data_confirmation_text_ = LoadLocalizedBitmap("wipe_data_confirmation_text"); - wipe_data_menu_header_text_ = LoadLocalizedBitmap("wipe_data_menu_header_text"); - return true; -} - -bool ScreenRecoveryUI::Init(const std::string& locale) { - RecoveryUI::Init(locale); - - if (gr_init() == -1) { - return false; - } - - if (!InitTextParams()) { - return false; - } - - // Are we portrait or landscape? - layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; - // Are we the large variant of our base layout? - if (gr_fb_height() > PixelsFromDp(800)) ++layout_; - - text_ = Alloc2d(text_rows_, text_cols_ + 1); - file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); - - text_col_ = text_row_ = 0; - - // Set up the locale info. - SetLocale(locale); - - error_icon_ = LoadBitmap("icon_error"); - - progress_bar_empty_ = LoadBitmap("progress_empty"); - progress_bar_fill_ = LoadBitmap("progress_fill"); - stage_marker_empty_ = LoadBitmap("stage_empty"); - stage_marker_fill_ = LoadBitmap("stage_fill"); - - erasing_text_ = LoadLocalizedBitmap("erasing_text"); - no_command_text_ = LoadLocalizedBitmap("no_command_text"); - error_text_ = LoadLocalizedBitmap("error_text"); - - if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { - fastbootd_logo_ = LoadBitmap("fastbootd"); - } - - // Background text for "installing_update" could be "installing update" or - // "installing security update". It will be set after Init() according to the commands in BCB. - installing_text_.reset(); - - LoadWipeDataMenuText(); - - LoadAnimation(); - - // Keep the progress bar updated, even when the process is otherwise busy. - progress_thread_ = std::thread(&ScreenRecoveryUI::ProgressThreadLoop, this); - - return true; -} - -std::string ScreenRecoveryUI::GetLocale() const { - return locale_; -} - -void ScreenRecoveryUI::LoadAnimation() { - std::unique_ptr dir(opendir(Paths::Get().resource_dir().c_str()), - closedir); - dirent* de; - std::vector intro_frame_names; - std::vector loop_frame_names; - - while ((de = readdir(dir.get())) != nullptr) { - int value, num_chars; - if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { - intro_frame_names.emplace_back(de->d_name, num_chars); - } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { - loop_frame_names.emplace_back(de->d_name, num_chars); - } - } - - size_t intro_frames = intro_frame_names.size(); - size_t loop_frames = loop_frame_names.size(); - - // It's okay to not have an intro. - if (intro_frames == 0) intro_done_ = true; - // But you must have an animation. - if (loop_frames == 0) abort(); - - std::sort(intro_frame_names.begin(), intro_frame_names.end()); - std::sort(loop_frame_names.begin(), loop_frame_names.end()); - - intro_frames_.clear(); - intro_frames_.reserve(intro_frames); - for (const auto& frame_name : intro_frame_names) { - intro_frames_.emplace_back(LoadBitmap(frame_name)); - } - - loop_frames_.clear(); - loop_frames_.reserve(loop_frames); - for (const auto& frame_name : loop_frame_names) { - loop_frames_.emplace_back(LoadBitmap(frame_name)); - } -} - -void ScreenRecoveryUI::SetBackground(Icon icon) { - std::lock_guard lg(updateMutex); - - current_icon_ = icon; - update_screen_locked(); -} - -void ScreenRecoveryUI::SetProgressType(ProgressType type) { - std::lock_guard lg(updateMutex); - if (progressBarType != type) { - progressBarType = type; - } - progressScopeStart = 0; - progressScopeSize = 0; - progress = 0; - update_progress_locked(); -} - -void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { - std::lock_guard lg(updateMutex); - progressBarType = DETERMINATE; - progressScopeStart += progressScopeSize; - progressScopeSize = portion; - progressScopeTime = now(); - progressScopeDuration = seconds; - progress = 0; - update_progress_locked(); -} - -void ScreenRecoveryUI::SetProgress(float fraction) { - std::lock_guard lg(updateMutex); - if (fraction < 0.0) fraction = 0.0; - if (fraction > 1.0) fraction = 1.0; - if (progressBarType == DETERMINATE && fraction > progress) { - // Skip updates that aren't visibly different. - int width = gr_get_width(progress_bar_empty_.get()); - float scale = width * progressScopeSize; - if ((int)(progress * scale) != (int)(fraction * scale)) { - progress = fraction; - update_progress_locked(); - } - } -} - -void ScreenRecoveryUI::SetStage(int current, int max) { - std::lock_guard lg(updateMutex); - stage = current; - max_stage = max; -} - -void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { - std::string str; - android::base::StringAppendV(&str, fmt, ap); - - if (copy_to_stdout) { - fputs(str.c_str(), stdout); - } - - std::lock_guard lg(updateMutex); - if (text_rows_ > 0 && text_cols_ > 0) { - for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { - if (*ptr == '\n' || text_col_ >= text_cols_) { - text_[text_row_][text_col_] = '\0'; - text_col_ = 0; - text_row_ = (text_row_ + 1) % text_rows_; - } - if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; - } - text_[text_row_][text_col_] = '\0'; - update_screen_locked(); - } -} - -void ScreenRecoveryUI::Print(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - PrintV(fmt, true, ap); - va_end(ap); -} - -void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - PrintV(fmt, false, ap); - va_end(ap); -} - -void ScreenRecoveryUI::PutChar(char ch) { - std::lock_guard lg(updateMutex); - if (ch != '\n') text_[text_row_][text_col_++] = ch; - if (ch == '\n' || text_col_ >= text_cols_) { - text_col_ = 0; - ++text_row_; - } -} - -void ScreenRecoveryUI::ClearText() { - std::lock_guard lg(updateMutex); - text_col_ = 0; - text_row_ = 0; - for (size_t i = 0; i < text_rows_; ++i) { - memset(text_[i], 0, text_cols_ + 1); - } -} - -void ScreenRecoveryUI::ShowFile(FILE* fp) { - std::vector offsets; - offsets.push_back(ftello(fp)); - ClearText(); - - struct stat sb; - fstat(fileno(fp), &sb); - - bool show_prompt = false; - while (true) { - if (show_prompt) { - PrintOnScreenOnly("--(%d%% of %d bytes)--", - static_cast(100 * (double(ftello(fp)) / double(sb.st_size))), - static_cast(sb.st_size)); - Redraw(); - while (show_prompt) { - show_prompt = false; - int key = WaitKey(); - if (key == static_cast(KeyError::INTERRUPTED)) return; - if (key == KEY_POWER || key == KEY_ENTER) { - return; - } else if (key == KEY_UP || key == KEY_VOLUMEUP) { - if (offsets.size() <= 1) { - show_prompt = true; - } else { - offsets.pop_back(); - fseek(fp, offsets.back(), SEEK_SET); - } - } else { - if (feof(fp)) { - return; - } - offsets.push_back(ftello(fp)); - } - } - ClearText(); - } - - int ch = getc(fp); - if (ch == EOF) { - while (text_row_ < text_rows_ - 1) PutChar('\n'); - show_prompt = true; - } else { - PutChar(ch); - if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { - show_prompt = true; - } - } - } -} - -void ScreenRecoveryUI::ShowFile(const std::string& filename) { - std::unique_ptr fp(fopen(filename.c_str(), "re"), fclose); - if (!fp) { - Print(" Unable to open %s: %s\n", filename.c_str(), strerror(errno)); - return; - } - - char** old_text = text_; - size_t old_text_col = text_col_; - size_t old_text_row = text_row_; - - // Swap in the alternate screen and clear it. - text_ = file_viewer_text_; - ClearText(); - - ShowFile(fp.get()); - - text_ = old_text; - text_col_ = old_text_col; - text_row_ = old_text_row; -} - -std::unique_ptr ScreenRecoveryUI::CreateMenu( - const GRSurface* graphic_header, const std::vector& graphic_items, - const std::vector& text_headers, const std::vector& text_items, - size_t initial_selection) const { - // horizontal unusable area: margin width + menu indent - size_t max_width = ScreenWidth() - margin_width_ - kMenuIndent; - // vertical unusable area: margin height + title lines + helper message + high light bar. - // It is safe to reserve more space. - size_t max_height = ScreenHeight() - margin_height_ - char_height_ * (title_lines_.size() + 3); - if (GraphicMenu::Validate(max_width, max_height, graphic_header, graphic_items)) { - return std::make_unique(graphic_header, graphic_items, initial_selection, *this); - } - - fprintf(stderr, "Failed to initialize graphic menu, falling back to use the text menu.\n"); - - return CreateMenu(text_headers, text_items, initial_selection); -} - -std::unique_ptr ScreenRecoveryUI::CreateMenu(const std::vector& text_headers, - const std::vector& text_items, - size_t initial_selection) const { - if (text_rows_ > 0 && text_cols_ > 1) { - return std::make_unique(scrollable_menu_, text_rows_, text_cols_ - 1, text_headers, - text_items, initial_selection, char_height_, *this); - } - - fprintf(stderr, "Failed to create text menu, text_rows %zu, text_cols %zu.\n", text_rows_, - text_cols_); - return nullptr; -} - -int ScreenRecoveryUI::SelectMenu(int sel) { - std::lock_guard lg(updateMutex); - if (menu_) { - int old_sel = menu_->selection(); - sel = menu_->Select(sel); - - if (sel != old_sel) { - update_screen_locked(); - } - } - return sel; -} - -size_t ScreenRecoveryUI::ShowMenu(std::unique_ptr&& menu, bool menu_only, - const std::function& key_handler) { - // Throw away keys pressed previously, so user doesn't accidentally trigger menu items. - FlushKeys(); - - // If there is a key interrupt in progress, return KeyError::INTERRUPTED without starting the - // menu. - if (IsKeyInterrupted()) return static_cast(KeyError::INTERRUPTED); - - CHECK(menu != nullptr); - - // Starts and displays the menu - menu_ = std::move(menu); - Redraw(); - - int selected = menu_->selection(); - int chosen_item = -1; - while (chosen_item < 0) { - int key = WaitKey(); - if (key == static_cast(KeyError::INTERRUPTED)) { // WaitKey() was interrupted. - return static_cast(KeyError::INTERRUPTED); - } - if (key == static_cast(KeyError::TIMED_OUT)) { // WaitKey() timed out. - if (WasTextEverVisible()) { - continue; - } else { - LOG(INFO) << "Timed out waiting for key input; rebooting."; - menu_.reset(); - Redraw(); - return static_cast(KeyError::TIMED_OUT); - } - } - - bool visible = IsTextVisible(); - int action = key_handler(key, visible); - if (action < 0) { - switch (action) { - case Device::kHighlightUp: - selected = SelectMenu(--selected); - break; - case Device::kHighlightDown: - selected = SelectMenu(++selected); - break; - case Device::kInvokeItem: - chosen_item = selected; - break; - case Device::kNoAction: - break; - } - } else if (!menu_only) { - chosen_item = action; - } - } - - menu_.reset(); - Redraw(); - - return chosen_item; -} - -size_t ScreenRecoveryUI::ShowMenu(const std::vector& headers, - const std::vector& items, size_t initial_selection, - bool menu_only, - const std::function& key_handler) { - auto menu = CreateMenu(headers, items, initial_selection); - if (menu == nullptr) { - return initial_selection; - } - - return ShowMenu(CreateMenu(headers, items, initial_selection), menu_only, key_handler); -} - -size_t ScreenRecoveryUI::ShowPromptWipeDataMenu(const std::vector& backup_headers, - const std::vector& backup_items, - const std::function& key_handler) { - auto wipe_data_menu = CreateMenu(wipe_data_menu_header_text_.get(), - { try_again_text_.get(), factory_data_reset_text_.get() }, - backup_headers, backup_items, 0); - if (wipe_data_menu == nullptr) { - return 0; - } - - return ShowMenu(std::move(wipe_data_menu), true, key_handler); -} - -size_t ScreenRecoveryUI::ShowPromptWipeDataConfirmationMenu( - const std::vector& backup_headers, const std::vector& backup_items, - const std::function& key_handler) { - auto confirmation_menu = - CreateMenu(wipe_data_confirmation_text_.get(), - { cancel_wipe_data_text_.get(), factory_data_reset_text_.get() }, backup_headers, - backup_items, 0); - if (confirmation_menu == nullptr) { - return 0; - } - - return ShowMenu(std::move(confirmation_menu), true, key_handler); -} - -bool ScreenRecoveryUI::IsTextVisible() { - std::lock_guard lg(updateMutex); - int visible = show_text; - return visible; -} - -bool ScreenRecoveryUI::WasTextEverVisible() { - std::lock_guard lg(updateMutex); - int ever_visible = show_text_ever; - return ever_visible; -} - -void ScreenRecoveryUI::ShowText(bool visible) { - std::lock_guard lg(updateMutex); - show_text = visible; - if (show_text) show_text_ever = true; - update_screen_locked(); -} - -void ScreenRecoveryUI::Redraw() { - std::lock_guard lg(updateMutex); - update_screen_locked(); -} - -void ScreenRecoveryUI::KeyLongPress(int) { - // Redraw so that if we're in the menu, the highlight - // will change color to indicate a successful long press. - Redraw(); -} - -void ScreenRecoveryUI::SetLocale(const std::string& new_locale) { - locale_ = new_locale; - rtl_locale_ = false; - - if (!new_locale.empty()) { - size_t separator = new_locale.find('-'); - // lang has the language prefix prior to the separator, or full string if none exists. - std::string lang = new_locale.substr(0, separator); - - // A bit cheesy: keep an explicit list of supported RTL languages. - if (lang == "ar" || // Arabic - lang == "fa" || // Persian (Farsi) - lang == "he" || // Hebrew (new language code) - lang == "iw" || // Hebrew (old language code) - lang == "ur") { // Urdu - rtl_locale_ = true; - } - } -} diff --git a/screen_ui.h b/screen_ui.h deleted file mode 100644 index 5cda2a2e5..000000000 --- a/screen_ui.h +++ /dev/null @@ -1,412 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RECOVERY_SCREEN_UI_H -#define RECOVERY_SCREEN_UI_H - -#include - -#include -#include -#include -#include -#include -#include - -#include "ui.h" - -// From minui/minui.h. -class GRSurface; - -enum class UIElement { - HEADER, - MENU, - MENU_SEL_BG, - MENU_SEL_BG_ACTIVE, - MENU_SEL_FG, - LOG, - TEXT_FILL, - INFO -}; - -// Interface to draw the UI elements on the screen. -class DrawInterface { - public: - virtual ~DrawInterface() = default; - - // Sets the color to the predefined value for |element|. - virtual void SetColor(UIElement element) const = 0; - - // Draws a highlight bar at (x, y) - (x + width, y + height). - virtual void DrawHighlightBar(int x, int y, int width, int height) const = 0; - - // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis. - virtual int DrawHorizontalRule(int y) const = 0; - - // Draws a line of text. Returns the offset it should be moving along Y-axis. - virtual int DrawTextLine(int x, int y, const std::string& line, bool bold) const = 0; - - // Draws surface portion (sx, sy, w, h) at screen location (dx, dy). - virtual void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, - int dy) const = 0; - - // Draws rectangle at (x, y) - (x + w, y + h). - virtual void DrawFill(int x, int y, int w, int h) const = 0; - - // Draws given surface (surface->pixel_bytes = 1) as text at (x, y). - virtual void DrawTextIcon(int x, int y, const GRSurface* surface) const = 0; - - // Draws multiple text lines. Returns the offset it should be moving along Y-axis. - virtual int DrawTextLines(int x, int y, const std::vector& lines) const = 0; - - // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines. It - // keeps symmetrical margins of 'x' at each end of a line. Returns the offset it should be moving - // along Y-axis. - virtual int DrawWrappedTextLines(int x, int y, const std::vector& lines) const = 0; -}; - -// Interface for classes that maintain the menu selection and display. -class Menu { - public: - virtual ~Menu() = default; - // Returns the current menu selection. - size_t selection() const; - // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is - // scrollable. - virtual int Select(int sel) = 0; - // Displays the menu headers on the screen at offset x, y - virtual int DrawHeader(int x, int y) const = 0; - // Iterates over the menu items and displays each of them at offset x, y. - virtual int DrawItems(int x, int y, int screen_width, bool long_press) const = 0; - - protected: - Menu(size_t initial_selection, const DrawInterface& draw_func); - // Current menu selection. - size_t selection_; - // Reference to the class that implements all the draw functions. - const DrawInterface& draw_funcs_; -}; - -// This class uses strings as the menu header and items. -class TextMenu : public Menu { - public: - // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial - // selection to |initial_selection|. - TextMenu(bool scrollable, size_t max_items, size_t max_length, - const std::vector& headers, const std::vector& items, - size_t initial_selection, int char_height, const DrawInterface& draw_funcs); - - int Select(int sel) override; - int DrawHeader(int x, int y) const override; - int DrawItems(int x, int y, int screen_width, bool long_press) const override; - - bool scrollable() const { - return scrollable_; - } - - // Returns count of menu items. - size_t ItemsCount() const; - - // Returns the index of the first menu item. - size_t MenuStart() const; - - // Returns the index of the last menu item + 1. - size_t MenuEnd() const; - - // Menu example: - // info: Android Recovery - // .... - // help messages: Swipe up/down to move - // Swipe left/right to select - // empty line (horizontal rule): - // menu headers: Select file to view - // menu items: /cache/recovery/last_log - // /cache/recovery/last_log.1 - // /cache/recovery/last_log.2 - // ... - const std::vector& text_headers() const; - std::string TextItem(size_t index) const; - - // Checks if the menu items fit vertically on the screen. Returns true and set the - // |cur_selection_str| if the items exceed the screen limit. - bool ItemsOverflow(std::string* cur_selection_str) const; - - private: - // The menu is scrollable to display more items. Used on wear devices who have smaller screens. - const bool scrollable_; - // The max number of menu items to fit vertically on a screen. - const size_t max_display_items_; - // The length of each item to fit horizontally on a screen. - const size_t max_item_length_; - // The menu headers. - std::vector text_headers_; - // The actual menu items trimmed to fit the given properties. - std::vector text_items_; - // The first item to display on the screen. - size_t menu_start_; - - // Height in pixels of each character. - int char_height_; -}; - -// This class uses GRSurface's as the menu header and items. -class GraphicMenu : public Menu { - public: - // Constructs a Menu instance with the given |headers|, |items| and properties. Sets the initial - // selection to |initial_selection|. |headers| and |items| will be made local copies. - GraphicMenu(const GRSurface* graphic_headers, const std::vector& graphic_items, - size_t initial_selection, const DrawInterface& draw_funcs); - - int Select(int sel) override; - int DrawHeader(int x, int y) const override; - int DrawItems(int x, int y, int screen_width, bool long_press) const override; - - // Checks if all the header and items are valid GRSurface's; and that they can fit in the area - // defined by |max_width| and |max_height|. - static bool Validate(size_t max_width, size_t max_height, const GRSurface* graphic_headers, - const std::vector& graphic_items); - - // Returns true if |surface| fits on the screen with a vertical offset |y|. - static bool ValidateGraphicSurface(size_t max_width, size_t max_height, int y, - const GRSurface* surface); - - private: - // Menu headers and items in graphic icons. These are the copies owned by the class instance. - std::unique_ptr graphic_headers_; - std::vector> graphic_items_; -}; - -// Implementation of RecoveryUI appropriate for devices with a screen -// (shows an icon + a progress bar, text logging, menu, etc.) -class ScreenRecoveryUI : public RecoveryUI, public DrawInterface { - public: - ScreenRecoveryUI(); - explicit ScreenRecoveryUI(bool scrollable_menu); - ~ScreenRecoveryUI() override; - - bool Init(const std::string& locale) override; - std::string GetLocale() const override; - - // overall recovery state ("background image") - void SetBackground(Icon icon) override; - void SetSystemUpdateText(bool security_update) override; - - // progress indicator - void SetProgressType(ProgressType type) override; - void ShowProgress(float portion, float seconds) override; - void SetProgress(float fraction) override; - - void SetStage(int current, int max) override; - - // text log - void ShowText(bool visible) override; - bool IsTextVisible() override; - bool WasTextEverVisible() override; - - // printing messages - void Print(const char* fmt, ...) override __printflike(2, 3); - void PrintOnScreenOnly(const char* fmt, ...) override __printflike(2, 3); - void ShowFile(const std::string& filename) override; - - // menu display - size_t ShowMenu(const std::vector& headers, const std::vector& items, - size_t initial_selection, bool menu_only, - const std::function& key_handler) override; - void SetTitle(const std::vector& lines) override; - - void KeyLongPress(int) override; - - void Redraw(); - - // Checks the background text image, for debugging purpose. It iterates the locales embedded in - // the on-device resource files and shows the localized text, for manual inspection. - void CheckBackgroundTextImages(); - - // Displays the localized wipe data menu. - size_t ShowPromptWipeDataMenu(const std::vector& backup_headers, - const std::vector& backup_items, - const std::function& key_handler) override; - - // Displays the localized wipe data confirmation menu. - size_t ShowPromptWipeDataConfirmationMenu( - const std::vector& backup_headers, const std::vector& backup_items, - const std::function& key_handler) override; - - protected: - static constexpr int kMenuIndent = 4; - - // The margin that we don't want to use for showing texts (e.g. round screen, or screen with - // rounded corners). - const int margin_width_; - const int margin_height_; - - // Number of frames per sec (default: 30) for both parts of the animation. - const int animation_fps_; - - // The scale factor from dp to pixels. 1.0 for mdpi, 4.0 for xxxhdpi. - const float density_; - - virtual bool InitTextParams(); - - virtual bool LoadWipeDataMenuText(); - - // Creates a GraphicMenu with |graphic_header| and |graphic_items|. If the GraphicMenu isn't - // valid or it doesn't fit on the screen; falls back to create a TextMenu instead. If succeeds, - // returns a unique pointer to the created menu; otherwise returns nullptr. - virtual std::unique_ptr CreateMenu(const GRSurface* graphic_header, - const std::vector& graphic_items, - const std::vector& text_headers, - const std::vector& text_items, - size_t initial_selection) const; - - // Creates a TextMenu with |text_headers| and |text_items|; and sets the menu selection to - // |initial_selection|. - virtual std::unique_ptr CreateMenu(const std::vector& text_headers, - const std::vector& text_items, - size_t initial_selection) const; - - // Takes the ownership of |menu| and displays it. - virtual size_t ShowMenu(std::unique_ptr&& menu, bool menu_only, - const std::function& key_handler); - - // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item - // selected. - virtual int SelectMenu(int sel); - - virtual void draw_background_locked(); - virtual void draw_foreground_locked(); - virtual void draw_screen_locked(); - virtual void draw_menu_and_text_buffer_locked(const std::vector& help_message); - virtual void update_screen_locked(); - virtual void update_progress_locked(); - - const GRSurface* GetCurrentFrame() const; - const GRSurface* GetCurrentText() const; - - void ProgressThreadLoop(); - - virtual void ShowFile(FILE*); - virtual void PrintV(const char*, bool, va_list); - void PutChar(char); - void ClearText(); - - void LoadAnimation(); - std::unique_ptr LoadBitmap(const std::string& filename); - std::unique_ptr LoadLocalizedBitmap(const std::string& filename); - - int PixelsFromDp(int dp) const; - virtual int GetAnimationBaseline() const; - virtual int GetProgressBaseline() const; - virtual int GetTextBaseline() const; - - // Returns pixel width of draw buffer. - virtual int ScreenWidth() const; - // Returns pixel height of draw buffer. - virtual int ScreenHeight() const; - - // Implementation of the draw functions in DrawInterface. - void SetColor(UIElement e) const override; - void DrawHighlightBar(int x, int y, int width, int height) const override; - int DrawHorizontalRule(int y) const override; - void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, - int dy) const override; - void DrawFill(int x, int y, int w, int h) const override; - void DrawTextIcon(int x, int y, const GRSurface* surface) const override; - int DrawTextLine(int x, int y, const std::string& line, bool bold) const override; - int DrawTextLines(int x, int y, const std::vector& lines) const override; - int DrawWrappedTextLines(int x, int y, const std::vector& lines) const override; - - // The layout to use. - int layout_; - - // The images that contain localized texts. - std::unique_ptr erasing_text_; - std::unique_ptr error_text_; - std::unique_ptr installing_text_; - std::unique_ptr no_command_text_; - - // Localized text images for the wipe data menu. - std::unique_ptr cancel_wipe_data_text_; - std::unique_ptr factory_data_reset_text_; - std::unique_ptr try_again_text_; - std::unique_ptr wipe_data_confirmation_text_; - std::unique_ptr wipe_data_menu_header_text_; - - std::unique_ptr fastbootd_logo_; - - // current_icon_ points to one of the frames in intro_frames_ or loop_frames_, indexed by - // current_frame_, or error_icon_. - Icon current_icon_; - std::unique_ptr error_icon_; - std::vector> intro_frames_; - std::vector> loop_frames_; - size_t current_frame_; - bool intro_done_; - - // progress_bar and stage_marker images. - std::unique_ptr progress_bar_empty_; - std::unique_ptr progress_bar_fill_; - std::unique_ptr stage_marker_empty_; - std::unique_ptr stage_marker_fill_; - - ProgressType progressBarType; - - float progressScopeStart, progressScopeSize, progress; - double progressScopeTime, progressScopeDuration; - - // true when both graphics pages are the same (except for the progress bar). - bool pagesIdentical; - - size_t text_cols_, text_rows_; - - // Log text overlay, displayed when a magic key is pressed. - char** text_; - size_t text_col_, text_row_; - - bool show_text; - bool show_text_ever; // has show_text ever been true? - - std::vector title_lines_; - - bool scrollable_menu_; - std::unique_ptr menu_; - - // An alternate text screen, swapped with 'text_' when we're viewing a log file. - char** file_viewer_text_; - - std::thread progress_thread_; - std::atomic progress_thread_stopped_{ false }; - - int stage, max_stage; - - int char_width_; - int char_height_; - - // The locale that's used to show the rendered texts. - std::string locale_; - bool rtl_locale_; - - std::mutex updateMutex; - - private: - void SetLocale(const std::string&); - - // Display the background texts for "erasing", "error", "no_command" and "installing" for the - // selected locale. - void SelectAndShowBackgroundText(const std::vector& locales_entries, size_t sel); -}; - -#endif // RECOVERY_UI_H diff --git a/stub_ui.h b/stub_ui.h deleted file mode 100644 index fb1d8c7a6..000000000 --- a/stub_ui.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RECOVERY_STUB_UI_H -#define RECOVERY_STUB_UI_H - -#include -#include -#include - -#include "ui.h" - -// Stub implementation of RecoveryUI for devices without screen. -class StubRecoveryUI : public RecoveryUI { - public: - StubRecoveryUI() = default; - - std::string GetLocale() const override { - return ""; - } - void SetBackground(Icon /* icon */) override {} - void SetSystemUpdateText(bool /* security_update */) override {} - - // progress indicator - void SetProgressType(ProgressType /* type */) override {} - void ShowProgress(float /* portion */, float /* seconds */) override {} - void SetProgress(float /* fraction */) override {} - - void SetStage(int /* current */, int /* max */) override {} - - // text log - void ShowText(bool /* visible */) override {} - bool IsTextVisible() override { - return false; - } - bool WasTextEverVisible() override { - return false; - } - - // printing messages - void Print(const char* fmt, ...) override { - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - } - void PrintOnScreenOnly(const char* /* fmt */, ...) override {} - void ShowFile(const std::string& /* filename */) override {} - - // menu display - size_t ShowMenu(const std::vector& /* headers */, - const std::vector& /* items */, size_t initial_selection, - bool /* menu_only */, - const std::function& /* key_handler */) override { - return initial_selection; - } - - size_t ShowPromptWipeDataMenu(const std::vector& /* backup_headers */, - const std::vector& /* backup_items */, - const std::function& /* key_handle */) override { - return 0; - } - - size_t ShowPromptWipeDataConfirmationMenu( - const std::vector& /* backup_headers */, - const std::vector& /* backup_items */, - const std::function& /* key_handle */) override { - return 0; - } - - void SetTitle(const std::vector& /* lines */) override {} -}; - -#endif // RECOVERY_STUB_UI_H diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp index 647c7b2d3..883dfbde2 100644 --- a/tests/unit/screen_ui_test.cpp +++ b/tests/unit/screen_ui_test.cpp @@ -30,11 +30,11 @@ #include #include "common/test_constants.h" -#include "device.h" #include "minui/minui.h" #include "otautil/paths.h" #include "private/resources.h" -#include "screen_ui.h" +#include "recovery_ui/device.h" +#include "recovery_ui/screen_ui.h" static const std::vector HEADERS{ "header" }; static const std::vector ITEMS{ "item1", "item2", "item3", "item4", "1234567890" }; diff --git a/ui.cpp b/ui.cpp deleted file mode 100644 index c12a11b36..000000000 --- a/ui.cpp +++ /dev/null @@ -1,599 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ui.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "minui/minui.h" -#include "otautil/sysutil.h" -#include "roots.h" - -using namespace std::chrono_literals; - -constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; -constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; -constexpr const char* MAX_BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/max_brightness"; -constexpr const char* BRIGHTNESS_FILE_SDM = "/sys/class/backlight/panel0-backlight/brightness"; -constexpr const char* MAX_BRIGHTNESS_FILE_SDM = - "/sys/class/backlight/panel0-backlight/max_brightness"; - -constexpr int kDefaultTouchLowThreshold = 50; -constexpr int kDefaultTouchHighThreshold = 90; - -RecoveryUI::RecoveryUI() - : brightness_normal_(50), - brightness_dimmed_(25), - brightness_file_(BRIGHTNESS_FILE), - max_brightness_file_(MAX_BRIGHTNESS_FILE), - touch_screen_allowed_(false), - fastbootd_logo_enabled_(false), - touch_low_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_low_threshold", - kDefaultTouchLowThreshold)), - touch_high_threshold_(android::base::GetIntProperty("ro.recovery.ui.touch_high_threshold", - kDefaultTouchHighThreshold)), - key_interrupted_(false), - key_queue_len(0), - key_last_down(-1), - key_long_press(false), - key_down_count(0), - enable_reboot(true), - consecutive_power_keys(0), - last_key(-1), - has_power_key(false), - has_up_key(false), - has_down_key(false), - has_touch_screen(false), - touch_slot_(0), - is_bootreason_recovery_ui_(false), - screensaver_state_(ScreensaverState::DISABLED) { - memset(key_pressed, 0, sizeof(key_pressed)); -} - -RecoveryUI::~RecoveryUI() { - ev_exit(); - input_thread_stopped_ = true; - if (input_thread_.joinable()) { - input_thread_.join(); - } -} - -void RecoveryUI::OnKeyDetected(int key_code) { - if (key_code == KEY_POWER) { - has_power_key = true; - } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { - has_down_key = true; - } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { - has_up_key = true; - } else if (key_code == ABS_MT_POSITION_X || key_code == ABS_MT_POSITION_Y) { - has_touch_screen = true; - } -} - -bool RecoveryUI::InitScreensaver() { - // Disabled. - if (brightness_normal_ == 0 || brightness_dimmed_ > brightness_normal_) { - return false; - } - if (access(brightness_file_.c_str(), R_OK | W_OK)) { - brightness_file_ = BRIGHTNESS_FILE_SDM; - } - if (access(max_brightness_file_.c_str(), R_OK)) { - max_brightness_file_ = MAX_BRIGHTNESS_FILE_SDM; - } - // Set the initial brightness level based on the max brightness. Note that reading the initial - // value from BRIGHTNESS_FILE doesn't give the actual brightness value (bullhead, sailfish), so - // we don't have a good way to query the default value. - std::string content; - if (!android::base::ReadFileToString(max_brightness_file_, &content)) { - PLOG(WARNING) << "Failed to read max brightness"; - return false; - } - - unsigned int max_value; - if (!android::base::ParseUint(android::base::Trim(content), &max_value)) { - LOG(WARNING) << "Failed to parse max brightness: " << content; - return false; - } - - brightness_normal_value_ = max_value * brightness_normal_ / 100.0; - brightness_dimmed_value_ = max_value * brightness_dimmed_ / 100.0; - if (!android::base::WriteStringToFile(std::to_string(brightness_normal_value_), - brightness_file_)) { - PLOG(WARNING) << "Failed to set brightness"; - return false; - } - - LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ << "%)"; - screensaver_state_ = ScreensaverState::NORMAL; - return true; -} - -bool RecoveryUI::Init(const std::string& /* locale */) { - ev_init(std::bind(&RecoveryUI::OnInputEvent, this, std::placeholders::_1, std::placeholders::_2), - touch_screen_allowed_); - - ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); - - if (touch_screen_allowed_) { - ev_iterate_touch_inputs(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); - - // Parse /proc/cmdline to determine if it's booting into recovery with a bootreason of - // "recovery_ui". This specific reason is set by some (wear) bootloaders, to allow an easier way - // to turn on text mode. It will only be set if the recovery boot is triggered from fastboot, or - // with 'adb reboot recovery'. Note that this applies to all build variants. Otherwise the text - // mode will be turned on automatically on debuggable builds, even without a swipe. - std::string cmdline; - if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) { - is_bootreason_recovery_ui_ = cmdline.find("bootreason=recovery_ui") != std::string::npos; - } else { - // Non-fatal, and won't affect Init() result. - PLOG(WARNING) << "Failed to read /proc/cmdline"; - } - } - - if (!InitScreensaver()) { - LOG(INFO) << "Screensaver disabled"; - } - - // Create a separate thread that handles input events. - input_thread_ = std::thread([this]() { - while (!this->input_thread_stopped_) { - if (!ev_wait(500)) { - ev_dispatch(); - } - } - }); - - return true; -} - -void RecoveryUI::OnTouchDetected(int dx, int dy) { - enum SwipeDirection { UP, DOWN, RIGHT, LEFT } direction; - - // We only consider a valid swipe if: - // - the delta along one axis is below touch_low_threshold_; - // - and the delta along the other axis is beyond touch_high_threshold_. - if (abs(dy) < touch_low_threshold_ && abs(dx) > touch_high_threshold_) { - direction = dx < 0 ? SwipeDirection::LEFT : SwipeDirection::RIGHT; - } else if (abs(dx) < touch_low_threshold_ && abs(dy) > touch_high_threshold_) { - direction = dy < 0 ? SwipeDirection::UP : SwipeDirection::DOWN; - } else { - LOG(DEBUG) << "Ignored " << dx << " " << dy << " (low: " << touch_low_threshold_ - << ", high: " << touch_high_threshold_ << ")"; - return; - } - - // Allow turning on text mode with any swipe, if bootloader has set a bootreason of recovery_ui. - if (is_bootreason_recovery_ui_ && !IsTextVisible()) { - ShowText(true); - return; - } - - LOG(DEBUG) << "Swipe direction=" << direction; - switch (direction) { - case SwipeDirection::UP: - ProcessKey(KEY_UP, 1); // press up key - ProcessKey(KEY_UP, 0); // and release it - break; - - case SwipeDirection::DOWN: - ProcessKey(KEY_DOWN, 1); // press down key - ProcessKey(KEY_DOWN, 0); // and release it - break; - - case SwipeDirection::LEFT: - case SwipeDirection::RIGHT: - ProcessKey(KEY_POWER, 1); // press power key - ProcessKey(KEY_POWER, 0); // and release it - break; - }; -} - -int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { - struct input_event ev; - if (ev_get_input(fd, epevents, &ev) == -1) { - return -1; - } - - // Touch inputs handling. - // - // We handle the touch inputs by tracking the position changes between initial contacting and - // upon lifting. touch_start_X/Y record the initial positions, with touch_finger_down set. Upon - // detecting the lift, we unset touch_finger_down and detect a swipe based on position changes. - // - // Per the doc Multi-touch Protocol at below, there are two protocols. - // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt - // - // The main difference between the stateless type A protocol and the stateful type B slot protocol - // lies in the usage of identifiable contacts to reduce the amount of data sent to userspace. The - // slot protocol (i.e. type B) sends ABS_MT_TRACKING_ID with a unique id on initial contact, and - // sends ABS_MT_TRACKING_ID -1 upon lifting the contact. Protocol A doesn't send - // ABS_MT_TRACKING_ID -1 on lifting, but the driver may additionally report BTN_TOUCH event. - // - // For protocol A, we rely on BTN_TOUCH to recognize lifting, while for protocol B we look for - // ABS_MT_TRACKING_ID being -1. - // - // Touch input events will only be available if touch_screen_allowed_ is set. - - if (ev.type == EV_SYN) { - if (touch_screen_allowed_ && ev.code == SYN_REPORT) { - // There might be multiple SYN_REPORT events. We should only detect a swipe after lifting the - // contact. - if (touch_finger_down_ && !touch_swiping_) { - touch_start_X_ = touch_X_; - touch_start_Y_ = touch_Y_; - touch_swiping_ = true; - } else if (!touch_finger_down_ && touch_swiping_) { - touch_swiping_ = false; - OnTouchDetected(touch_X_ - touch_start_X_, touch_Y_ - touch_start_Y_); - } - } - return 0; - } - - if (ev.type == EV_REL) { - if (ev.code == REL_Y) { - // accumulate the up or down motion reported by - // the trackball. When it exceeds a threshold - // (positive or negative), fake an up/down - // key event. - rel_sum += ev.value; - if (rel_sum > 3) { - ProcessKey(KEY_DOWN, 1); // press down key - ProcessKey(KEY_DOWN, 0); // and release it - rel_sum = 0; - } else if (rel_sum < -3) { - ProcessKey(KEY_UP, 1); // press up key - ProcessKey(KEY_UP, 0); // and release it - rel_sum = 0; - } - } - } else { - rel_sum = 0; - } - - if (touch_screen_allowed_ && ev.type == EV_ABS) { - if (ev.code == ABS_MT_SLOT) { - touch_slot_ = ev.value; - } - // Ignore other fingers. - if (touch_slot_ > 0) return 0; - - switch (ev.code) { - case ABS_MT_POSITION_X: - touch_X_ = ev.value; - touch_finger_down_ = true; - break; - - case ABS_MT_POSITION_Y: - touch_Y_ = ev.value; - touch_finger_down_ = true; - break; - - case ABS_MT_TRACKING_ID: - // Protocol B: -1 marks lifting the contact. - if (ev.value < 0) touch_finger_down_ = false; - break; - } - return 0; - } - - if (ev.type == EV_KEY && ev.code <= KEY_MAX) { - if (touch_screen_allowed_) { - if (ev.code == BTN_TOUCH) { - // A BTN_TOUCH with value 1 indicates the start of contact (protocol A), with 0 means - // lifting the contact. - touch_finger_down_ = (ev.value == 1); - } - - // Intentionally ignore BTN_TOUCH and BTN_TOOL_FINGER, which would otherwise trigger - // additional scrolling (because in ScreenRecoveryUI::ShowFile(), we consider keys other than - // KEY_POWER and KEY_UP as KEY_DOWN). - if (ev.code == BTN_TOUCH || ev.code == BTN_TOOL_FINGER) { - return 0; - } - } - - ProcessKey(ev.code, ev.value); - } - - return 0; -} - -// Processes a key-up or -down event. A key is "registered" when it is pressed and then released, -// with no other keypresses or releases in between. Registered keys are passed to CheckKey() to -// see if it should trigger a visibility toggle, an immediate reboot, or be queued to be processed -// next time the foreground thread wants a key (eg, for the menu). -// -// We also keep track of which keys are currently down so that CheckKey() can call IsKeyPressed() -// to see what other keys are held when a key is registered. -// -// updown == 1 for key down events; 0 for key up events -void RecoveryUI::ProcessKey(int key_code, int updown) { - bool register_key = false; - bool long_press = false; - - { - std::lock_guard lg(key_queue_mutex); - key_pressed[key_code] = updown; - if (updown) { - ++key_down_count; - key_last_down = key_code; - key_long_press = false; - std::thread time_key_thread(&RecoveryUI::TimeKey, this, key_code, key_down_count); - time_key_thread.detach(); - } else { - if (key_last_down == key_code) { - long_press = key_long_press; - register_key = true; - } - key_last_down = -1; - } - } - - bool reboot_enabled = enable_reboot; - if (register_key) { - switch (CheckKey(key_code, long_press)) { - case RecoveryUI::IGNORE: - break; - - case RecoveryUI::TOGGLE: - ShowText(!IsTextVisible()); - break; - - case RecoveryUI::REBOOT: - if (reboot_enabled) { - reboot("reboot,"); - while (true) { - pause(); - } - } - break; - - case RecoveryUI::ENQUEUE: - EnqueueKey(key_code); - break; - } - } -} - -void RecoveryUI::TimeKey(int key_code, int count) { - std::this_thread::sleep_for(750ms); // 750 ms == "long" - bool long_press = false; - { - std::lock_guard lg(key_queue_mutex); - if (key_last_down == key_code && key_down_count == count) { - long_press = key_long_press = true; - } - } - if (long_press) KeyLongPress(key_code); -} - -void RecoveryUI::EnqueueKey(int key_code) { - std::lock_guard lg(key_queue_mutex); - const int queue_max = sizeof(key_queue) / sizeof(key_queue[0]); - if (key_queue_len < queue_max) { - key_queue[key_queue_len++] = key_code; - key_queue_cond.notify_one(); - } -} - -void RecoveryUI::SetScreensaverState(ScreensaverState state) { - switch (state) { - case ScreensaverState::NORMAL: - if (android::base::WriteStringToFile(std::to_string(brightness_normal_value_), - brightness_file_)) { - screensaver_state_ = ScreensaverState::NORMAL; - LOG(INFO) << "Brightness: " << brightness_normal_value_ << " (" << brightness_normal_ - << "%)"; - } else { - LOG(ERROR) << "Unable to set brightness to normal"; - } - break; - case ScreensaverState::DIMMED: - if (android::base::WriteStringToFile(std::to_string(brightness_dimmed_value_), - brightness_file_)) { - LOG(INFO) << "Brightness: " << brightness_dimmed_value_ << " (" << brightness_dimmed_ - << "%)"; - screensaver_state_ = ScreensaverState::DIMMED; - } else { - LOG(ERROR) << "Unable to set brightness to dim"; - } - break; - case ScreensaverState::OFF: - if (android::base::WriteStringToFile("0", brightness_file_)) { - LOG(INFO) << "Brightness: 0 (off)"; - screensaver_state_ = ScreensaverState::OFF; - } else { - LOG(ERROR) << "Unable to set brightness to off"; - } - break; - default: - LOG(ERROR) << "Invalid screensaver state"; - } -} - -int RecoveryUI::WaitKey() { - std::unique_lock lk(key_queue_mutex); - - // Check for a saved key queue interruption. - if (key_interrupted_) { - SetScreensaverState(ScreensaverState::NORMAL); - return static_cast(KeyError::INTERRUPTED); - } - - // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is plugged in. - do { - bool rc = key_queue_cond.wait_for(lk, std::chrono::seconds(UI_WAIT_KEY_TIMEOUT_SEC), [this] { - return this->key_queue_len != 0 || key_interrupted_; - }); - if (key_interrupted_) { - SetScreensaverState(ScreensaverState::NORMAL); - return static_cast(KeyError::INTERRUPTED); - } - if (screensaver_state_ != ScreensaverState::DISABLED) { - if (!rc) { - // Must be after a timeout. Lower the brightness level: NORMAL -> DIMMED; DIMMED -> OFF. - if (screensaver_state_ == ScreensaverState::NORMAL) { - SetScreensaverState(ScreensaverState::DIMMED); - } else if (screensaver_state_ == ScreensaverState::DIMMED) { - SetScreensaverState(ScreensaverState::OFF); - } - } else if (screensaver_state_ != ScreensaverState::NORMAL) { - // Drop the first key if it's changing from OFF to NORMAL. - if (screensaver_state_ == ScreensaverState::OFF) { - if (key_queue_len > 0) { - memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); - } - } - - // Reset the brightness to normal. - SetScreensaverState(ScreensaverState::NORMAL); - } - } - } while (IsUsbConnected() && key_queue_len == 0); - - int key = static_cast(KeyError::TIMED_OUT); - if (key_queue_len > 0) { - key = key_queue[0]; - memcpy(&key_queue[0], &key_queue[1], sizeof(int) * --key_queue_len); - } - return key; -} - -void RecoveryUI::InterruptKey() { - { - std::lock_guard lg(key_queue_mutex); - key_interrupted_ = true; - } - key_queue_cond.notify_one(); -} - -bool RecoveryUI::IsUsbConnected() { - int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); - if (fd < 0) { - printf("failed to open /sys/class/android_usb/android0/state: %s\n", strerror(errno)); - return 0; - } - - char buf; - // USB is connected if android_usb state is CONNECTED or CONFIGURED. - int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); - if (close(fd) < 0) { - printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno)); - } - return connected; -} - -bool RecoveryUI::IsKeyPressed(int key) { - std::lock_guard lg(key_queue_mutex); - int pressed = key_pressed[key]; - return pressed; -} - -bool RecoveryUI::IsLongPress() { - std::lock_guard lg(key_queue_mutex); - bool result = key_long_press; - return result; -} - -bool RecoveryUI::HasThreeButtons() { - return has_power_key && has_up_key && has_down_key; -} - -bool RecoveryUI::HasPowerKey() const { - return has_power_key; -} - -bool RecoveryUI::HasTouchScreen() const { - return has_touch_screen; -} - -void RecoveryUI::FlushKeys() { - std::lock_guard lg(key_queue_mutex); - key_queue_len = 0; -} - -RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { - { - std::lock_guard lg(key_queue_mutex); - key_long_press = false; - } - - // If we have power and volume up keys, that chord is the signal to toggle the text display. - if (HasThreeButtons() || (HasPowerKey() && HasTouchScreen() && touch_screen_allowed_)) { - if ((key == KEY_VOLUMEUP || key == KEY_UP) && IsKeyPressed(KEY_POWER)) { - return TOGGLE; - } - } else { - // Otherwise long press of any button toggles to the text display, - // and there's no way to toggle back (but that's pretty useless anyway). - if (is_long_press && !IsTextVisible()) { - return TOGGLE; - } - - // Also, for button-limited devices, a long press is translated to KEY_ENTER. - if (is_long_press && IsTextVisible()) { - EnqueueKey(KEY_ENTER); - return IGNORE; - } - } - - // Press power seven times in a row to reboot. - if (key == KEY_POWER) { - bool reboot_enabled = enable_reboot; - - if (reboot_enabled) { - ++consecutive_power_keys; - if (consecutive_power_keys >= 7) { - return REBOOT; - } - } - } else { - consecutive_power_keys = 0; - } - - last_key = key; - return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE; -} - -void RecoveryUI::KeyLongPress(int) { -} - -void RecoveryUI::SetEnableReboot(bool enabled) { - std::lock_guard lg(key_queue_mutex); - enable_reboot = enabled; -} diff --git a/ui.h b/ui.h deleted file mode 100644 index b387ae3c9..000000000 --- a/ui.h +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RECOVERY_UI_H -#define RECOVERY_UI_H - -#include // KEY_MAX - -#include -#include -#include -#include -#include -#include -#include - -// Abstract class for controlling the user interface during recovery. -class RecoveryUI { - public: - enum Icon { - NONE, - INSTALLING_UPDATE, - ERASING, - NO_COMMAND, - ERROR - }; - - enum ProgressType { - EMPTY, - INDETERMINATE, - DETERMINATE - }; - - enum KeyAction { - ENQUEUE, - TOGGLE, - REBOOT, - IGNORE - }; - - enum class KeyError : int { - TIMED_OUT = -1, - INTERRUPTED = -2, - }; - - RecoveryUI(); - - virtual ~RecoveryUI(); - - // Initializes the object; called before anything else. UI texts will be initialized according to - // the given locale. Returns true on success. - virtual bool Init(const std::string& locale); - - virtual std::string GetLocale() const = 0; - - // Shows a stage indicator. Called immediately after Init(). - virtual void SetStage(int current, int max) = 0; - - // Sets the overall recovery state ("background image"). - virtual void SetBackground(Icon icon) = 0; - virtual void SetSystemUpdateText(bool security_update) = 0; - - // --- progress indicator --- - virtual void SetProgressType(ProgressType determinate) = 0; - - // Shows a progress bar and define the scope of the next operation: - // portion - fraction of the progress bar the next operation will use - // seconds - expected time interval (progress bar moves at this minimum rate) - virtual void ShowProgress(float portion, float seconds) = 0; - - // Sets progress bar position (0.0 - 1.0 within the scope defined by the last call to - // ShowProgress). - virtual void SetProgress(float fraction) = 0; - - // --- text log --- - - virtual void ShowText(bool visible) = 0; - - virtual bool IsTextVisible() = 0; - - virtual bool WasTextEverVisible() = 0; - - // Writes a message to the on-screen log (shown if the user has toggled on the text display). - // Print() will also dump the message to stdout / log file, while PrintOnScreenOnly() not. - virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0; - virtual void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3) = 0; - - // Shows the contents of the given file. Caller ensures the patition that contains the file has - // been mounted. - virtual void ShowFile(const std::string& filename) = 0; - - // --- key handling --- - - // Waits for a key and return it. May return TIMED_OUT after timeout and - // KeyError::INTERRUPTED on a key interrupt. - virtual int WaitKey(); - - // Wakes up the UI if it is waiting on key input, causing WaitKey to return KeyError::INTERRUPTED. - virtual void InterruptKey(); - - virtual bool IsKeyPressed(int key); - virtual bool IsLongPress(); - - // Returns true if you have the volume up/down and power trio typical of phones and tablets, false - // otherwise. - virtual bool HasThreeButtons(); - - // Returns true if it has a power key. - virtual bool HasPowerKey() const; - - // Returns true if it supports touch inputs. - virtual bool HasTouchScreen() const; - - // Erases any queued-up keys. - virtual void FlushKeys(); - - // Called on each key press, even while operations are in progress. Return value indicates whether - // an immediate operation should be triggered (toggling the display, rebooting the device), or if - // the key should be enqueued for use by the main thread. - virtual KeyAction CheckKey(int key, bool is_long_press); - - // Called when a key is held down long enough to have been a long-press (but before the key is - // released). This means that if the key is eventually registered (released without any other keys - // being pressed in the meantime), CheckKey will be called with 'is_long_press' true. - virtual void KeyLongPress(int key); - - // Normally in recovery there's a key sequence that triggers immediate reboot of the device, - // regardless of what recovery is doing (with the default CheckKey implementation, it's pressing - // the power button 7 times in row). Call this to enable or disable that feature. It is enabled by - // default. - virtual void SetEnableReboot(bool enabled); - - // --- menu display --- - - virtual void SetTitle(const std::vector& lines) = 0; - - // Displays a menu with the given 'headers' and 'items'. The supplied 'key_handler' callback, - // which is typically bound to Device::HandleMenuKey(), should return the expected action for the - // given key code and menu visibility (e.g. to move the cursor or to select an item). Caller sets - // 'menu_only' to true to ensure only a menu item gets selected and returned. Otherwise if - // 'menu_only' is false, ShowMenu() will forward any non-negative value returned from the - // key_handler, which may be beyond the range of menu items. This could be used to trigger a - // device-specific action, even without that being listed in the menu. Caller needs to handle - // such a case accordingly (e.g. by calling Device::InvokeMenuItem() to process the action). - // Returns a non-negative value (the chosen item number or device-specific action code), or - // static_cast(TIMED_OUT) if timed out waiting for input or - // static_cast(ERR_KEY_INTERTUPT) if interrupted, such as by InterruptKey(). - virtual size_t ShowMenu(const std::vector& headers, - const std::vector& items, size_t initial_selection, - bool menu_only, const std::function& key_handler) = 0; - - // Displays the localized wipe data menu with pre-generated graphs. If there's an issue - // with the graphs, falls back to use the backup string headers and items instead. The initial - // selection is the 0th item in the menu, which is expected to reboot the device without a wipe. - virtual size_t ShowPromptWipeDataMenu(const std::vector& backup_headers, - const std::vector& backup_items, - const std::function& key_handler) = 0; - // Displays the localized wipe data confirmation menu with pre-generated images. Falls back to - // the text strings upon failures. The initial selection is the 0th item, which returns to the - // upper level menu. - virtual size_t ShowPromptWipeDataConfirmationMenu( - const std::vector& backup_headers, const std::vector& backup_items, - const std::function& key_handler) = 0; - - // Set whether or not the fastbootd logo is displayed. - void SetEnableFastbootdLogo(bool enable) { - fastbootd_logo_enabled_ = enable; - } - - // Resets the key interrupt status. - void ResetKeyInterruptStatus() { - key_interrupted_ = false; - } - - // Returns the key interrupt status. - bool IsKeyInterrupted() const { - return key_interrupted_; - } - - protected: - void EnqueueKey(int key_code); - - // The normal and dimmed brightness percentages (default: 50 and 25, which means 50% and 25% of - // the max_brightness). Because the absolute values may vary across devices. These two values can - // be configured via subclassing. Setting brightness_normal_ to 0 to disable screensaver. - unsigned int brightness_normal_; - unsigned int brightness_dimmed_; - std::string brightness_file_; - std::string max_brightness_file_; - - // Whether we should listen for touch inputs (default: false). - bool touch_screen_allowed_; - - bool fastbootd_logo_enabled_; - - private: - enum class ScreensaverState { - DISABLED, - NORMAL, - DIMMED, - OFF - }; - - // The sensitivity when detecting a swipe. - const int touch_low_threshold_; - const int touch_high_threshold_; - - void OnKeyDetected(int key_code); - void OnTouchDetected(int dx, int dy); - int OnInputEvent(int fd, uint32_t epevents); - void ProcessKey(int key_code, int updown); - void TimeKey(int key_code, int count); - - bool IsUsbConnected(); - - bool InitScreensaver(); - void SetScreensaverState(ScreensaverState state); - // Key event input queue - std::mutex key_queue_mutex; - std::condition_variable key_queue_cond; - bool key_interrupted_; - int key_queue[256], key_queue_len; - char key_pressed[KEY_MAX + 1]; // under key_queue_mutex - int key_last_down; // under key_queue_mutex - bool key_long_press; // under key_queue_mutex - int key_down_count; // under key_queue_mutex - bool enable_reboot; // under key_queue_mutex - int rel_sum; - - int consecutive_power_keys; - int last_key; - - bool has_power_key; - bool has_up_key; - bool has_down_key; - bool has_touch_screen; - - // Touch event related variables. See the comments in RecoveryUI::OnInputEvent(). - int touch_slot_; - int touch_X_; - int touch_Y_; - int touch_start_X_; - int touch_start_Y_; - bool touch_finger_down_; - bool touch_swiping_; - bool is_bootreason_recovery_ui_; - - std::thread input_thread_; - std::atomic input_thread_stopped_{ false }; - - ScreensaverState screensaver_state_; - - // The following two contain the absolute values computed from brightness_normal_ and - // brightness_dimmed_ respectively. - unsigned int brightness_normal_value_; - unsigned int brightness_dimmed_value_; -}; - -#endif // RECOVERY_UI_H diff --git a/vr_device.cpp b/vr_device.cpp deleted file mode 100644 index 61e15cbb6..000000000 --- a/vr_device.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device.h" -#include "vr_ui.h" - -Device* make_device() { - return new Device(new VrRecoveryUI); -} - diff --git a/vr_ui.cpp b/vr_ui.cpp deleted file mode 100644 index 1f0292c30..000000000 --- a/vr_ui.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "vr_ui.h" - -#include - -#include "minui/minui.h" - -constexpr int kDefaultStereoOffset = 0; - -VrRecoveryUI::VrRecoveryUI() - : stereo_offset_( - android::base::GetIntProperty("ro.recovery.ui.stereo_offset", kDefaultStereoOffset)) {} - -int VrRecoveryUI::ScreenWidth() const { - return gr_fb_width() / 2; -} - -int VrRecoveryUI::ScreenHeight() const { - return gr_fb_height(); -} - -void VrRecoveryUI::DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, - int dy) const { - gr_blit(surface, sx, sy, w, h, dx + stereo_offset_, dy); - gr_blit(surface, sx, sy, w, h, dx - stereo_offset_ + ScreenWidth(), dy); -} - -void VrRecoveryUI::DrawTextIcon(int x, int y, const GRSurface* surface) const { - gr_texticon(x + stereo_offset_, y, surface); - gr_texticon(x - stereo_offset_ + ScreenWidth(), y, surface); -} - -int VrRecoveryUI::DrawTextLine(int x, int y, const std::string& line, bool bold) const { - gr_text(gr_sys_font(), x + stereo_offset_, y, line.c_str(), bold); - gr_text(gr_sys_font(), x - stereo_offset_ + ScreenWidth(), y, line.c_str(), bold); - return char_height_ + 4; -} - -int VrRecoveryUI::DrawHorizontalRule(int y) const { - y += 4; - gr_fill(margin_width_ + stereo_offset_, y, ScreenWidth() - margin_width_ + stereo_offset_, y + 2); - gr_fill(ScreenWidth() + margin_width_ - stereo_offset_, y, - gr_fb_width() - margin_width_ - stereo_offset_, y + 2); - return y + 4; -} - -void VrRecoveryUI::DrawHighlightBar(int /* x */, int y, int /* width */, int height) const { - gr_fill(margin_width_ + stereo_offset_, y, ScreenWidth() - margin_width_ + stereo_offset_, - y + height); - gr_fill(ScreenWidth() + margin_width_ - stereo_offset_, y, - gr_fb_width() - margin_width_ - stereo_offset_, y + height); -} - -void VrRecoveryUI::DrawFill(int x, int y, int w, int h) const { - gr_fill(x + stereo_offset_, y, w, h); - gr_fill(x - stereo_offset_ + ScreenWidth(), y, w, h); -} diff --git a/vr_ui.h b/vr_ui.h deleted file mode 100644 index 2e8ac5921..000000000 --- a/vr_ui.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RECOVERY_VR_UI_H -#define RECOVERY_VR_UI_H - -#include - -#include "screen_ui.h" - -class VrRecoveryUI : public ScreenRecoveryUI { - public: - VrRecoveryUI(); - - protected: - // Pixel offsets to move drawing functions to visible range. - // Can vary per device depending on screen size and lens distortion. - const int stereo_offset_; - - int ScreenWidth() const override; - int ScreenHeight() const override; - - void DrawSurface(const GRSurface* surface, int sx, int sy, int w, int h, int dx, - int dy) const override; - int DrawHorizontalRule(int y) const override; - void DrawHighlightBar(int x, int y, int width, int height) const override; - void DrawFill(int x, int y, int w, int h) const override; - void DrawTextIcon(int x, int y, const GRSurface* surface) const override; - int DrawTextLine(int x, int y, const std::string& line, bool bold) const override; -}; - -#endif // RECOVERY_VR_UI_H diff --git a/wear_device.cpp b/wear_device.cpp deleted file mode 100644 index 3268130b0..000000000 --- a/wear_device.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device.h" -#include "wear_ui.h" - -Device* make_device() { - return new Device(new WearRecoveryUI); -} - diff --git a/wear_ui.cpp b/wear_ui.cpp deleted file mode 100644 index 6da84c924..000000000 --- a/wear_ui.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "wear_ui.h" - -#include - -#include -#include - -#include -#include -#include - -constexpr int kDefaultProgressBarBaseline = 259; -constexpr int kDefaultMenuUnusableRows = 9; - -WearRecoveryUI::WearRecoveryUI() - : ScreenRecoveryUI(true), - progress_bar_baseline_(android::base::GetIntProperty("ro.recovery.ui.progress_bar_baseline", - kDefaultProgressBarBaseline)), - menu_unusable_rows_(android::base::GetIntProperty("ro.recovery.ui.menu_unusable_rows", - kDefaultMenuUnusableRows)) { - // TODO: menu_unusable_rows_ should be computed based on the lines in draw_screen_locked(). - - touch_screen_allowed_ = true; -} - -int WearRecoveryUI::GetProgressBaseline() const { - return progress_bar_baseline_; -} - -// Draw background frame on the screen. Does not flip pages. -// Should only be called with updateMutex locked. -// TODO merge drawing routines with screen_ui -void WearRecoveryUI::draw_background_locked() { - pagesIdentical = false; - gr_color(0, 0, 0, 255); - gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - - if (current_icon_ != NONE) { - const auto& frame = GetCurrentFrame(); - int frame_width = gr_get_width(frame); - int frame_height = gr_get_height(frame); - int frame_x = (gr_fb_width() - frame_width) / 2; - int frame_y = (gr_fb_height() - frame_height) / 2; - gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); - - // Draw recovery text on screen above progress bar. - const auto& text = GetCurrentText(); - int text_x = (ScreenWidth() - gr_get_width(text)) / 2; - int text_y = GetProgressBaseline() - gr_get_height(text) - 10; - gr_color(255, 255, 255, 255); - gr_texticon(text_x, text_y, text); - } -} - -void WearRecoveryUI::draw_screen_locked() { - draw_background_locked(); - if (!show_text) { - draw_foreground_locked(); - } else { - SetColor(UIElement::TEXT_FILL); - gr_fill(0, 0, gr_fb_width(), gr_fb_height()); - - // clang-format off - static std::vector SWIPE_HELP = { - "Swipe up/down to move.", - "Swipe left/right to select.", - "", - }; - // clang-format on - draw_menu_and_text_buffer_locked(SWIPE_HELP); - } -} - -// TODO merge drawing routines with screen_ui -void WearRecoveryUI::update_progress_locked() { - draw_screen_locked(); - gr_flip(); -} - -void WearRecoveryUI::SetStage(int /* current */, int /* max */) {} - -std::unique_ptr WearRecoveryUI::CreateMenu(const std::vector& text_headers, - const std::vector& text_items, - size_t initial_selection) const { - if (text_rows_ > 0 && text_cols_ > 0) { - return std::make_unique(scrollable_menu_, text_rows_ - menu_unusable_rows_ - 1, - text_cols_ - 1, text_headers, text_items, initial_selection, - char_height_, *this); - } - - return nullptr; -} diff --git a/wear_ui.h b/wear_ui.h deleted file mode 100644 index 429af69d2..000000000 --- a/wear_ui.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef RECOVERY_WEAR_UI_H -#define RECOVERY_WEAR_UI_H - -#include -#include - -#include "screen_ui.h" - -class WearRecoveryUI : public ScreenRecoveryUI { - public: - WearRecoveryUI(); - - void SetStage(int current, int max) override; - - protected: - // progress bar vertical position, it's centered horizontally - const int progress_bar_baseline_; - - // Unusable rows when displaying the recovery menu, including the lines for headers (Android - // Recovery, build id and etc) and the bottom lines that may otherwise go out of the screen. - const int menu_unusable_rows_; - - std::unique_ptr CreateMenu(const std::vector& text_headers, - const std::vector& text_items, - size_t initial_selection) const override; - - int GetProgressBaseline() const override; - - void update_progress_locked() override; - - private: - void draw_background_locked() override; - void draw_screen_locked() override; -}; - -#endif // RECOVERY_WEAR_UI_H -- cgit v1.2.3