1 /* 2 * Copyright (C) 2013 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.android.supportv7.media; 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 /** 26 * PlaylistItem helps keep track of the current status of an media item. 27 */ 28 final class PlaylistItem { 29 // immutables 30 private final String mSessionId; 31 private final String mItemId; 32 private final Uri mUri; 33 private final String mTitle; 34 private final String mMime; 35 private final PendingIntent mUpdateReceiver; 36 // changeable states 37 private int mPlaybackState = MediaItemStatus.PLAYBACK_STATE_PENDING; 38 private long mContentPosition; 39 private long mContentDuration; 40 private long mTimestamp; 41 private String mRemoteItemId; 42 PlaylistItem(String qid, String iid, String title, Uri uri, String mime, PendingIntent pi)43 public PlaylistItem(String qid, String iid, String title, Uri uri, String mime, 44 PendingIntent pi) { 45 mSessionId = qid; 46 mItemId = iid; 47 mTitle = title; 48 mUri = uri; 49 mMime = mime; 50 mUpdateReceiver = pi; 51 setTimestamp(SystemClock.elapsedRealtime()); 52 } 53 setRemoteItemId(String riid)54 public void setRemoteItemId(String riid) { 55 mRemoteItemId = riid; 56 } 57 setState(int state)58 public void setState(int state) { 59 mPlaybackState = state; 60 } 61 setPosition(long pos)62 public void setPosition(long pos) { 63 mContentPosition = pos; 64 } 65 setTimestamp(long ts)66 public void setTimestamp(long ts) { 67 mTimestamp = ts; 68 } 69 setDuration(long duration)70 public void setDuration(long duration) { 71 mContentDuration = duration; 72 } 73 getSessionId()74 public String getSessionId() { 75 return mSessionId; 76 } 77 getItemId()78 public String getItemId() { 79 return mItemId; 80 } 81 getRemoteItemId()82 public String getRemoteItemId() { 83 return mRemoteItemId; 84 } 85 getTitle()86 public String getTitle() { 87 return mTitle; 88 } 89 getUri()90 public Uri getUri() { 91 return mUri; 92 } 93 getUpdateReceiver()94 public PendingIntent getUpdateReceiver() { 95 return mUpdateReceiver; 96 } 97 getState()98 public int getState() { 99 return mPlaybackState; 100 } 101 getPosition()102 public long getPosition() { 103 return mContentPosition; 104 } 105 getDuration()106 public long getDuration() { 107 return mContentDuration; 108 } 109 getTimestamp()110 public long getTimestamp() { 111 return mTimestamp; 112 } 113 getStatus()114 public MediaItemStatus getStatus() { 115 return new MediaItemStatus.Builder(mPlaybackState) 116 .setContentPosition(mContentPosition) 117 .setContentDuration(mContentDuration) 118 .setTimestamp(mTimestamp) 119 .build(); 120 } 121 122 @Override toString()123 public String toString() { 124 String state[] = { 125 "PENDING", 126 "PLAYING", 127 "PAUSED", 128 "BUFFERING", 129 "FINISHED", 130 "CANCELED", 131 "INVALIDATED", 132 "ERROR" 133 }; 134 return "[" + mSessionId + "|" + mItemId + "|" 135 + (mRemoteItemId != null ? mRemoteItemId : "-") + "|" 136 + state[mPlaybackState] + "] " + mTitle + ": " + mUri.toString(); 137 } 138 } 139