MediaSessionLegacyHelperpublic class MediaSessionLegacyHelper extends Object Helper for connecting existing APIs up to the new session APIs. This can be
used by RCC, AudioFocus, etc. to create a single session that translates to
all those components. |
Fields Summary |
---|
private static final String | TAG | private static final boolean | DEBUG | private static final Object | sLock | private static MediaSessionLegacyHelper | sInstance | private android.content.Context | mContext | private MediaSessionManager | mSessionManager | private android.os.Handler | mHandler | private android.util.ArrayMap | mSessions |
Constructors Summary |
---|
private MediaSessionLegacyHelper(android.content.Context context)
mContext = context;
mSessionManager = (MediaSessionManager) context
.getSystemService(Context.MEDIA_SESSION_SERVICE);
|
Methods Summary |
---|
public void | addMediaButtonListener(android.app.PendingIntent pi, android.content.ComponentName mbrComponent, android.content.Context context)
if (pi == null) {
Log.w(TAG, "Pending intent was null, can't addMediaButtonListener.");
return;
}
SessionHolder holder = getHolder(pi, true);
if (holder == null) {
return;
}
if (holder.mMediaButtonListener != null) {
// Already have this listener registered
if (DEBUG) {
Log.d(TAG, "addMediaButtonListener already added " + pi);
}
}
holder.mMediaButtonListener = new MediaButtonListener(pi, context);
// TODO determine if handling transport performer commands should also
// set this flag
holder.mFlags |= MediaSession.FLAG_HANDLES_MEDIA_BUTTONS;
holder.mSession.setFlags(holder.mFlags);
holder.mSession.setMediaButtonReceiver(pi);
holder.update();
if (DEBUG) {
Log.d(TAG, "addMediaButtonListener added " + pi);
}
| public void | addRccListener(android.app.PendingIntent pi, MediaSession.Callback listener)
if (pi == null) {
Log.w(TAG, "Pending intent was null, can't add rcc listener.");
return;
}
SessionHolder holder = getHolder(pi, true);
if (holder == null) {
return;
}
if (holder.mRccListener != null) {
if (holder.mRccListener == listener) {
if (DEBUG) {
Log.d(TAG, "addRccListener listener already added.");
}
// This is already the registered listener, ignore
return;
}
}
holder.mRccListener = listener;
holder.mFlags |= MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS;
holder.mSession.setFlags(holder.mFlags);
holder.update();
if (DEBUG) {
Log.d(TAG, "Added rcc listener for " + pi + ".");
}
| public static android.media.session.MediaSessionLegacyHelper | getHelper(android.content.Context context)
synchronized (sLock) {
if (sInstance == null) {
sInstance = new MediaSessionLegacyHelper(context.getApplicationContext());
}
}
return sInstance;
| private android.media.session.MediaSessionLegacyHelper$SessionHolder | getHolder(android.app.PendingIntent pi, boolean createIfMissing)
SessionHolder holder = mSessions.get(pi);
if (holder == null && createIfMissing) {
MediaSession session;
session = new MediaSession(mContext, TAG + "-" + pi.getCreatorPackage());
session.setActive(true);
holder = new SessionHolder(session, pi);
mSessions.put(pi, holder);
}
return holder;
| public static android.os.Bundle | getOldMetadata(android.media.MediaMetadata metadata, int artworkWidth, int artworkHeight)
boolean includeArtwork = artworkWidth != -1 && artworkHeight != -1;
Bundle oldMetadata = new Bundle();
if (metadata.containsKey(MediaMetadata.METADATA_KEY_ALBUM)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_ALBUM),
metadata.getString(MediaMetadata.METADATA_KEY_ALBUM));
}
if (includeArtwork && metadata.containsKey(MediaMetadata.METADATA_KEY_ART)) {
Bitmap art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART);
oldMetadata.putParcelable(String.valueOf(MediaMetadataEditor.BITMAP_KEY_ARTWORK),
scaleBitmapIfTooBig(art, artworkWidth, artworkHeight));
} else if (includeArtwork && metadata.containsKey(MediaMetadata.METADATA_KEY_ALBUM_ART)) {
// Fall back to album art if the track art wasn't available
Bitmap art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
oldMetadata.putParcelable(String.valueOf(MediaMetadataEditor.BITMAP_KEY_ARTWORK),
scaleBitmapIfTooBig(art, artworkWidth, artworkHeight));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_ALBUM_ARTIST)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST),
metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_ARTIST)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_ARTIST),
metadata.getString(MediaMetadata.METADATA_KEY_ARTIST));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_AUTHOR)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_AUTHOR),
metadata.getString(MediaMetadata.METADATA_KEY_AUTHOR));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_COMPILATION)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_COMPILATION),
metadata.getString(MediaMetadata.METADATA_KEY_COMPILATION));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_COMPOSER)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_COMPOSER),
metadata.getString(MediaMetadata.METADATA_KEY_COMPOSER));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_DATE)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_DATE),
metadata.getString(MediaMetadata.METADATA_KEY_DATE));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_DISC_NUMBER)) {
oldMetadata.putLong(String.valueOf(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER),
metadata.getLong(MediaMetadata.METADATA_KEY_DISC_NUMBER));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) {
oldMetadata.putLong(String.valueOf(MediaMetadataRetriever.METADATA_KEY_DURATION),
metadata.getLong(MediaMetadata.METADATA_KEY_DURATION));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_GENRE)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_GENRE),
metadata.getString(MediaMetadata.METADATA_KEY_GENRE));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS)) {
oldMetadata.putLong(String.valueOf(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS),
metadata.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_RATING)) {
oldMetadata.putParcelable(String.valueOf(MediaMetadataEditor.RATING_KEY_BY_OTHERS),
metadata.getRating(MediaMetadata.METADATA_KEY_RATING));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_USER_RATING)) {
oldMetadata.putParcelable(String.valueOf(MediaMetadataEditor.RATING_KEY_BY_USER),
metadata.getRating(MediaMetadata.METADATA_KEY_USER_RATING));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_TITLE)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_TITLE),
metadata.getString(MediaMetadata.METADATA_KEY_TITLE));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER)) {
oldMetadata.putLong(
String.valueOf(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER),
metadata.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_WRITER)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_WRITER),
metadata.getString(MediaMetadata.METADATA_KEY_WRITER));
}
if (metadata.containsKey(MediaMetadata.METADATA_KEY_YEAR)) {
oldMetadata.putString(String.valueOf(MediaMetadataRetriever.METADATA_KEY_YEAR),
metadata.getString(MediaMetadata.METADATA_KEY_YEAR));
}
return oldMetadata;
| public MediaSession | getSession(android.app.PendingIntent pi)
SessionHolder holder = mSessions.get(pi);
return holder == null ? null : holder.mSession;
| public boolean | isGlobalPriorityActive()
return mSessionManager.isGlobalPriorityActive();
| public void | removeMediaButtonListener(android.app.PendingIntent pi)
if (pi == null) {
return;
}
SessionHolder holder = getHolder(pi, false);
if (holder != null && holder.mMediaButtonListener != null) {
holder.mFlags &= ~MediaSession.FLAG_HANDLES_MEDIA_BUTTONS;
holder.mSession.setFlags(holder.mFlags);
holder.mMediaButtonListener = null;
holder.update();
if (DEBUG) {
Log.d(TAG, "removeMediaButtonListener removed " + pi);
}
}
| public void | removeRccListener(android.app.PendingIntent pi)
if (pi == null) {
return;
}
SessionHolder holder = getHolder(pi, false);
if (holder != null && holder.mRccListener != null) {
holder.mRccListener = null;
holder.mFlags &= ~MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS;
holder.mSession.setFlags(holder.mFlags);
holder.update();
if (DEBUG) {
Log.d(TAG, "Removed rcc listener for " + pi + ".");
}
}
| private static android.graphics.Bitmap | scaleBitmapIfTooBig(android.graphics.Bitmap bitmap, int maxWidth, int maxHeight)Scale a bitmap to fit the smallest dimension by uniformly scaling the
incoming bitmap. If the bitmap fits, then do nothing and return the
original.
if (bitmap != null) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
if (width > maxWidth || height > maxHeight) {
float scale = Math.min((float) maxWidth / width, (float) maxHeight / height);
int newWidth = Math.round(scale * width);
int newHeight = Math.round(scale * height);
Bitmap.Config newConfig = bitmap.getConfig();
if (newConfig == null) {
newConfig = Bitmap.Config.ARGB_8888;
}
Bitmap outBitmap = Bitmap.createBitmap(newWidth, newHeight, newConfig);
Canvas canvas = new Canvas(outBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, null,
new RectF(0, 0, outBitmap.getWidth(), outBitmap.getHeight()), paint);
bitmap = outBitmap;
}
}
return bitmap;
| public void | sendAdjustVolumeBy(int suggestedStream, int delta, int flags)
mSessionManager.dispatchAdjustVolume(suggestedStream, delta, flags);
if (DEBUG) {
Log.d(TAG, "dispatched volume adjustment");
}
| private static void | sendKeyEvent(android.app.PendingIntent pi, android.content.Context context, android.content.Intent intent)
try {
pi.send(context, 0, intent);
} catch (CanceledException e) {
Log.e(TAG, "Error sending media key down event:", e);
// Don't bother sending up if down failed
return;
}
| public void | sendMediaButtonEvent(android.view.KeyEvent keyEvent, boolean needWakeLock)
if (keyEvent == null) {
Log.w(TAG, "Tried to send a null key event. Ignoring.");
return;
}
mSessionManager.dispatchMediaKeyEvent(keyEvent, needWakeLock);
if (DEBUG) {
Log.d(TAG, "dispatched media key " + keyEvent);
}
| public void | sendVolumeKeyEvent(android.view.KeyEvent keyEvent, boolean musicOnly)
if (keyEvent == null) {
Log.w(TAG, "Tried to send a null key event. Ignoring.");
return;
}
boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
int direction = 0;
boolean isMute = false;
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
direction = AudioManager.ADJUST_RAISE;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
direction = AudioManager.ADJUST_LOWER;
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
isMute = true;
break;
}
if (down || up) {
int flags;
if (musicOnly) {
// This flag is used when the screen is off to only affect
// active media
flags = AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
} else {
// These flags are consistent with the home screen
if (up) {
flags = AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
} else {
flags = AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
}
}
if (direction != 0) {
// If this is action up we want to send a beep for non-music events
if (up) {
direction = 0;
}
mSessionManager.dispatchAdjustVolume(AudioManager.USE_DEFAULT_STREAM_TYPE,
direction, flags);
} else if (isMute) {
if (down) {
// We need to send two volume events on down, one to mute
// and one to show the UI
mSessionManager.dispatchAdjustVolume(AudioManager.USE_DEFAULT_STREAM_TYPE,
MediaSessionManager.DIRECTION_MUTE, flags);
}
mSessionManager.dispatchAdjustVolume(AudioManager.USE_DEFAULT_STREAM_TYPE,
0 /* direction, causes UI to show on down */, flags);
}
}
|
|