1 package com.android.bluetooth.avrcp; 2 3 import android.annotation.NonNull; 4 import android.annotation.Nullable; 5 import android.app.PendingIntent; 6 import android.content.Context; 7 import android.content.pm.ParceledListSlice; 8 import android.media.AudioAttributes; 9 import android.media.AudioManager; 10 import android.media.MediaMetadata; 11 import android.media.MediaDescription; 12 import android.media.Rating; 13 import android.media.VolumeProvider; 14 import android.media.session.PlaybackState; 15 import android.media.session.MediaSession; 16 import android.media.session.MediaSession.QueueItem; 17 import android.net.Uri; 18 import android.os.Bundle; 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.os.Message; 22 import android.os.RemoteException; 23 import android.os.ResultReceiver; 24 import android.text.TextUtils; 25 import android.util.Log; 26 import android.view.KeyEvent; 27 28 import java.lang.ref.WeakReference; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 public class MediaController { 33 public @NonNull android.media.session.MediaController mDelegate; 34 public android.media.session.MediaController.TransportControls mTransportDelegate; 35 public TransportControls mTransportControls; 36 37 @Nullable wrap(@ullable android.media.session.MediaController delegate)38 public static MediaController wrap(@Nullable android.media.session.MediaController delegate) { 39 return (delegate != null) ? new MediaController(delegate) : null; 40 } 41 MediaController(@onNull android.media.session.MediaController delegate)42 public MediaController(@NonNull android.media.session.MediaController delegate) { 43 mDelegate = delegate; 44 mTransportDelegate = delegate.getTransportControls(); 45 mTransportControls = new TransportControls(); 46 } 47 getWrappedInstance()48 public android.media.session.MediaController getWrappedInstance() { 49 return mDelegate; 50 } 51 getTransportControls()52 public @NonNull TransportControls getTransportControls() { 53 return mTransportControls; 54 } 55 dispatchMediaButtonEvent(@onNull KeyEvent keyEvent)56 public boolean dispatchMediaButtonEvent(@NonNull KeyEvent keyEvent) { 57 return mDelegate.dispatchMediaButtonEvent(keyEvent); 58 } 59 getPlaybackState()60 public @Nullable PlaybackState getPlaybackState() { 61 return mDelegate.getPlaybackState(); 62 } 63 getMetadata()64 public @Nullable MediaMetadata getMetadata() { 65 return mDelegate.getMetadata(); 66 } 67 getQueue()68 public @Nullable List<MediaSession.QueueItem> getQueue() { 69 return mDelegate.getQueue(); 70 } 71 getQueueTitle()72 public @Nullable CharSequence getQueueTitle() { 73 return mDelegate.getQueueTitle(); 74 } 75 getExtras()76 public @Nullable Bundle getExtras() { 77 return mDelegate.getExtras(); 78 } 79 getRatingType()80 public int getRatingType() { 81 return mDelegate.getRatingType(); 82 } 83 getFlags()84 public long getFlags() { 85 return mDelegate.getFlags(); 86 } 87 getPlaybackInfo()88 public @Nullable android.media.session.MediaController.PlaybackInfo getPlaybackInfo() { 89 return mDelegate.getPlaybackInfo(); 90 } 91 getSessionActivity()92 public @Nullable PendingIntent getSessionActivity() { 93 return mDelegate.getSessionActivity(); 94 } 95 getSessionToken()96 public @NonNull MediaSession.Token getSessionToken() { 97 return mDelegate.getSessionToken(); 98 } 99 setVolumeTo(int value, int flags)100 public void setVolumeTo(int value, int flags) { 101 mDelegate.setVolumeTo(value, flags); 102 } 103 adjustVolume(int direction, int flags)104 public void adjustVolume(int direction, int flags) { 105 mDelegate.adjustVolume(direction, flags); 106 } 107 registerCallback(@onNull Callback callback)108 public void registerCallback(@NonNull Callback callback) { 109 //TODO(apanicke): Add custom callback struct to be able to analyze and 110 // delegate callbacks 111 mDelegate.registerCallback(callback); 112 } 113 registerCallback(@onNull Callback callback, @Nullable Handler handler)114 public void registerCallback(@NonNull Callback callback, @Nullable Handler handler) { 115 mDelegate.registerCallback(callback, handler); 116 } 117 unregisterCallback(@onNull Callback callback)118 public void unregisterCallback(@NonNull Callback callback) { 119 mDelegate.unregisterCallback(callback); 120 } 121 sendCommand(@onNull String command, @Nullable Bundle args, @Nullable ResultReceiver cb)122 public void sendCommand(@NonNull String command, @Nullable Bundle args, 123 @Nullable ResultReceiver cb) { 124 mDelegate.sendCommand(command, args, cb); 125 } 126 getPackageName()127 public String getPackageName() { 128 return mDelegate.getPackageName(); 129 } 130 getTag()131 public String getTag() { 132 return mDelegate.getTag(); 133 } 134 controlsSameSession(MediaController other)135 public boolean controlsSameSession(MediaController other) { 136 return mDelegate.controlsSameSession(other.getWrappedInstance()); 137 } 138 controlsSameSession(android.media.session.MediaController other)139 public boolean controlsSameSession(android.media.session.MediaController other) { 140 return mDelegate.controlsSameSession(other); 141 } 142 143 @Override equals(Object o)144 public boolean equals(Object o) { 145 if (o instanceof android.media.session.MediaController) { 146 return mDelegate.equals(o); 147 } else if (o instanceof MediaController) { 148 MediaController other = (MediaController) o; 149 return mDelegate.equals(other.mDelegate); 150 } 151 return false; 152 } 153 154 @Override toString()155 public String toString() { 156 MediaMetadata data = getMetadata(); 157 MediaDescription desc = (data == null) ? null : data.getDescription(); 158 return "MediaController (" + getPackageName() + "@" 159 + Integer.toHexString(mDelegate.hashCode()) + ") " + desc; 160 } 161 162 public static abstract class Callback extends android.media.session.MediaController.Callback { } 163 164 public class TransportControls { 165 prepare()166 public void prepare() { 167 mTransportDelegate.prepare(); 168 } 169 prepareFromMediaId(String mediaId, Bundle extras)170 public void prepareFromMediaId(String mediaId, Bundle extras) { 171 mTransportDelegate.prepareFromMediaId(mediaId, extras); 172 } 173 prepareFromSearch(String query, Bundle extras)174 public void prepareFromSearch(String query, Bundle extras) { 175 mTransportDelegate.prepareFromSearch(query, extras); 176 } 177 prepareFromUri(Uri uri, Bundle extras)178 public void prepareFromUri(Uri uri, Bundle extras) { 179 mTransportDelegate.prepareFromUri(uri, extras); 180 } 181 play()182 public void play() { 183 mTransportDelegate.play(); 184 } 185 playFromMediaId(String mediaId, Bundle extras)186 public void playFromMediaId(String mediaId, Bundle extras) { 187 mTransportDelegate.playFromMediaId(mediaId, extras); 188 } 189 playFromSearch(String query, Bundle extras)190 public void playFromSearch(String query, Bundle extras) { 191 mTransportDelegate.playFromSearch(query, extras); 192 } 193 playFromUri(Uri uri, Bundle extras)194 public void playFromUri(Uri uri, Bundle extras) { 195 mTransportDelegate.playFromUri(uri, extras); 196 } 197 skipToQueueItem(long id)198 public void skipToQueueItem(long id) { 199 mTransportDelegate.skipToQueueItem(id); 200 } 201 pause()202 public void pause() { 203 mTransportDelegate.pause(); 204 } 205 stop()206 public void stop() { 207 mTransportDelegate.stop(); 208 } 209 seekTo(long pos)210 public void seekTo(long pos) { 211 mTransportDelegate.seekTo(pos); 212 } 213 fastForward()214 public void fastForward() { 215 mTransportDelegate.fastForward(); 216 } 217 skipToNext()218 public void skipToNext() { 219 mTransportDelegate.skipToNext(); 220 } 221 rewind()222 public void rewind() { 223 mTransportDelegate.rewind(); 224 } 225 skipToPrevious()226 public void skipToPrevious() { 227 mTransportDelegate.skipToPrevious(); 228 } 229 setRating(Rating rating)230 public void setRating(Rating rating) { 231 mTransportDelegate.setRating(rating); 232 } 233 sendCustomAction(@onNull PlaybackState.CustomAction customAction, @Nullable Bundle args)234 public void sendCustomAction(@NonNull PlaybackState.CustomAction customAction, 235 @Nullable Bundle args) { 236 mTransportDelegate.sendCustomAction(customAction, args); 237 } 238 sendCustomAction(@onNull String action, @Nullable Bundle args)239 public void sendCustomAction(@NonNull String action, @Nullable Bundle args) { 240 mTransportDelegate.sendCustomAction(action, args); 241 } 242 } 243 } 244 245