OSDN Git Service

45444744e131d43938ece61323d16bd5e6399bdf
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / ui / fragments / GenreFragment.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.andrew.apollo.ui.fragments;
13
14 import android.content.Intent;
15 import android.os.Bundle;
16 import android.provider.MediaStore;
17 import android.support.v4.app.Fragment;
18 import android.support.v4.app.LoaderManager.LoaderCallbacks;
19 import android.support.v4.content.Loader;
20 import android.view.ContextMenu;
21 import android.view.ContextMenu.ContextMenuInfo;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.AdapterView;
27 import android.widget.AdapterView.AdapterContextMenuInfo;
28 import android.widget.AdapterView.OnItemClickListener;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
32 import com.andrew.apollo.Config;
33 import com.andrew.apollo.R;
34 import com.andrew.apollo.adapters.GenreAdapter;
35 import com.andrew.apollo.loaders.GenreLoader;
36 import com.andrew.apollo.menu.FragmentMenuItems;
37 import com.andrew.apollo.model.Genre;
38 import com.andrew.apollo.recycler.RecycleHolder;
39 import com.andrew.apollo.ui.activities.ProfileActivity;
40 import com.andrew.apollo.utils.MusicUtils;
41
42 import java.util.List;
43
44 /**
45  * This class is used to display all of the genres on a user's device.
46  * 
47  * @author Andrew Neal (andrewdneal@gmail.com)
48  */
49 public class GenreFragment extends Fragment implements LoaderCallbacks<List<Genre>>,
50         OnItemClickListener {
51
52     /**
53      * Used to keep context menu items from bleeding into other fragments
54      */
55     private static final int GROUP_ID = 5;
56
57     /**
58      * LoaderCallbacks identifier
59      */
60     private static final int LOADER = 0;
61
62     /**
63      * Fragment UI
64      */
65     private ViewGroup mRootView;
66
67     /**
68      * The adapter for the list
69      */
70     private GenreAdapter mAdapter;
71
72     /**
73      * The list view
74      */
75     private ListView mListView;
76
77     /**
78      * Genre song list
79      */
80     private long[] mGenreList;
81
82     /**
83      * Represents a genre
84      */
85     private Genre mGenre;
86
87     /**
88      * Empty constructor as per the {@link Fragment} documentation
89      */
90     public GenreFragment() {
91     }
92
93     /**
94      * {@inheritDoc}
95      */
96     @Override
97     public void onCreate(final Bundle savedInstanceState) {
98         super.onCreate(savedInstanceState);
99         // Create the adpater
100         mAdapter = new GenreAdapter(getActivity(), R.layout.list_item_simple);
101     }
102
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
108             final Bundle savedInstanceState) {
109         // The View for the fragment's UI
110         mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
111         // Initialize the list
112         mListView = (ListView)mRootView.findViewById(R.id.list_base);
113         // Set the data behind the list
114         mListView.setAdapter(mAdapter);
115         // Release any references to the recycled Views
116         mListView.setRecyclerListener(new RecycleHolder());
117         // Listen for ContextMenus to be created
118         mListView.setOnCreateContextMenuListener(this);
119         // Show the albums and songs from the selected genre
120         mListView.setOnItemClickListener(this);
121         return mRootView;
122     }
123
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public void onActivityCreated(final Bundle savedInstanceState) {
129         super.onActivityCreated(savedInstanceState);
130         // Enable the options menu
131         setHasOptionsMenu(true);
132         // Start the loader
133         getLoaderManager().initLoader(LOADER, null, this);
134     }
135
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public void onCreateContextMenu(final ContextMenu menu, final View v,
141             final ContextMenuInfo menuInfo) {
142         super.onCreateContextMenu(menu, v, menuInfo);
143         // Get the position of the selected item
144         final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
145         // Create a new genre
146         mGenre = mAdapter.getItem(info.position);
147         // Create a list of the genre's songs
148         mGenreList = MusicUtils.getSongListForGenre(getActivity(), mGenre.mGenreId);
149
150         // Play the genre
151         menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
152                 R.string.context_menu_play_selection);
153         // Add the genre to the queue
154         menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE, R.string.add_to_queue);
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public boolean onContextItemSelected(final android.view.MenuItem item) {
162         if (item.getGroupId() == GROUP_ID) {
163             switch (item.getItemId()) {
164                 case FragmentMenuItems.PLAY_SELECTION:
165                     MusicUtils.playAll(getActivity(), mGenreList, 0, false);
166                     return true;
167                 case FragmentMenuItems.ADD_TO_QUEUE:
168                     MusicUtils.addToQueue(getActivity(), mGenreList);
169                     return true;
170                 default:
171                     break;
172             }
173         }
174         return super.onContextItemSelected(item);
175     }
176
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
182             final long id) {
183         mGenre = mAdapter.getItem(position);
184         // Create a new bundle to transfer the artist info
185         final Bundle bundle = new Bundle();
186         bundle.putLong(Config.ID, mGenre.mGenreId);
187         bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Genres.CONTENT_TYPE);
188         bundle.putString(Config.NAME, mGenre.mGenreName);
189
190         // Create the intent to launch the profile activity
191         final Intent intent = new Intent(getActivity(), ProfileActivity.class);
192         intent.putExtras(bundle);
193         startActivity(intent);
194     }
195
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public Loader<List<Genre>> onCreateLoader(final int id, final Bundle args) {
201         return new GenreLoader(getActivity());
202     }
203
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public void onLoadFinished(final Loader<List<Genre>> loader, final List<Genre> data) {
209         // Check for any errors
210         if (data.isEmpty()) {
211             // Set the empty text
212             final TextView empty = (TextView)mRootView.findViewById(R.id.empty);
213             empty.setText(getString(R.string.empty_music));
214             mListView.setEmptyView(empty);
215             return;
216         }
217
218         // Start fresh
219         mAdapter.unload();
220         // Add the data to the adpater
221         for (final Genre genre : data) {
222             mAdapter.add(genre);
223         }
224         // Build the cache
225         mAdapter.buildCache();
226     }
227
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public void onLoaderReset(final Loader<List<Genre>> loader) {
233         // Clear the data in the adapter
234         mAdapter.unload();
235     }
236
237 }