OSDN Git Service

Eleven: materialize
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / ui / fragments / SongFragment.java
1 /*
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.
12  */
13
14 package com.cyanogenmod.eleven.ui.fragments;
15
16 import android.content.Context;
17 import android.os.Bundle;
18 import android.support.v4.app.LoaderManager;
19 import android.support.v4.content.Loader;
20
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;
30
31 /**
32  * This class is used to display all of the songs on a user's device.
33  *
34  * @author Andrew Neal (andrewdneal@gmail.com)
35  */
36 public class SongFragment extends BasicSongFragment {
37
38     /**
39      * {@inheritDoc}
40      */
41     public void playAll(int position) {
42         int internalPosition = mAdapter.getInternalPosition(position);
43         final long[] list = mAdapter.getUnderlyingAdapter().getSongIds();
44         if (list != null) {
45             MusicUtils.playAll(getActivity(), list, internalPosition, -1, Config.IdType.NA, false);
46         }
47     }
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public Loader<SectionListContainer<Song>> onCreateLoader(final int id, final Bundle args) {
54         // show the loading progress bar
55         mLoadingEmptyContainer.showLoading();
56
57         // get the context
58         Context context = getActivity();
59
60         // create the underlying song loader
61         SongLoader songLoader = new SongLoader(context);
62
63         // get the song comparison method to create the headers with
64         SectionCreatorUtils.IItemCompare<Song> songComparison = SectionCreatorUtils.createSongComparison(context);
65
66         // return the wrapped section creator
67         return new SectionCreator<Song>(context, songLoader, songComparison);
68     }
69
70
71     @Override
72     public int getLoaderId() {
73         return PagerAdapter.MusicFragments.SONG.ordinal();
74     }
75
76     /**
77      * Scrolls the list to the currently playing song when the user touches the
78      * header in the {@link TitlePageIndicator}.
79      */
80     public void scrollToCurrentSong() {
81         final int currentSongPosition = getItemPositionBySong();
82
83         if (currentSongPosition != 0) {
84             mListView.setSelection(currentSongPosition);
85         }
86     }
87
88     /**
89      * @return The position of an item in the list based on the name of the
90      * currently playing song.
91      */
92     private int getItemPositionBySong() {
93         final long trackId = MusicUtils.getCurrentAudioId();
94         if (mAdapter == null) {
95             return 0;
96         }
97
98         int position = mAdapter.getItemPosition(trackId);
99
100         // if for some reason we don't find the item, just jump to the top
101         if (position < 0) {
102             return 0;
103         }
104
105         return position;
106     }
107
108     @Override
109     public LoaderManager getFragmentLoaderManager() {
110         return getParentFragment().getLoaderManager();
111     }
112 }