Skip to content

DRY: typed preferences facade — one owner for keys, defaults and clamping #162

Description

@almothafar

Problem

Preference keys and their defaults are re-stated at every read site. The sharpest case — the warning/critical levels' defaults exist as literals in three places while canonical constants already exist in a fourth:

  • NotificationService.java:1141-1142`` — 40 / `20`
  • BatteryLevelReceiver.java:90-91`` — 40 / `20`
  • MainActivity.java:308-309`` — 20 / `40`
  • [BatteryRangeSliderHelper.java:24-26](https://github.com/almothafar/SimpleBatteryNotifier/blob/f4460acc3ba99320c3efe310fb51456af0939ab0/app/src/main/java/com/almothafar/simplebatterynotifier/ui/preference/BatteryRangeSliderHelper.java#L24-L26)DEFAULT_CRITICAL / DEFAULT_WARNING (used only by the sliders)

If the default ever changes, the alert engine and the UI silently disagree. Beyond this, the getString(R.string._pref_key_…) + inline-default pair is repeated dozens of times across NotificationService, both receivers, both detectors, BatteryRateTracker, TemperatureUtils and the fragments.

Fix

Introduce a small typed facade (e.g. AppPrefs), the single owner of key + default + clamp per setting:

public final class AppPrefs {
    public static int warningLevel(Context ctx) { ... }      // key + default 40
    public static int criticalLevel(Context ctx) { ... }     // key + default 20
    public static boolean vibrateEnabled(Context ctx) { ... }
    public static String chargeNotificationStyle(Context ctx) { ... } // wraps resolveChargeStyle
    public static int drainLimitPph(Context ctx) { ... }      // absorbs clampDrainLimit
    ...
}
  • Existing clamp helpers move in or are wrapped: BatteryRateTracker.clampDrainLimit, FastDrainDetector.clampMinutesToMs — the "a corrupt stored value can't defeat the feature" pattern lives in one place.
  • Keep the pure clamps testable (they already are).
  • Migrate incrementally — start with the duplicated-default offenders above; a full sweep can trail.
  • XML-declared defaults (pref_*.xml) should reference the same values where the framework allows, or a comment ties them together.

(Permalinks pinned at f4460ac; grep the symbol names if lines have moved.)

Acceptance criteria

  • The 40/20 defaults exist in exactly one place, used by alert engine, receiver, and both sliders
  • No raw pref-key reads with inline defaults outside the facade for the migrated settings
  • Clamping behavior unchanged and unit-tested
  • BatteryRangeSliderHelper.DEFAULT_* either move to the facade or delegate to it

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions