xref: /MusicFree/src/entry/bootstrap.ts (revision 927dbe935e5ed3130d85ec27a6ac3efd1a163ad1)
1242960d3S猫头猫import MusicQueue from '@/core/musicQueue';
2e14deecfS猫头猫import MusicSheet from '@/core/musicSheet';
3bf6e62f2S猫头猫import {check, PERMISSIONS, request} from 'react-native-permissions';
4bf6e62f2S猫头猫import TrackPlayer, {Capability} from 'react-native-track-player';
5bf6e62f2S猫头猫import 'react-native-get-random-values';
66c6f45bdS猫头猫import {Platform, ToastAndroid} from 'react-native';
7242960d3S猫头猫import {loadConfig} from '@/core/localConfigManager';
80d39db21S猫头猫import RNBootSplash from 'react-native-bootsplash';
96c6f45bdS猫头猫import RNFS, {exists, mkdir} from 'react-native-fs';
10e14deecfS猫头猫import Download from '@/core/download';
116780f402S猫头猫import pathConst from '@/constants/pathConst';
12be0a3650S猫头猫import {checkAndCreateDir} from '@/utils/fileUtils';
13242960d3S猫头猫import {errorLog} from '@/utils/log';
1428bde18bS猫头猫import MediaMeta from '@/core/mediaMeta';
153d6d339aS猫头猫import Cache from '@/core/cache';
16*927dbe93S猫头猫import PluginManager from '@/core/pluginManager';
17bf6e62f2S猫头猫
188b88e961S猫头猫/** app加载前执行
198b88e961S猫头猫 * 1. 检查权限
208b88e961S猫头猫 * 2. 数据初始化
218b88e961S猫头猫 * 3.
228b88e961S猫头猫 */
2394a1b1fcS猫头猫async function _bootstrap() {
24*927dbe93S猫头猫  // 1. 检查权限
25bf6e62f2S猫头猫  const [readStoragePermission, writeStoragePermission] = await Promise.all([
26bf6e62f2S猫头猫    check(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE),
27bf6e62f2S猫头猫    check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE),
28bf6e62f2S猫头猫  ]);
29bf6e62f2S猫头猫  if (
30bf6e62f2S猫头猫    !(
31bf6e62f2S猫头猫      readStoragePermission === 'granted' &&
32bf6e62f2S猫头猫      writeStoragePermission === 'granted'
33bf6e62f2S猫头猫    )
34bf6e62f2S猫头猫  ) {
35bf6e62f2S猫头猫    await request(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);
36bf6e62f2S猫头猫    await request(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE);
37bf6e62f2S猫头猫  }
38bf6e62f2S猫头猫
39*927dbe93S猫头猫  // 2. 数据初始化
406780f402S猫头猫  /** 初始化路径 */
416780f402S猫头猫  await setupFolder();
42bf6e62f2S猫头猫  // 加载配置
43*927dbe93S猫头猫  await Promise.all([loadConfig(), MediaMeta.setup(), MusicSheet.setup()])
44bf6e62f2S猫头猫  // 加载插件
4594a1b1fcS猫头猫  try {
4694a1b1fcS猫头猫    await TrackPlayer.setupPlayer();
4794a1b1fcS猫头猫  } catch (e: any) {
4894a1b1fcS猫头猫    if (
4994a1b1fcS猫头猫      e?.message !== 'The player has already been initialized via setupPlayer.'
5094a1b1fcS猫头猫    ) {
5194a1b1fcS猫头猫      throw e;
5294a1b1fcS猫头猫    }
5394a1b1fcS猫头猫  }
54bf6e62f2S猫头猫  await TrackPlayer.updateOptions({
5594a1b1fcS猫头猫    progressUpdateEventInterval: 0.4,
56bf6e62f2S猫头猫    stopWithApp: false,
57bf6e62f2S猫头猫    alwaysPauseOnInterruption: true,
58bf6e62f2S猫头猫    capabilities: [
59bf6e62f2S猫头猫      Capability.Play,
60bf6e62f2S猫头猫      Capability.Pause,
61bf6e62f2S猫头猫      Capability.SkipToNext,
62bf6e62f2S猫头猫      Capability.SkipToPrevious,
63bf6e62f2S猫头猫    ],
64bf6e62f2S猫头猫    compactCapabilities: [
65bf6e62f2S猫头猫      Capability.Play,
66bf6e62f2S猫头猫      Capability.Pause,
67bf6e62f2S猫头猫      Capability.SkipToNext,
68bf6e62f2S猫头猫      Capability.SkipToPrevious,
69bf6e62f2S猫头猫    ],
70bf6e62f2S猫头猫    notificationCapabilities: [
71bf6e62f2S猫头猫      Capability.Play,
72bf6e62f2S猫头猫      Capability.Pause,
73bf6e62f2S猫头猫      Capability.SkipToNext,
74bf6e62f2S猫头猫      Capability.SkipToPrevious,
750d39db21S猫头猫    ],
760d39db21S猫头猫  });
77*927dbe93S猫头猫  await Cache.setup()
78e14deecfS猫头猫  await Download.setup();
798b88e961S猫头猫  await PluginManager.setup();
80*927dbe93S猫头猫  await MusicQueue.setup();
818b88e961S猫头猫
82bf6e62f2S猫头猫
83c79c8a57S猫头猫  ErrorUtils.setGlobalHandler(error => {
84c79c8a57S猫头猫    errorLog('未捕获的错误', error);
85c79c8a57S猫头猫  });
86bf6e62f2S猫头猫}
876c6f45bdS猫头猫
886c6f45bdS猫头猫/** 初始化 */
896c6f45bdS猫头猫async function setupFolder() {
906780f402S猫头猫  await Promise.all([
91be0a3650S猫头猫    checkAndCreateDir(pathConst.dataPath),
927f771613S猫头猫    checkAndCreateDir(pathConst.logPath),
937f771613S猫头猫    checkAndCreateDir(pathConst.cachePath),
94be0a3650S猫头猫    checkAndCreateDir(pathConst.storagePath),
95c79c8a57S猫头猫    checkAndCreateDir(pathConst.pluginPath),
9694a1b1fcS猫头猫    checkAndCreateDir(pathConst.lrcCachePath),
976780f402S猫头猫  ]);
986c6f45bdS猫头猫}
9994a1b1fcS猫头猫
10094a1b1fcS猫头猫export default async function () {
10194a1b1fcS猫头猫  try {
10294a1b1fcS猫头猫    await _bootstrap();
10394a1b1fcS猫头猫  } catch (e) {
10494a1b1fcS猫头猫    errorLog('初始化出错', e);
10594a1b1fcS猫头猫    console.log(e);
10694a1b1fcS猫头猫  }
10794a1b1fcS猫头猫  // 隐藏开屏动画
10894a1b1fcS猫头猫  console.log('HIDE');
10994a1b1fcS猫头猫  RNBootSplash.hide({fade: true});
11094a1b1fcS猫头猫}
111