• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "wear_ui.h"
18 
19 #include <pthread.h>
20 #include <stdio.h>  // TODO: Remove after killing the call to sprintf().
21 #include <string.h>
22 
23 #include <string>
24 
25 #include <android-base/properties.h>
26 #include <android-base/strings.h>
27 #include <minui/minui.h>
28 
WearRecoveryUI()29 WearRecoveryUI::WearRecoveryUI()
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().
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.
36   intro_frames = 22;
37   loop_frames = 60;
38 
39   touch_screen_allowed_ = true;
40 }
41 
GetProgressBaseline() const42 int WearRecoveryUI::GetProgressBaseline() const {
43   return kProgressBarBaseline;
44 }
45 
46 // Draw background frame on the screen.  Does not flip pages.
47 // Should only be called with updateMutex locked.
48 // TODO merge drawing routines with screen_ui
draw_background_locked()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());
53 
54   if (currentIcon != NONE) {
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);
61   }
62 }
63 
64 static const char* SWIPE_HELP[] = {
65   "Swipe up/down to move.",
66   "Swipe left/right to select.",
67   "",
68   NULL
69 };
70 
71 // TODO merge drawing routines with screen_ui
draw_screen_locked()72 void WearRecoveryUI::draw_screen_locked() {
73   char cur_selection_str[50];
74 
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());
81 
82     int y = kMarginHeight;
83     int x = kMarginWidth;
84     if (show_menu) {
85       std::string recovery_fingerprint =
86           android::base::GetProperty("ro.bootimage.build.fingerprint", "");
87       SetColor(HEADER);
88       y += DrawTextLine(x + 4, y, "Android Recovery", true);
89       for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
90         y += DrawTextLine(x + 4, y, chunk.c_str(), false);
91       }
92 
93       // This is actually the help strings.
94       y += DrawTextLines(x + 4, y, SWIPE_HELP);
95       SetColor(HEADER);
96       y += DrawTextLines(x + 4, y, menu_headers_);
97 
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       }
105 
106       // Menu begins here
107       SetColor(MENU);
108 
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]) {
117             gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1);
118           }
119           SetColor(MENU);
120         } else if (menu_[i][0]) {
121           gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0);
122         }
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;
129     }
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.
136     int row = text_row_;
137     size_t count = 0;
138     for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
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   }
145 }
146 
147 // TODO merge drawing routines with screen_ui
update_progress_locked()148 void WearRecoveryUI::update_progress_locked() {
149   draw_screen_locked();
150   gr_flip();
151 }
152 
SetStage(int,int)153 void WearRecoveryUI::SetStage(int /* current */, int /* max */) {}
154 
StartMenu(const char * const * headers,const char * const * items,int initial_selection)155 void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
156                                int initial_selection) {
157   pthread_mutex_lock(&updateMutex);
158   if (text_rows_ > 0 && text_cols_ > 0) {
159     menu_headers_ = headers;
160     menu_.clear();
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
166     for (size_t i = 0; items[i] != nullptr; i++) {
167       menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
168     }
169     menu_items = static_cast<int>(menu_.size());
170     show_menu = true;
171     menu_sel = initial_selection;
172     menu_start = 0;
173     menu_end = text_rows_ - 1 - kMenuUnusableRows;
174     if (menu_items <= menu_end) menu_end = menu_items;
175     update_screen_locked();
176   }
177   pthread_mutex_unlock(&updateMutex);
178 }
179 
SelectMenu(int sel)180 int WearRecoveryUI::SelectMenu(int sel) {
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++;
194     }
195     sel = menu_sel;
196     if (menu_sel != old_sel) update_screen_locked();
197   }
198   pthread_mutex_unlock(&updateMutex);
199   return sel;
200 }
201