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