Java tutorial
/* * Copyright (C) 2014 Kartik Sharma 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.ksharkapps.musicnow.ui.fragments.phone; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import com.ksharkapps.musicnow.R; import com.ksharkapps.musicnow.adapters.PagerAdapter; import com.ksharkapps.musicnow.adapters.PagerAdapter.MusicFragments; import com.ksharkapps.musicnow.adapters.ScrollingTabsAdapter; import com.ksharkapps.musicnow.ui.fragments.AlbumFragment; import com.ksharkapps.musicnow.ui.fragments.ArtistFragment; import com.ksharkapps.musicnow.ui.fragments.SongFragment; import com.ksharkapps.musicnow.ui.widgets.ScrollableTabView; import com.ksharkapps.musicnow.utils.MusicUtils; import com.ksharkapps.musicnow.utils.NavUtils; import com.ksharkapps.musicnow.utils.PreferenceUtils; import com.ksharkapps.musicnow.utils.SortOrder; /** * This class is used to hold the {@link ViewPager} used for swiping between the * playlists, recent, artists, albums, songs, and genre {@link Fragment} * s for phones. * * @NOTE: The reason the sort orders are taken care of in this fragment rather * than the individual fragments is to keep from showing all of the menu * items on tablet interfaces. That being said, I have a tablet interface * worked out, but I'm going to keep it in the Play Store version of * Apollo for a couple of weeks or so before merging it with CM. * @author Kartik Sharma (kshark.apps@gmail.com) */ public class MusicBrowserPhoneFragment extends Fragment { /** * Pager */ private ViewPager mViewPager; /** * VP's adapter */ private PagerAdapter mPagerAdapter; private PreferenceUtils mPreferences; private static ScrollableTabView pageIndicator; /** * Empty constructor as per the {@link Fragment} documentation */ public MusicBrowserPhoneFragment() { } /** * {@inheritDoc} */ @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the preferences mPreferences = PreferenceUtils.getInstance(getActivity()); } /** * {@inheritDoc} */ @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { // The View for the fragment's UI final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_music_browser_phone, container, false); // Initialize the adapter mPagerAdapter = new PagerAdapter(getActivity()); final MusicFragments[] mFragments = MusicFragments.values(); for (final MusicFragments mFragment : mFragments) { mPagerAdapter.add(mFragment.getFragmentClass(), null); } // Initialize the ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.fragment_home_phone_pager); // Attch the adapter mViewPager.setAdapter(mPagerAdapter); // Offscreen pager loading limit mViewPager.setOffscreenPageLimit(mPagerAdapter.getCount() - 1); // Start on the last page the user was on mViewPager.setCurrentItem(mPreferences.getStartPage()); // Initialze the TPI pageIndicator = (ScrollableTabView) rootView.findViewById(R.id.fragment_home_phone_pager_titles); // Attach the ViewPager ScrollingTabsAdapter mScrollingTabsAdapter = new ScrollingTabsAdapter(getActivity()); pageIndicator.setAdapter(mScrollingTabsAdapter); pageIndicator.setViewPager(mViewPager); return rootView; } @Override public void onResume() { super.onResume(); } /** * {@inheritDoc} */ @Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Enable the options menu setHasOptionsMenu(true); } /** * {@inheritDoc} */ @Override public void onPause() { super.onPause(); // Save the last page the use was on mPreferences.setStartPage(mViewPager.getCurrentItem()); } /** * {@inheritDoc} */ @Override public void onPrepareOptionsMenu(final Menu menu) { super.onPrepareOptionsMenu(menu); } /** * {@inheritDoc} */ @Override public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); // Favorite action // inflater.inflate(R.menu.favorite, menu); // Shuffle all inflater.inflate(R.menu.shuffle, menu); // Sort orders if (isRecentPage()) { //inflater.inflate(R.menu.view_as, menu); } else if (isArtistPage()) { inflater.inflate(R.menu.artist_sort_by, menu); // inflater.inflate(R.menu.view_as, menu); } else if (isAlbumPage()) { inflater.inflate(R.menu.album_sort_by, menu); // inflater.inflate(R.menu.view_as, menu); } else if (isSongPage()) { inflater.inflate(R.menu.song_sort_by, menu); } } /** * {@inheritDoc} */ @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case R.id.menu_shuffle: // Shuffle all the songs MusicUtils.shuffleAll(getActivity()); return true; case R.id.menu_favorite: // Toggle the current track as a favorite and update the menu // item MusicUtils.toggleFavorite(); getActivity().invalidateOptionsMenu(); return true; case R.id.menu_sort_by_az: if (isArtistPage()) { mPreferences.setArtistSortOrder(SortOrder.ArtistSortOrder.ARTIST_A_Z); getArtistFragment().refresh(); } else if (isAlbumPage()) { mPreferences.setAlbumSortOrder(SortOrder.AlbumSortOrder.ALBUM_A_Z); getAlbumFragment().refresh(); } else if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_A_Z); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_za: if (isArtistPage()) { mPreferences.setArtistSortOrder(SortOrder.ArtistSortOrder.ARTIST_Z_A); getArtistFragment().refresh(); } else if (isAlbumPage()) { mPreferences.setAlbumSortOrder(SortOrder.AlbumSortOrder.ALBUM_Z_A); getAlbumFragment().refresh(); } else if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_Z_A); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_artist: if (isAlbumPage()) { mPreferences.setAlbumSortOrder(SortOrder.AlbumSortOrder.ALBUM_ARTIST); getAlbumFragment().refresh(); } else if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_ARTIST); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_album: if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_ALBUM); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_year: if (isAlbumPage()) { mPreferences.setAlbumSortOrder(SortOrder.AlbumSortOrder.ALBUM_YEAR); getAlbumFragment().refresh(); } else if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_YEAR); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_duration: if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_DURATION); getSongFragment().refresh(); } return true; case R.id.menu_sort_by_number_of_songs: if (isArtistPage()) { mPreferences.setArtistSortOrder(SortOrder.ArtistSortOrder.ARTIST_NUMBER_OF_SONGS); getArtistFragment().refresh(); } else if (isAlbumPage()) { mPreferences.setAlbumSortOrder(SortOrder.AlbumSortOrder.ALBUM_NUMBER_OF_SONGS); getAlbumFragment().refresh(); } return true; case R.id.menu_sort_by_number_of_albums: if (isArtistPage()) { mPreferences.setArtistSortOrder(SortOrder.ArtistSortOrder.ARTIST_NUMBER_OF_ALBUMS); getArtistFragment().refresh(); } return true; case R.id.menu_sort_by_filename: if (isSongPage()) { mPreferences.setSongSortOrder(SortOrder.SongSortOrder.SONG_FILENAME); getSongFragment().refresh(); } return true; /* case R.id.menu_view_as_simple: if (isRecentPage()) { mPreferences.setRecentLayout("simple"); } else if (isArtistPage()) { mPreferences.setArtistLayout("simple"); } else if (isAlbumPage()) { mPreferences.setAlbumLayout("simple"); } NavUtils.goHome(getActivity()); return true; case R.id.menu_view_as_detailed: if (isRecentPage()) { mPreferences.setRecentLayout("detailed"); } else if (isArtistPage()) { mPreferences.setArtistLayout("detailed"); } else if (isAlbumPage()) { mPreferences.setAlbumLayout("detailed"); } NavUtils.goHome(getActivity()); return true; case R.id.menu_view_as_grid: if (isRecentPage()) { mPreferences.setRecentLayout("grid"); } else if (isArtistPage()) { mPreferences.setArtistLayout("grid"); } else if (isAlbumPage()) { mPreferences.setAlbumLayout("grid"); } NavUtils.goHome(getActivity()); return true;*/ default: break; } return super.onOptionsItemSelected(item); } private boolean isArtistPage() { return mViewPager.getCurrentItem() == 2; } private ArtistFragment getArtistFragment() { return (ArtistFragment) mPagerAdapter.getFragment(2); } private boolean isAlbumPage() { return mViewPager.getCurrentItem() == 3; } private AlbumFragment getAlbumFragment() { return (AlbumFragment) mPagerAdapter.getFragment(3); } private boolean isSongPage() { return mViewPager.getCurrentItem() == 4; } private SongFragment getSongFragment() { return (SongFragment) mPagerAdapter.getFragment(4); } private boolean isRecentPage() { return mViewPager.getCurrentItem() == 1; } }