切换到播放详情歌词界面时将阻止屏幕息屏

This commit is contained in:
lyswhut 2021-05-22 13:49:49 +08:00
parent d1a558248a
commit 0a6a6d9243
4 changed files with 52 additions and 8 deletions

View File

@ -1,9 +1,11 @@
package com.lxmusicmobile.utils;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.WindowManager;
import androidx.core.content.FileProvider;
@ -96,5 +98,35 @@ public class UtilsModule extends ReactContextBaseJavaModule {
promise.resolve(null);
}
}
@ReactMethod
public void screenkeepAwake() {
// https://github.com/corbt/react-native-keep-awake/blob/master/android/src/main/java/com/corbt/keepawake/KCKeepAwake.java
final Activity activity = getCurrentActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
}
@ReactMethod
public void screenUnkeepAwake() {
// https://github.com/corbt/react-native-keep-awake/blob/master/android/src/main/java/com/corbt/keepawake/KCKeepAwake.java
final Activity activity = getCurrentActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
}
}

View File

@ -1,3 +1,3 @@
### 新增
### 优化
- 新增竖屏下的播放详情页
- 切换到播放详情歌词界面时将阻止屏幕息屏

View File

@ -3,6 +3,7 @@ import { View, Text, StyleSheet, FlatList } from 'react-native'
import { useGetter, useDispatch } from '@/store'
import { useLayout } from '@/utils/hooks'
import { useLrcPlay, useLrcSet } from '@/plugins/lyric'
import { screenkeepAwake, screenUnkeepAwake } from '@/utils/utils'
const LrcLine = memo(({ text, line, activeLine }) => {
const theme = useGetter('common', 'theme')
@ -41,12 +42,19 @@ export default memo(() => {
// const imgWidth = useMemo(() => layout.width * 0.75, [layout.width])
const handleScrollToActive = useCallback((index = lineRef.current) => {
if (!scrollViewRef.current || !linesRef.current.length) return
screenkeepAwake()
if (scrollViewRef.current && linesRef.current.length) {
scrollViewRef.current.scrollToIndex({
index,
animated: true,
viewPosition: 0.4,
})
}
return () => {
screenUnkeepAwake()
}
}, [])
const handleScrollBeginDrag = () => {

View File

@ -7,3 +7,7 @@ export const exitApp = UtilsModule.exitApp
export const getSupportedAbis = UtilsModule.getSupportedAbis
export const installApk = (filePath, fileProviderAuthority) => UtilsModule.installApk(filePath, fileProviderAuthority)
export const screenkeepAwake = UtilsModule.screenkeepAwake
export const screenUnkeepAwake = UtilsModule.screenUnkeepAwake