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