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.cyngn.eleven.ui.fragments.profile;
14 import android.app.Activity;
15 import android.database.Cursor;
16 import android.os.Bundle;
17 import android.os.SystemClock;
18 import android.support.v4.app.Fragment;
19 import android.support.v4.app.LoaderManager.LoaderCallbacks;
20 import android.support.v4.content.Loader;
21 import android.view.ContextMenu;
22 import android.view.ContextMenu.ContextMenuInfo;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.SubMenu;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.AdapterView;
29 import android.widget.AdapterView.AdapterContextMenuInfo;
30 import android.widget.AdapterView.OnItemClickListener;
31 import android.widget.ListView;
33 import com.cyngn.eleven.Config;
34 import com.cyngn.eleven.R;
35 import com.cyngn.eleven.adapters.ProfileSongAdapter;
36 import com.cyngn.eleven.loaders.ArtistSongLoader;
37 import com.cyngn.eleven.menu.CreateNewPlaylist;
38 import com.cyngn.eleven.menu.DeleteDialog;
39 import com.cyngn.eleven.menu.FragmentMenuItems;
40 import com.cyngn.eleven.model.Song;
41 import com.cyngn.eleven.provider.FavoritesStore;
42 import com.cyngn.eleven.recycler.RecycleHolder;
43 import com.cyngn.eleven.utils.MusicUtils;
44 import com.cyngn.eleven.widgets.ProfileTabCarousel;
45 import com.cyngn.eleven.widgets.VerticalScrollListener;
47 import java.util.List;
50 * This class is used to display all of the songs from a particular artist.
52 * @author Andrew Neal (andrewdneal@gmail.com)
54 public class ArtistSongFragment extends Fragment implements LoaderCallbacks<List<Song>>,
58 * Used to keep context menu items from bleeding into other fragments
60 private static final int GROUP_ID = 9;
63 * LoaderCallbacks identifier
65 private static final int LOADER = 0;
68 * The adapter for the list
70 private ProfileSongAdapter mAdapter;
75 private ListView mListView;
83 * Position of a context menu item
85 private int mSelectedPosition;
88 * Id of a context menu item
90 private long mSelectedId;
93 * Song, album, and artist name used in the context menu
95 private String mSongName, mAlbumName, mArtistName;
100 private ProfileTabCarousel mProfileTabCarousel;
103 * Empty constructor as per the {@link Fragment} documentation
105 public ArtistSongFragment() {
112 public void onAttach(final Activity activity) {
113 super.onAttach(activity);
114 mProfileTabCarousel = (ProfileTabCarousel)activity
115 .findViewById(R.id.acivity_profile_base_tab_carousel);
122 public void onCreate(final Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
124 // Create the adpater
125 mAdapter = new ProfileSongAdapter(getActivity(), R.layout.list_item_simple);
132 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
133 final Bundle savedInstanceState) {
134 // The View for the fragment's UI
135 final ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
136 // Initialize the list
137 mListView = (ListView)rootView.findViewById(R.id.list_base);
138 // Set the data behind the list
139 mListView.setAdapter(mAdapter);
140 // Release any references to the recycled Views
141 mListView.setRecyclerListener(new RecycleHolder());
142 // Listen for ContextMenus to be created
143 mListView.setOnCreateContextMenuListener(this);
144 // Play the selected song
145 mListView.setOnItemClickListener(this);
146 // To help make scrolling smooth
147 mListView.setOnScrollListener(new VerticalScrollListener(null, mProfileTabCarousel, 0));
148 // Remove the scrollbars and padding for the fast scroll
149 mListView.setVerticalScrollBarEnabled(false);
150 mListView.setFastScrollEnabled(false);
151 mListView.setPadding(0, 0, 0, 0);
159 public void onActivityCreated(final Bundle savedInstanceState) {
160 super.onActivityCreated(savedInstanceState);
161 // Enable the options menu
162 setHasOptionsMenu(true);
164 final Bundle arguments = getArguments();
165 if (arguments != null) {
166 getLoaderManager().initLoader(LOADER, arguments, this);
174 public void onSaveInstanceState(final Bundle outState) {
175 super.onSaveInstanceState(outState);
176 outState.putAll(getArguments() != null ? getArguments() : new Bundle());
183 public void onCreateContextMenu(final ContextMenu menu, final View v,
184 final ContextMenuInfo menuInfo) {
185 super.onCreateContextMenu(menu, v, menuInfo);
186 // Get the position of the selected item
187 final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
188 mSelectedPosition = info.position - 1;
190 mSong = mAdapter.getItem(mSelectedPosition);
191 mSelectedId = mSong.mSongId;
192 mSongName = mSong.mSongName;
193 mAlbumName = mSong.mAlbumName;
194 mArtistName = mSong.mArtistName;
197 menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
198 getString(R.string.context_menu_play_selection));
201 menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
202 getString(R.string.context_menu_play_next));
204 // Add the song to the queue
205 menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE,
206 getString(R.string.add_to_queue));
208 // Add the song to a playlist
209 final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST,
210 Menu.NONE, R.string.add_to_playlist);
211 MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu, true);
213 // Make the song a ringtone
214 menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
215 getString(R.string.context_menu_use_as_ringtone));
218 menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
219 getString(R.string.context_menu_delete));
223 public boolean onContextItemSelected(final android.view.MenuItem item) {
224 if (item.getGroupId() == GROUP_ID) {
225 switch (item.getItemId()) {
226 case FragmentMenuItems.PLAY_SELECTION:
227 MusicUtils.playAll(getActivity(), new long[] {
231 case FragmentMenuItems.PLAY_NEXT:
232 MusicUtils.playNext(new long[] {
236 case FragmentMenuItems.ADD_TO_QUEUE:
237 MusicUtils.addToQueue(getActivity(), new long[] {
241 case FragmentMenuItems.ADD_TO_FAVORITES:
242 FavoritesStore.getInstance(getActivity()).addSongId(
243 mSelectedId, mSongName, mAlbumName, mArtistName);
245 case FragmentMenuItems.NEW_PLAYLIST:
246 CreateNewPlaylist.getInstance(new long[] {
248 }).show(getFragmentManager(), "CreatePlaylist");
250 case FragmentMenuItems.PLAYLIST_SELECTED:
251 final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
252 MusicUtils.addToPlaylist(getActivity(), new long[] {
256 case FragmentMenuItems.USE_AS_RINGTONE:
257 MusicUtils.setRingtone(getActivity(), mSelectedId);
259 case FragmentMenuItems.DELETE:
260 DeleteDialog.newInstance(mSong.mSongName, new long[] {
262 }, null).show(getFragmentManager(), "DeleteDialog");
269 return super.onContextItemSelected(item);
276 public void onItemClick(final AdapterView<?> parent, final View view, final int position,
281 Cursor cursor = ArtistSongLoader.makeArtistSongCursor(getActivity(), getArguments()
282 .getLong(Config.ID));
283 final long[] list = MusicUtils.getSongListForCursor(cursor);
284 MusicUtils.playAll(getActivity(), list, position - 1, false);
293 public Loader<List<Song>> onCreateLoader(final int id, final Bundle args) {
294 return new ArtistSongLoader(getActivity(), args.getLong(Config.ID));
301 public void onLoadFinished(final Loader<List<Song>> loader, final List<Song> data) {
302 // Check for any errors
303 if (data.isEmpty()) {
309 // Return the correct count
310 mAdapter.setCount(data);
311 // Add the data to the adpater
312 for (final Song song : data) {
321 public void onLoaderReset(final Loader<List<Song>> loader) {
322 // Clear the data in the adapter
327 * Restarts the loader.
329 public void refresh() {
330 // Scroll to the stop of the list before restarting the loader.
331 // Otherwise, if the user has scrolled enough to move the header, it
332 // becomes misplaced and needs to be reset.
333 mListView.setSelection(0);
334 // Wait a moment for the preference to change.
335 SystemClock.sleep(10);
336 mAdapter.notifyDataSetChanged();
337 getLoaderManager().restartLoader(LOADER, getArguments(), this);