summaryrefslogtreecommitdiffstats
path: root/tests/OneMedia/src/com/android/onemedia/playback/Renderer.java
blob: 2451bdfb0b23c3b24e06755371e52f61048bfa05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.android.onemedia.playback;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: Insert description here. (generated by epastern)
 */
public abstract class Renderer {
    public static final String FEATURE_SET_CONTENT = "com.android.media.SET_CONTENT";
    public static final String FEATURE_SET_NEXT_CONTENT = "com.android.media.SET_NEXT_CONTENT";
    public static final String FEATURE_PLAY = "com.android.media.PLAY";
    public static final String FEATURE_PAUSE = "com.android.media.PAUSE";
    public static final String FEATURE_NEXT = "com.android.media.NEXT";
    public static final String FEATURE_PREVIOUS = "com.android.media.PREVIOUS";
    public static final String FEATURE_SEEK_TO = "com.android.media.SEEK_TO";
    public static final String FEATURE_STOP = "com.android.media.STOP";
    // TODO move states somewhere else
    public static final int STATE_ERROR = 0;
    /**
     * The state MediaPlayerManager starts in before any action has been
     * performed.
     */
    public static final int STATE_INIT = 1 << 0;
    /**
     * Indicates the source has been set and it is being prepared/buffered
     * before starting playback.
     */
    public static final int STATE_PREPARING = 1 << 1;
    /**
     * The media is ready and playback can be started.
     */
    public static final int STATE_READY = 1 << 2;
    /**
     * The media is currently playing.
     */
    public static final int STATE_PLAYING = 1 << 3;
    /**
     * The media is currently paused.
     */
    public static final int STATE_PAUSED = 1 << 4;
    /**
     * The service has been stopped and cannot be started again until a new
     * source has been set.
     */
    public static final int STATE_STOPPED = 1 << 5;
    /**
     * The playback has reached the end. It can be restarted by calling play().
     */
    public static final int STATE_ENDED = 1 << 6;

    // TODO decide on proper way of describing features
    protected List<String> mFeatures = new ArrayList<String>();
    protected List<Listener> mListeners = new ArrayList<Listener>();

    public Renderer(Context context, Bundle params) {
        onCreate(params);
        initFeatures(params);
    }

    abstract public void setContent(Bundle request);

    public void onCreate(Bundle params) {
        // Do nothing by default
    }

    public void setNextContent(Bundle request) {
        throw new UnsupportedOperationException("setNextContent() is not supported.");
    }

    public List<String> getFeatures() {
        return mFeatures;
    }

    public boolean onPlay() {
        throw new UnsupportedOperationException("play is not supported.");
    }

    public boolean onPause() {
        throw new UnsupportedOperationException("pause is not supported.");
    }

    public boolean onNext() {
        throw new UnsupportedOperationException("next is not supported.");
    }

    public boolean onPrevious() {
        throw new UnsupportedOperationException("previous is not supported.");
    }

    public boolean onStop() {
        throw new UnsupportedOperationException("stop is not supported.");
    }

    public boolean onSeekTo(int time) {
        throw new UnsupportedOperationException("seekTo is not supported.");
    }

    public long getSeekPosition() {
        throw new UnsupportedOperationException("getSeekPosition is not supported.");
    }

    public long getDuration() {
        throw new UnsupportedOperationException("getDuration is not supported.");
    }

    public int getPlayState() {
        throw new UnsupportedOperationException("getPlayState is not supported.");
    }

    public void onDestroy() {
        // Do nothing by default
    }

    public void registerListener(Listener listener) {
        if (!mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }

    public void unregisterListener(Listener listener) {
        mListeners.remove(listener);
    }

    protected void initFeatures(Bundle params) {
        mFeatures.add(FEATURE_SET_CONTENT);
    }

    protected void pushOnError(int type, int extra, Bundle extras, Throwable error) {
        for (Listener listener : mListeners) {
            listener.onError(type, extra, extras, error);
        }
    }

    protected void pushOnStateChanged(int newState) {
        for (Listener listener : mListeners) {
            listener.onStateChanged(newState);
        }
    }

    protected void pushOnBufferingUpdate(int percent) {
        for (Listener listener : mListeners) {
            listener.onBufferingUpdate(percent);
        }
    }

    protected void pushOnFocusLost() {
        for (Listener listener : mListeners) {
            listener.onFocusLost();
        }
    }

    protected void pushOnNextStarted() {
        for (Listener listener : mListeners) {
            listener.onNextStarted();
        }
    }

    public interface Listener {
        public static final int ERROR_LOAD_FAILED = 1770;
        public static final int ERROR_PREPARE_ERROR = 1771;
        public static final int ERROR_PLAYBACK_FAILED = 1772;

        /**
         * When an error occurs onError will be called but not onStateChanged.
         * The Manager will remain in the error state until
         * {@link #setContent()} is called again.
         */
        public void onError(int type, int extra, Bundle extras,
                Throwable error);

        /**
         * onStateChanged will be called whenever the state of the manager
         * transitions except to an error state.
         */
        public void onStateChanged(int newState);

        /**
         * This is a passthrough of
         * {@link MediaPlayer.OnBufferingUpdateListener}.
         */
        public void onBufferingUpdate(int percent);

        /**
         * Called when audio focus is lost and it is not transient or ducking.
         */
        public void onFocusLost();

        /**
         * Called when the next item was started playing. Only called if a next
         * item has been set and the current item has ended.
         */
        public void onNextStarted();
    }
}