blob: a1c2cb102d95f24aa88d6b04cbc0ccb90cb89f09 [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
Abhishek Nigamebc02272023-06-13 22:28:43 +000017#include "otautil/paths.h"
Tianjie Xub5108c32018-08-20 13:40:47 -070018#include "recovery_ui/wear_ui.h"
Tao Bao736d59c2017-01-03 10:15:33 -080019
Tao Bao337db142015-08-20 14:52:57 -070020#include <string.h>
Tao Bao337db142015-08-20 14:52:57 -070021
Tao Bao736d59c2017-01-03 10:15:33 -080022#include <string>
Tao Bao1fe1afe2018-05-01 15:56:05 -070023#include <vector>
Tao Bao337db142015-08-20 14:52:57 -070024
Ludvig Hansson510589f2022-11-07 12:39:20 +010025#include <android-base/logging.h>
Tao Bao0ecbd762017-01-16 21:16:58 -080026#include <android-base/properties.h>
Tao Baoee8a96a2017-09-01 11:37:50 -070027#include <android-base/strings.h>
Abhishek Nigamebc02272023-06-13 22:28:43 +000028
Tao Bao0ecbd762017-01-16 21:16:58 -080029#include <minui/minui.h>
30
Tao Bao0bc88de2018-07-31 14:53:16 -070031constexpr int kDefaultProgressBarBaseline = 259;
32constexpr int kDefaultMenuUnusableRows = 9;
Abhishek Nigamebc02272023-06-13 22:28:43 +000033constexpr int kProgressBarVerticalOffsetDp = 72;
34constexpr bool kDefaultIsScreenCircle = true;
Tao Bao0bc88de2018-07-31 14:53:16 -070035
Tao Bao5d2e3bd2017-06-23 22:23:50 -070036WearRecoveryUI::WearRecoveryUI()
Alessandro Astone09caacf2020-03-09 23:17:50 +010037 : ScreenRecoveryUI(),
Tao Bao0bc88de2018-07-31 14:53:16 -070038 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",
Abhishek Nigamebc02272023-06-13 22:28:43 +000041 kDefaultMenuUnusableRows)),
42 is_screen_circle_(android::base::GetBoolProperty("ro.recovery.ui.is_screen_circle",
43 kDefaultIsScreenCircle)) {
Tao Bao0bc88de2018-07-31 14:53:16 -070044 // TODO: menu_unusable_rows_ should be computed based on the lines in draw_screen_locked().
Tao Bao0470cee2017-08-02 17:11:04 -070045 touch_screen_allowed_ = true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070046}
47
Ludvig Hansson510589f2022-11-07 12:39:20 +010048static 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
61enum 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
68static 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
81bool 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
Tao Bao337db142015-08-20 14:52:57 -070092// Draw background frame on the screen. Does not flip pages.
93// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070094// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070095void 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());
Tao Bao337db142015-08-20 14:52:57 -070099
Abhishek Nigamebc02272023-06-13 22:28:43 +0000100 if (current_icon_ == ERROR) {
Tao Baoda409fb2018-10-21 23:36:26 -0700101 const auto& frame = GetCurrentFrame();
Tao Bao79127102017-08-30 15:23:34 -0700102 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);
Abhishek Nigamebc02272023-06-13 22:28:43 +0000107 }
Karl Shaffer633c01b2018-07-19 11:58:54 -0700108
Abhishek Nigamebc02272023-06-13 22:28:43 +0000109 if (current_icon_ != NONE) {
110 // Draw recovery text on screen centered
Tao Baoda409fb2018-10-21 23:36:26 -0700111 const auto& text = GetCurrentText();
Karl Shaffer633c01b2018-07-19 11:58:54 -0700112 int text_x = (ScreenWidth() - gr_get_width(text)) / 2;
Abhishek Nigamebc02272023-06-13 22:28:43 +0000113 int text_y = (ScreenHeight() - gr_get_height(text)) / 2;
Karl Shaffer633c01b2018-07-19 11:58:54 -0700114 gr_color(255, 255, 255, 255);
115 gr_texticon(text_x, text_y, text);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700116 }
Tao Bao337db142015-08-20 14:52:57 -0700117}
118
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700119void WearRecoveryUI::draw_screen_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700120 if (!show_text) {
Abhishek Nigamebc02272023-06-13 22:28:43 +0000121 draw_background_locked();
122 if (is_screen_circle_) {
123 draw_circle_foreground_locked();
124 } else {
125 draw_foreground_locked();
126 }
127 return;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700128 }
Abhishek Nigamebc02272023-06-13 22:28:43 +0000129
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
143void 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
170void 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
211void 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
230int WearRecoveryUI::GetProgressBaseline() const {
231 int progress_height = gr_get_height(progress_frames_[0].get());
232 return (ScreenHeight() - progress_height) / 2 + PixelsFromDp(kProgressBarVerticalOffsetDp);
233}
234
235int 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
244size_t WearRecoveryUI::GetProgressFrameIndex(float fraction) const {
245 return static_cast<size_t>(fraction * (progress_frames_.size() - 1));
Tao Bao337db142015-08-20 14:52:57 -0700246}
247
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700248// TODO merge drawing routines with screen_ui
249void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700250 draw_screen_locked();
251 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700252}
253
Abhishek Nigamebc02272023-06-13 22:28:43 +0000254bool WearRecoveryUI::IsWearable() {
255 return true;
256}
257
Tao Bao99f0d9e2016-10-13 12:46:38 -0700258void WearRecoveryUI::SetStage(int /* current */, int /* max */) {}
Tao Bao337db142015-08-20 14:52:57 -0700259
Tianjie Xub99e6062018-10-16 15:13:09 -0700260std::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 {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700263 if (text_rows_ > 0 && text_cols_ > 0) {
Alessandro Astone660b5fe2020-06-25 19:54:18 +0200264 return std::make_unique<TextMenu>(false, text_cols_ - 1, text_headers, text_items,
265 initial_selection, char_height_, *this);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700266 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700267
268 return nullptr;
Tao Bao93e46ad2018-05-02 14:57:21 -0700269}