Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 3 | * Copyright (C) 2019 The LineageOS Project |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 18 | #include "screen_ui.h" |
| 19 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 20 | #include <dirent.h> |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <linux/input.h> |
| 24 | #include <pthread.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 35 | #include <memory> |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 36 | #include <string> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 37 | #include <unordered_map> |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 40 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 43 | #include <android-base/strings.h> |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 44 | #include <minui/minui.h> |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 45 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 46 | #include <healthd/BatteryMonitor.h> |
| 47 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 48 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 49 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 50 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 51 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 52 | // Return the current time as a double (including fractions of a second). |
| 53 | static double now() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 54 | struct timeval tv; |
| 55 | gettimeofday(&tv, nullptr); |
| 56 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 59 | static void get_battery_status(bool& charged, int& capacity) { |
| 60 | struct healthd_config healthd_config = { |
| 61 | .batteryStatusPath = android::String8(android::String8::kEmptyString), |
| 62 | .batteryHealthPath = android::String8(android::String8::kEmptyString), |
| 63 | .batteryPresentPath = android::String8(android::String8::kEmptyString), |
| 64 | .batteryCapacityPath = android::String8(android::String8::kEmptyString), |
| 65 | .batteryVoltagePath = android::String8(android::String8::kEmptyString), |
| 66 | .batteryTemperaturePath = android::String8(android::String8::kEmptyString), |
| 67 | .batteryTechnologyPath = android::String8(android::String8::kEmptyString), |
| 68 | .batteryCurrentNowPath = android::String8(android::String8::kEmptyString), |
| 69 | .batteryCurrentAvgPath = android::String8(android::String8::kEmptyString), |
| 70 | .batteryChargeCounterPath = android::String8(android::String8::kEmptyString), |
| 71 | .batteryFullChargePath = android::String8(android::String8::kEmptyString), |
| 72 | .batteryCycleCountPath = android::String8(android::String8::kEmptyString), |
| 73 | .energyCounter = NULL, |
| 74 | .boot_min_cap = 0, |
| 75 | .screen_on = NULL |
| 76 | }; |
| 77 | healthd_board_init(&healthd_config); |
| 78 | |
| 79 | android::BatteryMonitor monitor; |
| 80 | monitor.init(&healthd_config); |
| 81 | |
| 82 | int charge_status = monitor.getChargeStatus(); |
| 83 | // Treat unknown status as charged. |
| 84 | charged = (charge_status != android::BATTERY_STATUS_DISCHARGING && |
| 85 | charge_status != android::BATTERY_STATUS_NOT_CHARGING); |
| 86 | android::BatteryProperty prop; |
| 87 | android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &prop); |
| 88 | // If we can't read battery percentage, it may be a device without battery. In this |
| 89 | // situation, use 100 as a fake battery percentage. |
| 90 | if (status != 0) { |
| 91 | prop.valueInt64 = 100; |
| 92 | } |
| 93 | capacity = (int)prop.valueInt64; |
| 94 | } |
| 95 | |
| 96 | ScreenMenuItem::~ScreenMenuItem() { |
| 97 | if (icon_) { |
| 98 | res_free_surface(icon_); |
| 99 | } |
| 100 | if (icon_sel_) { |
| 101 | res_free_surface(icon_sel_); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | GRSurface* ScreenMenuItem::icon() { |
| 106 | if (!icon_) { |
| 107 | res_create_display_surface(icon_name_.c_str(), &icon_); |
| 108 | } |
| 109 | return icon_; |
| 110 | } |
| 111 | |
| 112 | GRSurface* ScreenMenuItem::icon_sel() { |
| 113 | if (icon_name_sel_.empty()) { |
| 114 | return icon(); |
| 115 | } |
| 116 | if (!icon_sel_) { |
| 117 | res_create_display_surface(icon_name_sel_.c_str(), &icon_sel_); |
| 118 | } |
| 119 | return icon_sel_; |
| 120 | } |
| 121 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 122 | ScreenRecoveryUI::ScreenRecoveryUI() |
Tao Bao | 4521b70 | 2017-06-20 18:11:21 -0700 | [diff] [blame] | 123 | : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH), |
| 124 | kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT), |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 125 | kAnimationFps(RECOVERY_UI_ANIMATION_FPS), |
Tao Bao | 7577965 | 2017-09-10 11:28:32 -0700 | [diff] [blame] | 126 | kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f), |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 127 | currentIcon(NONE), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 128 | progressBarType(EMPTY), |
| 129 | progressScopeStart(0), |
| 130 | progressScopeSize(0), |
| 131 | progress(0), |
| 132 | pagesIdentical(false), |
| 133 | text_cols_(0), |
| 134 | text_rows_(0), |
| 135 | text_(nullptr), |
| 136 | text_col_(0), |
| 137 | text_row_(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 138 | show_text(false), |
| 139 | show_text_ever(false), |
Alessandro Astone | 20c8d41 | 2019-04-01 11:11:33 +0200 | [diff] [blame] | 140 | previous_row_ended(false), |
| 141 | update_screen_on_print(false), |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 142 | menu_is_main_(true), |
| 143 | menu_type_(MT_NONE), |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 144 | menu_headers_(nullptr), |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 145 | menu_start_y_(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 146 | show_menu(false), |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 147 | menu_show_start(0), |
| 148 | menu_show_count(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 149 | menu_sel(0), |
| 150 | file_viewer_text_(nullptr), |
| 151 | intro_frames(0), |
| 152 | loop_frames(0), |
| 153 | current_frame(0), |
| 154 | intro_done(false), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 155 | stage(-1), |
| 156 | max_stage(-1), |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 157 | locale_(""), |
| 158 | rtl_locale_(false), |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 159 | updateMutex(PTHREAD_MUTEX_INITIALIZER), |
| 160 | rainbow(false), |
| 161 | wrap_count(0) {} |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 162 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 163 | GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 164 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
| 165 | return intro_done ? loopFrames[current_frame] : introFrames[current_frame]; |
| 166 | } |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 167 | return nullptr; |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 170 | GRSurface* ScreenRecoveryUI::GetCurrentText() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 171 | switch (currentIcon) { |
| 172 | case ERASING: |
| 173 | return erasing_text; |
| 174 | case ERROR: |
| 175 | return error_text; |
| 176 | case INSTALLING_UPDATE: |
| 177 | return installing_text; |
| 178 | case NO_COMMAND: |
| 179 | return no_command_text; |
| 180 | case NONE: |
| 181 | abort(); |
| 182 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 185 | int ScreenRecoveryUI::PixelsFromDp(int dp) const { |
Tao Bao | 7577965 | 2017-09-10 11:28:32 -0700 | [diff] [blame] | 186 | return dp * kDensity; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Here's the intended layout: |
| 190 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 191 | // | portrait large landscape large |
| 192 | // ---------+------------------------------------------------- |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 193 | // gap | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 194 | // icon | (200dp) |
| 195 | // gap | 68dp 68dp 56dp 112dp |
| 196 | // text | (14sp) |
| 197 | // gap | 32dp 32dp 26dp 52dp |
| 198 | // progress | (2dp) |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 199 | // gap | |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 200 | |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 201 | // Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines |
| 202 | // work), so that's the more useful measurement for calling code. We use even top and bottom gaps. |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 203 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 204 | enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 205 | enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 206 | static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 207 | { 32, 68, }, // PORTRAIT |
| 208 | { 32, 68, }, // PORTRAIT_LARGE |
| 209 | { 26, 56, }, // LANDSCAPE |
| 210 | { 52, 112, }, // LANDSCAPE_LARGE |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 213 | int ScreenRecoveryUI::GetAnimationBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 214 | return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 217 | int ScreenRecoveryUI::GetTextBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 218 | return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - |
| 219 | gr_get_height(installing_text); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 222 | int ScreenRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 223 | int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) + |
| 224 | gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) + |
| 225 | gr_get_height(progressBarFill); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 226 | int bottom_gap = (ScreenHeight() - elements_sum) / 2; |
| 227 | return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 230 | // Draw the currently selected stage icon(s) (if any). |
| 231 | // Does not flip pages. Should only be called with updateMutex locked. |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 232 | void ScreenRecoveryUI::draw_background_locked() { |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 233 | if (currentIcon != NONE && currentIcon != NO_COMMAND) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 234 | if (max_stage != -1) { |
| 235 | int stage_height = gr_get_height(stageMarkerEmpty); |
| 236 | int stage_width = gr_get_width(stageMarkerEmpty); |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 237 | int stage_x = kMarginWidth + (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
| 238 | int stage_y = kMarginHeight; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 239 | for (int i = 0; i < max_stage; ++i) { |
| 240 | GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty; |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 241 | DrawSurface(stage_surface, 0, 0, stage_width, stage_height, stage_x, stage_y); |
| 242 | stage_x += stage_width; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 243 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 244 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 245 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 248 | // Draws either the animation and progress bar or the currently |
| 249 | // selected icon and text on the screen. |
| 250 | // Does not flip pages. Should only be called with updateMutex locked. |
| 251 | void ScreenRecoveryUI::draw_foreground_locked(int& y) { |
| 252 | GRSurface* frame = GetCurrentFrame(); |
| 253 | if (frame) { |
| 254 | // Show animation frame and progress bar |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 255 | int frame_width = gr_get_width(frame); |
| 256 | int frame_height = gr_get_height(frame); |
Tom Marshall | 84221d8 | 2018-02-26 20:46:24 +0100 | [diff] [blame] | 257 | int frame_x = kMarginWidth + (ScreenWidth() - frame_width) / 2; |
| 258 | int frame_y = kMarginHeight + GetAnimationBaseline(); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 259 | DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 260 | y = frame_y + frame_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 261 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 262 | if (progressBarType != EMPTY) { |
| 263 | int width = gr_get_width(progressBarEmpty); |
| 264 | int height = gr_get_height(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 265 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 266 | int progress_x = kMarginWidth + (ScreenWidth() - width) / 2; |
| 267 | int progress_y = kMarginHeight + GetProgressBaseline(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 268 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 269 | // Erase behind the progress bar (in case this was a progress-only update) |
| 270 | gr_color(0, 0, 0, 255); |
| 271 | DrawFill(progress_x, progress_y, width, height); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 272 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 273 | if (progressBarType == DETERMINATE) { |
| 274 | float p = progressScopeStart + progress * progressScopeSize; |
| 275 | int pos = static_cast<int>(p * width); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 276 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 277 | if (rtl_locale_) { |
| 278 | // Fill the progress bar from right to left. |
| 279 | if (pos > 0) { |
| 280 | DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos, |
| 281 | progress_y); |
| 282 | } |
| 283 | if (pos < width - 1) { |
| 284 | DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y); |
| 285 | } |
| 286 | } else { |
| 287 | // Fill the progress bar from left to right. |
| 288 | if (pos > 0) { |
| 289 | DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y); |
| 290 | } |
| 291 | if (pos < width - 1) { |
| 292 | DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, |
| 293 | progress_y); |
| 294 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 297 | y = progress_y + height; |
| 298 | } |
| 299 | } else { |
| 300 | // Show icon and text |
| 301 | if (currentIcon != NONE && currentIcon != NO_COMMAND) { |
| 302 | GRSurface* icon_surface = error_icon; |
| 303 | int icon_width = gr_get_width(icon_surface); |
| 304 | int icon_height = gr_get_height(icon_surface); |
| 305 | int icon_x = kMarginWidth + (gr_fb_width() - icon_width) / 2; |
| 306 | int icon_y = kMarginHeight + GetAnimationBaseline(); |
| 307 | gr_blit(icon_surface, 0, 0, icon_width, icon_height, icon_x, icon_y); |
| 308 | |
| 309 | GRSurface* text_surface = GetCurrentText(); |
| 310 | int text_width = gr_get_width(text_surface); |
| 311 | int text_height = gr_get_height(text_surface); |
| 312 | int text_x = kMarginWidth + (gr_fb_width() - text_width) / 2; |
| 313 | int text_y = kMarginHeight + GetTextBaseline(); |
| 314 | gr_color(255, 255, 255, 255); |
| 315 | gr_texticon(text_x, text_y, text_surface); |
| 316 | |
| 317 | y = text_y + text_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 318 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 319 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 322 | /* Lineage teal: #167c80 */ |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 323 | void ScreenRecoveryUI::SetColor(UIElement e) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 324 | switch (e) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 325 | case STATUSBAR: |
| 326 | gr_color(255, 255, 255, 255); |
| 327 | break; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 328 | case INFO: |
| 329 | gr_color(249, 194, 0, 255); |
| 330 | break; |
| 331 | case HEADER: |
| 332 | gr_color(247, 0, 6, 255); |
| 333 | break; |
| 334 | case MENU: |
| 335 | case MENU_SEL_BG: |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 336 | gr_color(0xd8, 0xd8, 0xd8, 255); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 337 | break; |
| 338 | case MENU_SEL_BG_ACTIVE: |
Tom Marshall | 5333a06 | 2014-11-24 16:38:37 -0800 | [diff] [blame] | 339 | gr_color(138, 135, 134, 255); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 340 | break; |
| 341 | case MENU_SEL_FG: |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 342 | gr_color(0x16, 0x7c, 0x80, 255); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 343 | break; |
| 344 | case LOG: |
| 345 | gr_color(196, 196, 196, 255); |
| 346 | break; |
| 347 | case TEXT_FILL: |
| 348 | gr_color(0, 0, 0, 160); |
| 349 | break; |
| 350 | default: |
| 351 | gr_color(255, 255, 255, 255); |
| 352 | break; |
| 353 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 354 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 355 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 356 | void ScreenRecoveryUI::SelectAndShowBackgroundText(const std::vector<std::string>& locales_entries, |
| 357 | size_t sel) { |
| 358 | SetLocale(locales_entries[sel]); |
| 359 | std::vector<std::string> text_name = { "erasing_text", "error_text", "installing_text", |
| 360 | "installing_security_text", "no_command_text" }; |
| 361 | std::unordered_map<std::string, std::unique_ptr<GRSurface, decltype(&free)>> surfaces; |
| 362 | for (const auto& name : text_name) { |
| 363 | GRSurface* text_image = nullptr; |
| 364 | LoadLocalizedBitmap(name.c_str(), &text_image); |
| 365 | if (!text_image) { |
| 366 | Print("Failed to load %s\n", name.c_str()); |
| 367 | return; |
| 368 | } |
| 369 | surfaces.emplace(name, std::unique_ptr<GRSurface, decltype(&free)>(text_image, &free)); |
| 370 | } |
| 371 | |
| 372 | pthread_mutex_lock(&updateMutex); |
| 373 | gr_color(0, 0, 0, 255); |
| 374 | gr_clear(); |
| 375 | |
| 376 | int text_y = kMarginHeight; |
| 377 | int text_x = kMarginWidth; |
| 378 | int line_spacing = gr_sys_font()->char_height; // Put some extra space between images. |
| 379 | // Write the header and descriptive texts. |
| 380 | SetColor(INFO); |
| 381 | std::string header = "Show background text image"; |
| 382 | text_y += DrawTextLine(text_x, text_y, header.c_str(), true); |
| 383 | std::string locale_selection = android::base::StringPrintf( |
| 384 | "Current locale: %s, %zu/%zu", locales_entries[sel].c_str(), sel, locales_entries.size()); |
| 385 | const char* instruction[] = { locale_selection.c_str(), |
| 386 | "Use volume up/down to switch locales and power to exit.", |
| 387 | nullptr }; |
| 388 | text_y += DrawWrappedTextLines(text_x, text_y, instruction); |
| 389 | |
| 390 | // Iterate through the text images and display them in order for the current locale. |
| 391 | for (const auto& p : surfaces) { |
| 392 | text_y += line_spacing; |
| 393 | SetColor(LOG); |
| 394 | text_y += DrawTextLine(text_x, text_y, p.first.c_str(), false); |
| 395 | gr_color(255, 255, 255, 255); |
| 396 | gr_texticon(text_x, text_y, p.second.get()); |
| 397 | text_y += gr_get_height(p.second.get()); |
| 398 | } |
| 399 | // Update the whole screen. |
| 400 | gr_flip(); |
| 401 | pthread_mutex_unlock(&updateMutex); |
| 402 | } |
| 403 | |
| 404 | void ScreenRecoveryUI::CheckBackgroundTextImages(const std::string& saved_locale) { |
| 405 | // Load a list of locales embedded in one of the resource files. |
| 406 | std::vector<std::string> locales_entries = get_locales_in_png("installing_text"); |
| 407 | if (locales_entries.empty()) { |
| 408 | Print("Failed to load locales from the resource files\n"); |
| 409 | return; |
| 410 | } |
| 411 | size_t selected = 0; |
| 412 | SelectAndShowBackgroundText(locales_entries, selected); |
| 413 | |
| 414 | FlushKeys(); |
| 415 | while (true) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 416 | RecoveryUI::InputEvent evt = WaitInputEvent(); |
| 417 | if (evt.type() != RecoveryUI::EVENT_TYPE_KEY) { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 418 | break; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 419 | } |
| 420 | if (evt.key() == KEY_POWER || evt.key() == KEY_ENTER) { |
| 421 | break; |
| 422 | } else if (evt.key() == KEY_UP || evt.key() == KEY_VOLUMEUP) { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 423 | selected = (selected == 0) ? locales_entries.size() - 1 : selected - 1; |
| 424 | SelectAndShowBackgroundText(locales_entries, selected); |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 425 | } else if (evt.key() == KEY_DOWN || evt.key() == KEY_VOLUMEDOWN) { |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 426 | selected = (selected == locales_entries.size() - 1) ? 0 : selected + 1; |
| 427 | SelectAndShowBackgroundText(locales_entries, selected); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | SetLocale(saved_locale); |
| 432 | } |
| 433 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 434 | int ScreenRecoveryUI::ScreenWidth() const { |
| 435 | return gr_fb_width(); |
| 436 | } |
| 437 | |
| 438 | int ScreenRecoveryUI::ScreenHeight() const { |
| 439 | return gr_fb_height(); |
| 440 | } |
| 441 | |
| 442 | void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, |
| 443 | int dy) const { |
| 444 | gr_blit(surface, sx, sy, w, h, dx, dy); |
| 445 | } |
| 446 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 447 | int ScreenRecoveryUI::DrawHorizontalRule(int y) const { |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 448 | gr_fill(0, y + 4, ScreenWidth(), y + 6); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 449 | return 8; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 452 | void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 453 | gr_fill(x, y, x + width, y + height); |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 456 | void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const { |
| 457 | gr_fill(x, y, w, h); |
| 458 | } |
| 459 | |
| 460 | void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const { |
| 461 | gr_texticon(x, y, surface); |
| 462 | } |
| 463 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 464 | int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const { |
| 465 | gr_text(gr_sys_font(), x, y, line, bold); |
| 466 | return char_height_ + 4; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 469 | int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const { |
| 470 | int offset = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 471 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 472 | offset += DrawTextLine(x, y + offset, lines[i], false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 473 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 474 | return offset; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 477 | int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const { |
| 478 | int offset = 0; |
| 479 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
| 480 | // The line will be wrapped if it exceeds text_cols_. |
| 481 | std::string line(lines[i]); |
| 482 | size_t next_start = 0; |
| 483 | while (next_start < line.size()) { |
| 484 | std::string sub = line.substr(next_start, text_cols_ + 1); |
| 485 | if (sub.size() <= text_cols_) { |
| 486 | next_start += sub.size(); |
| 487 | } else { |
| 488 | // Line too long and must be wrapped to text_cols_ columns. |
| 489 | size_t last_space = sub.find_last_of(" \t\n"); |
| 490 | if (last_space == std::string::npos) { |
| 491 | // No space found, just draw as much as we can |
| 492 | sub.resize(text_cols_); |
| 493 | next_start += text_cols_; |
| 494 | } else { |
| 495 | sub.resize(last_space); |
| 496 | next_start += last_space + 1; |
| 497 | } |
| 498 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 499 | gr_text(gr_menu_font(), x, y + offset, sub.c_str(), false); |
| 500 | offset += menu_char_height_ + 4; |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | return offset; |
| 504 | } |
| 505 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 506 | void ScreenRecoveryUI::draw_statusbar_locked() { |
| 507 | int y = kMarginHeight; |
| 508 | int x; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 509 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 510 | int icon_x, icon_y, icon_h, icon_w; |
| 511 | |
| 512 | // Battery status |
| 513 | bool batt_charged; |
| 514 | int batt_capacity; |
| 515 | get_battery_status(batt_charged, batt_capacity); |
| 516 | char batt_capacity_str[3 + 1 + 1]; |
| 517 | snprintf(batt_capacity_str, sizeof(batt_capacity_str), "%d%%", batt_capacity); |
| 518 | |
| 519 | // Draw status bar from right to left |
| 520 | |
| 521 | // Battery icon |
Tom Marshall | bd5a394 | 2018-03-06 20:32:45 +0100 | [diff] [blame] | 522 | x = gr_fb_width() - RECOVERY_UI_MARGIN_STATUSBAR; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 523 | x -= 1 * char_width_; |
| 524 | SetColor((batt_capacity < 20) ? HEADER : STATUSBAR); |
| 525 | |
| 526 | // Top |
| 527 | icon_x = x + char_width_ / 3; |
| 528 | icon_y = y; |
| 529 | icon_w = char_width_ / 3; |
| 530 | icon_h = char_height_ / 12; |
| 531 | gr_fill(icon_x, icon_y, icon_x + icon_w, icon_y + icon_h); |
| 532 | |
| 533 | // Main rect |
| 534 | icon_x = x; |
| 535 | icon_y = y + icon_h; |
| 536 | icon_w = char_width_; |
| 537 | icon_h = char_height_ - (char_height_ / 12); |
| 538 | gr_fill(icon_x, icon_y, icon_x + icon_w, icon_y + icon_h); |
| 539 | |
| 540 | // Capacity |
| 541 | icon_x = x + char_width_ / 6; |
| 542 | icon_y = y + char_height_ / 12; |
| 543 | icon_w = char_width_ - (2 * char_width_ / 6); |
| 544 | icon_h = char_height_ - (3 * char_height_ / 12); |
| 545 | int cap_h = icon_h * batt_capacity / 100; |
| 546 | gr_fill(icon_x, icon_y + icon_h - cap_h, icon_x + icon_w, icon_y + icon_h); |
| 547 | gr_color(0, 0, 0, 255); |
| 548 | gr_fill(icon_x, icon_y, icon_x + icon_w, icon_y + icon_h - cap_h); |
| 549 | SetColor(STATUSBAR); |
| 550 | |
| 551 | x -= char_width_; // Separator |
| 552 | |
| 553 | // Battery text |
| 554 | x -= strlen(batt_capacity_str) * char_width_; |
| 555 | gr_text(gr_sys_font(), x, y, batt_capacity_str, false); |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Header layout: |
| 560 | * * 1/32: Status bar |
| 561 | * * Header image |
| 562 | * * 1/32: Margin |
| 563 | */ |
| 564 | void ScreenRecoveryUI::draw_header_locked(int& y) { |
| 565 | int h_unit = gr_fb_width() / 9; |
| 566 | int v_unit = gr_fb_height() / 16; |
| 567 | |
| 568 | GRSurface* icon; |
| 569 | int icon_x, icon_y, icon_h, icon_w; |
| 570 | |
| 571 | y += v_unit / 2; // Margin |
| 572 | |
| 573 | // Draw back icon if not in main menu |
| 574 | if (!menu_is_main_) { |
| 575 | icon = (menu_sel == -1 ? ic_back_sel : ic_back); |
| 576 | icon_w = gr_get_width(icon); |
| 577 | icon_h = gr_get_height(icon); |
| 578 | icon_x = kMarginWidth + (h_unit / 2) + ((h_unit * 1) - icon_w) / 2; |
| 579 | icon_y = y + ((v_unit * 1) - icon_h) / 2; |
| 580 | gr_blit(icon, 0, 0, icon_w, icon_h, icon_x, icon_y); |
| 581 | } |
| 582 | y += v_unit; |
| 583 | |
| 584 | // Draw logo |
| 585 | icon = logo_image; |
| 586 | icon_w = gr_get_width(icon); |
| 587 | icon_h = gr_get_height(icon); |
| 588 | icon_x = kMarginWidth + (gr_fb_width() - icon_w) / 2; |
| 589 | icon_y = y + ((v_unit * 4) - icon_h) / 2; |
| 590 | gr_blit(icon, 0, 0, icon_w, icon_h, icon_x, icon_y); |
| 591 | y += v_unit * 4; |
| 592 | |
| 593 | y += v_unit * 1; // Margin |
| 594 | } |
| 595 | |
| 596 | void ScreenRecoveryUI::draw_text_menu_locked(int& y) { |
| 597 | static constexpr int kMenuIndent = 4; |
| 598 | int x = kMarginWidth + kMenuIndent; |
Alessandro Astone | feaa81c | 2019-03-29 16:42:38 +0100 | [diff] [blame] | 599 | // An item should not be displayed if it's shown height would be less than 75% of its true height |
| 600 | static const int kMinItemHeight = MenuItemHeight() * 3 / 4; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 601 | |
| 602 | draw_statusbar_locked(); |
| 603 | draw_header_locked(y); |
| 604 | |
| 605 | if (menu_headers_) { |
| 606 | SetColor(HEADER); |
| 607 | // Ignore kMenuIndent, which is not taken into account by text_cols_. |
| 608 | y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_); |
| 609 | |
| 610 | SetColor(MENU); |
| 611 | y += DrawHorizontalRule(y) + 4; |
| 612 | } |
| 613 | |
| 614 | menu_start_y_ = y; |
| 615 | int i; |
Alessandro Astone | feaa81c | 2019-03-29 16:42:38 +0100 | [diff] [blame] | 616 | for (i = menu_show_start; i < (int)menu_items_.size() && y + kMinItemHeight < gr_fb_height(); |
| 617 | ++i) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 618 | const ScreenMenuItem& item = menu_items_.at(i); |
| 619 | if (i == menu_sel) { |
| 620 | SetColor(MENU_SEL_FG); |
| 621 | y += menu_char_height_; |
| 622 | gr_text(gr_menu_font(), x, y, item.text().c_str(), false); |
| 623 | y += menu_char_height_; |
| 624 | y += menu_char_height_; |
| 625 | } else { |
| 626 | SetColor(MENU); |
| 627 | y += menu_char_height_; |
| 628 | gr_text(gr_menu_font(), x, y, item.text().c_str(), false); |
| 629 | y += menu_char_height_; |
| 630 | y += menu_char_height_; |
| 631 | } |
| 632 | } |
| 633 | menu_show_count = i - menu_show_start; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Grid layout. |
| 638 | * |
| 639 | * Grid item: |
| 640 | * Horizontal: |
| 641 | * * 3/9 of screen per item. |
| 642 | * * 1/9 of screen margin around/between items. |
| 643 | * Vertical: |
| 644 | * * 3/16 of screen per item. |
| 645 | * * No margin between items. |
| 646 | * |
| 647 | * Within a grid item: |
| 648 | * Asher's icons 1/5 of grid both dimensions. |
| 649 | * Current icons 2/5 of grid both dimensions. |
| 650 | * Horizontal: |
| 651 | * * All items centered. |
| 652 | * Vertical: |
| 653 | * * Icon lower aligned in top 2/3. |
| 654 | * * Text upper aligned in low 1/3 plus half line margin. |
| 655 | */ |
| 656 | void ScreenRecoveryUI::draw_grid_menu_locked(int& y) { |
| 657 | int h_unit = gr_fb_width() / 9; |
| 658 | int v_unit = gr_fb_height() / 16; |
| 659 | |
| 660 | int grid_w = h_unit * 3; |
| 661 | int grid_h = v_unit * 3; |
| 662 | |
| 663 | draw_statusbar_locked(); |
| 664 | draw_header_locked(y); |
| 665 | |
| 666 | menu_start_y_ = y; |
| 667 | int i; |
| 668 | for (i = menu_show_start; i < (int)menu_items_.size() && y + grid_h < gr_fb_height(); ++i) { |
| 669 | ScreenMenuItem& item = menu_items_.at(i); |
| 670 | int grid_x = kMarginWidth + ((i % 2) ? h_unit * 5 : h_unit * 1); |
| 671 | int grid_y = y; |
| 672 | if (item.icon()) { |
| 673 | GRSurface* icon = (i == menu_sel) ? item.icon_sel() : item.icon(); |
| 674 | int icon_w = gr_get_width(icon); |
| 675 | int icon_h = gr_get_height(icon); |
| 676 | int icon_x = grid_x + (grid_w - icon_w) / 2; |
| 677 | int icon_y = grid_y + ((grid_h * 2 / 3) - icon_h) / 2; |
| 678 | gr_blit(icon, 0, 0, icon_w, icon_h, icon_x, icon_y); |
| 679 | } |
| 680 | if (!item.text().empty()) { |
| 681 | int text_w = item.text().size() * char_width_; |
| 682 | int text_x = grid_x + (grid_w - text_w) / 2; |
| 683 | int text_y = grid_y + (grid_h * 2 / 3) + (char_height_ / 2); |
| 684 | SetColor(i == menu_sel ? MENU_SEL_FG : MENU); |
| 685 | gr_text(gr_sys_font(), text_x, text_y, item.text().c_str(), false); |
| 686 | } |
| 687 | if (i % 2) { |
| 688 | y += grid_h; |
| 689 | grid_y = y; |
| 690 | } |
| 691 | } |
| 692 | menu_show_count = i - menu_show_start; |
| 693 | } |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 694 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 695 | // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex |
| 696 | // locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 697 | void ScreenRecoveryUI::draw_screen_locked() { |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 698 | pagesIdentical = false; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 699 | gr_color(0, 0, 0, 255); |
| 700 | gr_clear(); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 701 | |
Tao Bao | 4521b70 | 2017-06-20 18:11:21 -0700 | [diff] [blame] | 702 | int y = kMarginHeight; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 703 | if (show_menu) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 704 | switch (menu_type_) { |
| 705 | case MT_LIST: |
| 706 | draw_text_menu_locked(y); |
| 707 | break; |
| 708 | case MT_GRID: |
| 709 | draw_grid_menu_locked(y); |
| 710 | break; |
| 711 | default: |
| 712 | break; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 713 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 714 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 715 | // Draw version info |
| 716 | if (menu_is_main_) { |
| 717 | int text_x, text_y; |
| 718 | text_x = kMarginWidth + (gr_fb_width() - (android_version_.size() * char_width_)) / 2; |
| 719 | text_y = gr_fb_height() - 2 * (char_height_ + 4); |
| 720 | SetColor(MENU); |
| 721 | DrawTextLine(text_x, text_y, android_version_.c_str(), false); |
| 722 | text_x = kMarginWidth + (gr_fb_width() - (lineage_version_.size() * char_width_)) / 2; |
| 723 | text_y = gr_fb_height() - 1 * (char_height_ + 4); |
| 724 | DrawTextLine(text_x, text_y, lineage_version_.c_str(), false); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 725 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 726 | } else { |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 727 | draw_background_locked(); |
| 728 | draw_foreground_locked(y); |
| 729 | |
| 730 | if (show_text) { |
| 731 | // Display from the bottom up, until we hit the top of the screen, the |
| 732 | // bottom of the foreground, or we've displayed the entire text buffer. |
| 733 | SetColor(LOG); |
| 734 | int row = (text_rows_ - 1) % text_rows_; |
| 735 | size_t count = 0; |
| 736 | for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_; |
| 737 | ty -= char_height_, ++count) { |
| 738 | DrawTextLine(kMarginWidth, ty, text_[row], false); |
| 739 | --row; |
| 740 | if (row < 0) row = text_rows_ - 1; |
| 741 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 742 | } |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 743 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // Redraw everything on the screen and flip the screen (make it visible). |
| 747 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 748 | void ScreenRecoveryUI::update_screen_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 749 | draw_screen_locked(); |
| 750 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 754 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 755 | void ScreenRecoveryUI::update_progress_locked() { |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 756 | if (!pagesIdentical) { |
| 757 | draw_screen_locked(); |
| 758 | pagesIdentical = true; |
| 759 | } else { |
| 760 | int y = kMarginHeight; |
| 761 | draw_foreground_locked(y); |
| 762 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 763 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 767 | void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 768 | reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop(); |
| 769 | return nullptr; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 772 | void ScreenRecoveryUI::ProgressThreadLoop() { |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 773 | double interval = 1.0 / kAnimationFps; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 774 | while (true) { |
| 775 | double start = now(); |
| 776 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 777 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 778 | bool redraw = false; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 779 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 780 | // update the installation animation, if active |
| 781 | // skip this if we have a text overlay (too expensive to update) |
Steve Kondik | 17b88b0 | 2014-09-12 18:52:28 -0700 | [diff] [blame] | 782 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING)) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 783 | if (!intro_done) { |
| 784 | if (current_frame == intro_frames - 1) { |
| 785 | intro_done = true; |
| 786 | current_frame = 0; |
| 787 | } else { |
| 788 | ++current_frame; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 789 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 790 | } else { |
| 791 | current_frame = (current_frame + 1) % loop_frames; |
| 792 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 793 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 794 | redraw = true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 795 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 796 | |
| 797 | // move the progress bar forward on timed intervals, if configured |
| 798 | int duration = progressScopeDuration; |
| 799 | if (progressBarType == DETERMINATE && duration > 0) { |
| 800 | double elapsed = now() - progressScopeTime; |
| 801 | float p = 1.0 * elapsed / duration; |
| 802 | if (p > 1.0) p = 1.0; |
| 803 | if (p > progress) { |
| 804 | progress = p; |
| 805 | redraw = true; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (redraw) update_progress_locked(); |
| 810 | |
| 811 | pthread_mutex_unlock(&updateMutex); |
Steve Kondik | 17b88b0 | 2014-09-12 18:52:28 -0700 | [diff] [blame] | 812 | |
| 813 | if (progressBarType == EMPTY) break; |
| 814 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 815 | double end = now(); |
| 816 | // minimum of 20ms delay between frames |
| 817 | double delay = interval - (end - start); |
| 818 | if (delay < 0.02) delay = 0.02; |
| 819 | usleep(static_cast<useconds_t>(delay * 1000000)); |
| 820 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 823 | void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 824 | int result = res_create_display_surface(filename, surface); |
| 825 | if (result < 0) { |
| 826 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 827 | } |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 828 | } |
| 829 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 830 | void ScreenRecoveryUI::FreeBitmap(GRSurface* surface) { |
| 831 | res_free_surface(surface); |
| 832 | } |
| 833 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 834 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 835 | int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface); |
| 836 | if (result < 0) { |
| 837 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 838 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 841 | static char** Alloc2d(size_t rows, size_t cols) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 842 | char** result = new char*[rows]; |
| 843 | for (size_t i = 0; i < rows; ++i) { |
| 844 | result[i] = new char[cols]; |
| 845 | memset(result[i], 0, cols); |
| 846 | } |
| 847 | return result; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 850 | // Choose the right background string to display during update. |
| 851 | void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 852 | if (security_update) { |
| 853 | LoadLocalizedBitmap("installing_security_text", &installing_text); |
| 854 | } else { |
| 855 | LoadLocalizedBitmap("installing_text", &installing_text); |
| 856 | } |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 859 | bool ScreenRecoveryUI::InitTextParams() { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 860 | if (gr_init() < 0) { |
| 861 | return false; |
| 862 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 863 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 864 | gr_font_size(gr_sys_font(), &char_width_, &char_height_); |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 865 | gr_font_size(gr_menu_font(), &menu_char_width_, &menu_char_height_); |
Luke Song | 92eda4d | 2017-09-19 10:51:35 -0700 | [diff] [blame] | 866 | text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_; |
| 867 | text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 868 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 871 | bool ScreenRecoveryUI::Init(const std::string& locale) { |
| 872 | RecoveryUI::Init(locale); |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 873 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 874 | if (!InitTextParams()) { |
| 875 | return false; |
| 876 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 877 | |
Michael Bestas | 5d5abd8 | 2019-03-23 17:28:22 +0200 | [diff] [blame] | 878 | #ifdef RECOVERY_UI_BLANK_UNBLANK_ON_INIT |
| 879 | gr_fb_blank(true); |
| 880 | gr_fb_blank(false); |
| 881 | #endif |
| 882 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 883 | // Are we portrait or landscape? |
| 884 | layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; |
| 885 | // Are we the large variant of our base layout? |
| 886 | if (gr_fb_height() > PixelsFromDp(800)) ++layout_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 887 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 888 | text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 889 | file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 890 | |
Alessandro Astone | 20c8d41 | 2019-04-01 11:11:33 +0200 | [diff] [blame] | 891 | text_row_ = text_rows_ - 1; // Printed text grows bottom up |
| 892 | text_col_ = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 893 | |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 894 | // Set up the locale info. |
| 895 | SetLocale(locale); |
| 896 | |
Tom Marshall | c9ab109 | 2018-06-21 01:13:19 +0200 | [diff] [blame] | 897 | // Load logo and scale it if necessary |
| 898 | // Note 2/45 is our standard margin on each side so the maximum image |
| 899 | // width is 41/45 of the screen width. |
| 900 | GRSurface* image; |
| 901 | LoadBitmap("logo_image", &image); |
| 902 | if ((int)gr_get_width(image) > gr_fb_width() * 41 / 45) { |
| 903 | float scale = (float)gr_fb_width() / (float)gr_get_width(image) * (41.0f / 45.0f); |
| 904 | GRSurface* scaled_image; |
| 905 | res_create_scaled_surface(&scaled_image, image, scale, scale); |
| 906 | logo_image = scaled_image; |
| 907 | res_free_surface(image); |
| 908 | } else { |
| 909 | logo_image = image; |
| 910 | } |
| 911 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 912 | LoadBitmap("ic_back", &ic_back); |
| 913 | LoadBitmap("ic_back_sel", &ic_back_sel); |
| 914 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 915 | LoadBitmap("icon_error", &error_icon); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 916 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 917 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 918 | LoadBitmap("progress_fill", &progressBarFill); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 919 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 920 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 921 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 922 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 923 | // Background text for "installing_update" could be "installing update" |
| 924 | // or "installing security update". It will be set after UI init according |
| 925 | // to commands in BCB. |
| 926 | installing_text = nullptr; |
| 927 | LoadLocalizedBitmap("erasing_text", &erasing_text); |
| 928 | LoadLocalizedBitmap("no_command_text", &no_command_text); |
| 929 | LoadLocalizedBitmap("error_text", &error_text); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 930 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 931 | LoadAnimation(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 932 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 933 | return true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Tom Marshall | cfcafa1 | 2017-09-08 19:48:46 +0000 | [diff] [blame] | 936 | void ScreenRecoveryUI::Stop() { |
| 937 | RecoveryUI::Stop(); |
| 938 | gr_fb_blank(true); |
| 939 | } |
| 940 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 941 | void ScreenRecoveryUI::LoadAnimation() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 942 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir); |
| 943 | dirent* de; |
| 944 | std::vector<std::string> intro_frame_names; |
| 945 | std::vector<std::string> loop_frame_names; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 946 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 947 | while ((de = readdir(dir.get())) != nullptr) { |
| 948 | int value, num_chars; |
| 949 | if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { |
| 950 | intro_frame_names.emplace_back(de->d_name, num_chars); |
| 951 | } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { |
| 952 | loop_frame_names.emplace_back(de->d_name, num_chars); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 953 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 954 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 955 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 956 | intro_frames = intro_frame_names.size(); |
| 957 | loop_frames = loop_frame_names.size(); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 958 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 959 | // It's okay to not have an intro. |
| 960 | if (intro_frames == 0) intro_done = true; |
| 961 | // But you must have an animation. |
| 962 | if (loop_frames == 0) abort(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 963 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 964 | std::sort(intro_frame_names.begin(), intro_frame_names.end()); |
| 965 | std::sort(loop_frame_names.begin(), loop_frame_names.end()); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 966 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 967 | introFrames = new GRSurface*[intro_frames]; |
| 968 | for (size_t i = 0; i < intro_frames; i++) { |
| 969 | LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]); |
| 970 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 971 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 972 | loopFrames = new GRSurface*[loop_frames]; |
| 973 | for (size_t i = 0; i < loop_frames; i++) { |
| 974 | LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]); |
| 975 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 978 | void ScreenRecoveryUI::SetBackground(Icon icon) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 979 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 980 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 981 | if (icon != currentIcon) { |
| 982 | currentIcon = icon; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 983 | } |
Doug Zongker | 52eeea4 | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 984 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 985 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 988 | void ScreenRecoveryUI::SetProgressType(ProgressType type) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 989 | pthread_mutex_lock(&updateMutex); |
| 990 | if (progressBarType != type) { |
| 991 | progressBarType = type; |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 992 | progressScopeStart = 0; |
| 993 | progressScopeSize = 0; |
| 994 | progress = 0; |
Steve Kondik | 17b88b0 | 2014-09-12 18:52:28 -0700 | [diff] [blame] | 995 | if (progressBarType != EMPTY) { |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 996 | update_screen_locked(); |
Steve Kondik | 17b88b0 | 2014-09-12 18:52:28 -0700 | [diff] [blame] | 997 | pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this); |
| 998 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 999 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1000 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1003 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1004 | pthread_mutex_lock(&updateMutex); |
| 1005 | progressBarType = DETERMINATE; |
| 1006 | progressScopeStart += progressScopeSize; |
| 1007 | progressScopeSize = portion; |
| 1008 | progressScopeTime = now(); |
| 1009 | progressScopeDuration = seconds; |
| 1010 | progress = 0; |
| 1011 | update_progress_locked(); |
| 1012 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1015 | void ScreenRecoveryUI::SetProgress(float fraction) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1016 | pthread_mutex_lock(&updateMutex); |
| 1017 | if (fraction < 0.0) fraction = 0.0; |
| 1018 | if (fraction > 1.0) fraction = 1.0; |
| 1019 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 1020 | // Skip updates that aren't visibly different. |
| 1021 | int width = gr_get_width(progressBarEmpty); |
| 1022 | float scale = width * progressScopeSize; |
| 1023 | if ((int)(progress * scale) != (int)(fraction * scale)) { |
| 1024 | progress = fraction; |
| 1025 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1026 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1027 | } |
| 1028 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1031 | void ScreenRecoveryUI::SetStage(int current, int max) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1032 | pthread_mutex_lock(&updateMutex); |
| 1033 | stage = current; |
| 1034 | max_stage = max; |
| 1035 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Alessandro Astone | 20c8d41 | 2019-04-01 11:11:33 +0200 | [diff] [blame] | 1038 | void ScreenRecoveryUI::NewLine() { |
| 1039 | // Shift the rows array up |
| 1040 | char* p = text_[0]; |
| 1041 | for (size_t i = 0; i < text_rows_ - 1; i++) { |
| 1042 | text_[i] = text_[i + 1]; |
| 1043 | } |
| 1044 | text_[text_rows_ - 1] = p; |
| 1045 | memset(text_[text_rows_ - 1], 0, text_cols_ + 1); |
| 1046 | |
| 1047 | text_col_ = 0; |
| 1048 | } |
| 1049 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1050 | void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1051 | std::string str; |
| 1052 | android::base::StringAppendV(&str, fmt, ap); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1053 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1054 | if (copy_to_stdout) { |
| 1055 | fputs(str.c_str(), stdout); |
| 1056 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1057 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1058 | pthread_mutex_lock(&updateMutex); |
| 1059 | if (text_rows_ > 0 && text_cols_ > 0) { |
Alessandro Astone | 20c8d41 | 2019-04-01 11:11:33 +0200 | [diff] [blame] | 1060 | if (previous_row_ended) { |
| 1061 | NewLine(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1062 | } |
Alessandro Astone | 20c8d41 | 2019-04-01 11:11:33 +0200 | [diff] [blame] | 1063 | previous_row_ended = false; |
| 1064 | |
| 1065 | size_t row = text_rows_ - 1; |
| 1066 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
| 1067 | if (*ptr == '\n' && *(ptr + 1) == '\0') { |
| 1068 | // Scroll on the next print |
| 1069 | text_[row][text_col_] = '\0'; |
| 1070 | previous_row_ended = true; |
| 1071 | } else if ((*ptr == '\n' && *(ptr + 1) != '\0') || text_col_ >= text_cols_) { |
| 1072 | // We need to keep printing, scroll now |
| 1073 | text_[row][text_col_] = '\0'; |
| 1074 | NewLine(); |
| 1075 | } |
| 1076 | if (*ptr != '\n') text_[row][text_col_++] = *ptr; |
| 1077 | } |
| 1078 | text_[row][text_col_] = '\0'; |
| 1079 | |
| 1080 | if (show_text && update_screen_on_print) { |
| 1081 | update_screen_locked(); |
| 1082 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1083 | } |
| 1084 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1087 | void ScreenRecoveryUI::Print(const char* fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1088 | va_list ap; |
| 1089 | va_start(ap, fmt); |
| 1090 | PrintV(fmt, true, ap); |
| 1091 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1095 | va_list ap; |
| 1096 | va_start(ap, fmt); |
| 1097 | PrintV(fmt, false, ap); |
| 1098 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1101 | void ScreenRecoveryUI::PutChar(char ch) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1102 | pthread_mutex_lock(&updateMutex); |
| 1103 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 1104 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 1105 | text_col_ = 0; |
| 1106 | ++text_row_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1107 | } |
| 1108 | pthread_mutex_unlock(&updateMutex); |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1111 | void ScreenRecoveryUI::ClearText() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1112 | pthread_mutex_lock(&updateMutex); |
| 1113 | text_col_ = 0; |
| 1114 | text_row_ = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1115 | for (size_t i = 0; i < text_rows_; ++i) { |
| 1116 | memset(text_[i], 0, text_cols_ + 1); |
| 1117 | } |
| 1118 | pthread_mutex_unlock(&updateMutex); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1121 | int ScreenRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1122 | std::vector<off_t> offsets; |
| 1123 | offsets.push_back(ftello(fp)); |
| 1124 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1125 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1126 | struct stat sb; |
| 1127 | fstat(fileno(fp), &sb); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1128 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1129 | bool show_prompt = false; |
| 1130 | while (true) { |
| 1131 | if (show_prompt) { |
| 1132 | PrintOnScreenOnly("--(%d%% of %d bytes)--", |
| 1133 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
| 1134 | static_cast<int>(sb.st_size)); |
| 1135 | Redraw(); |
| 1136 | while (show_prompt) { |
| 1137 | show_prompt = false; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1138 | RecoveryUI::InputEvent evt = WaitInputEvent(); |
| 1139 | if (evt.type() != RecoveryUI::EVENT_TYPE_KEY) { |
| 1140 | show_prompt = true; |
| 1141 | continue; |
| 1142 | } |
| 1143 | if (evt.key() == KEY_POWER || evt.key() == KEY_ENTER || evt.key() == KEY_BACKSPACE || |
| 1144 | evt.key() == KEY_BACK || evt.key() == KEY_HOME || evt.key() == KEY_HOMEPAGE) { |
| 1145 | return evt.key(); |
| 1146 | } else if (evt.key() == KEY_UP || evt.key() == KEY_VOLUMEUP) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1147 | if (offsets.size() <= 1) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1148 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1149 | } else { |
| 1150 | offsets.pop_back(); |
| 1151 | fseek(fp, offsets.back(), SEEK_SET); |
| 1152 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1153 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1154 | if (feof(fp)) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1155 | return -1; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1156 | } |
| 1157 | offsets.push_back(ftello(fp)); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1158 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1159 | } |
| 1160 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1161 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1162 | |
| 1163 | int ch = getc(fp); |
| 1164 | if (ch == EOF) { |
| 1165 | while (text_row_ < text_rows_ - 1) PutChar('\n'); |
| 1166 | show_prompt = true; |
| 1167 | } else { |
| 1168 | PutChar(ch); |
| 1169 | if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { |
| 1170 | show_prompt = true; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1174 | return -1; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1177 | int ScreenRecoveryUI::ShowFile(const char* filename) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1178 | FILE* fp = fopen_path(filename, "re"); |
| 1179 | if (fp == nullptr) { |
| 1180 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1181 | return -1; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1182 | } |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1183 | |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 1184 | Icon oldIcon = currentIcon; |
| 1185 | currentIcon = NONE; |
| 1186 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1187 | char** old_text = text_; |
| 1188 | size_t old_text_col = text_col_; |
| 1189 | size_t old_text_row = text_row_; |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1190 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1191 | // Swap in the alternate screen and clear it. |
| 1192 | text_ = file_viewer_text_; |
| 1193 | ClearText(); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1194 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1195 | int key = ShowFile(fp); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1196 | fclose(fp); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 1197 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1198 | text_ = old_text; |
| 1199 | text_col_ = old_text_col; |
| 1200 | text_row_ = old_text_row; |
Tom Marshall | f780f1c | 2018-03-01 23:51:51 +0100 | [diff] [blame] | 1201 | currentIcon = oldIcon; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1202 | return key; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1205 | void ScreenRecoveryUI::StartMenu(bool is_main, menu_type_t type, const char* const* headers, |
| 1206 | const MenuItemVector& items, int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1207 | pthread_mutex_lock(&updateMutex); |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1208 | menu_is_main_ = is_main; |
| 1209 | menu_type_ = type; |
| 1210 | menu_headers_ = headers; |
| 1211 | for (auto& item : items) { |
| 1212 | menu_items_.push_back(ScreenMenuItem(item)); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1213 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1214 | show_menu = true; |
| 1215 | menu_sel = initial_selection; |
Alessandro Astone | 54f55ed | 2019-03-29 00:16:49 +0100 | [diff] [blame] | 1216 | menu_show_start = 0; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1217 | draw_screen_locked(); |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1218 | if (menu_sel >= menu_show_start + menu_show_count) { |
| 1219 | menu_show_start = menu_sel - (menu_show_count - 1); |
| 1220 | } |
| 1221 | update_screen_locked(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1222 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | int ScreenRecoveryUI::SelectMenu(int sel) { |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1226 | int wrapped = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1227 | pthread_mutex_lock(&updateMutex); |
| 1228 | if (show_menu) { |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1229 | int old_menu_sel = menu_sel; |
| 1230 | |
| 1231 | // Handle wrapping and back item |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1232 | menu_sel = sel; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1233 | if (sel < 0 && (menu_is_main_ || sel < -1)) { |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1234 | menu_sel = (int)menu_items_.size() - 1; |
| 1235 | wrapped = -1; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1236 | } |
| 1237 | if (sel >= (int)menu_items_.size()) { |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1238 | menu_sel = (menu_is_main_ ? 0 : -1); |
| 1239 | wrapped = 1; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1240 | } |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 1241 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1242 | // Scroll |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1243 | if (menu_sel != -1 && menu_sel < menu_show_start) { |
| 1244 | menu_show_start = menu_sel; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1245 | } |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1246 | if (menu_sel >= menu_show_start + menu_show_count) { |
| 1247 | menu_show_start = menu_sel - (menu_show_count - 1); |
| 1248 | } |
| 1249 | |
| 1250 | // Rainbows |
| 1251 | if (rainbow) { |
| 1252 | if (sel > old_menu_sel) { |
| 1253 | move_rainbow(-1); |
| 1254 | } else if (sel < old_menu_sel) { |
| 1255 | move_rainbow(1); |
| 1256 | } |
| 1257 | } |
| 1258 | if (wrapped != 0) { |
| 1259 | if (wrap_count / wrapped > 0) { |
| 1260 | wrap_count += wrapped; |
| 1261 | } else { |
| 1262 | wrap_count = wrapped; |
| 1263 | } |
| 1264 | if (wrap_count / wrapped >= 5) { |
| 1265 | wrap_count = 0; |
| 1266 | OMGRainbows(); |
| 1267 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1268 | } |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 1269 | |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1270 | if (menu_sel != old_menu_sel) update_screen_locked(); |
| 1271 | } |
| 1272 | pthread_mutex_unlock(&updateMutex); |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1273 | return menu_sel; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | int ScreenRecoveryUI::SelectMenu(const Point& point) { |
| 1277 | int sel = Device::kNoAction; |
| 1278 | int h_unit = gr_fb_width() / 9; |
| 1279 | int v_unit = gr_fb_height() / 16; |
| 1280 | pthread_mutex_lock(&updateMutex); |
| 1281 | if (show_menu) { |
| 1282 | if (point.y() < menu_start_y_) { |
| 1283 | if (!menu_is_main_ && point.x() >= h_unit / 2 && point.x() < h_unit * 3 / 2 && |
| 1284 | point.y() >= v_unit * 1 / 2 && point.y() < v_unit * 3 / 2) { |
| 1285 | sel = Device::kGoBack; |
| 1286 | } |
| 1287 | } else { |
| 1288 | int row = -1, col = -1; |
| 1289 | switch (menu_type_) { |
| 1290 | case MT_LIST: |
Alessandro Astone | 92a8ed5 | 2019-03-28 22:26:18 +0100 | [diff] [blame] | 1291 | sel = (point.y() - menu_start_y_) / (menu_char_height_ * 3) + menu_show_start; |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1292 | break; |
| 1293 | case MT_GRID: |
| 1294 | row = (point.y() - menu_start_y_) / (gr_fb_height() * 3 / 16); |
| 1295 | col = (point.x()) / (gr_fb_width() / 9); |
| 1296 | if ((col % 4) != 0) { |
| 1297 | sel = row * 2 + ((col - 1) / 4); |
| 1298 | } |
| 1299 | break; |
| 1300 | default: |
| 1301 | break; |
| 1302 | } |
| 1303 | if (sel >= (int)menu_items_.size()) { |
| 1304 | sel = Device::kNoAction; |
| 1305 | } |
| 1306 | } |
| 1307 | if (sel != -1 && sel != menu_sel) { |
| 1308 | menu_sel = sel; |
| 1309 | update_screen_locked(); |
| 1310 | usleep(100 * 1000); |
| 1311 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1312 | } |
| 1313 | pthread_mutex_unlock(&updateMutex); |
| 1314 | return sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Alessandro Astone | d6eb74e | 2019-03-28 22:02:02 +0100 | [diff] [blame] | 1317 | int ScreenRecoveryUI::ScrollMenu(int updown) { |
| 1318 | pthread_mutex_lock(&updateMutex); |
| 1319 | if ((updown > 0 && menu_show_start + menu_show_count < (int)menu_items_.size()) || |
| 1320 | (updown < 0 && menu_show_start > 0)) { |
| 1321 | menu_show_start += updown; |
| 1322 | |
| 1323 | /* We can receive a kInvokeItem event from a different source than touch, |
| 1324 | like from Power button. For this reason, selection should not get out of |
| 1325 | the screen. Constrain it to the first or last visible item of the list */ |
| 1326 | if (menu_sel < menu_show_start) { |
| 1327 | menu_sel = menu_show_start; |
| 1328 | } else if (menu_sel >= menu_show_start + menu_show_count) { |
| 1329 | menu_sel = menu_show_start + menu_show_count - 1; |
| 1330 | } |
| 1331 | |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1332 | // Rainbows |
| 1333 | int sign = (updown > 0) - (updown < 0); |
| 1334 | move_rainbow(sign); |
| 1335 | |
Alessandro Astone | d6eb74e | 2019-03-28 22:02:02 +0100 | [diff] [blame] | 1336 | update_screen_locked(); |
| 1337 | } |
| 1338 | pthread_mutex_unlock(&updateMutex); |
| 1339 | return menu_sel; |
| 1340 | } |
| 1341 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1342 | void ScreenRecoveryUI::EndMenu() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1343 | pthread_mutex_lock(&updateMutex); |
| 1344 | if (show_menu && text_rows_ > 0 && text_cols_ > 0) { |
| 1345 | show_menu = false; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1346 | } |
Tom Marshall | 820517b | 2019-01-14 14:28:55 -0800 | [diff] [blame] | 1347 | menu_type_ = MT_NONE; |
| 1348 | menu_headers_ = nullptr; |
| 1349 | menu_items_.clear(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1350 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1353 | bool ScreenRecoveryUI::IsTextVisible() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1354 | pthread_mutex_lock(&updateMutex); |
| 1355 | int visible = show_text; |
| 1356 | pthread_mutex_unlock(&updateMutex); |
| 1357 | return visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1360 | bool ScreenRecoveryUI::WasTextEverVisible() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1361 | pthread_mutex_lock(&updateMutex); |
| 1362 | int ever_visible = show_text_ever; |
| 1363 | pthread_mutex_unlock(&updateMutex); |
| 1364 | return ever_visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1367 | void ScreenRecoveryUI::ShowText(bool visible) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1368 | pthread_mutex_lock(&updateMutex); |
| 1369 | show_text = visible; |
| 1370 | if (show_text) show_text_ever = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1371 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1372 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1373 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 1374 | void ScreenRecoveryUI::Redraw() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1375 | pthread_mutex_lock(&updateMutex); |
| 1376 | update_screen_locked(); |
| 1377 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 1378 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1379 | |
| 1380 | void ScreenRecoveryUI::KeyLongPress(int) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 1381 | // Redraw so that if we're in the menu, the highlight |
| 1382 | // will change color to indicate a successful long press. |
| 1383 | Redraw(); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 1384 | } |
Tao Bao | efb49ad | 2017-01-31 23:03:10 -0800 | [diff] [blame] | 1385 | |
| 1386 | void ScreenRecoveryUI::SetLocale(const std::string& new_locale) { |
| 1387 | locale_ = new_locale; |
| 1388 | rtl_locale_ = false; |
| 1389 | |
| 1390 | if (!new_locale.empty()) { |
| 1391 | size_t underscore = new_locale.find('_'); |
| 1392 | // lang has the language prefix prior to '_', or full string if '_' doesn't exist. |
| 1393 | std::string lang = new_locale.substr(0, underscore); |
| 1394 | |
| 1395 | // A bit cheesy: keep an explicit list of supported RTL languages. |
| 1396 | if (lang == "ar" || // Arabic |
| 1397 | lang == "fa" || // Persian (Farsi) |
| 1398 | lang == "he" || // Hebrew (new language code) |
| 1399 | lang == "iw" || // Hebrew (old language code) |
| 1400 | lang == "ur") { // Urdu |
| 1401 | rtl_locale_ = true; |
| 1402 | } |
| 1403 | } |
| 1404 | } |
CEnnis91 | 0ccad61 | 2013-05-02 13:29:53 -0400 | [diff] [blame] | 1405 | |
| 1406 | void ScreenRecoveryUI::OMGRainbows() { |
| 1407 | rainbow = rainbow ? false : true; |
| 1408 | set_rainbow_mode(rainbow); |
| 1409 | } |