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.andrew.apollo.ui.fragments;
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;
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;
42 import java.util.List;
45 * This class is used to display all of the genres on a user's device.
47 * @author Andrew Neal (andrewdneal@gmail.com)
49 public class GenreFragment extends Fragment implements LoaderCallbacks<List<Genre>>,
53 * Used to keep context menu items from bleeding into other fragments
55 private static final int GROUP_ID = 5;
58 * LoaderCallbacks identifier
60 private static final int LOADER = 0;
65 private ViewGroup mRootView;
68 * The adapter for the list
70 private GenreAdapter mAdapter;
75 private ListView mListView;
80 private long[] mGenreList;
88 * Empty constructor as per the {@link Fragment} documentation
90 public GenreFragment() {
97 public void onCreate(final Bundle savedInstanceState) {
98 super.onCreate(savedInstanceState);
100 mAdapter = new GenreAdapter(getActivity(), R.layout.list_item_simple);
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);
128 public void onActivityCreated(final Bundle savedInstanceState) {
129 super.onActivityCreated(savedInstanceState);
130 // Enable the options menu
131 setHasOptionsMenu(true);
133 getLoaderManager().initLoader(LOADER, null, this);
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);
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);
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);
167 case FragmentMenuItems.ADD_TO_QUEUE:
168 MusicUtils.addToQueue(getActivity(), mGenreList);
174 return super.onContextItemSelected(item);
181 public void onItemClick(final AdapterView<?> parent, final View view, final int position,
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);
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);
200 public Loader<List<Genre>> onCreateLoader(final int id, final Bundle args) {
201 return new GenreLoader(getActivity());
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);
220 // Add the data to the adpater
221 for (final Genre genre : data) {
225 mAdapter.buildCache();
232 public void onLoaderReset(final Loader<List<Genre>> loader) {
233 // Clear the data in the adapter