OSDN Git Service

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