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