No Description

ui.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #ifndef RECOVERY_UI_H
  17. #define RECOVERY_UI_H
  18. #include <linux/input.h>
  19. #include <pthread.h>
  20. #include <time.h>
  21. #include <string>
  22. #include <vector>
  23. enum menu_type_t { MT_NONE, MT_LIST, MT_GRID };
  24. class MenuItem {
  25. public:
  26. MenuItem() {}
  27. explicit MenuItem(const std::string& text,
  28. const std::string& icon_name = "",
  29. const std::string& icon_name_sel = "") :
  30. text_(text),
  31. icon_name_(icon_name),
  32. icon_name_sel_(icon_name_sel) {}
  33. const std::string& text() const { return text_; }
  34. const std::string& icon_name() const { return icon_name_; }
  35. const std::string& icon_name_sel() const { return icon_name_sel_; }
  36. private:
  37. std::string text_;
  38. std::string icon_name_;
  39. std::string icon_name_sel_;
  40. };
  41. typedef std::vector<MenuItem> MenuItemVector;
  42. /*
  43. * Simple representation of a (x,y) coordinate with convenience operators
  44. */
  45. class Point {
  46. public:
  47. Point() : x_(0), y_(0) {}
  48. Point(int x, int y) : x_(x), y_(y) {}
  49. int x() const { return x_; }
  50. int y() const { return y_; }
  51. void x(int x) { x_ = x; }
  52. void y(int y) { y_ = y; }
  53. bool operator==(const Point& rhs) const {
  54. return (x() == rhs.x() && y() == rhs.y());
  55. }
  56. bool operator!=(const Point& rhs) const {
  57. return !(*this == rhs);
  58. }
  59. Point operator+(const Point& rhs) const {
  60. Point tmp;
  61. tmp.x_ = x_ + rhs.x_;
  62. tmp.y_ = y_ + rhs.y_;
  63. return tmp;
  64. }
  65. Point operator-(const Point& rhs) const {
  66. Point tmp;
  67. tmp.x_ = x_ - rhs.x_;
  68. tmp.y_ = y_ - rhs.y_;
  69. return tmp;
  70. }
  71. private:
  72. int x_;
  73. int y_;
  74. };
  75. // Abstract class for controlling the user interface during recovery.
  76. class RecoveryUI {
  77. public:
  78. enum Icon {
  79. NONE,
  80. INSTALLING_UPDATE,
  81. ERASING,
  82. NO_COMMAND,
  83. ERROR
  84. };
  85. enum ProgressType {
  86. EMPTY,
  87. INDETERMINATE,
  88. DETERMINATE
  89. };
  90. enum KeyAction {
  91. ENQUEUE,
  92. TOGGLE,
  93. REBOOT,
  94. IGNORE
  95. };
  96. RecoveryUI();
  97. virtual ~RecoveryUI() {}
  98. // Initializes the object; called before anything else. UI texts will be initialized according to
  99. // the given locale. Returns true on success.
  100. virtual bool Init(const std::string& locale);
  101. virtual void Stop();
  102. // Shows a stage indicator. Called immediately after Init().
  103. virtual void SetStage(int current, int max) = 0;
  104. // Sets the overall recovery state ("background image").
  105. virtual void SetBackground(Icon icon) = 0;
  106. virtual void SetSystemUpdateText(bool security_update) = 0;
  107. // --- progress indicator ---
  108. virtual void SetProgressType(ProgressType determinate) = 0;
  109. // Shows a progress bar and define the scope of the next operation:
  110. // portion - fraction of the progress bar the next operation will use
  111. // seconds - expected time interval (progress bar moves at this minimum rate)
  112. virtual void ShowProgress(float portion, float seconds) = 0;
  113. // Sets progress bar position (0.0 - 1.0 within the scope defined by the last call to
  114. // ShowProgress).
  115. virtual void SetProgress(float fraction) = 0;
  116. // --- text log ---
  117. virtual void ShowText(bool visible) = 0;
  118. virtual bool IsTextVisible() = 0;
  119. virtual bool WasTextEverVisible() = 0;
  120. // Writes a message to the on-screen log (shown if the user has toggled on the text display).
  121. // Print() will also dump the message to stdout / log file, while PrintOnScreenOnly() not.
  122. virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0;
  123. virtual void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3) = 0;
  124. virtual int ShowFile(const char* filename) = 0;
  125. virtual void Redraw() = 0;
  126. // --- event handling ---
  127. enum event_type_t { EVENT_TYPE_NONE, EVENT_TYPE_KEY, EVENT_TYPE_TOUCH };
  128. class InputEvent {
  129. public:
  130. InputEvent() :
  131. type_(EVENT_TYPE_NONE),
  132. evt_({0}) {}
  133. explicit InputEvent(int key) :
  134. type_(EVENT_TYPE_KEY),
  135. evt_({key}) {}
  136. explicit InputEvent(const Point& pos) :
  137. type_(EVENT_TYPE_TOUCH),
  138. evt_({0}) { evt_.pos = pos; }
  139. event_type_t type() const { return type_; }
  140. int key() const { return evt_.key; }
  141. const Point& pos() const { return evt_.pos; }
  142. private:
  143. event_type_t type_;
  144. union {
  145. int key;
  146. Point pos;
  147. } evt_;
  148. };
  149. // Waits for a key and return it. May return -1 after timeout.
  150. virtual InputEvent WaitInputEvent();
  151. // Cancel a WaitKey()
  152. virtual void CancelWaitKey();
  153. virtual bool IsKeyPressed(int key);
  154. virtual bool IsLongPress();
  155. // Returns true if you have the volume up/down and power trio typical of phones and tablets, false
  156. // otherwise.
  157. virtual bool HasThreeButtons();
  158. // Returns true if it has a power key.
  159. virtual bool HasPowerKey() const;
  160. // Returns true if it supports touch inputs.
  161. virtual bool HasTouchScreen() const;
  162. // Erases any queued-up keys.
  163. virtual void FlushKeys();
  164. // Called on each key press, even while operations are in progress. Return value indicates whether
  165. // an immediate operation should be triggered (toggling the display, rebooting the device), or if
  166. // the key should be enqueued for use by the main thread.
  167. virtual KeyAction CheckKey(int key, bool is_long_press);
  168. // Called when a key is held down long enough to have been a long-press (but before the key is
  169. // released). This means that if the key is eventually registered (released without any other keys
  170. // being pressed in the meantime), CheckKey will be called with 'is_long_press' true.
  171. virtual void KeyLongPress(int key);
  172. // Normally in recovery there's a key sequence that triggers immediate reboot of the device,
  173. // regardless of what recovery is doing (with the default CheckKey implementation, it's pressing
  174. // the power button 7 times in row). Call this to enable or disable that feature. It is enabled by
  175. // default.
  176. virtual void SetEnableReboot(bool enabled);
  177. // --- menu display ---
  178. // Display some header text followed by a menu of items, which appears at the top of the screen
  179. // (in place of any scrolling ui_print() output, if necessary).
  180. virtual void StartMenu(bool is_main,
  181. menu_type_t type,
  182. const char* const* headers,
  183. const MenuItemVector& items,
  184. int initial_selection) = 0;
  185. // Sets the menu highlight to the given index, wrapping if necessary. Returns the actual item
  186. // selected.
  187. virtual int SelectMenu(int sel) = 0;
  188. virtual int SelectMenu(const Point& point) = 0;
  189. // Ends menu mode, resetting the text overlay so that ui_print() statements will be displayed.
  190. virtual void EndMenu() = 0;
  191. // Notify of volume state change
  192. void onVolumeChanged() { volumes_changed_ = 1; }
  193. virtual bool MenuShowing() const = 0;
  194. virtual bool MenuScrollable() const = 0;
  195. virtual int MenuItemStart() const = 0;
  196. virtual int MenuItemHeight() const = 0;
  197. protected:
  198. void EnqueueKey(int key_code);
  199. void EnqueueTouch(const Point& pos);
  200. std::string android_version_;
  201. std::string lineage_version_;
  202. // The normal and dimmed brightness percentages (default: 50 and 25, which means 50% and 25% of
  203. // the max_brightness). Because the absolute values may vary across devices. These two values can
  204. // be configured via subclassing. Setting brightness_normal_ to 0 to disable screensaver.
  205. unsigned int brightness_normal_;
  206. unsigned int brightness_dimmed_;
  207. std::string brightness_file_;
  208. std::string max_brightness_file_;
  209. // Whether we should listen for touch inputs (default: true).
  210. bool touch_screen_allowed_;
  211. private:
  212. enum class ScreensaverState {
  213. DISABLED,
  214. NORMAL,
  215. DIMMED,
  216. OFF
  217. };
  218. struct key_timer_t {
  219. RecoveryUI* ui;
  220. int key_code;
  221. int count;
  222. };
  223. // The sensitivity when detecting a swipe.
  224. const int kTouchLowThreshold;
  225. const int kTouchHighThreshold;
  226. void OnTouchDeviceDetected(int fd);
  227. void OnKeyDetected(int key_code);
  228. void OnTouchPress();
  229. void OnTouchTrack();
  230. void OnTouchRelease();
  231. int OnInputEvent(int fd, uint32_t epevents);
  232. void ProcessKey(int key_code, int updown);
  233. bool IsUsbConnected();
  234. bool VolumesChanged();
  235. static void* time_key_helper(void* cookie);
  236. void time_key(int key_code, int count);
  237. bool InitScreensaver();
  238. // Key event input queue
  239. pthread_mutex_t event_queue_mutex;
  240. pthread_cond_t event_queue_cond;
  241. InputEvent event_queue[256];
  242. int event_queue_len;
  243. char key_pressed[KEY_MAX + 1]; // under event_queue_mutex
  244. int key_last_down; // under event_queue_mutex
  245. bool key_long_press; // under event_queue_mutex
  246. int key_down_count; // under event_queue_mutex
  247. bool enable_reboot; // under event_queue_mutex
  248. int rel_sum;
  249. int consecutive_power_keys;
  250. int last_key;
  251. bool has_power_key;
  252. bool has_up_key;
  253. bool has_down_key;
  254. bool has_touch_screen;
  255. struct vkey_t {
  256. bool inside (const Point& p) const {
  257. return (p.x() >= min_.x() && p.x() < max_.x() &&
  258. p.y() >= min_.y() && p.y() < max_.y());
  259. }
  260. int keycode;
  261. Point min_;
  262. Point max_;
  263. };
  264. // Touch event related variables. See the comments in RecoveryUI::OnInputEvent().
  265. int touch_slot_;
  266. bool touch_finger_down_;
  267. bool touch_saw_x_;
  268. bool touch_saw_y_;
  269. bool touch_reported_;
  270. Point touch_pos_;
  271. Point touch_start_;
  272. Point touch_track_;
  273. std::vector<vkey_t> virtual_keys_;
  274. bool is_bootreason_recovery_ui_;
  275. pthread_t input_thread_;
  276. bool volumes_changed_;
  277. ScreensaverState screensaver_state_;
  278. // The following two contain the absolute values computed from brightness_normal_ and
  279. // brightness_dimmed_ respectively.
  280. unsigned int brightness_normal_value_;
  281. unsigned int brightness_dimmed_value_;
  282. };
  283. #endif // RECOVERY_UI_H