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.pi.android.brainbeats.ui; import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; import android.support.v4.content.ContextCompat; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.PlaybackStateCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import com.pi.android.brainbeats.MusicService; import com.pi.android.brainbeats.R; import com.pi.android.brainbeats.data.Tagger; import com.pi.android.brainbeats.utils.LogHelper; /** * A class that shows the Media Queue to the user. */ public class PlaybackControlsFragment extends Fragment { private static final String TAG = LogHelper.makeLogTag(PlaybackControlsFragment.class); private ImageButton mPlayPause; private TextView mTitle; private TextView mSubtitle; private TextView mExtraInfo; private ImageView mAlbumArt; private String mArtUrl; private TagButton exited; private RadioButton calm; private Tagger tagger; private int currentMusicID = -1; // Receive callbacks from the MediaController. Here we update our state such as which queue // is being shown, the current title and description and the PlaybackState. private final MediaControllerCompat.Callback mCallback = new MediaControllerCompat.Callback() { @Override public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) { LogHelper.d(TAG, "Received playback state change to state ", state.getState()); PlaybackControlsFragment.this.onPlaybackStateChanged(state); } @Override public void onMetadataChanged(MediaMetadataCompat metadata) { if (metadata == null) { return; } LogHelper.d(TAG, "Received metadata state change to mediaId=", metadata.getDescription().getMediaId(), " song=", metadata.getDescription().getTitle()); PlaybackControlsFragment.this.onMetadataChanged(metadata); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_playback_controls, container, false); mPlayPause = (ImageButton) rootView.findViewById(R.id.play_pause); mPlayPause.setEnabled(true); mPlayPause.setOnClickListener(mButtonListener); mTitle = (TextView) rootView.findViewById(R.id.title); mSubtitle = (TextView) rootView.findViewById(R.id.artist); mExtraInfo = (TextView) rootView.findViewById(R.id.extra_info); mAlbumArt = (ImageView) rootView.findViewById(R.id.album_art); tagger = new Tagger(getActivity()); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), FullScreenPlayerActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); MediaMetadataCompat metadata = controller.getMetadata(); if (metadata != null) { intent.putExtra(MusicPlayerActivity.EXTRA_CURRENT_MEDIA_DESCRIPTION, metadata.getDescription()); } startActivity(intent); } }); exited = new TagButton((RadioButton) rootView.findViewById(R.id.exitedButton)); exited.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { tagger.addTagToSong(currentMusicID, exited.getText().toString()); } }); calm = (RadioButton) rootView.findViewById(R.id.calmButton); calm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { tagger.addTagToSong(currentMusicID, calm.getText().toString()); } }); return rootView; } private int getMusicID(MediaMetadataCompat metadata) { if (metadata == null) { return -1; } String musicId = metadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID); return Integer.parseInt(musicId); } @Override public void onStart() { super.onStart(); LogHelper.d(TAG, "fragment.onStart"); MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { onConnected(); } } @Override public void onStop() { super.onStop(); LogHelper.d(TAG, "fragment.onStop"); MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { controller.unregisterCallback(mCallback); } } public void onConnected() { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); LogHelper.d(TAG, "onConnected, mediaController==null? ", controller == null); if (controller != null) { onMetadataChanged(controller.getMetadata()); onPlaybackStateChanged(controller.getPlaybackState()); controller.registerCallback(mCallback); } } private void onMetadataChanged(MediaMetadataCompat metadata) { LogHelper.d(TAG, "onMetadataChanged ", metadata); if (getActivity() == null) { LogHelper.w(TAG, "onMetadataChanged called when getActivity null," + "this should not happen if the callback was properly unregistered. Ignoring."); return; } if (metadata == null) { return; } mTitle.setText(metadata.getDescription().getTitle()); mSubtitle.setText(metadata.getDescription().getSubtitle()); } private void updateTags(MediaMetadataCompat metadata) { currentMusicID = getMusicID(metadata); final String tag = tagger.getTagBySongID(currentMusicID); if (tag.equals(exited.getText().toString())) { exited.setChecked(true); calm.setChecked(false); } else if (tag.equals(calm.getText().toString())) { exited.setChecked(false); calm.setChecked(true); } else { exited.setChecked(false); calm.setChecked(false); } } public void setExtraInfo(String extraInfo) { if (extraInfo == null) { mExtraInfo.setVisibility(View.GONE); } else { mExtraInfo.setText(extraInfo); mExtraInfo.setVisibility(View.VISIBLE); } } private void onPlaybackStateChanged(PlaybackStateCompat state) { LogHelper.d(TAG, "onPlaybackStateChanged ", state); if (getActivity() == null) { LogHelper.w(TAG, "onPlaybackStateChanged called when getActivity null," + "this should not happen if the callback was properly unregistered. Ignoring."); return; } if (state == null) { return; } boolean enablePlay = false; switch (state.getState()) { case PlaybackStateCompat.STATE_PAUSED: case PlaybackStateCompat.STATE_STOPPED: enablePlay = true; break; case PlaybackStateCompat.STATE_ERROR: LogHelper.e(TAG, "error playbackstate: ", state.getErrorMessage()); Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show(); break; } if (enablePlay) { mPlayPause.setImageDrawable( ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_36dp)); } else { mPlayPause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_36dp)); } MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); String extraInfo = null; if (controller != null && controller.getExtras() != null) { String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST); if (castName != null) { extraInfo = getResources().getString(R.string.casting_to_device, castName); } } final MediaMetadataCompat metadata = controller.getMetadata(); if (metadata != null) { updateTags(metadata); } setExtraInfo(extraInfo); } private final View.OnClickListener mButtonListener = new View.OnClickListener() { @Override public void onClick(View v) { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); PlaybackStateCompat stateObj = controller.getPlaybackState(); final int state = stateObj == null ? PlaybackStateCompat.STATE_NONE : stateObj.getState(); LogHelper.d(TAG, "Button pressed, in state " + state); switch (v.getId()) { case R.id.play_pause: LogHelper.d(TAG, "Play button pressed, in state " + state); if (state == PlaybackStateCompat.STATE_PAUSED || state == PlaybackStateCompat.STATE_STOPPED || state == PlaybackStateCompat.STATE_NONE) { playMedia(); } else if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_BUFFERING || state == PlaybackStateCompat.STATE_CONNECTING) { pauseMedia(); } break; } } }; private void playMedia() { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { controller.getTransportControls().play(); } } private void pauseMedia() { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { controller.getTransportControls().pause(); } } class TagButton { private RadioButton radioButton; private int counter = 1; public TagButton(RadioButton radioButton) { this.radioButton = radioButton; } public void setChecked(boolean b) { System.out.println("CHANGED: " + counter); counter++; radioButton.setChecked(b); } public void setOnClickListener(View.OnClickListener listener) { radioButton.setOnClickListener(listener); } public CharSequence getText() { return radioButton.getText(); } } }