blob: ca6b1b1028ba16deb97a0fb068dbfbd0d6f65c6e [file] [log] [blame]
Tao Bao337db142015-08-20 14:52:57 -07001/*
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 Bao736d59c2017-01-03 10:15:33 -080017#include "wear_ui.h"
18
Tao Baocff82692017-08-26 07:56:44 -070019#include <pthread.h>
Tao Baof05e2bc2017-09-05 15:27:41 -070020#include <stdio.h> // TODO: Remove after killing the call to sprintf().
Tao Bao337db142015-08-20 14:52:57 -070021#include <string.h>
Tao Bao337db142015-08-20 14:52:57 -070022
Tao Bao736d59c2017-01-03 10:15:33 -080023#include <string>
Tao Bao337db142015-08-20 14:52:57 -070024
Tao Bao0ecbd762017-01-16 21:16:58 -080025#include <android-base/properties.h>
Tao Baoee8a96a2017-09-01 11:37:50 -070026#include <android-base/strings.h>
Tao Bao0ecbd762017-01-16 21:16:58 -080027#include <minui/minui.h>
28
Tao Bao5d2e3bd2017-06-23 22:23:50 -070029WearRecoveryUI::WearRecoveryUI()
Tao Baoeea3af32017-08-11 13:50:24 -070030 : 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 Bao0470cee2017-08-02 17:11:04 -070033
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 Bao5d2e3bd2017-06-23 22:23:50 -070036 intro_frames = 22;
37 loop_frames = 60;
Tao Bao0470cee2017-08-02 17:11:04 -070038
39 touch_screen_allowed_ = true;
Tao Bao337db142015-08-20 14:52:57 -070040}
41
Tao Bao99b2d772017-06-23 22:47:03 -070042int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070043 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070044}
45
Tao Bao337db142015-08-20 14:52:57 -070046// Draw background frame on the screen. Does not flip pages.
47// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070048// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070049void 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 Bao337db142015-08-20 14:52:57 -070053
Tao Bao5d2e3bd2017-06-23 22:23:50 -070054 if (currentIcon != NONE) {
Tao Bao79127102017-08-30 15:23:34 -070055 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 Bao5d2e3bd2017-06-23 22:23:50 -070061 }
Tao Bao337db142015-08-20 14:52:57 -070062}
63
Tao Baoea78d862017-06-28 14:52:17 -070064static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070065 "Swipe up/down to move.",
66 "Swipe left/right to select.",
67 "",
68 NULL
Tao Bao337db142015-08-20 14:52:57 -070069};
70
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070071// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070072void WearRecoveryUI::draw_screen_locked() {
73 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -070074
Tao Bao5d2e3bd2017-06-23 22:23:50 -070075 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 Bao337db142015-08-20 14:52:57 -070081
Tao Bao0470cee2017-08-02 17:11:04 -070082 int y = kMarginHeight;
83 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070084 if (show_menu) {
85 std::string recovery_fingerprint =
86 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
87 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -070088 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -070089 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -070090 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -070091 }
Tao Bao337db142015-08-20 14:52:57 -070092
Tao Bao5d2e3bd2017-06-23 22:23:50 -070093 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -070094 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -070096 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -070097
Tao Bao5d2e3bd2017-06-23 22:23:50 -070098 // 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 Bao337db142015-08-20 14:52:57 -0700105
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700106 // Menu begins here
107 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700108
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700109 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 Baoe15d7a52017-09-07 13:38:51 -0700117 gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700118 }
119 SetColor(MENU);
120 } else if (menu_[i][0]) {
Tao Baoe15d7a52017-09-07 13:38:51 -0700121 gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0);
Tao Bao337db142015-08-20 14:52:57 -0700122 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700123 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 Bao337db142015-08-20 14:52:57 -0700129 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700130
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 Baocb5524c2017-09-08 21:25:32 -0700136 int row = text_row_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700137 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700138 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700139 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 Bao337db142015-08-20 14:52:57 -0700145}
146
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700147// TODO merge drawing routines with screen_ui
148void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700149 draw_screen_locked();
150 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700151}
152
Tao Bao99f0d9e2016-10-13 12:46:38 -0700153void WearRecoveryUI::SetStage(int /* current */, int /* max */) {}
Tao Bao337db142015-08-20 14:52:57 -0700154
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700155void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700156 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700157 pthread_mutex_lock(&updateMutex);
158 if (text_rows_ > 0 && text_cols_ > 0) {
159 menu_headers_ = headers;
Tao Baoe15d7a52017-09-07 13:38:51 -0700160 menu_.clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700161 // "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 Baoe15d7a52017-09-07 13:38:51 -0700166 for (size_t i = 0; items[i] != nullptr; i++) {
167 menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
Tao Bao337db142015-08-20 14:52:57 -0700168 }
Tao Baoe15d7a52017-09-07 13:38:51 -0700169 menu_items = static_cast<int>(menu_.size());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700170 show_menu = true;
171 menu_sel = initial_selection;
172 menu_start = 0;
Tao Bao1e27d142017-08-26 08:32:29 -0700173 menu_end = text_rows_ - 1 - kMenuUnusableRows;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700174 if (menu_items <= menu_end) menu_end = menu_items;
175 update_screen_locked();
176 }
177 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700178}
179
180int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700181 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 Bao337db142015-08-20 14:52:57 -0700194 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700195 sel = menu_sel;
196 if (menu_sel != old_sel) update_screen_locked();
197 }
198 pthread_mutex_unlock(&updateMutex);
199 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700200}