diff --git a/mobile/src/app-update/app-update-runtime.ts b/mobile/src/app-update/app-update-runtime.ts index 2c99f428798..2f092848075 100644 --- a/mobile/src/app-update/app-update-runtime.ts +++ b/mobile/src/app-update/app-update-runtime.ts @@ -1,4 +1,5 @@ import Constants from 'expo-constants' +import { getCalendars, getLocales } from 'expo-localization' import * as Linking from 'expo-linking' import { useSyncExternalStore } from 'react' import { AppState, Platform } from 'react-native' @@ -11,6 +12,7 @@ import { import { appStoreUpdateSource } from './app-store-update-source' import { createAppUpdateChecker, type AppUpdateState } from './app-update-checker' import type { AppUpdateSource } from './app-update-source' +import { isLikelyInMainlandChina, shouldPreferGooglePlay } from './google-play-preference' import { ANDROID_RELEASE_TAG_PREFIX, createGithubReleaseUpdateSource, @@ -28,6 +30,32 @@ export const isRetiredGithubAndroidApp = export const isGooglePlayInstall = Platform.OS === 'android' && (OrcaPlayStorePresence?.isInstalledByPlayStore() ?? false) +export const GOOGLE_PLAY_URL = 'https://play.google.com/store/apps/details?id=dev.orca.app' + +function deviceIsInMainlandChina(): boolean { + try { + return isLikelyInMainlandChina({ + regionCodes: getLocales().map((locale) => locale.regionCode), + timeZone: getCalendars()[0]?.timeZone + }) + } catch { + // Unknown region: stay on GitHub, which works everywhere. + return true + } +} + +/** + * A GitHub (sideloaded) install outside mainland China is sent to Google Play, which updates it in + * place because both carry Google's signing key. Mainland China keeps GitHub: Play is unreachable. + */ +export const prefersGooglePlayUpdate = shouldPreferGooglePlay({ + isAndroid: Platform.OS === 'android', + isRetiredApp: isRetiredGithubAndroidApp, + isInstalledByPlay: isGooglePlayInstall, + playStoreUsable: OrcaPlayStorePresence?.isPlayStoreInstalled() ?? false, + inMainlandChina: deviceIsInMainlandChina() +}) + /** The channel that installed this binary. */ function resolveAppUpdateSource(): AppUpdateSource | null { if (Platform.OS === 'android') { @@ -73,7 +101,7 @@ export function useAppUpdateState(): AppUpdateState { return useSyncExternalStore(appUpdateChecker.subscribe, appUpdateChecker.getSnapshot) } -/** Opens the channel's page for the update: the GitHub release (Android) or App Store (iOS). */ +/** Opens the update's page: Google Play where it applies, else the GitHub release (Android) or App Store (iOS). */ export function openAppUpdate(url: string): void { - void Linking.openURL(url).catch(() => {}) + void Linking.openURL(prefersGooglePlayUpdate ? GOOGLE_PLAY_URL : url).catch(() => {}) } diff --git a/mobile/src/app-update/google-play-preference.test.ts b/mobile/src/app-update/google-play-preference.test.ts new file mode 100644 index 00000000000..a9af7853cbb --- /dev/null +++ b/mobile/src/app-update/google-play-preference.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from 'vitest' +import { isLikelyInMainlandChina, shouldPreferGooglePlay } from './google-play-preference' + +describe('isLikelyInMainlandChina', () => { + it('detects a Chinese region or a mainland time zone, either alone', () => { + expect(isLikelyInMainlandChina({ regionCodes: ['CN'], timeZone: 'America/New_York' })).toBe( + true + ) + expect(isLikelyInMainlandChina({ regionCodes: ['US'], timeZone: 'Asia/Shanghai' })).toBe(true) + expect(isLikelyInMainlandChina({ regionCodes: [null, 'cn'], timeZone: null })).toBe(true) + }) + + it('does not treat Hong Kong, Taiwan or other regions as mainland China', () => { + expect(isLikelyInMainlandChina({ regionCodes: ['HK'], timeZone: 'Asia/Hong_Kong' })).toBe(false) + expect(isLikelyInMainlandChina({ regionCodes: ['TW'], timeZone: 'Asia/Taipei' })).toBe(false) + expect(isLikelyInMainlandChina({ regionCodes: [], timeZone: undefined })).toBe(false) + }) +}) + +describe('shouldPreferGooglePlay', () => { + const base = { + isAndroid: true, + isRetiredApp: false, + isInstalledByPlay: false, + playStoreUsable: true, + inMainlandChina: false + } + + it('steers a GitHub install outside China to Play', () => { + expect(shouldPreferGooglePlay(base)).toBe(true) + }) + + it.each([ + ['iOS', { isAndroid: false }], + ['the retired app', { isRetiredApp: true }], + ['a Play install, which Play already updates', { isInstalledByPlay: true }], + ['a phone without a working Play Store', { playStoreUsable: false }], + ['mainland China', { inMainlandChina: true }] + ])('does not for %s', (_name, override) => { + expect(shouldPreferGooglePlay({ ...base, ...override })).toBe(false) + }) +}) diff --git a/mobile/src/app-update/google-play-preference.ts b/mobile/src/app-update/google-play-preference.ts new file mode 100644 index 00000000000..9593c214da3 --- /dev/null +++ b/mobile/src/app-update/google-play-preference.ts @@ -0,0 +1,46 @@ +// Google Play is unreachable from mainland China, so GitHub installs there must keep updating from GitHub. +// Anywhere else a GitHub install is steered to Play, which updates it in place (same signing key). + +// Mainland China's zones; Hong Kong, Macau and Taiwan reach Play and are not listed. +const MAINLAND_CHINA_TIME_ZONES: ReadonlySet = new Set([ + 'Asia/Shanghai', + 'Asia/Chongqing', + 'Asia/Harbin', + 'Asia/Urumqi', + 'Asia/Kashgar', + 'PRC' +]) + +export type DeviceRegionSignals = { + /** Region subtags of the phone's preferred languages, such as `CN` for zh-Hans-CN. */ + readonly regionCodes: readonly (string | null | undefined)[] + readonly timeZone: string | null | undefined +} + +/** + * True when either signal says mainland China. A phone set to US English but sitting in a Chinese + * time zone is still behind the firewall, so either signal alone is enough. + */ +export function isLikelyInMainlandChina(signals: DeviceRegionSignals): boolean { + if (signals.regionCodes.some((code) => code?.toUpperCase() === 'CN')) { + return true + } + return signals.timeZone != null && MAINLAND_CHINA_TIME_ZONES.has(signals.timeZone) +} + +/** A GitHub install is pointed at Play only where Play works and the Play Store app can open it. */ +export function shouldPreferGooglePlay(input: { + readonly isAndroid: boolean + readonly isRetiredApp: boolean + readonly isInstalledByPlay: boolean + readonly playStoreUsable: boolean + readonly inMainlandChina: boolean +}): boolean { + return ( + input.isAndroid && + !input.isRetiredApp && + !input.isInstalledByPlay && + input.playStoreUsable && + !input.inMainlandChina + ) +} diff --git a/mobile/src/app-update/use-wall-app-update.ts b/mobile/src/app-update/use-wall-app-update.ts index 6ca0c350038..86daadddd8c 100644 --- a/mobile/src/app-update/use-wall-app-update.ts +++ b/mobile/src/app-update/use-wall-app-update.ts @@ -1,7 +1,7 @@ import type { KnownAppUpdate } from '../storage/app-update-preferences' import { useAppUpdateState } from './app-update-runtime' -export { isGooglePlayInstall } from './app-update-runtime' +export { isGooglePlayInstall, prefersGooglePlayUpdate } from './app-update-runtime' // Ignore dismissal at update wall; checker’s existing cadence covers it without extra triggers. export function useWallAppUpdate(): KnownAppUpdate | null { diff --git a/mobile/src/app-update/use-wall-app-update.web.ts b/mobile/src/app-update/use-wall-app-update.web.ts index 8638ec553c4..5c1d48da609 100644 --- a/mobile/src/app-update/use-wall-app-update.web.ts +++ b/mobile/src/app-update/use-wall-app-update.web.ts @@ -2,6 +2,7 @@ import type { KnownAppUpdate } from '../storage/app-update-preferences' // The page bundle cannot see the native app's install source. export const isGooglePlayInstall = false +export const prefersGooglePlayUpdate = false // The page bundle must not carry the checker; the wall inside the page keeps the fallback link. export function useWallAppUpdate(): KnownAppUpdate | null { diff --git a/mobile/src/components/host-connection/ProtocolBlockScreen.tsx b/mobile/src/components/host-connection/ProtocolBlockScreen.tsx index a522afe63df..e17927e010a 100644 --- a/mobile/src/components/host-connection/ProtocolBlockScreen.tsx +++ b/mobile/src/components/host-connection/ProtocolBlockScreen.tsx @@ -1,4 +1,8 @@ -import { isGooglePlayInstall, useWallAppUpdate } from '../../app-update/use-wall-app-update' +import { + isGooglePlayInstall, + prefersGooglePlayUpdate, + useWallAppUpdate +} from '../../app-update/use-wall-app-update' import { openExternalLink } from '../../platform/external-links/external-link' import { useRouteHandoff } from '../../navigation/route-handoff' import { Platform, Pressable, Text, View } from 'react-native' @@ -43,12 +47,13 @@ function blockRemedy(verdict: BlockedVerdict): BlockRemedy { } } -// Play installs may only update through Play; other Android installs came from GitHub Releases. +// Play installs may only update through Play, and GitHub installs outside mainland China are steered +// there too; other Android installs came from GitHub Releases. function mobileUpdateChannel(): 'app-store' | 'google-play' | 'github-releases' { if (Platform.OS === 'ios') { return 'app-store' } - return isGooglePlayInstall ? 'google-play' : 'github-releases' + return isGooglePlayInstall || prefersGooglePlayUpdate ? 'google-play' : 'github-releases' } function mobileUpdateChannelTarget(t: Translate): { label: string; url: string } {