|
1 | 1 | package org.schabi.newpipe.player.mediasession; |
2 | 2 |
|
3 | 3 | import static org.schabi.newpipe.MainActivity.DEBUG; |
| 4 | +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_RECREATE_NOTIFICATION; |
4 | 5 |
|
5 | 6 | import android.content.Intent; |
6 | 7 | import android.content.SharedPreferences; |
7 | 8 | import android.graphics.Bitmap; |
| 9 | +import android.os.Build; |
8 | 10 | import android.support.v4.media.MediaMetadataCompat; |
9 | 11 | import android.support.v4.media.session.MediaSessionCompat; |
10 | 12 | import android.util.Log; |
|
14 | 16 | import androidx.media.session.MediaButtonReceiver; |
15 | 17 |
|
16 | 18 | import com.google.android.exoplayer2.ForwardingPlayer; |
| 19 | +import com.google.android.exoplayer2.Player.RepeatMode; |
17 | 20 | import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; |
18 | 21 |
|
19 | 22 | import org.schabi.newpipe.R; |
| 23 | +import org.schabi.newpipe.extractor.stream.StreamInfo; |
20 | 24 | import org.schabi.newpipe.player.Player; |
| 25 | +import org.schabi.newpipe.player.notification.NotificationActionData; |
| 26 | +import org.schabi.newpipe.player.notification.NotificationConstants; |
21 | 27 | import org.schabi.newpipe.player.ui.PlayerUi; |
22 | 28 | import org.schabi.newpipe.player.ui.VideoPlayerUi; |
23 | 29 | import org.schabi.newpipe.util.StreamTypeUtil; |
24 | 30 |
|
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
25 | 33 | import java.util.Optional; |
26 | 34 |
|
27 | 35 | public class MediaSessionPlayerUi extends PlayerUi |
@@ -163,4 +171,107 @@ private MediaMetadataCompat buildMediaMetadata() { |
163 | 171 |
|
164 | 172 | return builder.build(); |
165 | 173 | } |
| 174 | + |
| 175 | + |
| 176 | + private void updateMediaSessionActions() { |
| 177 | + // On Android 13+ (or Android T or API 33+) the actions in the player notification can't be |
| 178 | + // controlled directly anymore, but are instead derived from custom media session actions. |
| 179 | + // However the system allows customizing only two of these actions, since the other three |
| 180 | + // are fixed to play-pause-buffering, previous, next. |
| 181 | + |
| 182 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { |
| 183 | + // Although setting media session actions on older android versions doesn't seem to |
| 184 | + // cause any trouble, it also doesn't seem to do anything, so we don't do anything to |
| 185 | + // save battery. Check out NotificationUtil.updateActions() to see what happens on |
| 186 | + // older android versions. |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + final List<SessionConnectorActionProvider> actions = new ArrayList<>(2); |
| 191 | + for (int i = 3; i < 5; ++i) { |
| 192 | + // only use the fourth and fifth actions (the settings page also shows only the last 2) |
| 193 | + final int action = player.getPrefs().getInt( |
| 194 | + player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), |
| 195 | + NotificationConstants.SLOT_DEFAULTS[i]); |
| 196 | + |
| 197 | + @Nullable final NotificationActionData data = |
| 198 | + NotificationActionData.fromNotificationActionEnum(player, action); |
| 199 | + |
| 200 | + if (data != null) { |
| 201 | + actions.add(new SessionConnectorActionProvider(data, context)); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + sessionConnector.setCustomActionProviders( |
| 206 | + actions.toArray(new MediaSessionConnector.CustomActionProvider[0])); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public void onBlocked() { |
| 211 | + super.onBlocked(); |
| 212 | + updateMediaSessionActions(); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void onPlaying() { |
| 217 | + super.onPlaying(); |
| 218 | + updateMediaSessionActions(); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public void onBuffering() { |
| 223 | + super.onBuffering(); |
| 224 | + updateMediaSessionActions(); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void onPaused() { |
| 229 | + super.onPaused(); |
| 230 | + updateMediaSessionActions(); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public void onPausedSeek() { |
| 235 | + super.onPausedSeek(); |
| 236 | + updateMediaSessionActions(); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public void onCompleted() { |
| 241 | + super.onCompleted(); |
| 242 | + updateMediaSessionActions(); |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void onRepeatModeChanged(@RepeatMode final int repeatMode) { |
| 247 | + super.onRepeatModeChanged(repeatMode); |
| 248 | + updateMediaSessionActions(); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void onShuffleModeEnabledChanged(final boolean shuffleModeEnabled) { |
| 253 | + super.onShuffleModeEnabledChanged(shuffleModeEnabled); |
| 254 | + updateMediaSessionActions(); |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public void onBroadcastReceived(final Intent intent) { |
| 259 | + super.onBroadcastReceived(intent); |
| 260 | + if (ACTION_RECREATE_NOTIFICATION.equals(intent.getAction())) { |
| 261 | + // the notification actions changed |
| 262 | + updateMediaSessionActions(); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + @Override |
| 267 | + public void onMetadataChanged(@NonNull final StreamInfo info) { |
| 268 | + super.onMetadataChanged(info); |
| 269 | + updateMediaSessionActions(); |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public void onPlayQueueEdited() { |
| 274 | + super.onPlayQueueEdited(); |
| 275 | + updateMediaSessionActions(); |
| 276 | + } |
166 | 277 | } |
0 commit comments