Java tutorial
/* * </summary> * Source File : PlayerFragment.java * Project : Sarigama * Owner : Kasun * </summary> * * <license> * Copyright 2016 Kasun Gunathilaka * * 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. * </license> */ package com.kasungunathilaka.sarigama.fragment; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import com.kasungunathilaka.sarigama.R; import com.kasungunathilaka.sarigama.domain.Song; import com.kasungunathilaka.sarigama.service.PlayerService; import com.kasungunathilaka.sarigama.ui.HomeActivity; import com.kasungunathilaka.sarigama.util.PlayerQueue; import com.squareup.picasso.Picasso; import de.hdodenhof.circleimageview.CircleImageView; public class PlayerFragment extends Fragment { private SeekBar seekBar; ImageView imCoverImage; CircleImageView ibPlay; ImageButton ibMinimize, ivNext, ivPrevious, ivLoop, ivShuffle; PlayerService playerService; BroadcastReceiver broadcastReceiver; LocalBroadcastManager localBroadcastManager; ProgressDialog dialog; TextView totalTime, currentTime; String launchMode; HomeActivity activity; PlayerQueue playerQueue = PlayerQueue.getInstance(getActivity()); private Handler mHandler = new Handler(); public final static String PREVIOUS_AUDIO = "previous_audio"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_player, container, false); activity = (HomeActivity) getActivity(); totalTime = (TextView) view.findViewById(R.id.totalTime); currentTime = (TextView) view.findViewById(R.id.currentTime); seekBar = (SeekBar) view.findViewById(R.id.seekBar); ibPlay = (CircleImageView) view.findViewById(R.id.ibPlay); imCoverImage = (ImageView) view.findViewById(R.id.coverImage); ibMinimize = (ImageButton) view.findViewById(R.id.ibMinimize); ivNext = (ImageButton) view.findViewById(R.id.ivNext); ivPrevious = (ImageButton) view.findViewById(R.id.ivPrevious); ivLoop = (ImageButton) view.findViewById(R.id.ivLoop); ivShuffle = (ImageButton) view.findViewById(R.id.ivShuffle); ivNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { playerService.next(); } }); ivPrevious.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { playerService.previous(); } }); ivLoop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (playerService.isQueueOnLoop()) { playerService.setQueueOnLoop(false); playerService.setSongOnLoop(true); ivLoop.setImageResource(R.drawable.ic_repeat_one_black); ivLoop.setColorFilter(getResources().getColor(R.color.colorPrimaryDark)); } else if (playerService.isSongOnLoop()) { playerService.setQueueOnLoop(false); playerService.setSongOnLoop(false); ivLoop.setImageResource(R.drawable.ic_repeat_white); ivLoop.clearColorFilter(); } else { ivLoop.setColorFilter(getResources().getColor(R.color.colorPrimaryDark)); playerService.setQueueOnLoop(true); playerService.setSongOnLoop(false); } } }); ibPlay.setOnClickListener(playOnClick); ibMinimize.setOnClickListener(minimizeOnclick); localBroadcastManager = LocalBroadcastManager.getInstance(getActivity()); broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case HomeActivity.ON_PREPARED: int position = playerQueue.getCurrentPosition(); Song currentSong = playerQueue.getSongByPosition(position); if (currentSong.getSongImage().contentEquals("http://sarigama.lk/img/songs/default.jpg")) { imCoverImage.setImageResource(R.drawable.default_song_art); } else { Picasso.with(getActivity()).load(currentSong.getSongImage()).into(imCoverImage); } ibPlay.setImageResource(R.drawable.ic_pause_white); ((TextView) getActivity().findViewById(R.id.tvTitle)).setText(currentSong.getSongTitle()); ((TextView) getActivity().findViewById(R.id.tvArtist)).setText(currentSong.getSongArtist()); activity.setupMiniPlayer(currentSong.getSongTitle(), currentSong.getSongArtist()); mRunnable.run(); showProgressDialog(false); break; case HomeActivity.ON_PREPARING: showProgressDialog(true); break; case HomeActivity.ON_COMPLETED: ibPlay.setImageResource(R.drawable.ic_play_white); seekBar.setProgress(0); break; case HomeActivity.ON_PAUSE: ibPlay.setImageResource(R.drawable.ic_play_white); break; case HomeActivity.ON_PLAY: ibPlay.setImageResource(R.drawable.ic_pause_white); break; case HomeActivity.ON_PREPARE_ERROR: showProgressDialog(false); Toast.makeText(getActivity(), "Song cannot be played, Trying to play next song", Toast.LENGTH_SHORT).show(); break; case HomeActivity.ON_QUEUE_COMPLETED: playerService.setQueueOnLoop(false); playerService.setSongOnLoop(false); ivLoop.setImageResource(R.drawable.ic_repeat_white); ivLoop.clearColorFilter(); break; } } }; localBroadcastManager.sendBroadcast(new Intent(HomeActivity.ON_PLAYER_ENTER)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_PAUSE)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_PLAY)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_PREPARED)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_PREPARING)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_COMPLETED)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_PREPARE_ERROR)); LocalBroadcastManager.getInstance(getActivity()).registerReceiver((broadcastReceiver), new IntentFilter(HomeActivity.ON_QUEUE_COMPLETED)); return view; } public void setData(Bundle bundle) { launchMode = bundle.getString(HomeActivity.PLAYER_FRAGMENT_LAUNCH_MODE); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); playerService = ((HomeActivity) getActivity()).getPlayerService(); switch (launchMode) { case HomeActivity.PLAYER_FRAGMENT_LAUNCH_MODE_NEW_QUEUE: int position = playerQueue.getCurrentPosition(); Song currentSong = playerQueue.getSongByPosition(position); playerService.startPlayer(position); ((TextView) getActivity().findViewById(R.id.tvTitle)).setText(currentSong.getSongTitle()); ((TextView) getActivity().findViewById(R.id.tvArtist)).setText(currentSong.getSongArtist()); break; case HomeActivity.PLAYER_FRAGMENT_LAUNCH_MODE_MINI_PLAYER: position = playerQueue.getCurrentPosition(); currentSong = playerQueue.getSongByPosition(position); ((TextView) getActivity().findViewById(R.id.tvTitle)).setText(currentSong.getSongTitle()); ((TextView) getActivity().findViewById(R.id.tvArtist)).setText(currentSong.getSongArtist()); ibPlay.setImageResource( playerService.isPlaying() ? R.drawable.ic_pause_white : R.drawable.ic_play_white); if (currentSong.getSongImage().contentEquals("http://sarigama.lk/img/songs/default.jpg")) { imCoverImage.setImageResource(R.drawable.default_song_art); } else { Picasso.with(getActivity()).load(currentSong.getSongImage()).into(imCoverImage); } if (playerService.isQueueOnLoop()) { ivLoop.setImageResource(R.drawable.ic_repeat_white); ivLoop.setColorFilter(getResources().getColor(R.color.colorPrimaryDark)); } else if (playerService.isSongOnLoop()) { ivLoop.setImageResource(R.drawable.ic_repeat_one_black); ivLoop.setColorFilter(getResources().getColor(R.color.colorPrimaryDark)); } else { ivLoop.setImageResource(R.drawable.ic_repeat_white); ivLoop.clearColorFilter(); } mRunnable.run(); break; } } View.OnClickListener playOnClick = new View.OnClickListener() { @Override public void onClick(View v) { if (playerService.isPlaying()) { ibPlay.setImageResource(R.drawable.ic_play_white); playerService.setPlayPause(); } else { ibPlay.setImageResource(R.drawable.ic_pause_white); playerService.setPlayPause(); } } }; View.OnClickListener minimizeOnclick = new View.OnClickListener() { @Override public void onClick(View v) { activity.onBackPressed(); } }; private Runnable mRunnable = new Runnable() { @Override public void run() { int mDuration = playerService.getDuration(); seekBar.setMax(mDuration); totalTime.setText(getTimeString(mDuration)); int mCurrentPosition = playerService.getCurrentPosition(); seekBar.setProgress(mCurrentPosition); currentTime.setText(getTimeString(mCurrentPosition)); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { playerService.seekTo(progress); } } }); mHandler.postDelayed(this, 10); } }; private String getTimeString(long millis) { StringBuffer stringBuffer = new StringBuffer(); long hours = millis / (1000 * 60 * 60); long minutes = (millis % (1000 * 60 * 60)) / (1000 * 60); long seconds = ((millis % (1000 * 60 * 60)) % (1000 * 60)) / 1000; if (hours > 0) { stringBuffer.append(String.format("%02d", hours)).append(":").append(String.format("%02d", minutes)) .append(":").append(String.format("%02d", seconds)); } else { stringBuffer.append(String.format("%02d", minutes)).append(":").append(String.format("%02d", seconds)); } return stringBuffer.toString(); } @Override public void onDestroy() { super.onDestroy(); LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcastReceiver); localBroadcastManager.sendBroadcast(new Intent(HomeActivity.ON_PLAYER_EXIT)); } private void showProgressDialog(boolean show) { if (dialog != null) { if (dialog.isShowing() && !show) { dialog.dismiss(); } else if (show) { dialog = new ProgressDialog(getActivity()); dialog.setMessage(getString(R.string.loading)); dialog.setCancelable(false); dialog.getWindow().setGravity(Gravity.CENTER); dialog.show(); } } else { if (show) { dialog = new ProgressDialog(getActivity()); dialog.setMessage(getString(R.string.loading)); dialog.setCancelable(false); dialog.getWindow().setGravity(Gravity.CENTER); dialog.show(); } } } }