1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.mediarouting.data; 18 19 import android.app.PendingIntent; 20 import android.net.Uri; 21 import android.os.SystemClock; 22 23 import androidx.mediarouter.media.MediaItemStatus; 24 25 import org.jspecify.annotations.NonNull; 26 27 /** 28 * PlaylistItem helps keep track of the current status of an media item. 29 */ 30 public final class PlaylistItem { 31 // immutables 32 private final String mSessionId; 33 private final String mItemId; 34 private final Uri mUri; 35 private final String mMime; 36 private final String mTitle; 37 private final PendingIntent mUpdateReceiver; 38 // changeable states 39 private int mPlaybackState = MediaItemStatus.PLAYBACK_STATE_PENDING; 40 private long mContentPosition; 41 private long mContentDuration; 42 private long mTimestamp; 43 private String mRemoteItemId; 44 PlaylistItem(@onNull PlaylistItem item)45 public PlaylistItem(@NonNull PlaylistItem item) { 46 mSessionId = item.mSessionId; 47 mItemId = item.mItemId; 48 mTitle = item.mTitle; 49 mUri = item.mUri; 50 mMime = item.mMime; 51 mUpdateReceiver = null; 52 53 mPlaybackState = item.mPlaybackState; 54 mContentPosition = item.mContentPosition; 55 mContentDuration = item.mContentDuration; 56 mTimestamp = item.mTimestamp; 57 mRemoteItemId = item.mRemoteItemId; 58 } 59 PlaylistItem(@onNull String qid, @NonNull String iid, @NonNull String title, @NonNull Uri uri, @NonNull String mime, @NonNull PendingIntent pi)60 public PlaylistItem(@NonNull String qid, @NonNull String iid, @NonNull String title, 61 @NonNull Uri uri, @NonNull String mime, @NonNull PendingIntent pi) { 62 mSessionId = qid; 63 mItemId = iid; 64 mTitle = title; 65 mUri = uri; 66 mMime = mime; 67 mUpdateReceiver = pi; 68 setTimestamp(SystemClock.elapsedRealtime()); 69 } 70 setRemoteItemId(@onNull String riid)71 public void setRemoteItemId(@NonNull String riid) { 72 mRemoteItemId = riid; 73 } 74 setState(int state)75 public void setState(int state) { 76 mPlaybackState = state; 77 } 78 setPosition(long pos)79 public void setPosition(long pos) { 80 mContentPosition = pos; 81 } 82 setTimestamp(long ts)83 public void setTimestamp(long ts) { 84 mTimestamp = ts; 85 } 86 setDuration(long duration)87 public void setDuration(long duration) { 88 mContentDuration = duration; 89 } 90 getSessionId()91 public @NonNull String getSessionId() { 92 return mSessionId; 93 } 94 getItemId()95 public @NonNull String getItemId() { 96 return mItemId; 97 } 98 getRemoteItemId()99 public @NonNull String getRemoteItemId() { 100 return mRemoteItemId; 101 } 102 getTitle()103 public @NonNull String getTitle() { 104 return mTitle; 105 } 106 getUri()107 public @NonNull Uri getUri() { 108 return mUri; 109 } 110 getMime()111 public @NonNull String getMime() { 112 return mMime; 113 } 114 getUpdateReceiver()115 public @NonNull PendingIntent getUpdateReceiver() { 116 return mUpdateReceiver; 117 } 118 getState()119 public int getState() { 120 return mPlaybackState; 121 } 122 getPosition()123 public long getPosition() { 124 return mContentPosition; 125 } 126 getDuration()127 public long getDuration() { 128 return mContentDuration; 129 } 130 getTimestamp()131 public long getTimestamp() { 132 return mTimestamp; 133 } 134 getStatus()135 public @NonNull MediaItemStatus getStatus() { 136 return new MediaItemStatus.Builder(mPlaybackState) 137 .setContentPosition(mContentPosition) 138 .setContentDuration(mContentDuration) 139 .setTimestamp(mTimestamp) 140 .build(); 141 } 142 143 @Override toString()144 public @NonNull String toString() { 145 String[] state = { 146 "PENDING", 147 "PLAYING", 148 "PAUSED", 149 "BUFFERING", 150 "FINISHED", 151 "CANCELED", 152 "INVALIDATED", 153 "ERROR" 154 }; 155 return "[" + mSessionId + "|" + mItemId + "|" 156 + (mRemoteItemId != null ? mRemoteItemId : "-") + "|" 157 + state[mPlaybackState] + "] " + mTitle + ": " + mUri.toString(); 158 } 159 } 160