summaryrefslogtreecommitdiffstats
path: root/tests/OneMedia/src/com/android/onemedia/PlayerController.java
blob: 3686899a028401a6b65f25b5096cafd522c4ced7 (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
200
201
202
203
204
205
206
207

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.onemedia;

import android.media.MediaMetadata;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;

import com.android.onemedia.playback.RequestUtils;

public class PlayerController {
    private static final String TAG = "PlayerController";

    public static final int STATE_DISCONNECTED = 0;
    public static final int STATE_CONNECTED = 1;

    protected MediaController mController;
    protected IPlayerService mBinder;
    protected MediaController.TransportControls mTransportControls;

    private final Intent mServiceIntent;
    private Activity mContext;
    private Listener mListener;
    private SessionCallback mControllerCb;
    private MediaSessionManager mManager;
    private Handler mHandler = new Handler();

    private boolean mResumed;

    public PlayerController(Activity context, Intent serviceIntent) {
        mContext = context;
        if (serviceIntent == null) {
            mServiceIntent = new Intent(mContext, PlayerService.class);
        } else {
            mServiceIntent = serviceIntent;
        }
        mControllerCb = new SessionCallback();
        mManager = (MediaSessionManager) context
                .getSystemService(Context.MEDIA_SESSION_SERVICE);

        mResumed = false;
    }

    public void setListener(Listener listener) {
        mListener = listener;
        Log.d(TAG, "Listener set to " + listener + " session is " + mController);
        if (mListener != null) {
            mHandler = new Handler();
            mListener.onConnectionStateChange(
                    mController == null ? STATE_DISCONNECTED : STATE_CONNECTED);
        }
    }

    public void onResume() {
        mResumed = true;
        Log.d(TAG, "onResume. Binding to service with intent " + mServiceIntent.toString());
        bindToService();
    }

    public void onPause() {
        mResumed = false;
        Log.d(TAG, "onPause, unbinding from service");
        unbindFromService();
    }

    public void play() {
        if (mTransportControls != null) {
            mTransportControls.play();
        }
    }

    public void pause() {
        if (mTransportControls != null) {
            mTransportControls.pause();
        }
    }

    public void setContent(String source) {
        RequestUtils.ContentBuilder bob = new RequestUtils.ContentBuilder();
        bob.setSource(source);
        try {
            mBinder.sendRequest(RequestUtils.ACTION_SET_CONTENT, bob.build(), null);
        } catch (RemoteException e) {
            Log.d(TAG, "setContent failed, service may have died.", e);
        }
    }

    public void setNextContent(String source) {
        RequestUtils.ContentBuilder bob = new RequestUtils.ContentBuilder();
        bob.setSource(source);
        try {
            mBinder.sendRequest(RequestUtils.ACTION_SET_NEXT_CONTENT, bob.build(), null);
        } catch (RemoteException e) {
            Log.d(TAG, "setNexctContent failed, service may have died.", e);
        }
    }

    public void showRoutePicker() {
        // TODO
    }

    private void unbindFromService() {
        mContext.unbindService(mServiceConnection);
    }

    private void bindToService() {
        mContext.bindService(mServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (mController != null) {
                mController.removeCallback(mControllerCb);
            }
            mBinder = null;
            mController = null;
            mTransportControls = null;
            mContext.setMediaController(null);
            Log.d(TAG, "Disconnected from PlayerService");

            if (mListener != null) {
                mListener.onConnectionStateChange(STATE_DISCONNECTED);
            }
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = IPlayerService.Stub.asInterface(service);
            Log.d(TAG, "service is " + service + " binder is " + mBinder);
            MediaSession.Token token;
            try {
                token = mBinder.getSessionToken();
            } catch (RemoteException e) {
                Log.e(TAG, "Error getting session", e);
                return;
            }
            mController = new MediaController(mContext, token);
            mContext.setMediaController(mController);
            mController.addCallback(mControllerCb, mHandler);
            mTransportControls = mController.getTransportControls();
            Log.d(TAG, "Ready to use PlayerService");

            if (mListener != null) {
                mListener.onConnectionStateChange(STATE_CONNECTED);
                if (mTransportControls != null) {
                    mListener.onPlaybackStateChange(mController.getPlaybackState());
                }
            }
        }
    };

    private class SessionCallback extends MediaController.Callback {
        @Override
        public void onPlaybackStateChanged(PlaybackState state) {
            if (state == null) {
                return;
            }
            Log.d(TAG, "Received playback state change to state " + state.getState());
            if (mListener != null) {
                mListener.onPlaybackStateChange(state);
            }
        }

        @Override
        public void onMetadataChanged(MediaMetadata metadata) {
            if (metadata == null) {
                return;
            }
            Log.d(TAG, "Received metadata change, title is "
                    + metadata.getString(MediaMetadata.METADATA_KEY_TITLE));
        }
    }

    public interface Listener {
        public void onPlaybackStateChange(PlaybackState state);
        public void onMetadataChange(MediaMetadata metadata);
        public void onConnectionStateChange(int state);
    }

}