mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-06-02 09:08:17 +08:00
add pip backward/forward btns
Closes #2251 Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
@@ -3,6 +3,7 @@ package com.example.piliplus;
|
||||
import android.app.Activity;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.PictureInPictureParams;
|
||||
import android.app.RemoteAction;
|
||||
import android.app.SearchManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
@@ -15,6 +16,7 @@ import android.graphics.BitmapFactory;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.media.session.PlaybackState;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.MediaStore;
|
||||
@@ -22,12 +24,15 @@ import android.provider.Settings;
|
||||
import android.util.Rational;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.Keep;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.github.dart_lang.jni_flutter.JniFlutterPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
@Keep
|
||||
public final class AndroidHelper {
|
||||
@@ -149,12 +154,13 @@ public final class AndroidHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void enterPip(int width, int height, boolean autoEnter, long engineId) {
|
||||
public static void enterPip(long engineId, int width, int height, boolean autoEnter, boolean isLive, boolean isPlaying) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
Activity activity = JniFlutterPlugin.getActivity(engineId);
|
||||
assert activity != null;
|
||||
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder()
|
||||
.setAspectRatio(new Rational(width, height));
|
||||
setPipActions(activity, builder, isLive, isPlaying);
|
||||
if (autoEnter) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
builder.setAutoEnterEnabled(true);
|
||||
@@ -166,6 +172,42 @@ public final class AndroidHelper {
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public static void updatePipActions(long engineId, boolean isLive, boolean isPlaying) {
|
||||
Activity activity = JniFlutterPlugin.getActivity(engineId);
|
||||
assert activity != null;
|
||||
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
|
||||
setPipActions(activity, builder, isLive, isPlaying);
|
||||
activity.setPictureInPictureParams(builder.build());
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
private static void setPipActions(Activity activity, PictureInPictureParams.Builder builder, boolean isLive, boolean isPlaying) {
|
||||
ArrayList<RemoteAction> actionList = new ArrayList<>();
|
||||
if (!isLive) {
|
||||
actionList.add(getRemoteAction(activity, R.drawable.ic_baseline_replay_10_24, "ACTION_REWIND", PlaybackState.ACTION_REWIND));
|
||||
}
|
||||
if (isPlaying) {
|
||||
actionList.add(getRemoteAction(activity, android.R.drawable.ic_media_pause, "ACTION_PAUSE", PlaybackState.ACTION_PAUSE));
|
||||
} else {
|
||||
actionList.add(getRemoteAction(activity, android.R.drawable.ic_media_play, "ACTION_PLAY", PlaybackState.ACTION_PLAY));
|
||||
}
|
||||
if (!isLive) {
|
||||
actionList.add(getRemoteAction(activity, R.drawable.ic_baseline_forward_10_24, "ACTION_FAST_FORWARD", PlaybackState.ACTION_FAST_FORWARD));
|
||||
}
|
||||
builder.setActions(actionList);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
private static RemoteAction getRemoteAction(Activity activity, @DrawableRes int resId, String title, long action) {
|
||||
return new RemoteAction(
|
||||
Icon.createWithResource(activity, resId),
|
||||
title,
|
||||
title,
|
||||
Objects.requireNonNull(MediaHelper.buildMediaButtonPendingIntent(activity, action))
|
||||
);
|
||||
}
|
||||
|
||||
public static void disableAutoEnterPip(long engineId) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
Activity activity = JniFlutterPlugin.getActivity(engineId);
|
||||
@@ -225,4 +267,4 @@ public final class AndroidHelper {
|
||||
private ToDart() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
100
android/app/src/main/java/com/example/piliplus/MediaHelper.java
Normal file
100
android/app/src/main/java/com/example/piliplus/MediaHelper.java
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.piliplus;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.media.session.PlaybackState;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MediaHelper {
|
||||
private static final String TAG = "MediaButtonReceiver";
|
||||
|
||||
public static PendingIntent buildMediaButtonPendingIntent(Context context, long action) {
|
||||
ComponentName mbrComponent = getMediaButtonReceiverComponent(context);
|
||||
if (mbrComponent == null) {
|
||||
Log.w(TAG, "A unique media button receiver could not be found in the given context, so "
|
||||
+ "couldn't build a pending intent.");
|
||||
return null;
|
||||
}
|
||||
return buildMediaButtonPendingIntent(context, mbrComponent, action);
|
||||
}
|
||||
|
||||
public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action) {
|
||||
if (mbrComponent == null) {
|
||||
Log.w(TAG, "The component name of media button receiver should be provided.");
|
||||
return null;
|
||||
}
|
||||
int keyCode = PlaybackStateCompat_toKeyCode(action);
|
||||
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
|
||||
Log.w(TAG,
|
||||
"Cannot build a media button pending intent with the given action: " + action);
|
||||
return null;
|
||||
}
|
||||
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
|
||||
intent.setComponent(mbrComponent);
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static ComponentName getMediaButtonReceiverComponent(Context context) {
|
||||
Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
|
||||
queryIntent.setPackage(context.getPackageName());
|
||||
PackageManager pm = context.getPackageManager();
|
||||
List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
|
||||
if (resolveInfos.size() == 1) {
|
||||
ResolveInfo resolveInfo = resolveInfos.get(0);
|
||||
return new ComponentName(resolveInfo.activityInfo.packageName,
|
||||
resolveInfo.activityInfo.name);
|
||||
} else if (resolveInfos.size() > 1) {
|
||||
Log.w(TAG, "More than one BroadcastReceiver that handles "
|
||||
+ Intent.ACTION_MEDIA_BUTTON + " was found, returning null.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user