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 "otautil/paths.h"
18 #include "recovery_ui/wear_ui.h"
19
20 #include <string.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android-base/strings.h>
28
29 #include <minui/minui.h>
30
31 constexpr int kDefaultProgressBarBaseline = 259;
32 constexpr int kDefaultMenuUnusableRows = 9;
33 constexpr int kProgressBarVerticalOffsetDp = 72;
34 constexpr bool kDefaultIsScreenCircle = true;
35
WearRecoveryUI()36 WearRecoveryUI::WearRecoveryUI()
37 : ScreenRecoveryUI(true),
38 progress_bar_baseline_(android::base::GetIntProperty("ro.recovery.ui.progress_bar_baseline",
39 kDefaultProgressBarBaseline)),
40 menu_unusable_rows_(android::base::GetIntProperty("ro.recovery.ui.menu_unusable_rows",
41 kDefaultMenuUnusableRows)),
42 is_screen_circle_(android::base::GetBoolProperty("ro.recovery.ui.is_screen_circle",
43 kDefaultIsScreenCircle)) {
44 // TODO: menu_unusable_rows_ should be computed based on the lines in draw_screen_locked().
45 touch_screen_allowed_ = true;
46 }
47
FlipOrientation()48 static void FlipOrientation() {
49 auto rotation = gr_get_rotation();
50 if (rotation == GRRotation::NONE) {
51 gr_rotate(GRRotation::DOWN);
52 } else if (rotation == GRRotation::DOWN) {
53 gr_rotate(GRRotation::NONE);
54 } else {
55 LOG(WARNING) << "Unsupported rotation for wrist orientation" << static_cast<int>(rotation);
56 }
57 }
58
59 // Match values in
60 // frameworks/opt/wear/src/com/android/clockwork/wristorientation/WristOrientationService.java
61 enum class WristOrientation : unsigned {
62 LEFT_WRIST_ROTATION_0 = 0,
63 LEFT_WRIST_ROTATION_180 = 1,
64 RIGHT_WRIST_ROTATION_0 = 2,
65 RIGHT_WRIST_ROTATION_180 = 3,
66 };
67
InitWristOrientation()68 static void InitWristOrientation() {
69 auto prop = android::base::GetUintProperty("ro.boot.wrist_orientation", 0u);
70 WristOrientation orientation{ prop };
71 if (orientation == WristOrientation::LEFT_WRIST_ROTATION_180 ||
72 orientation == WristOrientation::RIGHT_WRIST_ROTATION_180) {
73 LOG(INFO)
74 << "InitWristOrientation(): flipping orientation because, 'ro.boot.wrist_orientation'="
75 << prop;
76
77 FlipOrientation();
78 }
79 }
80
Init(const std::string & locale)81 bool WearRecoveryUI::Init(const std::string& locale) {
82 auto result = ScreenRecoveryUI::Init(locale);
83 auto wrist_orientation_enabled =
84 android::base::GetBoolProperty("config.enable_wristorientation", false);
85 LOG(INFO) << "WearRecoveryUI::Init(): enable_wristorientation=" << wrist_orientation_enabled;
86 if (wrist_orientation_enabled) {
87 InitWristOrientation();
88 }
89 return result;
90 }
91
92 // Draw background frame on the screen. Does not flip pages.
93 // Should only be called with updateMutex locked.
94 // TODO merge drawing routines with screen_ui
draw_background_locked()95 void WearRecoveryUI::draw_background_locked() {
96 pagesIdentical = false;
97 gr_color(0, 0, 0, 255);
98 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
99
100 if (current_icon_ == ERROR) {
101 const auto& frame = GetCurrentFrame();
102 int frame_width = gr_get_width(frame);
103 int frame_height = gr_get_height(frame);
104 int frame_x = (gr_fb_width() - frame_width) / 2;
105 int frame_y = (gr_fb_height() - frame_height) / 2;
106 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
107 }
108
109 if (current_icon_ != NONE) {
110 // Draw recovery text on screen centered
111 const auto& text = GetCurrentText();
112 int text_x = (ScreenWidth() - gr_get_width(text)) / 2;
113 int text_y = (ScreenHeight() - gr_get_height(text)) / 2;
114 gr_color(255, 255, 255, 255);
115 gr_texticon(text_x, text_y, text);
116 }
117 }
118
draw_screen_locked()119 void WearRecoveryUI::draw_screen_locked() {
120 if (!show_text) {
121 draw_background_locked();
122 if (is_screen_circle_) {
123 draw_circle_foreground_locked();
124 } else {
125 draw_foreground_locked();
126 }
127 return;
128 }
129
130 SetColor(UIElement::TEXT_FILL);
131 gr_clear();
132
133 // clang-format off
134 static std::vector<std::string> SWIPE_HELP = {
135 "Swipe up/down to move.",
136 "Swipe left/right to select.",
137 "",
138 };
139 // clang-format on
140 draw_menu_and_text_buffer_locked(SWIPE_HELP);
141 }
142
draw_circle_foreground_locked()143 void WearRecoveryUI::draw_circle_foreground_locked() {
144 if (current_icon_ != NONE) {
145 const auto& frame = GetCurrentFrame();
146 int frame_width = gr_get_width(frame);
147 int frame_height = gr_get_height(frame);
148 int frame_x = (ScreenWidth() - frame_width) / 2;
149 int frame_y = GetAnimationBaseline();
150 DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
151 }
152
153 if (progressBarType == DETERMINATE) {
154 const auto& first_progress_frame = rtl_locale_ ? rtl_progress_frames_[0].get()
155 :progress_frames_[0].get();
156 int width = gr_get_width(first_progress_frame);
157 int height = gr_get_height(first_progress_frame);
158
159 int progress_x = (ScreenWidth() - width) / 2;
160 int progress_y = GetProgressBaseline();
161
162 const auto index = GetProgressFrameIndex(progress);
163 const auto& frame = rtl_locale_ ? rtl_progress_frames_[index].get()
164 : progress_frames_[index].get();
165
166 DrawSurface(frame, 0, 0, width, height, progress_x, progress_y);
167 }
168 }
169
LoadAnimation()170 void WearRecoveryUI::LoadAnimation() {
171 ScreenRecoveryUI::LoadAnimation();
172 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(Paths::Get().resource_dir().c_str()),
173 closedir);
174 dirent* de;
175 std::vector<std::string> progress_frame_names;
176 std::vector<std::string> rtl_progress_frame_names;
177
178 if(dir.get() == nullptr) abort();
179
180 while ((de = readdir(dir.get())) != nullptr) {
181 int value, num_chars;
182 if (sscanf(de->d_name, "progress%d%n.png", &value, &num_chars) == 1) {
183 progress_frame_names.emplace_back(de->d_name, num_chars);
184 } else if (sscanf(de->d_name, "rtl_progress%d%n.png", &value, &num_chars) == 1) {
185 rtl_progress_frame_names.emplace_back(de->d_name, num_chars);
186 }
187 }
188
189 size_t progress_frames = progress_frame_names.size();
190 size_t rtl_progress_frames = rtl_progress_frame_names.size();
191
192 // You must have an animation.
193 if (progress_frames == 0 || rtl_progress_frames == 0) abort();
194
195 std::sort(progress_frame_names.begin(), progress_frame_names.end());
196 std::sort(rtl_progress_frame_names.begin(), rtl_progress_frame_names.end());
197
198 progress_frames_.clear();
199 progress_frames_.reserve(progress_frames);
200 for (const auto& frame_name : progress_frame_names) {
201 progress_frames_.emplace_back(LoadBitmap(frame_name));
202 }
203
204 rtl_progress_frames_.clear();
205 rtl_progress_frames_.reserve(rtl_progress_frames);
206 for (const auto& frame_name : rtl_progress_frame_names) {
207 rtl_progress_frames_.emplace_back(LoadBitmap(frame_name));
208 }
209 }
210
SetProgress(float fraction)211 void WearRecoveryUI::SetProgress(float fraction) {
212 if (is_screen_circle_) {
213 std::lock_guard<std::mutex> lg(updateMutex);
214 if (fraction < 0.0) fraction = 0.0;
215 if (fraction > 1.0) fraction = 1.0;
216 if (progressBarType == DETERMINATE && fraction > progress) {
217 // Skip updates that aren't visibly different.
218 if (GetProgressFrameIndex(fraction) != GetProgressFrameIndex(progress)) {
219 // circular display
220 progress = fraction;
221 update_progress_locked();
222 }
223 }
224 } else {
225 // rectangular display
226 ScreenRecoveryUI::SetProgress(fraction);
227 }
228 }
229
GetProgressBaseline() const230 int WearRecoveryUI::GetProgressBaseline() const {
231 int progress_height = gr_get_height(progress_frames_[0].get());
232 return (ScreenHeight() - progress_height) / 2 + PixelsFromDp(kProgressBarVerticalOffsetDp);
233 }
234
GetTextBaseline() const235 int WearRecoveryUI::GetTextBaseline() const {
236 if (is_screen_circle_) {
237 return GetProgressBaseline() - PixelsFromDp(kProgressBarVerticalOffsetDp) -
238 gr_get_height(installing_text_.get());
239 } else {
240 return ScreenRecoveryUI::GetTextBaseline();
241 }
242 }
243
GetProgressFrameIndex(float fraction) const244 size_t WearRecoveryUI::GetProgressFrameIndex(float fraction) const {
245 return static_cast<size_t>(fraction * (progress_frames_.size() - 1));
246 }
247
248 // TODO merge drawing routines with screen_ui
update_progress_locked()249 void WearRecoveryUI::update_progress_locked() {
250 draw_screen_locked();
251 gr_flip();
252 }
253
IsWearable()254 bool WearRecoveryUI::IsWearable() {
255 return true;
256 }
257
SetStage(int,int)258 void WearRecoveryUI::SetStage(int /* current */, int /* max */) {}
259
CreateMenu(const std::vector<std::string> & text_headers,const std::vector<std::string> & text_items,size_t initial_selection) const260 std::unique_ptr<Menu> WearRecoveryUI::CreateMenu(const std::vector<std::string>& text_headers,
261 const std::vector<std::string>& text_items,
262 size_t initial_selection) const {
263 if (text_rows_ > 0 && text_cols_ > 0) {
264 return std::make_unique<TextMenu>(scrollable_menu_, text_rows_ - menu_unusable_rows_ - 1,
265 text_cols_ - 1, text_headers, text_items, initial_selection,
266 char_height_, *this);
267 }
268
269 return nullptr;
270 }
271