No Description

wear_ui.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2014 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. #include "wear_ui.h"
  17. #include <pthread.h>
  18. #include <stdio.h> // TODO: Remove after killing the call to sprintf().
  19. #include <string.h>
  20. #include <string>
  21. #include <android-base/properties.h>
  22. #include <android-base/strings.h>
  23. #include <minui/minui.h>
  24. WearRecoveryUI::WearRecoveryUI()
  25. : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE),
  26. kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) {
  27. // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked().
  28. // TODO: The following three variables are likely not needed. The first two are detected
  29. // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device.
  30. intro_frames = 22;
  31. loop_frames = 60;
  32. touch_screen_allowed_ = true;
  33. }
  34. int WearRecoveryUI::GetProgressBaseline() const {
  35. return kProgressBarBaseline;
  36. }
  37. // Draw background frame on the screen. Does not flip pages.
  38. // Should only be called with updateMutex locked.
  39. // TODO merge drawing routines with screen_ui
  40. void WearRecoveryUI::draw_background_locked() {
  41. pagesIdentical = false;
  42. gr_color(0, 0, 0, 255);
  43. gr_fill(0, 0, gr_fb_width(), gr_fb_height());
  44. if (currentIcon != NONE) {
  45. GRSurface* frame = GetCurrentFrame();
  46. int frame_width = gr_get_width(frame);
  47. int frame_height = gr_get_height(frame);
  48. int frame_x = (gr_fb_width() - frame_width) / 2;
  49. int frame_y = (gr_fb_height() - frame_height) / 2;
  50. gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
  51. }
  52. }
  53. static const char* SWIPE_HELP[] = {
  54. "Swipe up/down to move.",
  55. "Swipe left/right to select.",
  56. "",
  57. NULL
  58. };
  59. // TODO merge drawing routines with screen_ui
  60. void WearRecoveryUI::draw_screen_locked() {
  61. char cur_selection_str[50];
  62. int y = kMarginHeight;
  63. draw_background_locked();
  64. if (!show_text) {
  65. draw_foreground_locked(y);
  66. } else {
  67. SetColor(TEXT_FILL);
  68. gr_fill(0, 0, gr_fb_width(), gr_fb_height());
  69. int x = kMarginWidth;
  70. if (show_menu) {
  71. std::string recovery_fingerprint =
  72. android::base::GetProperty("ro.bootimage.build.fingerprint", "");
  73. SetColor(HEADER);
  74. y += DrawTextLine(x + 4, y, "Android Recovery", true);
  75. for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
  76. y += DrawTextLine(x + 4, y, chunk.c_str(), false);
  77. }
  78. // This is actually the help strings.
  79. y += DrawTextLines(x + 4, y, SWIPE_HELP);
  80. SetColor(HEADER);
  81. y += DrawTextLines(x + 4, y, menu_headers_);
  82. // Show the current menu item number in relation to total number if
  83. // items don't fit on the screen.
  84. if ((int)menu_items_.size() > menu_end - menu_start) {
  85. sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, (int)menu_items_.size());
  86. gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
  87. y += char_height_ + 4;
  88. }
  89. // Menu begins here
  90. SetColor(MENU);
  91. for (int i = menu_start; i < menu_end; ++i) {
  92. const ScreenMenuItem& item = menu_items_.at(i);
  93. if (i == menu_sel) {
  94. // draw the highlight bar
  95. SetColor(MENU_SEL_BG);
  96. gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
  97. // white text of selected item
  98. SetColor(MENU_SEL_FG);
  99. if (!item.text().empty()) {
  100. gr_text(gr_sys_font(), x + 4, y, item.text().c_str(), 1);
  101. }
  102. SetColor(MENU);
  103. } else if (!item.text().empty()) {
  104. gr_text(gr_sys_font(), x + 4, y, item.text().c_str(), 0);
  105. }
  106. y += char_height_ + 4;
  107. }
  108. SetColor(MENU);
  109. y += 4;
  110. gr_fill(0, y, gr_fb_width(), y + 2);
  111. y += 4;
  112. }
  113. SetColor(LOG);
  114. // display from the bottom up, until we hit the top of the
  115. // screen, the bottom of the menu, or we've displayed the
  116. // entire text buffer.
  117. int row = text_row_;
  118. size_t count = 0;
  119. for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
  120. ty -= char_height_, ++count) {
  121. gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
  122. --row;
  123. if (row < 0) row = text_rows_ - 1;
  124. }
  125. }
  126. }
  127. // TODO merge drawing routines with screen_ui
  128. void WearRecoveryUI::update_progress_locked() {
  129. draw_screen_locked();
  130. gr_flip();
  131. }
  132. void WearRecoveryUI::SetStage(int /* current */, int /* max */) {}
  133. void WearRecoveryUI::StartMenu(bool is_main,
  134. menu_type_t type,
  135. const char* const* headers,
  136. const MenuItemVector& items,
  137. int initial_selection) {
  138. (void)is_main;
  139. (void)type;
  140. pthread_mutex_lock(&updateMutex);
  141. if (text_rows_ > 0 && text_cols_ > 0) {
  142. menu_headers_ = headers;
  143. menu_.clear();
  144. // "i < text_rows_" is removed from the loop termination condition,
  145. // which is different from the one in ScreenRecoveryUI::StartMenu().
  146. // Because WearRecoveryUI supports scrollable menu, it's fine to have
  147. // more entries than text_rows_. The menu may be truncated otherwise.
  148. // Bug: 23752519
  149. for (auto& item : items) {
  150. menu_items_.push_back(ScreenMenuItem(item));
  151. }
  152. show_menu = true;
  153. menu_sel = initial_selection;
  154. menu_start = 0;
  155. menu_end = text_rows_ - 1 - kMenuUnusableRows;
  156. if ((int)menu_items_.size() <= menu_end) menu_end = (int)menu_items_.size();
  157. update_screen_locked();
  158. }
  159. pthread_mutex_unlock(&updateMutex);
  160. }
  161. int WearRecoveryUI::SelectMenu(int sel) {
  162. int old_sel;
  163. pthread_mutex_lock(&updateMutex);
  164. if (show_menu) {
  165. old_sel = menu_sel;
  166. menu_sel = sel;
  167. if (menu_sel < 0) menu_sel = 0;
  168. if (menu_sel >= (int)menu_items_.size()) menu_sel = (int)menu_items_.size() - 1;
  169. if (menu_sel < menu_start) {
  170. menu_start--;
  171. menu_end--;
  172. } else if (menu_sel >= menu_end && menu_sel < (int)menu_items_.size()) {
  173. menu_end++;
  174. menu_start++;
  175. }
  176. sel = menu_sel;
  177. if (menu_sel != old_sel) update_screen_locked();
  178. }
  179. pthread_mutex_unlock(&updateMutex);
  180. return sel;
  181. }
  182. int WearRecoveryUI::SelectMenu(const Point& point) {
  183. (void)point;
  184. return -1;
  185. }