1 /* 2 * Copyright (C) 2014 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 package com.example.android.mediabrowserservice; 17 18 import android.app.Activity; 19 import android.media.browse.MediaBrowser; 20 import android.os.Bundle; 21 22 /** 23 * Main activity for the music player. 24 */ 25 public class MusicPlayerActivity extends Activity 26 implements BrowseFragment.FragmentDataHelper { 27 28 @Override onCreate(Bundle savedInstanceState)29 public void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_player); 32 if (savedInstanceState == null) { 33 getFragmentManager().beginTransaction() 34 .add(R.id.container, BrowseFragment.newInstance(null)) 35 .commit(); 36 } 37 } 38 39 @Override onMediaItemSelected(MediaBrowser.MediaItem item)40 public void onMediaItemSelected(MediaBrowser.MediaItem item) { 41 if (item.isPlayable()) { 42 getMediaController().getTransportControls().playFromMediaId(item.getMediaId(), null); 43 QueueFragment queueFragment = QueueFragment.newInstance(); 44 getFragmentManager().beginTransaction() 45 .replace(R.id.container, queueFragment) 46 .addToBackStack(null) 47 .commit(); 48 } else if (item.isBrowsable()) { 49 getFragmentManager().beginTransaction() 50 .replace(R.id.container, BrowseFragment.newInstance(item.getMediaId())) 51 .addToBackStack(null) 52 .commit(); 53 } 54 } 55 } 56