Skip to content

Commit

Permalink
Optimized some code, removed non-standard flag isTranslateCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorModi committed Feb 20, 2024
1 parent 671a309 commit 78719d4
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 73 deletions.
24 changes: 11 additions & 13 deletions app/src/main/java/statusbar/finder/LrcGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.moji4j.MojiConverter;
import com.moji4j.MojiDetector;
import statusbar.finder.livedatas.GetResult;
import statusbar.finder.misc.Constants;
import statusbar.finder.misc.checkStringLang;
import statusbar.finder.provider.ILrcProvider;
import statusbar.finder.provider.KugouProvider;
Expand All @@ -30,11 +29,11 @@ public class LrcGetter {
};
private static MessageDigest messageDigest;

public static Lyric getLyric(Context context, MediaMetadata mediaMetadata, String sysLang, String packageName) {
return getLyric(context, new ILrcProvider.MediaInfo(mediaMetadata), sysLang, packageName);
public static Lyric getLyric(Context context, MediaMetadata mediaMetadata, String sysLang, String packageName, boolean requireTranslate) {
return getLyric(context, new ILrcProvider.MediaInfo(mediaMetadata), sysLang, packageName, requireTranslate);
}

public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo, String sysLang, String packageName) {
public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo, String sysLang, String packageName, boolean requireTranslate) {
LyricsDatabase lyricsDatabase = new LyricsDatabase(context);
// Log.d(TAG, "curMediaData" + new SimpleSongInfo(mediaMetadata));
ILrcProvider.MediaInfo hiraganaMediaInfo;
Expand All @@ -50,11 +49,10 @@ public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo,

ILrcProvider.LyricResult currentResult = lyricsDatabase.searchLyricFromDatabase(mediaInfo, packageName);
if (currentResult != null) {
currentResult.mSource += " (Database)";
GetResult.getInstance().notifyResult(new GetResult.Data(mediaInfo, currentResult));
return LyricUtils.parseLyric(currentResult, mediaInfo);
}
currentResult = searchLyricsResultByInfo(mediaInfo);
currentResult = searchLyricsResultByInfo(mediaInfo, requireTranslate);
if (currentResult == null) {
MojiDetector detector = new MojiDetector();
MojiConverter converter = new MojiConverter();
Expand All @@ -67,7 +65,7 @@ public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo,
lyricsDatabase.close();
return null;
}
currentResult = searchLyricsResultByInfo(hiraganaMediaInfo);
currentResult = searchLyricsResultByInfo(hiraganaMediaInfo, requireTranslate);
} catch (CloneNotSupportedException e) {
e.fillInStackTrace();
}
Expand All @@ -76,7 +74,7 @@ public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo,
if (currentResult == null) {
mediaInfo.setTitle(converter.convertRomajiToKatakana(mediaInfo.getTitle()));
// Log.d(TAG, "newSearchInfo:" + new SimpleSongInfo(mediaMetadata));
currentResult = searchLyricsResultByInfo(mediaInfo);
currentResult = searchLyricsResultByInfo(mediaInfo, requireTranslate);
}

}
Expand All @@ -88,7 +86,7 @@ public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo,
}
}
String allLyrics;
if (Constants.isTranslateCheck) {
if (requireTranslate) {
if (currentResult.mTranslatedLyric != null) {
allLyrics = LyricUtils.getAllLyrics(false, currentResult.mTranslatedLyric);
} else {
Expand All @@ -115,19 +113,19 @@ public static Lyric getLyric(Context context, ILrcProvider.MediaInfo mediaInfo,
if (lyricsDatabase.insertLyricIntoDatabase(currentResult, mediaInfo, packageName)) {
GetResult.getInstance().notifyResult(new GetResult.Data(mediaInfo, currentResult));
lyricsDatabase.close();
currentResult.mSource += " (Internet)";
return LyricUtils.parseLyric(currentResult, currentResult.resultInfo);
currentResult.mOrigin = ILrcProvider.Origin.INTERNET;
return LyricUtils.parseLyric(currentResult, currentResult.mResultInfo);
}
GetResult.getInstance().notifyResult(new GetResult.Data(mediaInfo, null));
lyricsDatabase.close();
return null;
}

private static ILrcProvider.LyricResult searchLyricsResultByInfo(ILrcProvider.MediaInfo mediaInfo) {
private static ILrcProvider.LyricResult searchLyricsResultByInfo(ILrcProvider.MediaInfo mediaInfo, boolean requireTranslate) {
ILrcProvider.LyricResult currentResult = null;
for (ILrcProvider provider : providers) {
try {
ILrcProvider.LyricResult lyricResult = provider.getLyric(mediaInfo);
ILrcProvider.LyricResult lyricResult = provider.getLyric(mediaInfo, requireTranslate);
if (lyricResult != null) {
if (LyricSearchUtil.isLyricContent(lyricResult.mLyric) && (currentResult == null || currentResult.mDistance > lyricResult.mDistance)) {
currentResult = lyricResult;
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/statusbar/finder/LyricsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public boolean insertLyricIntoDatabase(ILrcProvider.LyricResult lyricResult, ILr
packageName, originMediaInfo.getDuration(), -1, null, null, null, null, null, null, 0});
} else {
db.execSQL(query, new Object[]{originMediaInfo.getTitle(), originMediaInfo.getArtist(), originMediaInfo.getAlbum(),
packageName, originMediaInfo.getDuration(), lyricResult.mDistance, lyricResult.resultInfo.getTitle()
, lyricResult.resultInfo.getArtist(), lyricResult.resultInfo.getAlbum(), lyricResult.mLyric, lyricResult.mTranslatedLyric,
packageName, originMediaInfo.getDuration(), lyricResult.mDistance, lyricResult.mResultInfo.getTitle()
, lyricResult.mResultInfo.getArtist(), lyricResult.mResultInfo.getAlbum(), lyricResult.mLyric, lyricResult.mTranslatedLyric,
lyricResult.mSource, 0});
}
db.setTransactionSuccessful();
Expand Down Expand Up @@ -106,7 +106,7 @@ public ILrcProvider.LyricResult searchLyricFromDatabase(ILrcProvider.MediaInfo m
result.mDistance = cursor.getLong(cursor.getColumnIndex("distance"));
result.mOffset = (int) cursor.getLong(cursor.getColumnIndex("_offset"));

result.resultInfo = new ILrcProvider.MediaInfo(
result.mResultInfo = new ILrcProvider.MediaInfo(
cursor.getString(cursor.getColumnIndex("result_title")),
cursor.getString(cursor.getColumnIndex("result_artist")),
cursor.getString(cursor.getColumnIndex("result_album")),
Expand All @@ -115,6 +115,7 @@ public ILrcProvider.LyricResult searchLyricFromDatabase(ILrcProvider.MediaInfo m
);

cursor.close();
result.mOrigin = ILrcProvider.Origin.DATABASE;
return result;
}
cursor.close();
Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/statusbar/finder/MusicListenerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.Locale;
import java.util.Objects;

import static statusbar.finder.misc.Constants.PREFERENCE_KEY_REQUIRE_TRANSLATE;

public class MusicListenerService extends NotificationListenerService {

private static final int NOTIFICATION_ID_LRC = 1;
Expand Down Expand Up @@ -122,7 +124,7 @@ public void onMetadataChanged(@Nullable MediaMetadata metadata) {
requiredLrcTitle = metadata.getString(MediaMetadata.METADATA_KEY_TITLE);
mCurrentMediaInfo = new ILrcProvider.MediaInfo(metadata);
if (curLrcUpdateThread == null || !curLrcUpdateThread.isAlive()) {
curLrcUpdateThread = new LrcUpdateThread(getApplicationContext(), mHandler, metadata, mMediaController.getPackageName());
curLrcUpdateThread = new LrcUpdateThread(getApplicationContext(), mHandler, metadata, mMediaController.getPackageName(), mSharedPreferences.getBoolean(PREFERENCE_KEY_REQUIRE_TRANSLATE, false));
curLrcUpdateThread.start();
}
}
Expand Down Expand Up @@ -247,7 +249,10 @@ private Notification buildLrcNotification(Object data) {
}
} else {
builder.setContentTitle("Failed to retrieve current playback media information");
builder.setContentText("Please check if the target application is correctly configured and if the target application is currently playing media.");
builder.setContentText("Please check if the target application is correctly configured and if the target application is currently playing media.")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Please check if the target application is correctly configured and if the target application is currently playing media."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
Notification notification = builder.build();
notification.extras.putLong("ticker_icon", R.drawable.ic_music);
Expand All @@ -263,10 +268,11 @@ private String notificationResultContentText(GetResult.Data data) {
}

String contentTextResult;
contentTextResult = String.format("Result: %s - %s", mCurrentResult.getResult().resultInfo.getTitle(),
mCurrentResult.getResult().resultInfo.getArtist());
if (mCurrentResult.getResult().resultInfo.getAlbum() != null) {
contentTextResult += " - " + mCurrentResult.getResult().resultInfo.getAlbum();
contentTextResult = String.format("Result: %s - %s", mCurrentResult.getResult().mResultInfo.getTitle(),
mCurrentResult.getResult().mResultInfo.getArtist());
contentTextResult += String.format("Source: %s (%s)", mCurrentResult.getResult().mSource, mCurrentResult.getResult().mOrigin.getCapitalizedName());
if (mCurrentResult.getResult().mResultInfo.getAlbum() != null) {
contentTextResult += " - " + mCurrentResult.getResult().mResultInfo.getAlbum();
}
return contentTextResult;
}
Expand All @@ -278,7 +284,6 @@ private String notificationLyricsContentText(LyricsChanged.Data data) {
contentTextLyric += "\nTranslatedLyric: " + data.getTranslatedLyric();
}
contentTextLyric += "\nLyricDelay: " + data.getDelay() + " sec";
contentTextLyric += "\nLyricsSource: " + mCurrentResult.getResult().mSource;
return contentTextResult + "\n" + contentTextLyric;
}

Expand Down Expand Up @@ -334,7 +339,7 @@ private void updateLyric(long position) {
}
String curLyric = sentence.content.trim();
Lyric.Sentence translatedSentence = null;
if (Constants.isTranslateCheck) { // 增添翻译
if (mSharedPreferences.getBoolean(PREFERENCE_KEY_REQUIRE_TRANSLATE, false)) { // 增添翻译
translatedSentence = getTranslatedSentence(position);
if (translatedSentence != null && !Objects.equals(translatedSentence.content, "") && !Objects.equals(sentence.content, "")) {
curLyric += "\n\r" + translatedSentence.content.trim();
Expand Down Expand Up @@ -385,19 +390,21 @@ public static class LrcUpdateThread extends Thread {
private final MediaMetadata data;
private final Context context;
private final String packageName;
private final boolean requireTranslate;

public LrcUpdateThread(Context context, Handler handler, MediaMetadata data, String packageName) {
public LrcUpdateThread(Context context, Handler handler, MediaMetadata data, String packageName, boolean requireTranslate) {
super();
this.data = data;
this.handler = handler;
this.context = context;
this.packageName = packageName;
this.requireTranslate = requireTranslate;
}

@Override
public void run() {
if (handler == null) return;
Lyric lrc = LrcGetter.getLyric(context, data, systemLanguage, packageName);
Lyric lrc = LrcGetter.getLyric(context, data, systemLanguage, packageName, requireTranslate);
Message message = new Message();
message.what = MSG_LYRIC_UPDATE_DONE;
message.obj = lrc;
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/java/statusbar/finder/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class SettingsActivity extends FragmentActivity {

private final static Map<String, String> mUrlMap = new HashMap<>();
public final static boolean lyricsGetterApiHasEnable = new API().getHasEnable();


@Override
Expand Down Expand Up @@ -132,7 +133,7 @@ public static class SettingsFragment
implements Preference.OnPreferenceClickListener {

private SwitchPreference mEnabledPreference;
private SwitchPreference mTranslate;
private SwitchPreference mTranslateSwitch;
private Preference mConnectionStatusPreference;

@Override
Expand All @@ -149,7 +150,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
manager.cancelAll();
mEnabledPreference = findPreference(Constants.PREFERENCE_KEY_ENABLED);
mConnectionStatusPreference = findPreference(Constants.PREFERENCE_KEY_CONNECTION_STATUS);
mTranslate = findPreference(Constants.PREFERENCE_KEY_TRANSLATE);
mTranslateSwitch = findPreference(Constants.PREFERENCE_KEY_REQUIRE_TRANSLATE);
// try {
// mNotificationFields[0] =
// Notification.class.getDeclaredField("FLAG_ALWAYS_SHOW_TICKER").getInt(null);
Expand All @@ -161,7 +162,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// mEnabledPreference.setSummary(R.string.unsupport_rom_summary);
// }
// );
final boolean lyricsGetterApiHasEnable = new API().getHasEnable();
// boolean lyricsGetterApiHasEnable = true; For Debug
if (mConnectionStatusPreference != null){
mConnectionStatusPreference.setSummary(String.valueOf(lyricsGetterApiHasEnable));
Expand All @@ -173,9 +173,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mEnabledPreference.setEnabled(true);
mEnabledPreference.setOnPreferenceClickListener(this);
}
if (mTranslate != null) {
mTranslate.setOnPreferenceClickListener(this);
Constants.isTranslateCheck = mTranslate.isChecked();
if (mTranslateSwitch != null) {
mTranslateSwitch.setOnPreferenceClickListener(this);
}
Preference appInfoPreference = findPreference("app");
if (appInfoPreference != null) {
Expand All @@ -202,7 +201,7 @@ public void onResume() {
public boolean onPreferenceClick(@NotNull Preference preference) {
if (preference == mEnabledPreference) {
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
} else if(preference == mConnectionStatusPreference) {
} else if (preference == mConnectionStatusPreference) {
Intent intent = new Intent();
// Open LyricsGetter
try {
Expand All @@ -213,8 +212,8 @@ public boolean onPreferenceClick(@NotNull Preference preference) {
}
// 启动活动
return true;
} else if (preference == mTranslate) {
Constants.isTranslateCheck = mTranslate.isChecked();
} else if (preference == mTranslateSwitch) {
Toast.makeText(requireContext(), "TranslateLyrics: " + (mTranslateSwitch.isChecked() ? "Enable" : "Disable"), Toast.LENGTH_SHORT).show();
} else {
String url = mUrlMap.get(preference.getKey());
if (TextUtils.isEmpty(url)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ public class AppsListChanged extends LiveData<Void> {
private static AppsListChanged instance;

public static AppsListChanged getInstance() {
if (instance == null) {
instance = new AppsListChanged();
}
return instance;
return instance != null ? instance : (instance = new AppsListChanged());
}

public void notifyChange() {
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/statusbar/finder/livedatas/GetResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ public class GetResult extends LiveData<GetResult.Data> {
private static GetResult instance;

public static GetResult getInstance() {
if (instance == null) {
instance = new GetResult();
}
return instance;
return instance != null ? instance : (instance = new GetResult());
}

public void notifyResult(GetResult.Data data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ public class LyricsChanged extends LiveData<LyricsChanged.Data> {
private static LyricsChanged instance;

public static LyricsChanged getInstance() {
if (instance == null) {
instance = new LyricsChanged();
}
return instance;
return instance != null ? instance : (instance = new LyricsChanged());
}

public void notifyLyrics(LyricsChanged.Data data) {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/statusbar/finder/misc/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {
public static final String PREFERENCE_KEY_ENABLED = "enabled";
public static final String PREFERENCE_KEY_CONNECTION_STATUS = "connection_status";
public static final String PREFERENCE_KEY_TRANSLATE = "translate";
public static final String PREFERENCE_KEY_REQUIRE_TRANSLATE = "require_translate";
public static final String PREFERENCE_KEY_ABOUT = "about";
public static final String PREFERENCE_KEY_TARGET_PACKAGES = "target_packages";

Expand All @@ -13,5 +13,4 @@ public class Constants {
public static final int FLAG_ONLY_UPDATE_TICKER = 0x2000000;

public static final String SETTINGS_ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
public static boolean isTranslateCheck = false;
}
9 changes: 7 additions & 2 deletions app/src/main/java/statusbar/finder/misc/checkStringLang.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class checkStringLang {

private static MojiDetector detector;
// public static boolean isJapenese(String text) {
// Set<Character.UnicodeBlock> japaneseUnicodeBlocks = new HashSet<Character.UnicodeBlock>() {{
// add(Character.UnicodeBlock.HIRAGANA);
Expand All @@ -20,7 +21,11 @@ public class checkStringLang {
// return false;
// }

private static MojiDetector getDetector() {
return detector != null ? detector : (detector = new MojiDetector());
}

public static boolean isJapanese(String str) {
return new MojiDetector().hasKana(str);
return getDetector().hasKana(str);
}
}
}
21 changes: 17 additions & 4 deletions app/src/main/java/statusbar/finder/provider/ILrcProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,37 @@
import java.io.IOException;

public interface ILrcProvider {
LyricResult getLyric(MediaMetadata data) throws IOException;
LyricResult getLyric(MediaInfo mediaInfo) throws IOException;
LyricResult getLyric(MediaMetadata data, boolean requireTranslate) throws IOException;
LyricResult getLyric(MediaInfo mediaInfo, boolean requireTranslate) throws IOException;

class LyricResult {
public String mLyric;
public String mTranslatedLyric;
public long mDistance = 0;
public String mSource = "Local";
public int mOffset = 0;
public MediaInfo resultInfo;
public MediaInfo mResultInfo;
public Origin mOrigin = Origin.UNDEFINED;

public String toSting() {
return "Distance: " + mDistance + "\n" +
"Source: " + mSource + "\n" +
"Offset: " + mOffset + "\n" +
"Lyric: " + mLyric + "\n" +
"TranslatedLyric: " + mTranslatedLyric + "\n" +
"RealInfo: " + resultInfo;
"RealInfo: " + mResultInfo;
}
}

enum Origin {
UNDEFINED,
INTERNET,
DATABASE;

public String getCapitalizedName() {
// 将枚举值转换为首字母大写的字符串
String enumName = this.name().toLowerCase();
return Character.toUpperCase(enumName.charAt(0)) + enumName.substring(1);
}
}

Expand Down
Loading

0 comments on commit 78719d4

Please sign in to comment.