xref: /MusicFree/src/entry/bootstrap.ts (revision 2d0ec5c16a4ba4edc62f6ae92da18cdc33751ff4)
1import MusicQueue from '@/common/musicQueue';
2import MusicSheet from '@/common/musicSheetManager';
3import {check, PERMISSIONS, request} from 'react-native-permissions';
4import TrackPlayer, {Capability} from 'react-native-track-player';
5import {pluginManager} from '../common/pluginManager';
6import 'react-native-get-random-values';
7import {Platform, ToastAndroid} from 'react-native';
8import {loadConfig} from '@/common/localConfigManager';
9import RNBootSplash from 'react-native-bootsplash';
10import RNFS, {exists, mkdir} from 'react-native-fs';
11import DownloadManager from '@/common/downloadManager';
12import pathConst from '@/constants/pathConst';
13import {checkAndCreateDir} from '@/utils/fileUtils';
14import {errorLog} from '@/common/logManager';
15import MediaMetaManager from '@/common/mediaMetaManager';
16
17/** app加载前执行 */
18export default async function () {
19  // 检查权限
20  const [readStoragePermission, writeStoragePermission] = await Promise.all([
21    check(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE),
22    check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE),
23  ]);
24  if (
25    !(
26      readStoragePermission === 'granted' &&
27      writeStoragePermission === 'granted'
28    )
29  ) {
30    await request(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);
31    await request(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE);
32  }
33
34  /** 初始化路径 */
35  await setupFolder();
36  // 加载配置
37  await loadConfig();
38  // 加载插件
39
40  Promise.all([
41    await pluginManager.setupPlugins(),
42    await TrackPlayer.setupPlayer(),
43    await MediaMetaManager.setupMediaMeta(),
44  ]);
45  await TrackPlayer.updateOptions({
46    progressUpdateEventInterval: 0.8,
47    stopWithApp: false,
48    alwaysPauseOnInterruption: true,
49    capabilities: [
50      Capability.Play,
51      Capability.Pause,
52      Capability.SkipToNext,
53      Capability.SkipToPrevious,
54    ],
55    compactCapabilities: [
56      Capability.Play,
57      Capability.Pause,
58      Capability.SkipToNext,
59      Capability.SkipToPrevious,
60    ],
61    notificationCapabilities: [
62      Capability.Play,
63      Capability.Pause,
64      Capability.SkipToNext,
65      Capability.SkipToPrevious,
66    ],
67  });
68  await MusicQueue.setupMusicQueue();
69  await MusicSheet.setupMusicSheet();
70  await DownloadManager.setupDownload();
71
72  ErrorUtils.setGlobalHandler(error => {
73    errorLog('未捕获的错误', error);
74  });
75  // 隐藏开屏动画
76  RNBootSplash.hide({fade: true});
77}
78
79/** 初始化 */
80async function setupFolder() {
81  await Promise.all([
82    checkAndCreateDir(pathConst.dataPath),
83    checkAndCreateDir(pathConst.logPath),
84    checkAndCreateDir(pathConst.cachePath),
85    checkAndCreateDir(pathConst.storagePath),
86    checkAndCreateDir(pathConst.pluginPath),
87    checkAndCreateDir(pathConst.lrcCachePath)
88  ]);
89}
90