Skip to content

Commit

Permalink
支持状态栏歌词
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Jun 1, 2024
1 parent 797ef64 commit a33f744
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 79 deletions.
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ dependencies {
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation group: 'wang.harlon.quickjs', name: 'wrapper-android', version: '1.0.0'

implementation 'com.github.xiaowine:Lyric-Getter-Api:6.0.0'

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
Expand Down
4 changes: 4 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
**[] $VALUES;
public *;
}

# 排除混淆墨•状态栏歌词相关API
-keep class cn.lyric.getter.api.data.*{*;}
-keep class cn.lyric.getter.api.API{*;}
153 changes: 123 additions & 30 deletions android/app/src/main/java/cn/toside/music/mobile/lyric/Lyric.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@
import java.util.List;
import java.util.Objects;

import cn.lyric.getter.api.API;
import cn.lyric.getter.api.data.ExtraData;

public class Lyric extends LyricPlayer {
LyricView lyricView = null;
LyricEvent lyricEvent = null;
API statusBarLyric = null;
ExtraData extraData = null;
ReactApplicationContext reactAppContext;

boolean isShowLyric = false;
boolean isUseDesktopLyric = true;
boolean isAutoPause = true;
// String lastText = "LX Music ^-^";
int lastLine = 0;
List lines = new ArrayList();
Expand All @@ -47,6 +54,7 @@ private void registerScreenBroadcastReceiver() {
BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!isAutoPause) return;
String strAction = intent.getAction();

switch (Objects.requireNonNull(strAction)) {
Expand All @@ -69,7 +77,7 @@ private void handleScreenOff() {
if (!isShowLyric) return;
setTempPause(true);

if (lyricView != null) {
if (isUseDesktopLyric && lyricView != null) {
lyricView.runOnUiThread(() -> {
lyricView.destroyView();
});
Expand All @@ -78,55 +86,73 @@ private void handleScreenOff() {

private void handleScreenOn() {
if (!isShowLyric) return;
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
lyricView.runOnUiThread(() -> {
lyricView.showLyricView();
setViewLyric(lastLine);
if (isUseDesktopLyric) {
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
lyricView.runOnUiThread(() -> {
lyricView.showLyricView();
updateLyric(lastLine);
setTempPause(false);
});
} else {
updateLyric(lastLine);
setTempPause(false);
});
}
}

private void setViewLyric(int lineNum) {
private void updateLyric(int lineNum) {
lastLine = lineNum;
if (lyricView == null) return;
if (lineNum >= 0 && lineNum < lines.size()) {
if (!isShowLyric) return;
String lineLyric;
ArrayList<String> extendedLyrics;
if (lineNum < 0 || lineNum > lines.size() - 1) {
lineLyric = "";
extendedLyrics = new ArrayList<>(0);
} else {
HashMap line = (HashMap) lines.get(lineNum);
if (line != null) {
lyricView.setLyric((String) line.get("text"), (ArrayList<String>) line.get("extendedLyrics"));
return;
if (line == null) {
lineLyric = "";
extendedLyrics = new ArrayList<>(0);
} else {
lineLyric = (String) line.get("text");
extendedLyrics = (ArrayList<String>) line.get("extendedLyrics");
}
}
lyricView.setLyric("", new ArrayList<>(0));

if (isUseDesktopLyric) {
if (lyricView == null) return;
lyricView.setLyric(lineLyric, extendedLyrics);
} else {
if (statusBarLyric == null) return;
statusBarLyric.sendLyric(lineLyric, extraData);
}
}

public void showLyric(Bundle options, Promise promise) {
if (lyricEvent == null) lyricEvent = new LyricEvent(reactAppContext);
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
try {
lyricView.showLyricView(options);
} catch (Exception e) {
promise.reject(e);
Log.e("Lyric", e.getMessage());
return;
hideLyric();
isUseDesktopLyric = options.getBoolean("isUseDesktopLyric", true);
isAutoPause = options.getBoolean("isAutoPause", true);
if (isUseDesktopLyric) {
showDesktopLyric(options, promise);
} else {
showStatusBarLyric(options, promise);
}

isShowLyric = true;
promise.resolve(null);
}

public void hideLyric() {
this.pause();
if (lyricView != null) {
lyricView.destroy();
if (isUseDesktopLyric) {
hideViewLyric();
} else {
hideStatusBarLyric();
}
isShowLyric = false;
}

private void refreshLyric() {
ArrayList<String> extendedLyrics = new ArrayList<>(2);
if (isShowTranslation && !"".equals(translationText)) extendedLyrics.add(translationText);
if (isShowRoma && !"".equals(romaLyricText)) extendedLyrics.add(romaLyricText);
if (lyricView != null) super.setLyric(lyricText, extendedLyrics);
if (isShowLyric) super.setLyric(lyricText, extendedLyrics);
}

public void setLyric(String lyric, String translation, String romaLyric) {
Expand All @@ -139,7 +165,7 @@ public void setLyric(String lyric, String translation, String romaLyric) {
@Override
public void onSetLyric(List lines) {
this.lines = lines;
setViewLyric(-1);
updateLyric(-1);
// for (int i = 0; i < lines.size(); i++) {
// HashMap line = (HashMap) lines.get(i);
// Log.d("Lyric", "onSetLyric: " +(String) line.get("text") + " " + line.get("extendedLyrics"));
Expand All @@ -148,14 +174,22 @@ public void onSetLyric(List lines) {

@Override
public void onPlay(int lineNum) {
setViewLyric(lineNum);
updateLyric(lineNum);
// Log.d("Lyric", lineNum + " " + text + " " + (String) line.get("translation"));
}

public void pauseLyric() {
pause();
if (!isShowLyric) return;
if (lyricView != null) lyricView.setLyric("", new ArrayList<>(0));
if (isUseDesktopLyric) {
if (lyricView != null) lyricView.setLyric("", new ArrayList<>(0));
} else {
if (statusBarLyric != null) statusBarLyric.clearLyric();
}
}

public void toggleAutoPause(boolean isAutoPause) {
this.isAutoPause = isAutoPause;
}

public void lockLyric() {
Expand Down Expand Up @@ -209,4 +243,63 @@ public void setPlayedColor(String unplayColor, String playedColor, String shadow
public void setLyricTextPosition(String positionX, String positionY) {
lyricView.setLyricTextPosition(positionX, positionY);
}

public void setUseDesktopLyric(boolean enable, Bundle options, Promise promise) {
if (isShowLyric) {
if (isUseDesktopLyric) {
hideViewLyric();
} else {
hideStatusBarLyric();
}
}
isUseDesktopLyric = enable;
if (enable) {
showDesktopLyric(options, promise);
} else {
showStatusBarLyric(options, promise);
}
}

public void showDesktopLyric (Bundle options, Promise promise) {
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
try {
lyricView.showLyricView(options);
} catch (Exception e) {
promise.reject(e);
Log.e("Lyric", e.getMessage());
return;
}
isShowLyric = true;
promise.resolve(null);
}

public void hideViewLyric() {
if (lyricView != null) {
lyricView.destroy();
}
isShowLyric = false;
}

public void showStatusBarLyric(Bundle options, Promise promise) {
if (statusBarLyric == null) {
statusBarLyric = new API();
extraData = new ExtraData();
extraData.setPackageName(reactAppContext.getPackageName());
}
if (statusBarLyric.getHasEnable()) {
// statusBarLyric.updateLyric(lyricText);
isShowLyric = true;
promise.resolve(null);
} else {
isShowLyric = false;
promise.reject(new Exception("statusBar lyric disabled"));
}
}

public void hideStatusBarLyric() {
if (statusBarLyric != null) {
statusBarLyric.clearLyric();
}
isShowLyric = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public void pause(Promise promise) {
promise.resolve(null);
}

@ReactMethod
public void toggleAutoPause(boolean isAutoPause, Promise promise) {
if (lyric != null) {
lyric.toggleAutoPause(isAutoPause);
}
promise.resolve(null);
}

@ReactMethod
public void toggleLock(boolean isLock, Promise promise) {
if (lyric != null) {
Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android.enableJetifier=true
# Use this property to specify which architecture you want to build.
# You can also override it from the CLI using
# ./gradlew <task> -PreactNativeArchitectures=x86_64
reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
reactNativeArchitectures=arm64-v8a

# Use this property to enable support to the new architecture.
# This will allow you to use TurboModules and the Fabric render in
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lx-music-mobile",
"version": "1.4.0",
"version": "1.4.0-sl",
"versionCode": 65,
"private": true,
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/config/defaultSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const defaultSetting: LX.AppSetting = {
'playDetail.isShowLyricProgressSetting': false,

'desktopLyric.enable': false,
'desktopLyric.isUseDesktopLyric': true,
'desktopLyric.isAutoPause': true,
'desktopLyric.isLock': false,
'desktopLyric.width': 100,
'desktopLyric.maxLineNum': 5,
Expand Down
8 changes: 8 additions & 0 deletions src/core/desktopLyric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ import {
checkOverlayPermission,
openOverlayPermissionActivity,
onPositionChange,
toggleAutoPause,
} from '@/utils/nativeModules/lyricDesktop'
import settingState from '@/store/setting/state'
import playerState from '@/store/player/state'
import { tranditionalize } from '@/utils/simplify-chinese-main'
import { getPosition } from '@/plugins/player'
import { toast } from '@/utils/tools'

export const showDesktopLyric = async() => {
const setting = settingState.setting
await showLyric({
enable: setting['desktopLyric.enable'],
isUseDesktopLyric: setting['desktopLyric.isUseDesktopLyric'],
isAutoPause: setting['desktopLyric.isAutoPause'],
isShowToggleAnima: setting['desktopLyric.showToggleAnima'],
isSingleLine: setting['desktopLyric.isSingleLine'],
isLock: setting['desktopLyric.isLock'],
Expand All @@ -43,6 +48,8 @@ export const showDesktopLyric = async() => {
positionY: setting['desktopLyric.position.y'],
textPositionX: setting['desktopLyric.textPosition.x'],
textPositionY: setting['desktopLyric.textPosition.y'],
}).catch((err: any) => {
toast(err.message, 'long')
})
let lrc = playerState.musicInfo.lrc ?? ''
let tlrc = playerState.musicInfo.tlrc ?? ''
Expand Down Expand Up @@ -70,6 +77,7 @@ export const setDesktopLyricPlaybackRate = setPlaybackRate
export const toggleDesktopLyricTranslation = toggleTranslation
export const toggleDesktopLyricRoma = toggleRoma
export const toggleDesktopLyricLock = toggleLock
export const toggleDesktopAutoPause = toggleAutoPause
export const setDesktopLyricColor = async(unplayColor: string | null, playedColor: string | null, shadowColor: string | null) => {
return setColor(unplayColor ?? settingState.setting['desktopLyric.style.lyricUnplayColor'],
playedColor ?? settingState.setting['desktopLyric.style.lyricPlayedColor'],
Expand Down
4 changes: 4 additions & 0 deletions src/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@
"setting_list_show_album_name": "Show song album name",
"setting_lyric_dektop_permission_tip": "The desktop lyrics function needs to be granted the permission of LX Music to display the floating window in the system permission setting before it can be used. Do you go to the relevant interface to grant this permission?",
"setting_lyric_desktop": "Desktop lyrics",
"setting_lyric_desktop_auto_pause": "Automatically pause lyrics when the screen is off",
"setting_lyric_desktop_enable": "Show desktop lyrics",
"setting_lyric_desktop_lock": "Lock lyrics",
"setting_lyric_desktop_maxlineNum": "maximum number of lines",
"setting_lyric_desktop_show_lyric_type": "How to display lyrics",
"setting_lyric_desktop_show_lyric_type_desktop_lyric": "Desktop lyrics",
"setting_lyric_desktop_show_lyric_type_statusbar_lyric": "StatusBarLyric (requires \\\"StatusBarLyric\\\" installed)",
"setting_lyric_desktop_single_line": "Use single line lyrics",
"setting_lyric_desktop_text_opacity": "Lyric font transparency",
"setting_lyric_desktop_text_size": "Lyric font size",
Expand Down
4 changes: 4 additions & 0 deletions src/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@
"setting_list_show_album_name": "显示歌曲专辑名",
"setting_lyric_dektop_permission_tip": "桌面歌词功能需要在系统权限设置中授予LX Music显示悬浮窗口的权限才能使用,是否去相关界面授予此权限?",
"setting_lyric_desktop": "桌面歌词",
"setting_lyric_desktop_auto_pause": "息屏时自动暂停歌词",
"setting_lyric_desktop_enable": "显示桌面歌词",
"setting_lyric_desktop_lock": "锁定歌词",
"setting_lyric_desktop_maxlineNum": "最大行数",
"setting_lyric_desktop_show_lyric_type": "歌词显示方式",
"setting_lyric_desktop_show_lyric_type_desktop_lyric": "桌面歌词",
"setting_lyric_desktop_show_lyric_type_statusbar_lyric": "墨•状态栏歌词(需要安装“墨•状态栏歌词”模块)",
"setting_lyric_desktop_single_line": "使用单行歌词",
"setting_lyric_desktop_text_opacity": "歌词字体透明度",
"setting_lyric_desktop_text_size": "歌词字体大小",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { memo } from 'react'
import { View } from 'react-native'
import { useSettingValue } from '@/store/setting/hook'
import { useI18n } from '@/lang'
import { createStyle } from '@/utils/tools'


import CheckBoxItem from '../../components/CheckBoxItem'
import { toggleDesktopAutoPause } from '@/core/desktopLyric'
import { updateSetting } from '@/core/common'

export default memo(() => {
const t = useI18n()
const isAutoPause = useSettingValue('desktopLyric.isAutoPause')
const setLock = (isAutoPause: boolean) => {
void toggleDesktopAutoPause(isAutoPause).then(() => {
updateSetting({ 'desktopLyric.isAutoPause': isAutoPause })
})
}

return (
<View style={styles.content}>
<CheckBoxItem check={isAutoPause} onChange={setLock} label={t('setting_lyric_desktop_auto_pause')} />
</View>
)
})


const styles = createStyle({
content: {
marginTop: 5,
},
})
Loading

0 comments on commit a33f744

Please sign in to comment.