2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.ui.fragments;
14 import android.content.Context;
15 import android.database.Cursor;
16 import android.os.Bundle;
17 import android.support.v4.app.LoaderManager;
18 import android.support.v4.content.Loader;
20 import com.cyngn.eleven.adapters.PagerAdapter;
21 import com.cyngn.eleven.loaders.SongLoader;
22 import com.cyngn.eleven.model.Song;
23 import com.cyngn.eleven.sectionadapter.SectionCreator;
24 import com.cyngn.eleven.sectionadapter.SectionListContainer;
25 import com.cyngn.eleven.ui.fragments.profile.BasicSongFragment;
26 import com.cyngn.eleven.utils.MusicUtils;
27 import com.cyngn.eleven.utils.SectionCreatorUtils;
28 import com.viewpagerindicator.TitlePageIndicator;
31 * This class is used to display all of the songs on a user's device.
33 * @author Andrew Neal (andrewdneal@gmail.com)
35 public class SongFragment extends BasicSongFragment {
40 public void playAll(int position) {
41 int internalPosition = mAdapter.getInternalPosition(position);
42 Cursor cursor = SongLoader.makeSongCursor(getActivity(), null);
43 final long[] list = MusicUtils.getSongListForCursor(cursor);
44 MusicUtils.playAll(getActivity(), list, internalPosition, false);
53 public Loader<SectionListContainer<Song>> onCreateLoader(final int id, final Bundle args) {
55 Context context = getActivity();
57 // create the underlying song loader
58 SongLoader songLoader = new SongLoader(context);
60 // get the song comparison method to create the headers with
61 SectionCreatorUtils.IItemCompare<Song> songComparison = SectionCreatorUtils.createSongComparison(context);
63 // return the wrapped section creator
64 return new SectionCreator<Song>(context, songLoader, songComparison);
69 public int getLoaderId() {
70 return PagerAdapter.MusicFragments.SONG.ordinal();
74 * Scrolls the list to the currently playing song when the user touches the
75 * header in the {@link TitlePageIndicator}.
77 public void scrollToCurrentSong() {
78 final int currentSongPosition = getItemPositionBySong();
80 if (currentSongPosition != 0) {
81 mListView.setSelection(currentSongPosition);
86 * @return The position of an item in the list based on the name of the
87 * currently playing song.
89 private int getItemPositionBySong() {
90 final long trackId = MusicUtils.getCurrentAudioId();
91 if (mAdapter == null) {
95 int position = mAdapter.getItemPosition(trackId);
97 // if for some reason we don't find the item, just jump to the top
106 public LoaderManager getFragmentLoaderManager() {
107 return getParentFragment().getLoaderManager();