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 | |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 17 | #include "otautil/paths.h" |
Tianjie Xu | b5108c3 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 18 | #include "recovery_ui/wear_ui.h" |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 19 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 20 | #include <string.h> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 21 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 22 | #include <string> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 23 | #include <vector> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 24 | |
Ludvig Hansson | 510589f | 2022-11-07 12:39:20 +0100 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Tao Bao | ee8a96a | 2017-09-01 11:37:50 -0700 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 28 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 29 | #include <minui/minui.h> |
| 30 | |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 31 | constexpr int kDefaultProgressBarBaseline = 259; |
| 32 | constexpr int kDefaultMenuUnusableRows = 9; |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 33 | constexpr int kProgressBarVerticalOffsetDp = 72; |
| 34 | constexpr bool kDefaultIsScreenCircle = true; |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 35 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 36 | WearRecoveryUI::WearRecoveryUI() |
Alessandro Astone | 09caacf | 2020-03-09 23:17:50 +0100 | [diff] [blame] | 37 | : ScreenRecoveryUI(), |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 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", |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 41 | kDefaultMenuUnusableRows)), |
| 42 | is_screen_circle_(android::base::GetBoolProperty("ro.recovery.ui.is_screen_circle", |
| 43 | kDefaultIsScreenCircle)) { |
Tao Bao | 0bc88de | 2018-07-31 14:53:16 -0700 | [diff] [blame] | 44 | // TODO: menu_unusable_rows_ should be computed based on the lines in draw_screen_locked(). |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 45 | touch_screen_allowed_ = true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ludvig Hansson | 510589f | 2022-11-07 12:39:20 +0100 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 92 | // Draw background frame on the screen. Does not flip pages. |
| 93 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 94 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 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()); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 99 | |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 100 | if (current_icon_ == ERROR) { |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 101 | const auto& frame = GetCurrentFrame(); |
Tao Bao | 7912710 | 2017-08-30 15:23:34 -0700 | [diff] [blame] | 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); |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 107 | } |
Karl Shaffer | 633c01b | 2018-07-19 11:58:54 -0700 | [diff] [blame] | 108 | |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 109 | if (current_icon_ != NONE) { |
| 110 | // Draw recovery text on screen centered |
Tao Bao | da409fb | 2018-10-21 23:36:26 -0700 | [diff] [blame] | 111 | const auto& text = GetCurrentText(); |
Karl Shaffer | 633c01b | 2018-07-19 11:58:54 -0700 | [diff] [blame] | 112 | int text_x = (ScreenWidth() - gr_get_width(text)) / 2; |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 113 | int text_y = (ScreenHeight() - gr_get_height(text)) / 2; |
Karl Shaffer | 633c01b | 2018-07-19 11:58:54 -0700 | [diff] [blame] | 114 | gr_color(255, 255, 255, 255); |
| 115 | gr_texticon(text_x, text_y, text); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 116 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 119 | void WearRecoveryUI::draw_screen_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 120 | if (!show_text) { |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 121 | draw_background_locked(); |
| 122 | if (is_screen_circle_) { |
| 123 | draw_circle_foreground_locked(); |
| 124 | } else { |
| 125 | draw_foreground_locked(); |
| 126 | } |
| 127 | return; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 128 | } |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
| 230 | 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 | |
| 235 | 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 | |
| 244 | size_t WearRecoveryUI::GetProgressFrameIndex(float fraction) const { |
| 245 | return static_cast<size_t>(fraction * (progress_frames_.size() - 1)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 248 | // TODO merge drawing routines with screen_ui |
| 249 | void WearRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 250 | draw_screen_locked(); |
| 251 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Abhishek Nigam | ebc0227 | 2023-06-13 22:28:43 +0000 | [diff] [blame] | 254 | bool WearRecoveryUI::IsWearable() { |
| 255 | return true; |
| 256 | } |
| 257 | |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 258 | void WearRecoveryUI::SetStage(int /* current */, int /* max */) {} |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 259 | |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 260 | 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 { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 263 | if (text_rows_ > 0 && text_cols_ > 0) { |
Alessandro Astone | 660b5fe | 2020-06-25 19:54:18 +0200 | [diff] [blame] | 264 | return std::make_unique<TextMenu>(false, text_cols_ - 1, text_headers, text_items, |
| 265 | initial_selection, char_height_, *this); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 266 | } |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 267 | |
| 268 | return nullptr; |
Tao Bao | 93e46ad | 2018-05-02 14:57:21 -0700 | [diff] [blame] | 269 | } |