add pip backward/forward btns

Closes #2251

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-06-01 10:04:11 +08:00
parent fb9568a628
commit 9ac37d6fb3
8 changed files with 247 additions and 26 deletions

View 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;
}
}