1a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/*
2a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Copyright (C) 2015 The Android Open Source Project
3a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
4a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Licensed under the Apache License, Version 2.0 (the "License");
5a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * you may not use this file except in compliance with the License.
6a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * You may obtain a copy of the License at
7a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
8a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *      http://www.apache.org/licenses/LICENSE-2.0
9a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
10a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Unless required by applicable law or agreed to in writing, software
11a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * distributed under the License is distributed on an "AS IS" BASIS,
12a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * See the License for the specific language governing permissions and
14a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * limitations under the License.
15a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */
16a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
17a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpackage com.example.android.supportv4.media;
18a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
19a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.content.ComponentName;
20a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.os.Bundle;
21a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.os.RemoteException;
2224919ee8c30d91ec056cae6c4f578d7007cbe968Aurimas Liutikasimport android.support.v4.media.MediaBrowserCompat;
2324919ee8c30d91ec056cae6c4f578d7007cbe968Aurimas Liutikasimport android.support.v4.media.session.MediaControllerCompat;
24a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.support.v4.media.session.MediaSessionCompat;
25a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.support.v4.media.session.PlaybackStateCompat;
26a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.util.Log;
27a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.LayoutInflater;
28a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.View;
29a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.ViewGroup;
30a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.AdapterView;
31a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.ImageButton;
32a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.ListView;
33a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
34def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.content.ContextCompat;
35def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.fragment.app.Fragment;
36def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas
37a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport com.example.android.supportv4.R;
38a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
39a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.util.List;
40a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
41a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/**
42a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * A class that shows the Media Queue to the user.
43a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */
44a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpublic class QueueFragment extends Fragment {
45a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
46a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private static final String TAG = "QueueFragment";
47a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
48a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private ImageButton mSkipNext;
49a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private ImageButton mSkipPrevious;
50a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private ImageButton mPlayPause;
51a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
52a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private MediaBrowserCompat mMediaBrowser;
53a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private MediaControllerCompat.TransportControls mTransportControls;
54a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private MediaControllerCompat mMediaController;
55a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private PlaybackStateCompat mPlaybackState;
56a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
57a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private QueueAdapter mQueueAdapter;
58a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
59a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private MediaBrowserCompat.ConnectionCallback mConnectionCallback =
60a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            new MediaBrowserCompat.ConnectionCallback() {
61a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
62a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onConnected() {
63a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "onConnected: session token " + mMediaBrowser.getSessionToken());
64a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
65a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            if (mMediaBrowser.getSessionToken() == null) {
66a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                throw new IllegalArgumentException("No Session token");
67a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
68a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
69a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            try {
70a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mMediaController = new MediaControllerCompat(getActivity(),
71a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                        mMediaBrowser.getSessionToken());
72a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            } catch (RemoteException e) {
73a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                Log.e(TAG, "Failed to create MediaControllerCompat.", e);
74a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
75a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls = mMediaController.getTransportControls();
76a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaController.registerCallback(mSessionCallback);
77a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
78a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            ((MediaBrowserSupport) getActivity()).setMediaController(mMediaController);
79a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mPlaybackState = mMediaController.getPlaybackState();
80a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
81a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            List<MediaSessionCompat.QueueItem> queue = mMediaController.getQueue();
82a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            if (queue != null) {
83a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.clear();
84a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.notifyDataSetInvalidated();
85a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.addAll(queue);
86a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.notifyDataSetChanged();
87a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
88a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            onPlaybackStateChanged(mPlaybackState);
89a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
90a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
91a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
92a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onConnectionFailed() {
93a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "onConnectionFailed");
94a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
95a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
96a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
97a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onConnectionSuspended() {
98a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "onConnectionSuspended");
99a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaController.unregisterCallback(mSessionCallback);
100a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls = null;
101a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaController = null;
102ff7a534d72423924d6087a011d17634a42199c03Aurimas Liutikas            ((MediaBrowserSupport) getActivity()).setMediaController((MediaControllerCompat) null);
103a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
104a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    };
105a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
106a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    // Receive callbacks from the MediaController. Here we update our state such as which queue
107a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    // is being shown, the current title and description and the PlaybackState.
108a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private MediaControllerCompat.Callback mSessionCallback = new MediaControllerCompat.Callback() {
109a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
110a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
111a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onSessionDestroyed() {
112a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "Session destroyed. Need to fetch a new Media Session");
113a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
114a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
115a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
116a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onPlaybackStateChanged(PlaybackStateCompat state) {
117a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            if (state == null) {
118a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                return;
119a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
120a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "Received playback state change to state " + state.getState());
121a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mPlaybackState = state;
122a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            QueueFragment.this.onPlaybackStateChanged(state);
123a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
124a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
125a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
126a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onQueueChanged(List<MediaSessionCompat.QueueItem> queue) {
127a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            Log.d(TAG, "onQueueChanged " + queue);
128a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            if (queue != null) {
129a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.clear();
130a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.notifyDataSetInvalidated();
131a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.addAll(queue);
132a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mQueueAdapter.notifyDataSetChanged();
133a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
134a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
135a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    };
136a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
137a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public static QueueFragment newInstance() {
138a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        return new QueueFragment();
139a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
140a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
141a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    @Override
142a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public View onCreateView(LayoutInflater inflater, ViewGroup container,
143a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                             Bundle savedInstanceState) {
144a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        View rootView = inflater.inflate(R.layout.fragment_list, container, false);
145a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
146a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipPrevious = (ImageButton) rootView.findViewById(R.id.skip_previous);
147a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipPrevious.setEnabled(false);
148a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipPrevious.setOnClickListener(mButtonListener);
149a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
150a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipNext = (ImageButton) rootView.findViewById(R.id.skip_next);
151a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipNext.setEnabled(false);
152a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipNext.setOnClickListener(mButtonListener);
153a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
154a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mPlayPause = (ImageButton) rootView.findViewById(R.id.play_pause);
155a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mPlayPause.setEnabled(true);
156a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mPlayPause.setOnClickListener(mButtonListener);
157a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
158a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mQueueAdapter = new QueueAdapter(getActivity());
159a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
160a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        ListView mListView = (ListView) rootView.findViewById(R.id.list_view);
161a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mListView.setAdapter(mQueueAdapter);
162a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mListView.setFocusable(true);
163a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
164a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            @Override
165a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
166a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                MediaSessionCompat.QueueItem item = mQueueAdapter.getItem(position);
167a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mTransportControls.skipToQueueItem(item.getQueueId());
168a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
169a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        });
170a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
171a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mMediaBrowser = new MediaBrowserCompat(getActivity(),
172a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                new ComponentName(getActivity(), MediaBrowserServiceSupport.class),
173a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                mConnectionCallback, null);
174a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
175a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        return rootView;
176a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
177a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
178a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    @Override
179a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public void onResume() {
180a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        super.onResume();
181a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mMediaBrowser != null) {
182a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaBrowser.connect();
183a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
184a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
185a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
186a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    @Override
187a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public void onPause() {
188a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        super.onPause();
189a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mMediaController != null) {
190a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaController.unregisterCallback(mSessionCallback);
191a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
192a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mMediaBrowser != null) {
193a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mMediaBrowser.disconnect();
194a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
195a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
196a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
197a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
198a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private void onPlaybackStateChanged(PlaybackStateCompat state) {
199a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        Log.d(TAG, "onPlaybackStateChanged " + state);
200a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (state == null) {
201a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            return;
202a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
203a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mQueueAdapter.setActiveQueueItemId(state.getActiveQueueItemId());
204a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mQueueAdapter.notifyDataSetChanged();
205a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        boolean enablePlay = false;
206a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        StringBuilder statusBuilder = new StringBuilder();
207a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        switch (state.getState()) {
208a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_PLAYING:
209a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("playing");
210a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                enablePlay = false;
211a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
212a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_PAUSED:
213a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("paused");
214a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                enablePlay = true;
215a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
216a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_STOPPED:
217a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("ended");
218a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                enablePlay = true;
219a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
220a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_ERROR:
221a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("error: ").append(state.getErrorMessage());
222a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
223a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_BUFFERING:
224a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("buffering");
225a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
226a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_NONE:
227a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("none");
228a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                enablePlay = false;
229a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
230a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            case PlaybackStateCompat.STATE_CONNECTING:
231a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append("connecting");
232a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                break;
233a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            default:
234a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                statusBuilder.append(mPlaybackState);
235a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
236a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        statusBuilder.append(" -- At position: ").append(state.getPosition());
237a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        Log.d(TAG, statusBuilder.toString());
238a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
239a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (enablePlay) {
240a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mPlayPause.setImageDrawable(
24167949f5f3bc1f721fa5f63250f0fd74a44f5381fAurimas Liutikas                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_white_24dp));
242a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        } else {
243a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mPlayPause.setImageDrawable(
24467949f5f3bc1f721fa5f63250f0fd74a44f5381fAurimas Liutikas                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_white_24dp));
245a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
246a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
247a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipPrevious.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
248a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                != 0);
249a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        mSkipNext.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0);
250a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
251a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        Log.d(TAG, "Queue From MediaController *** Title "
252a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                + mMediaController.getQueueTitle() + "\n: Queue: " + mMediaController.getQueue()
253a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                + "\n Metadata " + mMediaController.getMetadata());
254a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
255a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
256a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private View.OnClickListener mButtonListener = new View.OnClickListener() {
257a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        @Override
258a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        public void onClick(View v) {
259a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            final int state = mPlaybackState == null ?
260a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    PlaybackStateCompat.STATE_NONE : mPlaybackState.getState();
261a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            switch (v.getId()) {
262a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                case R.id.play_pause:
263a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    Log.d(TAG, "Play button pressed, in state " + state);
264a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    if (state == PlaybackStateCompat.STATE_PAUSED ||
265a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                            state == PlaybackStateCompat.STATE_STOPPED ||
266a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                            state == PlaybackStateCompat.STATE_NONE) {
267a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                        playMedia();
268a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    } else if (state == PlaybackStateCompat.STATE_PLAYING) {
269a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                        pauseMedia();
270a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    }
271a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    break;
272a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                case R.id.skip_previous:
273a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    Log.d(TAG, "Start button pressed, in state " + state);
274a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    skipToPrevious();
275a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    break;
276a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                case R.id.skip_next:
277a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    skipToNext();
278a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    break;
279a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            }
280a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
281a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    };
282a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
283a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private void playMedia() {
284a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mTransportControls != null) {
285a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls.play();
286a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
287a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
288a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
289a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private void pauseMedia() {
290a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mTransportControls != null) {
291a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls.pause();
292a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
293a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
294a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
295a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private void skipToPrevious() {
296a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mTransportControls != null) {
297a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls.skipToPrevious();
298a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
299a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
300a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
301a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private void skipToNext() {
302a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mTransportControls != null) {
303a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            mTransportControls.skipToNext();
304a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
305a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
306a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim}
307