Java tutorial
/* * Copyright 2016 andryr * * 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.andryr.musicplayer.fragments; import android.app.Activity; import android.graphics.PorterDuff; import android.graphics.drawable.GradientDrawable; import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.andryr.musicplayer.MainActivity; import com.andryr.musicplayer.R; import com.andryr.musicplayer.adapters.AlbumSongListAdapter; import com.andryr.musicplayer.adapters.BaseAdapter; import com.andryr.musicplayer.adapters.SongListAdapter; import com.andryr.musicplayer.fragments.dialog.ID3TagEditorDialog; import com.andryr.musicplayer.fragments.dialog.PlaylistPicker; import com.andryr.musicplayer.images.ArtworkCache; import com.andryr.musicplayer.loaders.SongLoader; import com.andryr.musicplayer.model.Album; import com.andryr.musicplayer.model.Playlist; import com.andryr.musicplayer.model.Song; import com.andryr.musicplayer.utils.Playlists; import java.util.List; public class AlbumFragment extends BaseFragment { private static final String ARG_ID = "id"; private static final String ARG_NAME = "name"; private static final String ARG_ARTIST = "artist"; private static final String ARG_YEAR = "year"; private static final String ARG_TRACK_COUNT = "track_count"; private Toolbar mToolbar; private Album mAlbum; private AlbumSongListAdapter mAdapter; private RecyclerView mRecyclerView; private LoaderManager.LoaderCallbacks<List<Song>> mLoaderCallbacks = new LoaderManager.LoaderCallbacks<List<Song>>() { @Override public Loader<List<Song>> onCreateLoader(int id, Bundle args) { SongLoader loader = new SongLoader(getActivity()); loader.setSelection(MediaStore.Audio.Media.ALBUM_ID + " = ?", new String[] { String.valueOf(mAlbum.getId()) }); loader.setSortOrder(MediaStore.Audio.Media.TRACK); return loader; } @Override public void onLoadFinished(Loader<List<Song>> loader, List<Song> songList) { mAdapter.setData(songList); Log.e("test", "" + mAdapter.getItemCount()); } @Override public void onLoaderReset(Loader<List<Song>> loader) { // TODO Auto-generated method stub } }; private ID3TagEditorDialog.OnTagsEditionSuccessListener mOnTagsEditionSuccessListener = new ID3TagEditorDialog.OnTagsEditionSuccessListener() { @Override public void onTagsEditionSuccess() { ((MainActivity) getActivity()).refresh(); } }; private MainActivity mActivity; private View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.shuffle_fab: if (mActivity != null) { mActivity.onShuffleRequested(mAdapter.getSongList(), true); } break; } } }; private BaseAdapter.OnItemClickListener mOnItemClickListener = new BaseAdapter.OnItemClickListener() { @Override public void onItemClick(int position, View view) { switch (view.getId()) { case R.id.item_view: selectSong(position); break; case R.id.menu_button: showMenu(position, view); break; } } }; private int mArtworkWidth; private int mArtworkHeight; public AlbumFragment() { // Required empty public constructor } public static AlbumFragment newInstance(Album album) { AlbumFragment fragment = new AlbumFragment(); Bundle args = new Bundle(); args.putLong(ARG_ID, album.getId()); args.putString(ARG_NAME, album.getAlbumName()); args.putString(ARG_ARTIST, album.getArtistName()); args.putInt(ARG_YEAR, album.getYear()); args.putInt(ARG_TRACK_COUNT, album.getTrackCount()); fragment.setArguments(args); return fragment; } private void selectSong(int position) { if (mActivity != null) { mActivity.onSongSelected(mAdapter.getSongList(), position); } } public void showMenu(final int position, View v) { PopupMenu popup = new PopupMenu(getActivity(), v); MenuInflater inflater = popup.getMenuInflater(); final Song song = mAdapter.getItem(position); inflater.inflate(R.menu.song_list_item, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_add_to_queue: ((MainActivity) getActivity()).addToQueue(song); return true; case R.id.action_set_as_next_track: ((MainActivity) getActivity()).setAsNextTrack(song); return true; case R.id.action_edit_tags: showID3TagEditor(song); return true; case R.id.action_add_to_playlist: showPlaylistPicker(song); return true; } return false; } }); popup.show(); } private void showID3TagEditor(Song song) { ID3TagEditorDialog dialog = ID3TagEditorDialog.newInstance(song); dialog.setOnTagsEditionSuccessListener(mOnTagsEditionSuccessListener); dialog.show(getChildFragmentManager(), "edit_tags"); } private void showPlaylistPicker(final Song song) { PlaylistPicker picker = PlaylistPicker.newInstance(); picker.setListener(new PlaylistPicker.OnPlaylistPickedListener() { @Override public void onPlaylistPicked(Playlist playlist) { Playlists.addSongToPlaylist(getActivity().getContentResolver(), playlist.getId(), song.getId()); } }); picker.show(getChildFragmentManager(), "pick_playlist"); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mActivity = (MainActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); if (args != null) { long id = args.getLong(ARG_ID); String title = args.getString(ARG_NAME); String artist = args.getString(ARG_ARTIST); int year = args.getInt(ARG_YEAR); int trackCount = args.getInt(ARG_TRACK_COUNT); mAlbum = new Album(id, title, artist, year, trackCount); } mArtworkWidth = getResources().getDimensionPixelSize(R.dimen.artist_image_req_width); mArtworkHeight = getResources().getDimensionPixelSize(R.dimen.artist_image_req_height); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_album, container, false); mRecyclerView = (RecyclerView) rootView.findViewById(R.id.song_list); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); mAdapter = new AlbumSongListAdapter(); mAdapter.setOnItemClickListener(mOnItemClickListener); mRecyclerView.setAdapter(mAdapter); FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.shuffle_fab); fab.setColorFilter(getActivity().getResources().getColor(android.R.color.white), PorterDuff.Mode.SRC_ATOP); fab.setOnClickListener(mOnClickListener); ImageView artworkView = (ImageView) rootView.findViewById(R.id.album_artwork); ArtworkCache.getInstance().loadBitmap(mAlbum.getId(), artworkView, mArtworkWidth, mArtworkHeight); ((GradientDrawable) rootView.findViewById(R.id.overlay).getBackground()) .setGradientRadius(getResources().getDimensionPixelSize(R.dimen.gradient_radius)); CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) rootView .findViewById(R.id.collapsing_toolbar); collapsingToolbar.setTitle(mAlbum.getAlbumName()); mToolbar = (Toolbar) rootView.findViewById(R.id.toolbar); AppCompatActivity activity = (AppCompatActivity) getActivity(); activity.setSupportActionBar(mToolbar); activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); /* Drawable background = mToolbar.getBackground(); background.mutate(); background.setAlpha(0); mAdapter.setParallaxEnabled(true); mAdapter.setOnParallaxScrollListener(new RecyclerViewAdapter.OnParallaxScrollListener() { @Override public void onParallaxScroll(float offset) { Drawable background = mToolbar.getBackground(); background.setAlpha(Math.round(offset * 255)); } });*/ return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getLoaderManager().initLoader(0, null, mLoaderCallbacks); } @Override public void load() { getLoaderManager().restartLoader(0, null, mLoaderCallbacks); } }