xref: /MusicFree/src/entry/bootstrap.ts (revision c79c8a57d1eaae257d88248736ff89af5135adde)
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';
15
16/** app加载前执行 */
17export default async function () {
18  // 检查权限
19  const [readStoragePermission, writeStoragePermission] = await Promise.all([
20    check(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE),
21    check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE),
22  ]);
23  if (
24    !(
25      readStoragePermission === 'granted' &&
26      writeStoragePermission === 'granted'
27    )
28  ) {
29    await request(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);
30    await request(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE);
31  }
32
33  /** 初始化路径 */
34  await setupFolder();
35  // 加载配置
36  await loadConfig();
37  // 加载插件
38
39  Promise.all([
40    await pluginManager.setupPlugins(),
41    await TrackPlayer.setupPlayer(),
42  ]);
43  await TrackPlayer.updateOptions({
44    progressUpdateEventInterval: 2,
45    stopWithApp: false,
46    alwaysPauseOnInterruption: true,
47    capabilities: [
48      Capability.Play,
49      Capability.Pause,
50      Capability.SkipToNext,
51      Capability.SkipToPrevious,
52    ],
53    compactCapabilities: [
54      Capability.Play,
55      Capability.Pause,
56      Capability.SkipToNext,
57      Capability.SkipToPrevious,
58    ],
59    notificationCapabilities: [
60      Capability.Play,
61      Capability.Pause,
62      Capability.SkipToNext,
63      Capability.SkipToPrevious,
64    ],
65  });
66  await MusicQueue.setupMusicQueue();
67  await MusicSheet.setupMusicSheet();
68  await DownloadManager.setupDownload();
69
70  ErrorUtils.setGlobalHandler(error => {
71    errorLog('未捕获的错误', error);
72  });
73  // 隐藏开屏动画
74  RNBootSplash.hide({fade: true});
75}
76
77/** 初始化 */
78async function setupFolder() {
79  await Promise.all([
80    checkAndCreateDir(pathConst.dataPath),
81    checkAndCreateDir(pathConst.logPath),
82    checkAndCreateDir(pathConst.cachePath),
83    checkAndCreateDir(pathConst.storagePath),
84    checkAndCreateDir(pathConst.pluginPath),
85  ]);
86}
87