mirror of
https://github.com/bulwarkmail/native.git
synced 2026-10-31 16:06:32 +00:00
Android syncs the contacts and calendars of other services through
accounts and sync adapters. This adds the Android side of device sync:
a "Bulwark" account type (`${applicationId}.account`, set in
build.gradle), an authenticator that sends Settings → Add account to
the app instead of asking for credentials, and one sync adapter each
for the contacts and the calendar provider.
An adapter does no syncing itself. It starts the BulwarkDeviceSync
headless JS task from the main thread, through the helper the push job
uses, waits for the task's report and hands it to SyncManager as the
SyncResult. The engine that will write the report runs in JS, so the
existing client stays the only owner of token refresh, mTLS and
session discovery; for now the task only reports that device sync is
off. Nothing in the app creates an account yet, so the feature stays
unreachable until its settings land. Debug builds get an adb hook for
the device tests (DeviceSyncDebugReceiver, shell only).
Hard errors are reported without progress: SyncManager retries a
failed sync at once when it made progress, which would loop an auth
failure.
Checked on the API 34 and API 32 emulators with the debug build, from
logcat and `dumpsys content`:
- app in the foreground: report after 319-334 ms
- app in the background: 320-327 ms
- process killed: SyncManager starts it for the sync service, React
Native starts headless, and the report arrives after 16.2 s (API 34)
and 8.5 s (API 32), most of it the debug bundle download from Metro
- an io report is retried with back-off and an auth report is not
retried; too many deletions shows the system's "Attempted to delete
too many Contacts." notification
- with Metro unreachable the adapter gives up after 2 s instead of
waiting for its deadline
- `gradlew :app:testDebugUnitTest`: 10 JVM tests for the report mapping
41 lines
2.0 KiB
TypeScript
41 lines
2.0 KiB
TypeScript
import { createElement } from 'react';
|
|
import { AppRegistry } from 'react-native';
|
|
import { registerRootComponent } from 'expo';
|
|
import { registerWidgetTaskHandler } from 'react-native-android-widget';
|
|
|
|
import { pushBackgroundTask } from './src/lib/push-background-task';
|
|
import { widgetTaskHandler } from './src/widgets/task-handler';
|
|
import { refreshWidgetsInBackground } from './src/widgets/sync';
|
|
|
|
// Runs in a fresh headless JS runtime when BulwarkPushTaskService is started
|
|
// from BulwarkMessagingService on an FCM data message. Must be registered
|
|
// before the native service tries to invoke the task. New mail also refreshes
|
|
// any home-screen widgets (a no-op when none are placed).
|
|
AppRegistry.registerHeadlessTask('BulwarkPushTask', () => async (data: Parameters<typeof pushBackgroundTask>[0]) => {
|
|
await Promise.allSettled([pushBackgroundTask(data), refreshWidgetsInBackground()]);
|
|
});
|
|
|
|
// Device sync (Android, #34): the contacts and calendar sync adapters run the
|
|
// sync engine through this task, headless or next to the UI. The engine is
|
|
// required on first use so an app start does not load it.
|
|
AppRegistry.registerHeadlessTask('BulwarkDeviceSync', () => async (data: import('./src/device-sync/types').RunPayload) => {
|
|
const { runDeviceSyncTask } = require('./src/device-sync/task') as typeof import('./src/device-sync/task');
|
|
await runDeviceSyncTask(data);
|
|
});
|
|
|
|
// Home-screen widgets (Android): every widget event runs this headless task.
|
|
registerWidgetTaskHandler(widgetTaskHandler);
|
|
|
|
// A headless push task evaluates this file too, so App is required when the
|
|
// UI first renders instead of imported above: importing it here would load
|
|
// every screen, store and icon before the task could run.
|
|
function Root() {
|
|
const App = (require('./App') as typeof import('./App')).default;
|
|
return createElement(App);
|
|
}
|
|
|
|
// registerRootComponent calls AppRegistry.registerComponent('main', () => Root);
|
|
// It also ensures that whether you load the app in Expo Go or in a native build,
|
|
// the environment is set up appropriately
|
|
registerRootComponent(Root);
|