1import { ExpoConfig } from '@expo/config-types';
2
3export type SplashScreenConfig = {
4  xxxhdpi: string | null;
5  xxhdpi: string | null;
6  xhdpi: string | null;
7  hdpi: string | null;
8  mdpi: string | null;
9  backgroundColor: string | null;
10  resizeMode: 'contain' | 'cover' | 'native';
11};
12
13const defaultResizeMode = 'contain';
14
15export function getAndroidSplashConfig(
16  config: Pick<ExpoConfig, 'splash' | 'android'>
17): SplashScreenConfig | null {
18  // Respect the splash screen object, don't mix and match across different splash screen objects
19  // in case the user wants the top level splash to apply to every platform except android.
20  if (config.android?.splash) {
21    const splash = config.android?.splash;
22    return {
23      xxxhdpi: splash.xxxhdpi ?? splash.image ?? null,
24      xxhdpi: splash.xxhdpi ?? splash.image ?? null,
25      xhdpi: splash.xhdpi ?? splash.image ?? null,
26      hdpi: splash.hdpi ?? splash.image ?? null,
27      mdpi: splash.mdpi ?? splash.image ?? null,
28      backgroundColor: splash.backgroundColor ?? null,
29      resizeMode: splash.resizeMode ?? defaultResizeMode,
30    };
31  }
32
33  if (config.splash) {
34    const splash = config.splash;
35    return {
36      xxxhdpi: splash.image ?? null,
37      xxhdpi: splash.image ?? null,
38      xhdpi: splash.image ?? null,
39      hdpi: splash.image ?? null,
40      mdpi: splash.image ?? null,
41      backgroundColor: splash.backgroundColor ?? null,
42      resizeMode: splash.resizeMode ?? defaultResizeMode,
43    };
44  }
45
46  return null;
47}
48
49// TODO: dark isn't supported in the Expo config spec yet.
50export function getAndroidDarkSplashConfig(
51  config: Pick<ExpoConfig, 'splash' | 'android'>
52): SplashScreenConfig | null {
53  // Respect the splash screen object, don't mix and match across different splash screen objects
54  // in case the user wants the top level splash to apply to every platform except android.
55  if (config.android?.splash?.dark) {
56    const splash = config.android?.splash?.dark;
57    const lightTheme = getAndroidSplashConfig(config);
58    return {
59      xxxhdpi: splash.xxxhdpi ?? splash.image ?? null,
60      xxhdpi: splash.xxhdpi ?? splash.image ?? null,
61      xhdpi: splash.xhdpi ?? splash.image ?? null,
62      hdpi: splash.hdpi ?? splash.image ?? null,
63      mdpi: splash.mdpi ?? splash.image ?? null,
64      backgroundColor: splash.backgroundColor ?? null,
65      // Can't support dark resizeMode because the resize mode is hardcoded into the MainActivity.java
66      resizeMode: lightTheme?.resizeMode ?? defaultResizeMode,
67    };
68  }
69
70  return null;
71}
72