Java tutorial
/* * 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.phearom.um.ui; import android.app.SearchManager; import android.content.Intent; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.media.MediaBrowserCompat; import android.support.v7.app.ActionBarDrawerToggle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.phearom.um.R; import com.phearom.um.databinding.ActivityPlayerBinding; import com.phearom.um.ui.fragment.FragmentPager; import com.phearom.um.ui.fragment.PopSongFragment; import com.phearom.um.ui.fragment.SingerFragment; import com.phearom.um.utils.LogHelper; public class MusicPlayerActivity extends MyBaseActivity implements PopSongFragment.MediaFragmentListener { private ActivityPlayerBinding mBinding; private FragmentPager fragmentPager; private static final String TAG = LogHelper.makeLogTag(MusicPlayerActivity.class); private static final String SAVED_MEDIA_ID = "com.phearom.um.MEDIA_ID"; private static final String FRAGMENT_TAG = "uamp_list_container"; public static final String EXTRA_START_FULLSCREEN = "com.phearom.um.EXTRA_START_FULLSCREEN"; public static final String EXTRA_CURRENT_MEDIA_DESCRIPTION = "com.phearom.um.CURRENT_MEDIA_DESCRIPTION"; private Bundle mVoiceSearchParams; private ActionBarDrawerToggle mDrawerToggle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LogHelper.d(TAG, "Activity onCreate"); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_player); setSupportActionBar(mBinding.toolbarContainer.toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); fragmentPager = new FragmentPager(getSupportFragmentManager()); fragmentPager.add(new PopSongFragment()); fragmentPager.add(new SingerFragment()); mBinding.viewPager.setAdapter(fragmentPager); mBinding.toolbarContainer.tab.setupWithViewPager(mBinding.viewPager); mDrawerToggle = new ActionBarDrawerToggle(this, mBinding.drawerLayout, 0, 0); mBinding.drawerLayout.addDrawerListener(mDrawerToggle); mDrawerToggle.syncState(); initializeFromParams(savedInstanceState, getIntent()); if (savedInstanceState == null) { startFullScreenActivityIfNeeded(getIntent()); } mBinding.fabPlayPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getBrowseFragment().onConnected(); } }); } @Override protected void onSaveInstanceState(Bundle outState) { String mediaId = getMediaId(); if (mediaId != null) { outState.putString(SAVED_MEDIA_ID, mediaId); } super.onSaveInstanceState(outState); } @Override public void onMediaItemSelected(MediaBrowserCompat.MediaItem item) { LogHelper.d(TAG, "onMediaItemSelected, mediaId=" + item.getMediaId()); if (item.isPlayable()) { getSupportMediaController().getTransportControls().playFromMediaId(item.getMediaId(), null); } else if (item.isBrowsable()) { navigateToBrowser(item.getMediaId()); } else { LogHelper.w(TAG, "Ignoring MediaItem that is neither browsable nor playable: ", "mediaId=", item.getMediaId()); } } @Override public void setToolbarTitle(CharSequence title) { LogHelper.d(TAG, "Setting toolbar title to ", title); if (title == null) { title = getString(R.string.app_name); } setTitle(title); } @Override protected void onNewIntent(Intent intent) { LogHelper.d(TAG, "onNewIntent, intent=" + intent); initializeFromParams(null, intent); startFullScreenActivityIfNeeded(intent); } private void startFullScreenActivityIfNeeded(Intent intent) { if (intent != null && intent.getBooleanExtra(EXTRA_START_FULLSCREEN, false)) { Intent fullScreenIntent = new Intent(this, FullScreenPlayerActivity.class) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP) .putExtra(EXTRA_CURRENT_MEDIA_DESCRIPTION, intent.getParcelableExtra(EXTRA_CURRENT_MEDIA_DESCRIPTION)); startActivity(fullScreenIntent); } } protected void initializeFromParams(Bundle savedInstanceState, Intent intent) { String mediaId = null; if (intent.getAction() != null && intent.getAction().equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) { mVoiceSearchParams = intent.getExtras(); LogHelper.d(TAG, "Starting from voice search query=", mVoiceSearchParams.getString(SearchManager.QUERY)); } else { if (savedInstanceState != null) { mediaId = savedInstanceState.getString(SAVED_MEDIA_ID); } } navigateToBrowser(mediaId); } private void navigateToBrowser(String mediaId) { LogHelper.d(TAG, "navigateToBrowser, mediaId=" + mediaId); } public String getMediaId() { PopSongFragment fragment = getBrowseFragment(); if (fragment == null) { return null; } return fragment.getMediaId(); } private PopSongFragment getBrowseFragment() { return (PopSongFragment) fragmentPager.getItem(0); } @Override protected void onMediaControllerConnected() { if (mVoiceSearchParams != null) { String query = mVoiceSearchParams.getString(SearchManager.QUERY); getSupportMediaController().getTransportControls().playFromSearch(query, mVoiceSearchParams); mVoiceSearchParams = null; } getBrowseFragment().onConnected(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle != null && mDrawerToggle.onOptionsItemSelected(item)) { return true; } if (item != null && item.getItemId() == android.R.id.home) { onBackPressed(); return true; } return super.onOptionsItemSelected(item); } @Override public void onBackPressed() { mBinding.drawerLayout.closeDrawers(); } }