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