Skip to content

Commit ff02532

Browse files
robertyingclaude
andauthored
Say when the system keeps background refresh from running (#1804)
Settings › Notifications says when the system doesn't let the background sync run, so new content can't be found or notified about while learnX is closed, and offers a button to the system settings that turn it back on: Background App Refresh off on iOS (caption only when Screen Time or device management decides), background usage restricted on Android. Low Power Mode, battery optimization and the Mac aren't reported. It shows only while a notification the sync finds is on, and after the notification permission. A new local module, modules/background, reads the state, since expo-background-task's getStatusAsync reports only whether the API exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5808dcc commit ff02532

26 files changed

Lines changed: 468 additions & 13 deletions

‎jest/setup.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ jest.mock('../modules/shortcuts', () => ({ __esModule: true, default: null }));
5555
// The APK updater (Android's github flavor only); tests that install an
5656
// update mock it themselves.
5757
jest.mock('../modules/updater', () => ({ __esModule: true, default: null }));
58+
// Background refresh's state; services/notifications' tests mock it
59+
// themselves.
60+
jest.mock('../modules/background', () => ({
61+
__esModule: true,
62+
default: null,
63+
}));
5864
// Startup reporting (Android only); services/perf's tests mock it themselves.
5965
jest.mock('../modules/perf', () => ({ __esModule: true, default: null }));
6066

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'expo-module-gradle-plugin'
4+
}
5+
6+
group = 'io.robertying.learnx'
7+
version = '1.0.0'
8+
9+
android {
10+
namespace = "io.robertying.learnx.background"
11+
}
12+
13+
apply from: "../../unit-tests.gradle"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.robertying.learnx.background
2+
3+
import android.app.ActivityManager
4+
import android.content.Context
5+
import expo.modules.kotlin.modules.Module
6+
import expo.modules.kotlin.modules.ModuleDefinition
7+
8+
/**
9+
* Whether Android lets the background sync (a WorkManager job) run: "on", or "off" when the user has restricted
10+
* learnX's background battery use (App info › Battery › Restricted, or "Allow background usage" turned off), which
11+
* keeps its jobs from running until the app is opened. Battery optimization, the default, still runs them in
12+
* Doze's maintenance windows, so it isn't reported. "on" without a context to ask.
13+
*/
14+
fun backgroundRefreshState(context: Context?): String {
15+
val activityManager = context?.getSystemService(ActivityManager::class.java)
16+
return if (activityManager?.isBackgroundRestricted == true) "off" else "on"
17+
}
18+
19+
class BackgroundRefreshModule : Module() {
20+
override fun definition() = ModuleDefinition {
21+
Name("BackgroundRefresh")
22+
23+
AsyncFunction("getState") { backgroundRefreshState(appContext.reactContext) }
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.robertying.learnx.background
2+
3+
import android.app.ActivityManager
4+
import android.content.Context
5+
import org.robolectric.RuntimeEnvironment
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.robolectric.RobolectricTestRunner
10+
import org.robolectric.Shadows.shadowOf
11+
12+
@RunWith(RobolectricTestRunner::class)
13+
class BackgroundRefreshModuleTest {
14+
private val context: Context = RuntimeEnvironment.getApplication()
15+
private val activityManager = shadowOf(context.getSystemService(ActivityManager::class.java))
16+
17+
@Test
18+
fun definesItsFunctions() {
19+
BackgroundRefreshModule().definition()
20+
}
21+
22+
@Test
23+
fun isOnByDefault() {
24+
assertEquals("on", backgroundRefreshState(context))
25+
}
26+
27+
@Test
28+
fun isOnWithoutAContext() {
29+
assertEquals("on", backgroundRefreshState(null))
30+
}
31+
32+
@Test
33+
fun isOffWhenBackgroundUseIsRestricted() {
34+
activityManager.setBackgroundRestricted(true)
35+
assertEquals("off", backgroundRefreshState(context))
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The newest Android version this Robolectric runs.
2+
sdk=36
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"platforms": ["apple", "android"],
3+
"apple": {
4+
"modules": ["BackgroundRefreshModule"]
5+
},
6+
"android": {
7+
"modules": ["io.robertying.learnx.background.BackgroundRefreshModule"]
8+
}
9+
}

‎modules/background/index.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { requireOptionalNativeModule } from 'expo';
2+
3+
/**
4+
* Whether the system lets the background sync run: `on`; `off`, turned off
5+
* in the system settings (iOS: Background App Refresh; Android: background
6+
* battery use restricted); `lowPower`, paused by Low Power Mode (iOS);
7+
* `managed`, not allowed on this device by Screen Time or device management
8+
* (iOS).
9+
*/
10+
export type BackgroundRefreshState = 'on' | 'off' | 'lowPower' | 'managed';
11+
12+
interface BackgroundRefreshModule {
13+
getState(): Promise<BackgroundRefreshState>;
14+
}
15+
16+
/**
17+
* Null where the native module isn't in the binary (Jest, the Electron app,
18+
* or JS from Metro in an app built before it was added).
19+
*/
20+
export default requireOptionalNativeModule<BackgroundRefreshModule>(
21+
'BackgroundRefresh',
22+
);

‎modules/background/index.web.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type NativeBackgroundRefresh from './index';
2+
3+
export type { BackgroundRefreshState } from './index';
4+
5+
/** Windows and Linux have no system setting that stops the Electron app's sync. */
6+
const BackgroundRefresh: typeof NativeBackgroundRefresh = null;
7+
8+
export default BackgroundRefresh;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ExpoModulesCore
2+
import UIKit
3+
4+
/// What the system says about the app's background refresh, which runs the background sync.
5+
enum BackgroundRefreshState: String {
6+
/// It runs.
7+
case on
8+
/// Turned off in Settings, for learnX or for every app.
9+
case off
10+
/// Paused while Low Power Mode is on.
11+
case lowPower
12+
/// Not allowed on this device (Screen Time or device management); the user may not be able to change it.
13+
case managed
14+
}
15+
16+
/// What the state is read from; tests use a fake.
17+
@MainActor
18+
protocol BackgroundRefreshSystem {
19+
var refreshStatus: UIBackgroundRefreshStatus { get }
20+
var isLowPowerModeEnabled: Bool { get }
21+
var isMacCatalystApp: Bool { get }
22+
}
23+
24+
@MainActor
25+
struct DeviceBackgroundRefresh: BackgroundRefreshSystem {
26+
var refreshStatus: UIBackgroundRefreshStatus { UIApplication.shared.backgroundRefreshStatus }
27+
var isLowPowerModeEnabled: Bool { ProcessInfo.processInfo.isLowPowerModeEnabled }
28+
var isMacCatalystApp: Bool { ProcessInfo.processInfo.isMacCatalystApp }
29+
}
30+
31+
/// Says whether iOS lets the background sync run, so Settings can say why notifications of new content don't come.
32+
public final class BackgroundRefreshModule: Module {
33+
public func definition() -> ModuleDefinition {
34+
Name("BackgroundRefresh")
35+
36+
AsyncFunction("getState") { () -> String in
37+
MainActor.assumeIsolated {
38+
Self.state(of: DeviceBackgroundRefresh()).rawValue
39+
}
40+
}.runOnQueue(.main)
41+
}
42+
43+
@MainActor
44+
static func state(of system: BackgroundRefreshSystem) -> BackgroundRefreshState {
45+
// The Mac has no such setting, and runs the task while the app is open.
46+
if system.isMacCatalystApp {
47+
return .on
48+
}
49+
switch system.refreshStatus {
50+
case .available:
51+
return .on
52+
case .restricted:
53+
return .managed
54+
default:
55+
// Low Power Mode turns it off for every app until it ends, which isn't the setting to change.
56+
return system.isLowPowerModeEnabled ? .lowPower : .off
57+
}
58+
}
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require File.expand_path('../../learnx_module', File.dirname(__FILE__))
2+
3+
Pod::Spec.new do |s|
4+
LearnXModule.podspec(s, name: 'LearnXBackground', summary: 'Says whether the system lets the background sync run')
5+
end

0 commit comments

Comments
 (0)