diff --git a/src/lightbox/download.js b/src/lightbox/download.js index 03d95a862b6..cbc40231d4d 100644 --- a/src/lightbox/download.js +++ b/src/lightbox/download.js @@ -3,10 +3,10 @@ import { Platform, PermissionsAndroid } from 'react-native'; import type { Rationale } from 'react-native/Libraries/PermissionsAndroid/PermissionsAndroid'; import { CameraRoll } from '@react-native-camera-roll/camera-roll'; import RNFetchBlob from 'rn-fetch-blob'; -import invariant from 'invariant'; import type { Auth } from '../api/transportTypes'; import { getMimeTypeFromFileExtension } from '../utils/url'; +import { androidSdkVersion } from '../reactNativeUtils'; /** * Request permission WRITE_EXTERNAL_STORAGE if needed or throw if can't get it. @@ -18,17 +18,7 @@ import { getMimeTypeFromFileExtension } from '../utils/url'; * as a toast. */ export const androidEnsureStoragePermission = async (rationale: Rationale): Promise => { - invariant( - Platform.OS === 'android', - 'androidEnsureStoragePermission should only be called on Android', - ); - // Flow isn't refining `Platform` to a type that corresponds to values - // we'll see on Android. We do expect `Platform.Version` to be a number on - // Android; see https://reactnative.dev/docs/platform#version. Empirically - // (and this isn't in the doc yet), it's the SDK version, so for Android - // 10 it won't be 10, it'll be 29. - const androidSdkVersion = (Platform.Version: number); - if (androidSdkVersion > 28) { + if (androidSdkVersion() > 28) { return; } diff --git a/src/reactNativeUtils.js b/src/reactNativeUtils.js index 49c29652374..7af4d6afe76 100644 --- a/src/reactNativeUtils.js +++ b/src/reactNativeUtils.js @@ -1,10 +1,11 @@ /* @flow strict-local */ import React from 'react'; -import { AppState } from 'react-native'; +import { AppState, Platform } from 'react-native'; import type { AppStateValues } from 'react-native/Libraries/AppState/AppState'; // eslint-disable-next-line id-match import type { ____ViewStyle_Internal } from 'react-native/Libraries/StyleSheet/StyleSheetTypes'; +import invariant from 'invariant'; import * as logging from './utils/logging'; import type { BoundedDiff } from './generics'; @@ -62,3 +63,19 @@ export function useAppState(): null | AppStateValues { }, []); return value; } + +/** + * The Android SDK version (e.g., 33 for Android 13 a.k.a. Tiramisu). + * + * Throws if called on iOS. + */ +export function androidSdkVersion(): number { + invariant(Platform.OS === 'android', 'androidSdkVersion called on iOS'); + + // Flow isn't refining `Platform` to a type that corresponds to values + // we'll see on Android. We do expect `Platform.Version` to be a number on + // Android; see https://reactnative.dev/docs/platform#version. Empirically + // (and this isn't in the doc yet), it's the SDK version, so for Android + // 10 it won't be 10, it'll be 29. + return (Platform.Version: number); +}