修复与优化桌面歌词代码

This commit is contained in:
lyswhut 2022-05-15 12:40:35 +08:00
parent 572da436c4
commit 47f33e0b2e
4 changed files with 42 additions and 54 deletions

View File

@ -68,11 +68,8 @@ public class Lyric extends LyricPlayer {
setTempPause(true);
if (lyricView != null) {
lyricView.runOnUiThread(new Runnable() {
@Override
public void run() {
lyricView.destroyView();
}
lyricView.runOnUiThread(() -> {
lyricView.destroyView();
});
}
}
@ -80,13 +77,10 @@ public class Lyric extends LyricPlayer {
private void handleScreenOn() {
if (!isShowLyric) return;
if (lyricView == null) lyricView = new LyricView(reactAppContext, lyricEvent);
lyricView.runOnUiThread(new Runnable() {
@Override
public void run() {
lyricView.showLyricView();
setViewLyric(lastLine);
setTempPause(false);
}
lyricView.runOnUiThread(() -> {
lyricView.showLyricView();
setViewLyric(lastLine);
setTempPause(false);
});
}

View File

@ -8,6 +8,7 @@ import android.graphics.Point;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
@ -65,9 +66,13 @@ public class LyricView extends Activity implements View.OnTouchListener {
private int mLastRotation;
private OrientationEventListener orientationEventListener = null;
final Handler fixViewPositionHandler;
final Runnable fixViewPositionRunnable = this::fixViewPosition;
LyricView(ReactApplicationContext reactContext, LyricEvent lyricEvent) {
this.reactContext = reactContext;
this.lyricEvent = lyricEvent;
fixViewPositionHandler = new Handler();
}
private void listenOrientationEvent() {
@ -81,14 +86,14 @@ public class LyricView extends Activity implements View.OnTouchListener {
//rotation changed
// if (rotation == Surface.ROTATION_90){} // check rotations here
// if (rotation == Surface.ROTATION_270){} //
// Log.d("Lyric", "rotation: " + rotation);
fixViewPosition();
Log.d("Lyric", "rotation: " + rotation);
fixViewPositionHandler.postDelayed(fixViewPositionRunnable, 1000);
}
mLastRotation = rotation;
}
};
}
// Log.d("Lyric", "orientationEventListener: " + orientationEventListener.canDetectOrientation());
Log.d("Lyric", "orientationEventListener: " + orientationEventListener.canDetectOrientation());
if (orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
}
@ -112,16 +117,22 @@ public class LyricView extends Activity implements View.OnTouchListener {
return flag;
}
private void updateWH() {
/**
* update screen width and height
* @return has updated
*/
private boolean updateWH() {
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
if (maxWidth == size.x && maxHeight == size.y) return false;
maxWidth = size.x;
maxHeight = size.y;
return true;
}
private void fixViewPosition() {
updateWH();
if (!updateWH()) return;
int width = (int)(maxWidth * widthPercentage);
if (layoutParams.width != width) layoutParams.width = width;
@ -140,9 +151,9 @@ public class LyricView extends Activity implements View.OnTouchListener {
// layoutParams.x = x;
// layoutParams.y = y;
// Log.d("Lyric", "widthPercentage: " + widthPercentage + " prevViewPercentageX: " + prevViewPercentageX);
// Log.d("Lyric", "prevViewPercentageY: " + prevViewPercentageY + " layoutParams.x: " + layoutParams.x);
// Log.d("Lyric", "layoutParams.y: " + layoutParams.y + " layoutParams.width: " + layoutParams.width);
Log.d("Lyric", "widthPercentage: " + widthPercentage + " prevViewPercentageX: " + prevViewPercentageX);
Log.d("Lyric", "prevViewPercentageY: " + prevViewPercentageY + " layoutParams.x: " + layoutParams.x);
Log.d("Lyric", "layoutParams.y: " + layoutParams.y + " layoutParams.width: " + layoutParams.width);
windowManager.updateViewLayout(textView, layoutParams);
}
@ -511,12 +522,12 @@ public class LyricView extends Activity implements View.OnTouchListener {
if (textView == null || windowManager == null) return;
windowManager.removeView(textView);
textView = null;
removeOrientationEvent();
}
public void destroy() {
destroyView();
windowManager = null;
layoutParams = null;
removeOrientationEvent();
}
}

View File

@ -8,22 +8,19 @@ public class Utils {
return new TimeoutEvent(runnable, delay);
}
public static void clearTimeout(Object timeoutEvent) {
if (timeoutEvent != null && timeoutEvent instanceof TimeoutEvent) {
if (timeoutEvent instanceof TimeoutEvent) {
((TimeoutEvent) timeoutEvent).cancelTimeout();
}
}
private static class TimeoutEvent {
private static Handler handler = new Handler();
private static final Handler handler = new Handler();
private volatile Runnable runnable;
private TimeoutEvent(Runnable task, long delay) {
runnable = task;
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (runnable != null) {
runnable.run();
}
handler.postDelayed(() -> {
if (runnable != null) {
runnable.run();
}
}, delay);
}

View File

@ -1,5 +1,6 @@
package cn.toside.music.mobile.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
@ -24,6 +25,7 @@ import com.facebook.react.bridge.WritableNativeArray;
import java.io.File;
import java.util.List;
import java.util.Objects;
public class UtilsModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
@ -49,13 +51,7 @@ public class UtilsModule extends ReactContextBaseJavaModule {
Log.d("Utils", "killProcess");
android.os.Process.killProcess(android.os.Process.myPid());
} else {
if(Build.VERSION.SDK_INT >= 21){
currentActivity.finishAndRemoveTask();
} else if(Build.VERSION.SDK_INT >= 16){
currentActivity.finishAffinity();
} else{
currentActivity.finish();
}
currentActivity.finishAndRemoveTask();
System.exit(0);
}
}
@ -64,12 +60,8 @@ public class UtilsModule extends ReactContextBaseJavaModule {
public void getSupportedAbis(Promise promise) {
// https://github.com/react-native-device-info/react-native-device-info/blob/ff8f672cb08fa39a887567d6e23e2f08778e8340/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java#L877
WritableArray array = new WritableNativeArray();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (String abi : Build.SUPPORTED_ABIS) {
array.pushString(abi);
}
} else {
array.pushString(Build.CPU_ABI);
for (String abi : Build.SUPPORTED_ABIS) {
array.pushString(abi);
}
promise.resolve(array);
}
@ -128,11 +120,8 @@ public class UtilsModule extends ReactContextBaseJavaModule {
final Activity activity = getCurrentActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
activity.runOnUiThread(() -> {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
});
}
}
@ -143,11 +132,8 @@ public class UtilsModule extends ReactContextBaseJavaModule {
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);
}
activity.runOnUiThread(() -> {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
});
}
}
@ -165,7 +151,7 @@ public class UtilsModule extends ReactContextBaseJavaModule {
try {
WifiInfo info = wifi.getConnectionInfo();
int ipAddress = info.getIpAddress();
String stringip = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
@SuppressLint("DefaultLocale") String stringip = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
promise.resolve(stringip);
}catch (Exception e) {
@ -244,7 +230,7 @@ public class UtilsModule extends ReactContextBaseJavaModule {
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,text);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
reactContext.getCurrentActivity().startActivity(Intent.createChooser(shareIntent, shareTitle));
Objects.requireNonNull(reactContext.getCurrentActivity()).startActivity(Intent.createChooser(shareIntent, shareTitle));
}
}