2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.ui.fragments;
16 import android.content.Context;
17 import android.os.Bundle;
18 import android.support.v4.app.LoaderManager;
19 import android.support.v4.content.Loader;
21 import com.cyanogenmod.eleven.Config;
22 import com.cyanogenmod.eleven.adapters.PagerAdapter;
23 import com.cyanogenmod.eleven.loaders.SongLoader;
24 import com.cyanogenmod.eleven.model.Song;
25 import com.cyanogenmod.eleven.sectionadapter.SectionCreator;
26 import com.cyanogenmod.eleven.sectionadapter.SectionListContainer;
27 import com.cyanogenmod.eleven.ui.fragments.profile.BasicSongFragment;
28 import com.cyanogenmod.eleven.utils.MusicUtils;
29 import com.cyanogenmod.eleven.utils.SectionCreatorUtils;
32 * This class is used to display all of the songs on a user's device.
34 * @author Andrew Neal (andrewdneal@gmail.com)
36 public class SongFragment extends BasicSongFragment {
41 public void playAll(int position) {
42 int internalPosition = mAdapter.getInternalPosition(position);
43 final long[] list = mAdapter.getUnderlyingAdapter().getSongIds();
45 MusicUtils.playAll(getActivity(), list, internalPosition, -1, Config.IdType.NA, false);
53 public Loader<SectionListContainer<Song>> onCreateLoader(final int id, final Bundle args) {
54 // show the loading progress bar
55 mLoadingEmptyContainer.showLoading();
58 Context context = getActivity();
60 // create the underlying song loader
61 SongLoader songLoader = new SongLoader(context);
63 // get the song comparison method to create the headers with
64 SectionCreatorUtils.IItemCompare<Song> songComparison = SectionCreatorUtils.createSongComparison(context);
66 // return the wrapped section creator
67 return new SectionCreator<Song>(context, songLoader, songComparison);
72 public int getLoaderId() {
73 return PagerAdapter.MusicFragments.SONG.ordinal();
77 * Scrolls the list to the currently playing song when the user touches the
78 * header in the {@link TitlePageIndicator}.
80 public void scrollToCurrentSong() {
81 final int currentSongPosition = getItemPositionBySong();
83 if (currentSongPosition != 0) {
84 mListView.setSelection(currentSongPosition);
89 * @return The position of an item in the list based on the name of the
90 * currently playing song.
92 private int getItemPositionBySong() {
93 final long trackId = MusicUtils.getCurrentAudioId();
94 if (mAdapter == null) {
98 int position = mAdapter.getItemPosition(trackId);
100 // if for some reason we don't find the item, just jump to the top
109 public LoaderManager getFragmentLoaderManager() {
110 return getParentFragment().getLoaderManager();