修复与优化桌面歌词代码

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

View File

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

View File

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

View File

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