Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _RECOVERY_DEVICE_H |
| 18 | #define _RECOVERY_DEVICE_H |
| 19 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 22 | #include <memory> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Tao Bao | c16fd8a | 2018-04-30 17:12:03 -0700 | [diff] [blame] | 26 | // Forward declaration to avoid including "ui.h". |
| 27 | class RecoveryUI; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 28 | |
| 29 | class Device { |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 30 | public: |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 31 | static constexpr const int kNoAction = -1; |
| 32 | static constexpr const int kHighlightUp = -2; |
| 33 | static constexpr const int kHighlightDown = -3; |
| 34 | static constexpr const int kInvokeItem = -4; |
| 35 | |
| 36 | enum BuiltinAction { |
| 37 | NO_ACTION = 0, |
| 38 | REBOOT = 1, |
| 39 | APPLY_SDCARD = 2, |
| 40 | // APPLY_CACHE was 3. |
| 41 | APPLY_ADB_SIDELOAD = 4, |
| 42 | WIPE_DATA = 5, |
| 43 | WIPE_CACHE = 6, |
| 44 | REBOOT_BOOTLOADER = 7, |
| 45 | SHUTDOWN = 8, |
| 46 | VIEW_RECOVERY_LOGS = 9, |
| 47 | MOUNT_SYSTEM = 10, |
| 48 | RUN_GRAPHICS_TEST = 11, |
| 49 | RUN_LOCALE_TEST = 12, |
| 50 | }; |
| 51 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 52 | explicit Device(RecoveryUI* ui) : ui_(ui) {} |
| 53 | virtual ~Device() {} |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 54 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 55 | // Returns a raw pointer to the RecoveryUI object. |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 56 | virtual RecoveryUI* GetUI() { |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 57 | return ui_.get(); |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 58 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 59 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 60 | // Resets the UI object to the given UI. Used to override the default UI in case initialization |
| 61 | // failed, or we want a different UI for some reason. The device object will take the ownership. |
| 62 | virtual void ResetUI(RecoveryUI* ui) { |
| 63 | ui_.reset(ui); |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 66 | // Called when recovery starts up (after the UI has been obtained and initialized and after the |
| 67 | // arguments have been parsed, but before anything else). |
| 68 | virtual void StartRecovery() {}; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 69 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 70 | // Called from the main thread when recovery is at the main menu and waiting for input, and a key |
| 71 | // is pressed. (Note that "at" the main menu does not necessarily mean the menu is visible; |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 72 | // recovery will be at the main menu with it invisible after an unsuccessful operation, such as |
| 73 | // failed to install an OTA package, or if recovery is started with no command.) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 74 | // |
| 75 | // 'key' is the code of the key just pressed. (You can call IsKeyPressed() on the RecoveryUI |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 76 | // object you returned from GetUI() if you want to find out if other keys are held down.) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 77 | // |
| 78 | // 'visible' is true if the menu is visible. |
| 79 | // |
| 80 | // Returns one of the defined constants below in order to: |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 81 | // - move the menu highlight (kHighlight{Up,Down}: negative value) |
| 82 | // - invoke the highlighted item (kInvokeItem: negative value) |
| 83 | // - do nothing (kNoAction: negative value) |
| 84 | // - invoke a specific action (a menu position: non-negative value) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 85 | virtual int HandleMenuKey(int key, bool visible); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 86 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 87 | // Returns the list of menu items (a vector of strings). The menu_position passed to |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 88 | // InvokeMenuItem() will correspond to the indexes into this array. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 89 | virtual const std::vector<std::string>& GetMenuItems(); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 90 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 91 | // Performs a recovery action selected from the menu. 'menu_position' will be the index of the |
| 92 | // selected menu item, or a non-negative value returned from HandleMenuKey(). The menu will be |
| 93 | // hidden when this is called; implementations can call ui_print() to print information to the |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 94 | // screen. If the menu position is one of the builtin actions, you can just return the |
| 95 | // corresponding enum value. If it is an action specific to your device, you actually perform it |
| 96 | // here and return NO_ACTION. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 97 | virtual BuiltinAction InvokeMenuItem(size_t menu_position); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 98 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 99 | // Called before and after we do a wipe data/factory reset operation, either via a reboot from the |
| 100 | // main system with the --wipe_data flag, or when the user boots into recovery image manually and |
| 101 | // selects the option from the menu, to perform whatever device-specific wiping actions as needed. |
| 102 | // Returns true on success; returning false from PreWipeData will prevent the regular wipe, and |
| 103 | // returning false from PostWipeData will cause the wipe to be considered a failure. |
| 104 | virtual bool PreWipeData() { |
| 105 | return true; |
| 106 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 107 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 108 | virtual bool PostWipeData() { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | private: |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 113 | // The RecoveryUI object that should be used to display the user interface for this device. |
| 114 | std::unique_ptr<RecoveryUI> ui_; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 117 | // The device-specific library must define this function (or the default one will be used, if there |
| 118 | // is no device-specific library). It returns the Device object that recovery should use. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 119 | Device* make_device(); |
| 120 | |
| 121 | #endif // _DEVICE_H |