1daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker/*
2daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * Copyright (C) 2011 The Android Open Source Project
3daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker *
4daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * Licensed under the Apache License, Version 2.0 (the "License");
5daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * you may not use this file except in compliance with the License.
6daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * You may obtain a copy of the License at
7daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker *
8daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker *      http://www.apache.org/licenses/LICENSE-2.0
9daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker *
10daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * Unless required by applicable law or agreed to in writing, software
11daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * distributed under the License is distributed on an "AS IS" BASIS,
12daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * See the License for the specific language governing permissions and
14daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker * limitations under the License.
15daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker */
16daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
17daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker#ifndef _RECOVERY_DEVICE_H
18daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker#define _RECOVERY_DEVICE_H
19daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
20daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker#include "ui.h"
21daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
22daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongkerclass Device {
23daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker  public:
24daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual ~Device() { }
25daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
26daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Called to obtain the UI object that should be used to display
27daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // the recovery user interface for this device.  You should not
28daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // have called Init() on the UI object already, the caller will do
29daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // that after this method returns.
30daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual RecoveryUI* GetUI() = 0;
31daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
32daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Called when recovery starts up (after the UI has been obtained
33daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // and initialized and after the arguments have been parsed, but
34daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // before anything else).
35daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual void StartRecovery() { };
36daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
37daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // enum KeyAction { NONE, TOGGLE, REBOOT };
38daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
39daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // Called in the input thread when a new key (key_code) is
40daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // pressed.  *key_pressed is an array of KEY_MAX+1 bytes
41daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // indicating which other keys are already pressed.  Return a
42daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // KeyAction to indicate action should be taken immediately.
43daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // These actions happen when recovery is not waiting for input
44daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // // (eg, in the midst of installing a package).
45daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // virtual KeyAction CheckImmediateKeyAction(volatile char* key_pressed, int key_code) = 0;
46daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
47daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Called from the main thread when recovery is at the main menu
48daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // and waiting for input, and a key is pressed.  (Note that "at"
49daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // the main menu does not necessarily mean the menu is visible;
50daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // recovery will be at the main menu with it invisible after an
51daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // unsuccessful operation [ie OTA package failure], or if recovery
52daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // is started with no command.)
53daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //
54daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // key is the code of the key just pressed.  (You can call
55daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // IsKeyPressed() on the RecoveryUI object you returned from GetUI
56daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // if you want to find out if other keys are held down.)
57daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //
58daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // visible is true if the menu is visible.
59daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //
60daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Return one of the defined constants below in order to:
61daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //
62daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //   - move the menu highlight (kHighlight{Up,Down})
63daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //   - invoke the highlighted item (kInvokeItem)
64daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //   - do nothing (kNoAction)
65daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    //   - invoke a specific action (a menu position: any non-negative number)
66daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual int HandleMenuKey(int key, int visible) = 0;
67daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
6893950229cf9a991589f6bb071a966b00349d18d6Doug Zongker    enum BuiltinAction { NO_ACTION, REBOOT, APPLY_EXT,
6993950229cf9a991589f6bb071a966b00349d18d6Doug Zongker                         APPLY_CACHE,   // APPLY_CACHE is deprecated; has no effect
708d9d3d5cbe240d09db10d08956d152dce934e892Doug Zongker                         APPLY_ADB_SIDELOAD, WIPE_DATA, WIPE_CACHE,
71a9ad0324d1e351b1ef460972a871bcb03051cd6aNick Kralevich                         REBOOT_BOOTLOADER, SHUTDOWN, READ_RECOVERY_LASTLOG };
72daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
73daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Perform a recovery action selected from the menu.
74daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // 'menu_position' will be the item number of the selected menu
75daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // item, or a non-negative number returned from
76daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // device_handle_key().  The menu will be hidden when this is
77daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // called; implementations can call ui_print() to print
78daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // information to the screen.  If the menu position is one of the
79daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // builtin actions, you can just return the corresponding enum
80daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // value.  If it is an action specific to your device, you
81daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // actually perform it here and return NO_ACTION.
82daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual BuiltinAction InvokeMenuItem(int menu_position) = 0;
83daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
84daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    static const int kNoAction = -1;
85daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    static const int kHighlightUp = -2;
86daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    static const int kHighlightDown = -3;
87daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    static const int kInvokeItem = -4;
88daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
89daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Called when we do a wipe data/factory reset operation (either via a
90daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // reboot from the main system with the --wipe_data flag, or when the
91daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // user boots into recovery manually and selects the option from the
92daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // menu.)  Can perform whatever device-specific wiping actions are
93daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // needed.  Return 0 on success.  The userdata and cache partitions
94daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // are erased AFTER this returns (whether it returns success or not).
95daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual int WipeData() { return 0; }
96daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
97daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Return the headers (an array of strings, one per line,
98daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // NULL-terminated) for the main menu.  Typically these tell users
99daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // what to push to move the selection and invoke the selected
100daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // item.
101daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual const char* const* GetMenuHeaders() = 0;
102daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
103daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // Return the list of menu items (an array of strings,
104daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // NULL-terminated).  The menu_position passed to InvokeMenuItem
105daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    // will correspond to the indexes into this array.
106daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker    virtual const char* const* GetMenuItems() = 0;
107daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker};
108daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
109daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker// The device-specific library must define this function (or the
110daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker// default one will be used, if there is no device-specific library).
111daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker// It returns the Device object that recovery should use.
112daefc1d442fb421606680feb9aeb59c133f4c427Doug ZongkerDevice* make_device();
113daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker
114daefc1d442fb421606680feb9aeb59c133f4c427Doug Zongker#endif  // _DEVICE_H
115