Java Code Examples for android.media.session.MediaSession#Token

The following examples show how to use android.media.session.MediaSession#Token . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void pushSessionsChanged(int userId) {
    synchronized (mLock) {
        FullUserRecord user = getFullUserRecordLocked(userId);
        if (user == null) {
            Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId);
            return;
        }
        List<MediaSessionRecord> records = getActiveSessionsLocked(userId);
        int size = records.size();
        ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>();
        for (int i = 0; i < size; i++) {
            tokens.add(new MediaSession.Token(records.get(i).getControllerBinder()));
        }
        pushRemoteVolumeUpdateLocked(userId);
        for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
            SessionsListenerRecord record = mSessionsListeners.get(i);
            if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) {
                try {
                    record.mListener.onActiveSessionsChanged(tokens);
                } catch (RemoteException e) {
                    Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing",
                            e);
                    mSessionsListeners.remove(i);
                }
            }
        }
    }
}
 
Example 2
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
private void unregister() {
    for (Map.Entry<MediaSession.Token, Pair<MediaController, MediaController.Callback>> entry : mControllers.entrySet()) {
        Pair<MediaController, MediaController.Callback> pair = entry.getValue();
        pair.first.unregisterCallback(pair.second);
    }
    synchronized (mControllers) {
        mControllers.clear();
    }
}
 
Example 3
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
private void unregister() {
    for (Map.Entry<MediaSession.Token, Pair<MediaController, MediaController.Callback>> entry : mControllers.entrySet()) {
        Pair<MediaController, MediaController.Callback> pair = entry.getValue();
        pair.first.unregisterCallback(pair.second);
    }
    synchronized (mControllers) {
        mControllers.clear();
    }
}
 
Example 4
Source File: NotificationUtils.java    From RoMote with Apache License 2.0 4 votes vote down vote up
public static Notification buildNotification(
        Context context,
        String title,
        String text,
        Bitmap bitmap,
        MediaSession.Token token) {

    String contentTitle = "Roku";
    String contentText = "";

    if (title != null) {
        contentTitle = title;
    }

    if (text != null) {
        contentText = text;
    }

    PendingIntent contentIntent = PendingIntent.getActivity(
            context,
            ((int) System.currentTimeMillis()),
            new Intent(context,MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    Notification.MediaStyle style = new Notification.MediaStyle();
    style.setMediaSession(token);

    Notification.Builder builder = new Notification.Builder(context)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setContentIntent(contentIntent)
            .setWhen(0)
            .setSmallIcon(R.drawable.ic_notification_icon)
            .setPriority(Notification.PRIORITY_LOW);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(context.getString(R.string.app_name));
    }

    if (bitmap != null) {
        builder.setLargeIcon(bitmap);
    }

    builder.addAction(GenerateActionCompat(context, android.R.drawable.ic_media_rew, "Previous", 0, KeypressKeyValues.REV));
    builder.addAction(GenerateActionCompat(context, android.R.drawable.ic_media_pause, "Pause", 1, KeypressKeyValues.PLAY));
    builder.addAction(GenerateActionCompat(context, android.R.drawable.ic_media_ff, "Next", 2, KeypressKeyValues.FWD));

    style.setShowActionsInCompactView(0, 1, 2);
    //style.setShowCancelButton(true);

    builder.setStyle(style);

    return builder.build();
}
 
Example 5
Source File: MediaNotificationManager.java    From android-music-player with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}
 
Example 6
Source File: MediaNotificationManager.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}
 
Example 7
Source File: MediaControllerCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static Object fromToken(Context context, Object sessionToken) {
    return new MediaController(context, (MediaSession.Token) sessionToken);
}
 
Example 8
Source File: MediaController2Lollipop.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
public EventUpdateMetadata(@NonNull MediaSession.Token token) {
    super();
    mHandler = new Handler(Looper.getMainLooper());
    mToken = token;
}