Files
Crosspoint/src/activities/settings/ButtonRemapActivity.cpp
T
Xuan-Son NguyenandZach Nelson a94f6ca80b refactor: implement ActivityManager (#1016)
## Summary

Ref comment:
https://github.com/crosspoint-reader/crosspoint-reader/pull/1010#pullrequestreview-3828854640

This PR introduces `ActivityManager`, which mirrors the same concept of
Activity in Android, where an activity represents a single screen of the
UI. The manager is responsible for launching activities, and ensuring
that only one activity is active at a time.

Main differences from Android's ActivityManager:
- No concept of Bundle or Intent extras
- No onPause/onResume, since we don't have a concept of background
activities
- onActivityResult is implemented via a callback instead of a separate
method, for simplicity

## Key changes

- Single `renderTask` shared across all activities
- No more sub-activity, we manage them using a stack; Results can be
passed via `startActivityForResult` and `setResult`
- Activity can call `finish()` to destroy themself, but the actual
deletion will be handled by `ActivityManager` to avoid `delete this`
pattern

As a bonus: the manager will automatically call `requestUpdate()` when
returning from another activity

## Example usage

**BEFORE**:

```cpp
// caller
    enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
                                               [this](const bool connected) { onWifiSelectionComplete(connected); }));

// subactivity
  onComplete(true); // will eventually call exitActivity(), which deletes the caller instance (dangerous behavior)
``` 

**AFTER**: (mirrors the `startActivityForResult` and `setResult` from
android)

```cpp
// caller
  startActivityForResult(new NetworkModeSelectionActivity(renderer, mappedInput),
                         [this](const ActivityResult& result) { onNetworkModeSelected(result.selectedNetworkMode); });

// subactivity
  ActivityResult result;
  result.isCancelled = false;
  result.selectedNetworkMode = mode;
  setResult(result);
  finish(); // signals to ActivityManager to go back to last activity AFTER this function returns
```

TODO:
- [x] Reconsider if the `Intent` is really necessary or it should be
removed (note: it's inspired by
[Intent](https://developer.android.com/guide/components/intents-common)
from Android API) ==> I decided to keep this pattern fr clarity
- [x] Verify if behavior is still correct (i.e. back from sub-activity)
- [x] Refactor the `ActivityWithSubactivity` to just simple `Activity`
--> We are using a stack for keeping track of sub-activity now
- [x] Use single task for rendering --> avoid allocating 8KB stack per
activity
- [x] Implement the idea of [Activity
result](https://developer.android.com/training/basics/intents/result)
--> Allow sub-activity like Wifi to report back the status (connected,
failed, etc)

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **PARTIALLY**, some
repetitive migrations are done by Claude, but I'm the one how ultimately
approve it

---------

Co-authored-by: Zach Nelson <zach@zdnelson.com>
2026-02-27 00:32:40 -06:00

200 lines
6.7 KiB
C++

#include "ButtonRemapActivity.h"
#include <GfxRenderer.h>
#include <I18n.h>
#include "CrossPointSettings.h"
#include "MappedInputManager.h"
#include "components/UITheme.h"
#include "fontIds.h"
namespace {
// UI steps correspond to logical roles in order: Back, Confirm, Left, Right.
constexpr uint8_t kRoleCount = 4;
// Marker used when a role has not been assigned yet.
constexpr uint8_t kUnassigned = 0xFF;
// Duration to show temporary error text when reassigning a button.
constexpr unsigned long kErrorDisplayMs = 1500;
} // namespace
void ButtonRemapActivity::onEnter() {
Activity::onEnter();
// Start with all roles unassigned to avoid duplicate blocking.
currentStep = 0;
tempMapping[0] = kUnassigned;
tempMapping[1] = kUnassigned;
tempMapping[2] = kUnassigned;
tempMapping[3] = kUnassigned;
errorMessage.clear();
errorUntil = 0;
requestUpdate();
}
void ButtonRemapActivity::onExit() { Activity::onExit(); }
void ButtonRemapActivity::loop() {
// Clear any temporary warning after its timeout.
if (errorUntil > 0 && millis() > errorUntil) {
errorMessage.clear();
errorUntil = 0;
requestUpdate();
return;
}
// Side buttons:
// - Up: reset mapping to defaults and exit.
// - Down: cancel without saving.
if (mappedInput.wasPressed(MappedInputManager::Button::Up)) {
// Persist default mapping immediately so the user can recover quickly.
SETTINGS.frontButtonBack = CrossPointSettings::FRONT_HW_BACK;
SETTINGS.frontButtonConfirm = CrossPointSettings::FRONT_HW_CONFIRM;
SETTINGS.frontButtonLeft = CrossPointSettings::FRONT_HW_LEFT;
SETTINGS.frontButtonRight = CrossPointSettings::FRONT_HW_RIGHT;
SETTINGS.saveToFile();
finish();
return;
}
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
// Exit without changing settings.
finish();
return;
}
{
// Make sure UI done rendering before accepting another assignment.
// This avoids rapid double-presses that can advance the step without a visible redraw.
RenderLock lock(*this);
// Wait for a front button press to assign to the current role.
const int pressedButton = mappedInput.getPressedFrontButton();
if (pressedButton < 0) {
return;
}
// Update temporary mapping and advance the remap step.
// Only accept the press if this hardware button isn't already assigned elsewhere.
if (!validateUnassigned(static_cast<uint8_t>(pressedButton))) {
requestUpdate();
return;
}
tempMapping[currentStep] = static_cast<uint8_t>(pressedButton);
currentStep++;
if (currentStep >= kRoleCount) {
// All roles assigned; save to settings and exit.
applyTempMapping();
SETTINGS.saveToFile();
finish();
return;
}
requestUpdate();
}
}
void ButtonRemapActivity::render(RenderLock&&) {
const auto labelForHardware = [&](uint8_t hardwareIndex) -> const char* {
for (uint8_t i = 0; i < kRoleCount; i++) {
if (tempMapping[i] == hardwareIndex) {
return getRoleName(i);
}
}
return "-";
};
const auto& metrics = UITheme::getInstance().getMetrics();
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
renderer.clearScreen();
GUI.drawHeader(renderer, Rect{0, metrics.topPadding, pageWidth, metrics.headerHeight}, tr(STR_REMAP_FRONT_BUTTONS));
GUI.drawSubHeader(renderer, Rect{0, metrics.topPadding + metrics.headerHeight, pageWidth, metrics.tabBarHeight},
tr(STR_REMAP_PROMPT));
int topOffset = metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.verticalSpacing;
int contentHeight = pageHeight - topOffset - metrics.buttonHintsHeight - metrics.verticalSpacing;
GUI.drawList(
renderer, Rect{0, topOffset, pageWidth, contentHeight}, kRoleCount, currentStep,
[&](int index) { return getRoleName(static_cast<uint8_t>(index)); }, nullptr, nullptr,
[&](int index) {
uint8_t assignedButton = tempMapping[static_cast<uint8_t>(index)];
return (assignedButton == kUnassigned) ? tr(STR_UNASSIGNED) : getHardwareName(assignedButton);
},
true);
// Temporary warning banner for duplicates.
if (!errorMessage.empty()) {
GUI.drawHelpText(renderer,
Rect{0, pageHeight - metrics.buttonHintsHeight - metrics.contentSidePadding - 15, pageWidth, 20},
errorMessage.c_str());
}
// Provide side button actions at the bottom of the screen (split across two lines).
GUI.drawHelpText(renderer,
Rect{0, topOffset + 4 * metrics.listRowHeight + 4 * metrics.verticalSpacing, pageWidth, 20},
tr(STR_REMAP_RESET_HINT));
GUI.drawHelpText(renderer,
Rect{0, topOffset + 4 * metrics.listRowHeight + 5 * metrics.verticalSpacing + 20, pageWidth, 20},
tr(STR_REMAP_CANCEL_HINT));
// Live preview of logical labels under front buttons.
// This mirrors the on-device front button order: Back, Confirm, Left, Right.
GUI.drawButtonHints(renderer, labelForHardware(CrossPointSettings::FRONT_HW_BACK),
labelForHardware(CrossPointSettings::FRONT_HW_CONFIRM),
labelForHardware(CrossPointSettings::FRONT_HW_LEFT),
labelForHardware(CrossPointSettings::FRONT_HW_RIGHT));
renderer.displayBuffer();
}
void ButtonRemapActivity::applyTempMapping() {
// Commit temporary mapping into settings (logical role -> hardware).
SETTINGS.frontButtonBack = tempMapping[0];
SETTINGS.frontButtonConfirm = tempMapping[1];
SETTINGS.frontButtonLeft = tempMapping[2];
SETTINGS.frontButtonRight = tempMapping[3];
}
bool ButtonRemapActivity::validateUnassigned(const uint8_t pressedButton) {
// Block reusing a hardware button already assigned to another role.
for (uint8_t i = 0; i < kRoleCount; i++) {
if (tempMapping[i] == pressedButton && i != currentStep) {
errorMessage = tr(STR_ALREADY_ASSIGNED);
errorUntil = millis() + kErrorDisplayMs;
return false;
}
}
return true;
}
const char* ButtonRemapActivity::getRoleName(const uint8_t roleIndex) const {
switch (roleIndex) {
case 0:
return tr(STR_BACK);
case 1:
return tr(STR_CONFIRM);
case 2:
return tr(STR_DIR_LEFT);
case 3:
default:
return tr(STR_DIR_RIGHT);
}
}
const char* ButtonRemapActivity::getHardwareName(const uint8_t buttonIndex) const {
switch (buttonIndex) {
case CrossPointSettings::FRONT_HW_BACK:
return tr(STR_HW_BACK_LABEL);
case CrossPointSettings::FRONT_HW_CONFIRM:
return tr(STR_HW_CONFIRM_LABEL);
case CrossPointSettings::FRONT_HW_LEFT:
return tr(STR_HW_LEFT_LABEL);
case CrossPointSettings::FRONT_HW_RIGHT:
return tr(STR_HW_RIGHT_LABEL);
default:
return "Unknown";
}
}