MediaBrowserSupport.java revision 5e796344324f2214162daa2072b36960d5346a3a
1/*
2 * Copyright (C) 2015 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.supportv4.media;
18
19import android.os.Bundle;
20import android.support.v4.app.FragmentActivity;
21import android.support.v4.media.MediaBrowserCompat;
22import android.support.v4.media.session.MediaControllerCompat;
23
24import com.example.android.supportv4.R;
25
26/**
27 * Main activity for the music player.
28 */
29public class MediaBrowserSupport extends FragmentActivity
30        implements BrowseFragment.FragmentDataHelper {
31
32    @Override
33    public void onCreate(Bundle savedInstanceState) {
34        super.onCreate(savedInstanceState);
35        setContentView(R.layout.activity_player);
36        if (savedInstanceState == null) {
37            getSupportFragmentManager().beginTransaction()
38                    .add(R.id.container, BrowseFragment.newInstance(null))
39                    .commit();
40        }
41    }
42
43    @Override
44    public void onMediaItemSelected(MediaBrowserCompat.MediaItem item) {
45        if (item.isPlayable()) {
46            MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(this);
47            if (mediaController != null) {
48                mediaController.getTransportControls().playFromMediaId(item.getMediaId(), null);
49                QueueFragment queueFragment = QueueFragment.newInstance();
50                getSupportFragmentManager().beginTransaction()
51                        .replace(R.id.container, queueFragment)
52                        .addToBackStack(null)
53                        .commit();
54            }
55        } else if (item.isBrowsable()) {
56            getSupportFragmentManager().beginTransaction()
57                    .replace(R.id.container, BrowseFragment.newInstance(item.getMediaId()))
58                    .addToBackStack(null)
59                    .commit();
60        }
61    }
62
63    public void setMediaController(MediaControllerCompat mediaController) {
64        MediaControllerCompat.setMediaController(this, mediaController);
65    }
66}
67