Skip to content

watchPositionAsyncの直接コールバックを併用してAndroid 16のJobSchedulerクォータによるバックグラウンド位置更新のスロットルを回避 - #5427

Merged
TinyKitten merged 4 commits into
devfrom
claude/fix-android16-location-updates-LYkbg
Feb 22, 2026

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Feb 22, 2026 •

Copy link
Copy Markdown
Member

https://claude.ai/code/session_01RMxd9GFPAFTnDB531mMTrP

Summary by CodeRabbit

  • テスト

    • バックグラウンド位置更新のテストを拡張し、バイパス条件、直接コールバック、クリーンアップ、失敗時の挙動を確認するケースを追加しました
  • 改善

    • Androidの新しい制約に対応するバイパス経路を導入し、Foreground時のリアルタイム位置更新とクリーンな解除処理を強化しました
  • ログ

    • 直接コールバック失敗時に警告ログを出す挙動を明確化しました

@github-actions github-actions Bot added the react label Feb 22, 2026
@coderabbitai

coderabbitai Bot commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

useStartBackgroundLocationUpdatesにNEEDS_JOBSCHEDULER_BYPASS経由の直接watchPositionAsyncパスを追加し、サブスクリプション追跡・起動失敗時の警告・確実なクリーンアップを実装。NEEDS_JOBSCHEDULER_BYPASSとLOCATION_WATCH_OPTIONSの定数を追加しました。

Changes

Cohort / File(s) Summary
バックグラウンド位置更新ロジック
src/hooks/useStartBackgroundLocationUpdates.ts
NEEDS_JOBSCHEDULER_BYPASS参照分岐を追加。watchPositionSubを導入してLocation.watchPositionAsyncで直接サブスクリプションを開始・保持し、起動失敗時に警告ログ。キャンセル・アンマウント時の確実な削除(クリーンアップ)を追加。
定数追加・分割
src/constants/native.ts, src/constants/location.ts
NEEDS_JOBSCHEDULER_BYPASSを追加(Android API >= 36 判定)。LOCATION_WATCH_OPTIONSを新設し、LOCATION_TASK_OPTIONSを...LOCATION_WATCH_OPTIONSで構成するようにリファクタ。
テストスイート拡張
src/hooks/useStartBackgroundLocationUpdates.test.tsx
NEEDS_JOBSCHEDULER_BYPASSをモック可能にし、バイパス時のwatchPositionAsync起動/非起動パス、アンマウント時クリーンアップ、直接コールバック起動失敗時の警告ハンドリングを追加。既存テスト名を調整。

Sequence Diagram

sequenceDiagram
    participant App as App/Component
    participant Hook as useStartBackgroundLocationUpdates
    participant LocationAPI as Location API
    participant Watch as watchPositionAsync

    App->>Hook: マウント(bgPermGranted=true)
    activate Hook
    Hook->>LocationAPI: startLocationUpdatesAsync(...)
    activate LocationAPI
    LocationAPI-->>Hook: 成功
    deactivate LocationAPI

    alt NEEDS_JOBSCHEDULER_BYPASS == true
        Hook->>Watch: watchPositionAsync(LOCATION_WATCH_OPTIONS, cb)
        activate Watch
        Watch-->>Hook: Subscription を返す
        deactivate Watch
        Watch->>App: リアルタイム位置更新を送信
    end

    App->>Hook: アンマウント/キャンセル
    Hook->>Watch: remove()(watchPositionSub があれば)
    Hook->>LocationAPI: stopLocationUpdatesAsync(...)
    deactivate Hook
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested labels

android

Poem

🐰 うさぎの囁き、位置の詩
ジョブの網をすり抜けてぴょんぴょん
watchが走れば瞬間が届くよ
終わりにはきちんとサヨナラ掃除
新しい日へ、軽やかに跳ぶ 🌤️

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed プルリクエストのタイトルは、Android 16のJobSchedulerクォータ制限を回避するためにwatchPositionAsyncの直接コールバックを使用する変更について、正確かつ具体的に説明しており、変更セットの主要な目的を明確に反映しています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/fix-android16-location-updates-LYkbg

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/hooks/useStartBackgroundLocationUpdates.ts`:
- Line 34: watchPositionAsync is being passed LOCATION_TASK_OPTIONS (a
LocationTaskOptions) which includes task-only fields like
deferredUpdatesInterval that watchPositionAsync ignores; create a proper
Location.LocationOptions object (or derive one by picking only accuracy,
timeInterval, distanceInterval, mayShowUserSettingsDialog, etc.) from
LOCATION_TASK_OPTIONS and pass that to watchPositionAsync in the
useStartBackgroundLocationUpdates hook (update the two calls that currently pass
LOCATION_TASK_OPTIONS), ensuring the runtime type matches
Location.LocationOptions and removing task-only keys before invoking
watchPositionAsync.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/hooks/useStartBackgroundLocationUpdates.test.tsx (1)

52-60: ⚠️ Potential issue | 🟡 Minor

afterEachでのjest.clearAllMocks()が必要です。
ガイドラインに従い、afterEachにもクリアを追加してください(必要ならbeforeEachは維持でOK)。

修正案
   beforeEach(() => {
     // React Testing Libraryのauto-cleanupはafterEachでフックをunmountし、
     // effectクリーンアップ(stopLocationUpdatesAsync等)を発火する。
     // このクリーンアップは登録順の関係でafterEachよりも後に実行されるため、
     // beforeEachでclearAllMocksを行い、前テストの残留呼び出しを確実にリセットする。
     jest.clearAllMocks();
     mockAutoModeEnabled = false;
     mockNeedsJobSchedulerBypass = false;
     mockStartLocationUpdatesAsync.mockResolvedValue(undefined);
     mockStopLocationUpdatesAsync.mockResolvedValue(undefined);
     mockHasStartedLocationUpdatesAsync.mockResolvedValue(false);
     mockWatchPositionAsync.mockResolvedValue({ remove: mockRemove });
   });
+  afterEach(() => {
+    jest.clearAllMocks();
+  });

As per coding guidelines, “Call jest.clearAllMocks() in afterEach blocks in all test files”.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/hooks/useStartBackgroundLocationUpdates.test.tsx` around lines 52 - 60,
Add a call to jest.clearAllMocks() in the test file's afterEach block to match
the project guideline (you may keep the existing beforeEach clear).
Specifically, update the test teardown (afterEach) so it invokes
jest.clearAllMocks() before any other cleanup, ensuring mocks like
mockStartLocationUpdatesAsync and flags (mockAutoModeEnabled,
mockNeedsJobSchedulerBypass) are reset after each test; leave the existing
beforeEach setup intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/hooks/useStartBackgroundLocationUpdates.test.tsx`:
- Around line 52-60: Add a call to jest.clearAllMocks() in the test file's
afterEach block to match the project guideline (you may keep the existing
beforeEach clear). Specifically, update the test teardown (afterEach) so it
invokes jest.clearAllMocks() before any other cleanup, ensuring mocks like
mockStartLocationUpdatesAsync and flags (mockAutoModeEnabled,
mockNeedsJobSchedulerBypass) are reset after each test; leave the existing
beforeEach setup intact.

@TinyKitten
TinyKitten merged commit 1f81f86 into dev Feb 22, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the claude/fix-android16-location-updates-LYkbg branch February 22, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants