From da53d2dbed4e679cc2817c2990836760f139d7a2 Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Fri, 9 Feb 2024 13:19:44 -0500 Subject: [PATCH] settings: Add setting for auto-opening apps Certain apps can be configured to auto-open based on component events: - battery: when charging the watch - music: when playing music from the paired device - navigation: when opening a maps app on the paired device --- src/CMakeLists.txt | 1 + src/components/settings/Settings.h | 18 +++++ src/displayapp/DisplayApp.cpp | 30 +++++--- src/displayapp/apps/Apps.h.in | 1 + src/displayapp/fonts/fonts.json | 2 +- src/displayapp/screens/Symbols.h | 1 + .../screens/settings/SettingAutoOpen.cpp | 75 +++++++++++++++++++ .../screens/settings/SettingAutoOpen.h | 39 ++++++++++ src/displayapp/screens/settings/Settings.h | 1 + src/systemtask/SystemTask.cpp | 8 +- 10 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 src/displayapp/screens/settings/SettingAutoOpen.cpp create mode 100644 src/displayapp/screens/settings/SettingAutoOpen.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0f872f46e6..eb98eb7042 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -415,6 +415,7 @@ list(APPEND SOURCE_FILES displayapp/screens/settings/SettingSetDate.cpp displayapp/screens/settings/SettingSetTime.cpp displayapp/screens/settings/SettingChimes.cpp + displayapp/screens/settings/SettingAutoOpen.cpp displayapp/screens/settings/SettingShakeThreshold.cpp displayapp/screens/settings/SettingBluetooth.cpp diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 063120774a..e3be110a02 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -14,6 +14,7 @@ namespace Pinetime { enum class Notification : uint8_t { On, Off, Sleep }; enum class ChimesOption : uint8_t { None, Hours, HalfHours }; enum class WakeUpMode : uint8_t { SingleTap = 0, DoubleTap = 1, RaiseWrist = 2, Shake = 3, LowerWrist = 4 }; + enum class AutoOpen : uint8_t { Battery = 0, Music = 1, Navigation = 2 }; enum class Colors : uint8_t { White, Silver, @@ -253,6 +254,21 @@ namespace Pinetime { return getWakeUpModes()[static_cast(mode)]; } + void SetAutoOpen(AutoOpen app, bool enabled) { + if (enabled != IsAutoOpenOn(app)) { + settingsChanged = true; + } + settings.autoOpen.set(static_cast(app), enabled); + } + + bool IsAutoOpenOn(const AutoOpen app) const { + return GetAutoOpen()[static_cast(app)]; + } + + std::bitset<3> GetAutoOpen() const { + return settings.autoOpen; + } + void SetBrightness(Controllers::BrightnessController::Levels level) { if (level != settings.brightLevel) { settingsChanged = true; @@ -308,6 +324,8 @@ namespace Pinetime { uint16_t shakeWakeThreshold = 150; Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; + + std::bitset<3> autoOpen {0b000}; }; SettingsData settings; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 71e7fcd7fd..672ef659ca 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -46,6 +46,7 @@ #include "displayapp/screens/settings/SettingSteps.h" #include "displayapp/screens/settings/SettingSetDateTime.h" #include "displayapp/screens/settings/SettingChimes.h" +#include "displayapp/screens/settings/SettingAutoOpen.h" #include "displayapp/screens/settings/SettingShakeThreshold.h" #include "displayapp/screens/settings/SettingBluetooth.h" @@ -399,15 +400,23 @@ void DisplayApp::Refresh() { break; case Messages::OnChargingEvent: RestoreBrightness(); - if (batteryController.IsCharging() && currentApp == Apps::Clock) { - // Open the battery app if on the clock screen - LoadNewScreen(Apps::BatteryInfo, DisplayApp::FullRefreshDirections::None); - } else if (!batteryController.IsCharging() && currentApp == Apps::BatteryInfo) { - // Close the battery app after being unplugged - LoadNewScreen(Apps::Clock, DisplayApp::FullRefreshDirections::None); - } else { - // Vibrate normally otherwise as to not close any open app - motorController.RunForDuration(15); + switch (currentApp) { + case Apps::Clock: + if (batteryController.IsCharging() && settingsController.IsAutoOpenOn(Controllers::Settings::AutoOpen::Battery)) { + // Open the battery app if on the clock screen + LoadNewScreen(Apps::BatteryInfo, DisplayApp::FullRefreshDirections::None); + } + break; + case Apps::BatteryInfo: + if (!batteryController.IsCharging()) { + // Close the battery app after being unplugged + LoadNewScreen(Apps::Clock, DisplayApp::FullRefreshDirections::None); + } + break; + default: + // Vibrate normally otherwise as to not close any open app + motorController.RunForDuration(15); + break; } break; } @@ -546,6 +555,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio case Apps::SettingChimes: currentScreen = std::make_unique(settingsController); break; + case Apps::SettingAutoOpen: + currentScreen = std::make_unique(settingsController); + break; case Apps::SettingShakeThreshold: currentScreen = std::make_unique(settingsController, motionController, *systemTask); break; diff --git a/src/displayapp/apps/Apps.h.in b/src/displayapp/apps/Apps.h.in index 77d3b366f3..3858af5715 100644 --- a/src/displayapp/apps/Apps.h.in +++ b/src/displayapp/apps/Apps.h.in @@ -39,6 +39,7 @@ namespace Pinetime { SettingSteps, SettingSetDateTime, SettingChimes, + SettingAutoOpen, SettingShakeThreshold, SettingBluetooth, Error, diff --git a/src/displayapp/fonts/fonts.json b/src/displayapp/fonts/fonts.json index 57341245d5..e86f059e23 100644 --- a/src/displayapp/fonts/fonts.json +++ b/src/displayapp/fonts/fonts.json @@ -7,7 +7,7 @@ }, { "file": "FontAwesome5-Solid+Brands+Regular.woff", - "range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf522, 0xf743" + "range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf522, 0xf743, 0xf0ad" } ], "bpp": 1, diff --git a/src/displayapp/screens/Symbols.h b/src/displayapp/screens/Symbols.h index 4434194b6c..a19c48996e 100644 --- a/src/displayapp/screens/Symbols.h +++ b/src/displayapp/screens/Symbols.h @@ -38,6 +38,7 @@ namespace Pinetime { static constexpr const char* eye = "\xEF\x81\xAE"; static constexpr const char* home = "\xEF\x80\x95"; static constexpr const char* sleep = "\xEE\xBD\x84"; + static constexpr const char* wrench = "\xEF\x82\xAD"; // fontawesome_weathericons.c // static constexpr const char* sun = "\xEF\x86\x85"; diff --git a/src/displayapp/screens/settings/SettingAutoOpen.cpp b/src/displayapp/screens/settings/SettingAutoOpen.cpp new file mode 100644 index 0000000000..da9e0c4d43 --- /dev/null +++ b/src/displayapp/screens/settings/SettingAutoOpen.cpp @@ -0,0 +1,75 @@ +#include "displayapp/screens/settings/SettingAutoOpen.h" +#include +#include "displayapp/DisplayApp.h" +#include "displayapp/screens/Screen.h" +#include "displayapp/screens/Symbols.h" + +using namespace Pinetime::Applications::Screens; + +constexpr std::array SettingAutoOpen::options; + +namespace { + void event_handler(lv_obj_t* obj, lv_event_t event) { + auto* screen = static_cast(obj->user_data); + if (event == LV_EVENT_VALUE_CHANGED) { + screen->UpdateSelected(obj); + } + } +} + +SettingAutoOpen::SettingAutoOpen(Pinetime::Controllers::Settings& settingsController) : settingsController {settingsController} { + lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr); + + lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP); + lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10); + lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5); + lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0); + + lv_obj_set_pos(container1, 10, 35); + lv_obj_set_width(container1, LV_HOR_RES - 20); + lv_obj_set_height(container1, LV_VER_RES - 20); + lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT); + + lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr); + lv_label_set_text_static(title, "Auto Open"); + lv_label_set_align(title, LV_LABEL_ALIGN_CENTER); + lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15); + + lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE); + lv_label_set_text_static(icon, Symbols::wrench); + lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER); + lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0); + + for (unsigned int i = 0; i < options.size(); i++) { + cbOption[i] = lv_checkbox_create(container1, nullptr); + lv_checkbox_set_text(cbOption[i], options[i].name); + if (settingsController.IsAutoOpenOn(static_cast(i))) { + lv_checkbox_set_checked(cbOption[i], true); + } + cbOption[i]->user_data = this; + lv_obj_set_event_cb(cbOption[i], event_handler); + } +} + +SettingAutoOpen::~SettingAutoOpen() { + lv_obj_clean(lv_scr_act()); + settingsController.SaveSettings(); +} + +void SettingAutoOpen::UpdateSelected(lv_obj_t* object) { + // Find the index of the checkbox that triggered the event + for (size_t i = 0; i < options.size(); i++) { + if (cbOption[i] == object) { + bool currentState = settingsController.IsAutoOpenOn(options[i].app); + settingsController.SetAutoOpen(options[i].app, !currentState); + break; + } + } + + // Update checkbox according to current apps. + auto apps = settingsController.GetAutoOpen(); + for (size_t i = 0; i < options.size(); ++i) { + lv_checkbox_set_checked(cbOption[i], apps[i]); + } +} diff --git a/src/displayapp/screens/settings/SettingAutoOpen.h b/src/displayapp/screens/settings/SettingAutoOpen.h new file mode 100644 index 0000000000..6d9987de8c --- /dev/null +++ b/src/displayapp/screens/settings/SettingAutoOpen.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#include "components/settings/Settings.h" +#include "displayapp/screens/Screen.h" + +namespace Pinetime { + + namespace Applications { + namespace Screens { + + class SettingAutoOpen : public Screen { + public: + SettingAutoOpen(Pinetime::Controllers::Settings& settingsController); + ~SettingAutoOpen() override; + + void UpdateSelected(lv_obj_t* object); + + private: + struct Option { + Controllers::Settings::AutoOpen app; + const char* name; + }; + + Controllers::Settings& settingsController; + static constexpr std::array options = {{ + {Controllers::Settings::AutoOpen::Battery, "Battery"}, + {Controllers::Settings::AutoOpen::Music, "Music"}, + {Controllers::Settings::AutoOpen::Navigation, "Navigation"}, + }}; + + lv_obj_t* cbOption[options.size()]; + }; + } + } +} diff --git a/src/displayapp/screens/settings/Settings.h b/src/displayapp/screens/settings/Settings.h index a21b4ccd85..115efecce8 100644 --- a/src/displayapp/screens/settings/Settings.h +++ b/src/displayapp/screens/settings/Settings.h @@ -47,6 +47,7 @@ namespace Pinetime { {Symbols::check, "Firmware", Apps::FirmwareValidation}, {Symbols::bluetooth, "Bluetooth", Apps::SettingBluetooth}, + {Symbols::wrench, "Auto Open", Apps::SettingAutoOpen}, {Symbols::list, "About", Apps::SysInfo}, // {Symbols::none, "None", Apps::None}, diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index b5a501f475..bb1890d2c5 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -386,10 +386,14 @@ void SystemTask::Work() { } break; case Messages::OnMusicStarted: - displayApp.PushMessage(Pinetime::Applications::Display::Messages::MusicStarted); + if (settingsController.IsAutoOpenOn(Controllers::Settings::AutoOpen::Music)) { + displayApp.PushMessage(Pinetime::Applications::Display::Messages::MusicStarted); + } break; case Messages::OnNavChange: - displayApp.PushMessage(Pinetime::Applications::Display::Messages::NavStarted); + if (settingsController.IsAutoOpenOn(Controllers::Settings::AutoOpen::Navigation)) { + displayApp.PushMessage(Pinetime::Applications::Display::Messages::NavStarted); + } break; default: break;