No Description

screen_ui.cpp 40KB

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