Java tutorial
/* * Author: Scott Ware <scoot.software@gmail.com> * Copyright (c) 2015 Scott Ware * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.scooter1556.sms.android.fragment; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.scooter1556.sms.android.R; import com.scooter1556.sms.lib.android.domain.MediaElement; import com.scooter1556.sms.lib.android.service.RESTService; public class AudioPlayerSmallFragment extends Fragment { private AudioPlayerFragment.AudioControllerListener audioControllerListener; // User Interface Elements private ImageView coverArt; private ImageButton playPauseButton; private ImageButton previousButton; private ImageButton nextButton; private TextView titleTxt; private TextView artistTxt; /** * Use this factory method to create a new instance of * this fragment. * * @return A new instance of fragment AudioPlayerFragment. */ public static AudioPlayerSmallFragment newInstance() { AudioPlayerSmallFragment fragment = new AudioPlayerSmallFragment(); return fragment; } public AudioPlayerSmallFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Retain this fragment across configuration changes. setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_audio_player_small, container, false); // UI Elements coverArt = (ImageView) view.findViewById(R.id.cover_art); playPauseButton = (ImageButton) view.findViewById(R.id.play_pause); previousButton = (ImageButton) view.findViewById(R.id.previous); nextButton = (ImageButton) view.findViewById(R.id.next); titleTxt = (TextView) view.findViewById(R.id.title); titleTxt.setSelected(true); artistTxt = (TextView) view.findViewById(R.id.artist); artistTxt.setSelected(true); // UI Listeners playPauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View status) { if (audioControllerListener.isPlaying()) { audioControllerListener.pause(); playPauseButton.setImageResource(R.drawable.ic_play_dark); } else { audioControllerListener.start(); } } }); previousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View status) { audioControllerListener.playPrev(); } }); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View status) { audioControllerListener.playNext(); } }); // Inflate the layout for this fragment return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // Set current button state updatePlayerControls(); // Set current media info updateMediaInfo(); } @Override public void onAttach(Context context) { super.onAttach(context); try { audioControllerListener = (AudioPlayerFragment.AudioControllerListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement AudioControllerListener"); } } @Override public void onDetach() { super.onDetach(); audioControllerListener = null; } @Override public void onPause() { super.onPause(); } @Override public void onResume() { super.onResume(); // Update player controls updatePlayerControls(); // Set current media info updateMediaInfo(); } /** * Updates media info */ public void updateMediaInfo() { MediaElement element = audioControllerListener.getMediaElement(); if (element == null) { titleTxt.setText(""); artistTxt.setText(""); coverArt.setImageResource(R.drawable.ic_content_audio); } else { titleTxt.setText(element.getTitle()); artistTxt.setText(element.getArtist()); // Cover Art Glide.with(getActivity()).load( RESTService.getInstance().getConnection().getUrl() + "/image/" + element.getID() + "/cover/80") .error(R.drawable.ic_content_audio).into(coverArt); } } /** * Updates player controls */ public void updatePlayerControls() { if (audioControllerListener.isPlaying()) { playPauseButton.setImageResource(R.drawable.ic_pause_dark); } else { playPauseButton.setImageResource(R.drawable.ic_play_dark); } } }