Files
musicdl-catalog-sync-suite/MusicFree/docs/superpowers/plans/2026-04-21-ignore-battery-optimization-implementation.md
T

3.0 KiB

Ignore Battery Optimization Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.

Goal: 补齐 Android 忽略电池优化权限链路,减少锁屏后一段时间被系统回收导致的停播问题。

Architecture: 复用现有 NativeUtils 原生模块承接 Android 电池优化查询与申请能力,在权限页增加对应入口和状态展示。JS 层先补回归测试,再用最小改动补 Manifest、原生模块、类型声明与文案。

Tech Stack: React Native, Kotlin Android module, Jest, react-test-renderer


Task 1: Permissions Page Regression Test

Files:

  • Create: src/pages/permissions/index.test.tsx

  • Modify: src/pages/permissions/index.tsx

  • Step 1: Write the failing test

it("renders ignore battery optimization entry and calls native request handler", async () => {
  // mock NativeUtils.isIgnoringBatteryOptimizations -> true
  // render Permissions
  // assert translated entry exists
  // trigger onPress
  // expect NativeUtils.requestIgnoreBatteryOptimizations toHaveBeenCalled()
});
  • Step 2: Run test to verify it fails

Run: npx jest src/pages/permissions/index.test.tsx --runInBand Expected: FAIL because the current page does not render or call battery optimization APIs.

  • Step 3: Write minimal implementation
type IPermissionTypes = "floatingWindow" | "fileStorage" | "batteryOptimization";
updates.batteryOptimization = await NativeUtils.isIgnoringBatteryOptimizations();
onPress={() => NativeUtils.requestIgnoreBatteryOptimizations()}
  • Step 4: Run test to verify it passes

Run: npx jest src/pages/permissions/index.test.tsx --runInBand Expected: PASS

Task 2: Android Native Bridge

Files:

  • Modify: android/app/src/main/AndroidManifest.xml

  • Modify: android/app/src/main/java/fun/upup/musicfree/utils/UtilsModule.kt

  • Modify: src/native/utils/index.ts

  • Modify: src/types/core/i18n/index.d.ts

  • Modify: src/core/i18n/languages/zh-cn.json

  • Modify: src/core/i18n/languages/en-us.json

  • Modify: src/core/i18n/languages/zh-tw.json

  • Step 1: Add Android permission declaration

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
  • Step 2: Add native query and request methods
@ReactMethod
fun isIgnoringBatteryOptimizations(promise: Promise) { ... }

@ReactMethod
fun requestIgnoreBatteryOptimizations(promise: Promise) { ... }
  • Step 3: Expose methods to JS and add i18n keys
isIgnoringBatteryOptimizations: () => Promise<boolean>;
requestIgnoreBatteryOptimizations: () => Promise<boolean>;
  • Step 4: Run targeted verification

Run: npx jest src/pages/permissions/index.test.tsx --runInBand Expected: PASS

Run: npx eslint src/pages/permissions/index.tsx src/pages/permissions/index.test.tsx src/native/utils/index.ts Expected: exit code 0