From 3f818807b316fa84a7110b715fc10001868b6402 Mon Sep 17 00:00:00 2001 From: My-Responsitories <107370289+My-Responsitories@users.noreply.github.com> Date: Tue, 2 Jun 2026 13:44:02 +0800 Subject: [PATCH] opt: java --- .../com/example/piliplus/AndroidHelper.java | 14 +++---- .../com/example/piliplus/MediaHelper.java | 39 ++++++++----------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/android/app/src/main/java/com/example/piliplus/AndroidHelper.java b/android/app/src/main/java/com/example/piliplus/AndroidHelper.java index 67994542e..df209cdac 100644 --- a/android/app/src/main/java/com/example/piliplus/AndroidHelper.java +++ b/android/app/src/main/java/com/example/piliplus/AndroidHelper.java @@ -74,7 +74,7 @@ public final class AndroidHelper { } intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); - } catch (Throwable ignored) { + } catch (Exception ignored) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); @@ -126,23 +126,23 @@ public final class AndroidHelper { private static void setPipActions(Activity activity, PictureInPictureParams.Builder builder, boolean isLive, boolean isPlaying) { ComponentName mbrComponent = MediaHelper.getMediaButtonReceiverComponent(activity); if (mbrComponent == null) return; - ArrayList actionList = new ArrayList<>(); + ArrayList actionList = new ArrayList<>(3); if (!isLive) { - actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_rewind_10s, "ACTION_REWIND", PlaybackState.ACTION_REWIND)); + actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_rewind_10s, "ACTION_REWIND", (int) PlaybackState.ACTION_REWIND)); } if (isPlaying) { - actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_pause, "ACTION_PAUSE", PlaybackState.ACTION_PAUSE)); + actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_pause, "ACTION_PAUSE", (int) PlaybackState.ACTION_PAUSE)); } else { - actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_play, "ACTION_PLAY", PlaybackState.ACTION_PLAY)); + actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_play, "ACTION_PLAY", (int) PlaybackState.ACTION_PLAY)); } if (!isLive) { - actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_fast_forward_10s, "ACTION_FAST_FORWARD", PlaybackState.ACTION_FAST_FORWARD)); + actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_player_fast_forward_10s, "ACTION_FAST_FORWARD", (int) PlaybackState.ACTION_FAST_FORWARD)); } builder.setActions(actionList); } @RequiresApi(api = Build.VERSION_CODES.O) - private static RemoteAction getRemoteAction(@NonNull ComponentName mbrComponent, Activity activity, @DrawableRes int resId, String title, long action) { + private static RemoteAction getRemoteAction(@NonNull ComponentName mbrComponent, Activity activity, @DrawableRes int resId, String title, int action) { return new RemoteAction( Icon.createWithResource(activity, resId), title, diff --git a/android/app/src/main/java/com/example/piliplus/MediaHelper.java b/android/app/src/main/java/com/example/piliplus/MediaHelper.java index fda64108b..a3befb3e9 100644 --- a/android/app/src/main/java/com/example/piliplus/MediaHelper.java +++ b/android/app/src/main/java/com/example/piliplus/MediaHelper.java @@ -29,10 +29,10 @@ import android.view.KeyEvent; import java.util.List; -public class MediaHelper { +class MediaHelper { private static final String TAG = "MediaButtonReceiver"; - public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action) { + static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, int action) { if (mbrComponent == null) { Log.w(TAG, "The component name of media button receiver should be provided."); return null; @@ -48,31 +48,24 @@ public class MediaHelper { intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); return PendingIntent.getBroadcast(context, keyCode, intent, - Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0); + Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0); } - public static int PlaybackStateCompat_toKeyCode(long action) { - if (action == PlaybackState.ACTION_PLAY) { - return KeyEvent.KEYCODE_MEDIA_PLAY; - } else if (action == PlaybackState.ACTION_PAUSE) { - return KeyEvent.KEYCODE_MEDIA_PAUSE; - } else if (action == PlaybackState.ACTION_SKIP_TO_NEXT) { - return KeyEvent.KEYCODE_MEDIA_NEXT; - } else if (action == PlaybackState.ACTION_SKIP_TO_PREVIOUS) { - return KeyEvent.KEYCODE_MEDIA_PREVIOUS; - } else if (action == PlaybackState.ACTION_STOP) { - return KeyEvent.KEYCODE_MEDIA_STOP; - } else if (action == PlaybackState.ACTION_FAST_FORWARD) { - return KeyEvent.KEYCODE_MEDIA_FAST_FORWARD; - } else if (action == PlaybackState.ACTION_REWIND) { - return KeyEvent.KEYCODE_MEDIA_REWIND; - } else if (action == PlaybackState.ACTION_PLAY_PAUSE) { - return KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE; - } - return KeyEvent.KEYCODE_UNKNOWN; + private static int PlaybackStateCompat_toKeyCode(int action) { + return switch (action) { + case (int) PlaybackState.ACTION_STOP -> KeyEvent.KEYCODE_MEDIA_STOP; + case (int) PlaybackState.ACTION_PAUSE -> KeyEvent.KEYCODE_MEDIA_PAUSE; + case (int) PlaybackState.ACTION_PLAY -> KeyEvent.KEYCODE_MEDIA_PLAY; + case (int) PlaybackState.ACTION_REWIND -> KeyEvent.KEYCODE_MEDIA_REWIND; + case (int) PlaybackState.ACTION_SKIP_TO_PREVIOUS -> KeyEvent.KEYCODE_MEDIA_PREVIOUS; + case (int) PlaybackState.ACTION_SKIP_TO_NEXT -> KeyEvent.KEYCODE_MEDIA_NEXT; + case (int) PlaybackState.ACTION_FAST_FORWARD -> KeyEvent.KEYCODE_MEDIA_FAST_FORWARD; + case (int) PlaybackState.ACTION_PLAY_PAUSE -> KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE; + default -> KeyEvent.KEYCODE_UNKNOWN; + }; } - public static ComponentName getMediaButtonReceiverComponent(Context context) { + static ComponentName getMediaButtonReceiverComponent(Context context) { Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); queryIntent.setPackage(context.getPackageName()); PackageManager pm = context.getPackageManager();