flip_store_callback.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include <callback/flip_store_callback.h>
  2. bool flip_store_app_does_exist = false;
  3. // Callback for drawing the main screen
  4. void flip_store_view_draw_callback_main(Canvas *canvas, void *model)
  5. {
  6. UNUSED(model);
  7. canvas_set_font(canvas, FontSecondary);
  8. if (fhttp.state == INACTIVE)
  9. {
  10. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  11. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  12. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  13. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  14. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  15. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  16. return;
  17. }
  18. if (!flip_store_sent_request)
  19. {
  20. flip_store_sent_request = true;
  21. if (!flip_store_install_app(canvas, categories[flip_store_category_index]))
  22. {
  23. canvas_clear(canvas);
  24. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  25. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  26. }
  27. }
  28. else
  29. {
  30. if (flip_store_success)
  31. {
  32. if (fhttp.state == RECEIVING)
  33. {
  34. canvas_clear(canvas);
  35. canvas_draw_str(canvas, 0, 10, "Downloading app...");
  36. canvas_draw_str(canvas, 0, 60, "Please wait...");
  37. return;
  38. }
  39. else if (fhttp.state == IDLE)
  40. {
  41. canvas_clear(canvas);
  42. canvas_draw_str(canvas, 0, 10, "App installed successfully.");
  43. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  44. }
  45. }
  46. else
  47. {
  48. canvas_clear(canvas);
  49. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  50. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  51. }
  52. }
  53. }
  54. // Function to draw the message on the canvas with word wrapping
  55. void draw_description(Canvas *canvas, const char *description, int x, int y)
  56. {
  57. if (description == NULL || strlen(description) == 0)
  58. {
  59. FURI_LOG_E(TAG, "User message is NULL.");
  60. return;
  61. }
  62. if (!canvas)
  63. {
  64. FURI_LOG_E(TAG, "Canvas is NULL.");
  65. return;
  66. }
  67. size_t msg_length = strlen(description);
  68. size_t start = 0;
  69. int line_num = 0;
  70. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  71. while (start < msg_length && line_num < 4)
  72. {
  73. size_t remaining = msg_length - start;
  74. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  75. if (remaining > MAX_LINE_LENGTH)
  76. {
  77. // Find the last space within the first 'len' characters
  78. size_t last_space = len;
  79. while (last_space > 0 && description[start + last_space - 1] != ' ')
  80. {
  81. last_space--;
  82. }
  83. if (last_space > 0)
  84. {
  85. len = last_space; // Adjust len to the position of the last space
  86. }
  87. }
  88. // Copy the substring to 'line' and null-terminate it
  89. memcpy(line, description + start, len);
  90. line[len] = '\0'; // Ensure the string is null-terminated
  91. // Draw the string on the canvas
  92. // Adjust the y-coordinate based on the line number
  93. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  94. // Update the start position for the next line
  95. start += len;
  96. // Skip any spaces to avoid leading spaces on the next line
  97. while (start < msg_length && description[start] == ' ')
  98. {
  99. start++;
  100. }
  101. // Increment the line number
  102. line_num++;
  103. }
  104. }
  105. void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  106. {
  107. UNUSED(model);
  108. canvas_clear(canvas);
  109. canvas_set_font(canvas, FontPrimary);
  110. char title[30];
  111. snprintf(title, 30, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  112. canvas_draw_str(canvas, 0, 10, title);
  113. canvas_set_font(canvas, FontSecondary);
  114. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  115. if (flip_store_app_does_exist)
  116. {
  117. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  118. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  119. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  120. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  121. }
  122. else
  123. {
  124. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  125. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  126. }
  127. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  128. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  129. }
  130. bool flip_store_input_callback(InputEvent *event, void *context)
  131. {
  132. FlipStoreApp *app = (FlipStoreApp *)context;
  133. if (!app)
  134. {
  135. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  136. return false;
  137. }
  138. if (event->type == InputTypeShort)
  139. {
  140. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  141. {
  142. // Left button clicked, delete the app
  143. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  144. return true;
  145. }
  146. if (event->key == InputKeyRight)
  147. {
  148. // Right button clicked, download the app
  149. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  150. return true;
  151. }
  152. }
  153. else if (event->type == InputTypePress)
  154. {
  155. if (event->key == InputKeyBack)
  156. {
  157. // Back button clicked, switch to the previous view.
  158. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. void flip_store_text_updated_ssid(void *context)
  165. {
  166. FlipStoreApp *app = (FlipStoreApp *)context;
  167. if (!app)
  168. {
  169. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  170. return;
  171. }
  172. // store the entered text
  173. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  174. // Ensure null-termination
  175. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  176. // update the variable item text
  177. if (app->variable_item_ssid)
  178. {
  179. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  180. }
  181. // save the settings
  182. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  183. // if SSID and PASS are not empty, connect to the WiFi
  184. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  185. {
  186. // save wifi settings
  187. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  188. {
  189. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  190. }
  191. }
  192. // switch to the settings view
  193. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  194. }
  195. void flip_store_text_updated_pass(void *context)
  196. {
  197. FlipStoreApp *app = (FlipStoreApp *)context;
  198. if (!app)
  199. {
  200. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  201. return;
  202. }
  203. // store the entered text
  204. strncpy(app->uart_text_input_buffer_pass, app->uart_text_input_temp_buffer_pass, app->uart_text_input_buffer_size_pass);
  205. // Ensure null-termination
  206. app->uart_text_input_buffer_pass[app->uart_text_input_buffer_size_pass - 1] = '\0';
  207. // update the variable item text
  208. if (app->variable_item_pass)
  209. {
  210. variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer_pass);
  211. }
  212. // save the settings
  213. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  214. // if SSID and PASS are not empty, connect to the WiFi
  215. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  216. {
  217. // save wifi settings
  218. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  219. {
  220. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  221. }
  222. }
  223. // switch to the settings view
  224. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  225. }
  226. uint32_t callback_to_submenu(void *context)
  227. {
  228. if (!context)
  229. {
  230. FURI_LOG_E(TAG, "Context is NULL");
  231. return VIEW_NONE;
  232. }
  233. UNUSED(context);
  234. return FlipStoreViewSubmenu;
  235. }
  236. uint32_t callback_to_submenu_options(void *context)
  237. {
  238. if (!context)
  239. {
  240. FURI_LOG_E(TAG, "Context is NULL");
  241. return VIEW_NONE;
  242. }
  243. UNUSED(context);
  244. return FlipStoreViewSubmenuOptions;
  245. }
  246. uint32_t callback_to_app_list(void *context)
  247. {
  248. if (!context)
  249. {
  250. FURI_LOG_E(TAG, "Context is NULL");
  251. return VIEW_NONE;
  252. }
  253. UNUSED(context);
  254. flip_store_sent_request = false;
  255. flip_store_success = false;
  256. flip_store_saved_data = false;
  257. flip_store_saved_success = false;
  258. flip_store_app_does_exist = false;
  259. return FlipStoreViewAppList;
  260. }
  261. void settings_item_selected(void *context, uint32_t index)
  262. {
  263. FlipStoreApp *app = (FlipStoreApp *)context;
  264. if (!app)
  265. {
  266. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  267. return;
  268. }
  269. switch (index)
  270. {
  271. case 0: // Input SSID
  272. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  273. break;
  274. case 1: // Input Password
  275. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  276. break;
  277. default:
  278. FURI_LOG_E(TAG, "Unknown configuration item index");
  279. break;
  280. }
  281. }
  282. void dialog_callback(DialogExResult result, void *context)
  283. {
  284. furi_assert(context);
  285. FlipStoreApp *app = (FlipStoreApp *)context;
  286. if (result == DialogExResultLeft) // No
  287. {
  288. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  289. }
  290. else if (result == DialogExResultRight)
  291. {
  292. // delete the app then return to the app list
  293. if (!delete_app(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]))
  294. {
  295. // pop up a message
  296. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  297. popup_set_text(app->popup, "Issue deleting app.", 0, 50, AlignLeft, AlignTop);
  298. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  299. furi_delay_ms(2000); // delay for 2 seconds
  300. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  301. }
  302. else
  303. {
  304. // pop up a message
  305. popup_set_header(app->popup, "[SUCCESS]", 0, 0, AlignLeft, AlignTop);
  306. popup_set_text(app->popup, "App deleted successfully.", 0, 50, AlignLeft, AlignTop);
  307. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  308. furi_delay_ms(2000); // delay for 2 seconds
  309. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  310. }
  311. }
  312. }
  313. void popup_callback(void *context)
  314. {
  315. FlipStoreApp *app = (FlipStoreApp *)context;
  316. if (!app)
  317. {
  318. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  319. return;
  320. }
  321. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  322. }
  323. uint32_t callback_exit_app(void *context)
  324. {
  325. // Exit the application
  326. if (!context)
  327. {
  328. FURI_LOG_E(TAG, "Context is NULL");
  329. return VIEW_NONE;
  330. }
  331. UNUSED(context);
  332. return VIEW_NONE; // Return VIEW_NONE to exit the app
  333. }
  334. void callback_submenu_choices(void *context, uint32_t index)
  335. {
  336. FlipStoreApp *app = (FlipStoreApp *)context;
  337. if (!app)
  338. {
  339. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  340. return;
  341. }
  342. switch (index)
  343. {
  344. case FlipStoreSubmenuIndexMain:
  345. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  346. break;
  347. case FlipStoreSubmenuIndexAbout:
  348. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  349. break;
  350. case FlipStoreSubmenuIndexSettings:
  351. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  352. break;
  353. case FlipStoreSubmenuIndexOptions:
  354. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  355. break;
  356. case FlipStoreSubmenuIndexAppList:
  357. flip_store_category_index = 0;
  358. flip_store_app_does_exist = false;
  359. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  360. break;
  361. case FlipStoreSubmenuIndexFirmwares:
  362. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  363. break;
  364. case FlipStoreSubmenuIndexAppListBluetooth:
  365. flip_store_category_index = 0;
  366. flip_store_app_does_exist = false;
  367. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListBluetooth, "Bluetooth", &app->submenu_app_list_bluetooth));
  368. break;
  369. case FlipStoreSubmenuIndexAppListGames:
  370. flip_store_category_index = 1;
  371. flip_store_app_does_exist = false;
  372. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGames, "Games", &app->submenu_app_list_games));
  373. break;
  374. case FlipStoreSubmenuIndexAppListGPIO:
  375. flip_store_category_index = 2;
  376. flip_store_app_does_exist = false;
  377. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGPIO, "GPIO", &app->submenu_app_list_gpio));
  378. break;
  379. case FlipStoreSubmenuIndexAppListInfrared:
  380. flip_store_category_index = 3;
  381. flip_store_app_does_exist = false;
  382. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListInfrared, "Infrared", &app->submenu_app_list_infrared));
  383. break;
  384. case FlipStoreSubmenuIndexAppListiButton:
  385. flip_store_category_index = 4;
  386. flip_store_app_does_exist = false;
  387. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListiButton, "iButton", &app->submenu_app_list_ibutton));
  388. break;
  389. case FlipStoreSubmenuIndexAppListMedia:
  390. flip_store_category_index = 5;
  391. flip_store_app_does_exist = false;
  392. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListMedia, "Media", &app->submenu_app_list_media));
  393. break;
  394. case FlipStoreSubmenuIndexAppListNFC:
  395. flip_store_category_index = 6;
  396. flip_store_app_does_exist = false;
  397. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListNFC, "NFC", &app->submenu_app_list_nfc));
  398. break;
  399. case FlipStoreSubmenuIndexAppListRFID:
  400. flip_store_category_index = 7;
  401. flip_store_app_does_exist = false;
  402. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListRFID, "RFID", &app->submenu_app_list_rfid));
  403. break;
  404. case FlipStoreSubmenuIndexAppListSubGHz:
  405. flip_store_category_index = 8;
  406. flip_store_app_does_exist = false;
  407. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListSubGHz, "Sub-GHz", &app->submenu_app_list_subghz));
  408. break;
  409. case FlipStoreSubmenuIndexAppListTools:
  410. flip_store_category_index = 9;
  411. flip_store_app_does_exist = false;
  412. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListTools, "Tools", &app->submenu_app_list_tools));
  413. break;
  414. case FlipStoreSubmenuIndexAppListUSB:
  415. flip_store_category_index = 10;
  416. flip_store_app_does_exist = false;
  417. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListUSB, "USB", &app->submenu_app_list_usb));
  418. break;
  419. default:
  420. // Check if the index is within the firmwares list range
  421. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  422. {
  423. // Get the firmware index
  424. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  425. // Check if the firmware index is valid
  426. if ((int)firmware_index >= 0 && firmware_index < 3)
  427. {
  428. // Get the firmware name
  429. char *firmware_name = firmwares[firmware_index];
  430. // Check if the firmware name is valid
  431. if (firmware_name != NULL && strlen(firmware_name) > 0)
  432. {
  433. // do nothing for now
  434. popup_set_header(app->popup, firmware_name, 0, 0, AlignLeft, AlignTop);
  435. popup_set_text(app->popup, "Not implemented yet :D", 0, 50, AlignLeft, AlignTop);
  436. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  437. }
  438. else
  439. {
  440. FURI_LOG_E(TAG, "Invalid firmware name");
  441. }
  442. }
  443. else
  444. {
  445. FURI_LOG_E(TAG, "Invalid firmware index");
  446. }
  447. }
  448. // Check if the index is within the app list range
  449. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  450. {
  451. // Get the app index
  452. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  453. // Check if the app index is valid
  454. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  455. {
  456. // Get the app name
  457. char *app_name = flip_catalog[app_index].app_name;
  458. // Check if the app name is valid
  459. if (app_name != NULL && strlen(app_name) > 0)
  460. {
  461. app_selected_index = app_index;
  462. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  463. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  464. }
  465. else
  466. {
  467. FURI_LOG_E(TAG, "Invalid app name");
  468. }
  469. }
  470. else
  471. {
  472. FURI_LOG_E(TAG, "Invalid app index");
  473. }
  474. }
  475. else
  476. {
  477. FURI_LOG_E(TAG, "Unknown submenu index");
  478. }
  479. break;
  480. }
  481. }