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.AlbumSongLoader;
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.recycler.RecycleHolder;
42 import com.cyngn.eleven.utils.MusicUtils;
43 import com.cyngn.eleven.widgets.ProfileTabCarousel;
44 import com.cyngn.eleven.widgets.VerticalScrollListener;
46 import java.util.List;
49 * This class is used to display all of the songs from a particular album.
51 * @author Andrew Neal (andrewdneal@gmail.com)
53 public class AlbumSongFragment extends Fragment implements LoaderCallbacks<List<Song>>,
57 * Used to keep context menu items from bleeding into other fragments
59 private static final int GROUP_ID = 11;
62 * LoaderCallbacks identifier
64 private static final int LOADER = 0;
67 * The adapter for the list
69 private ProfileSongAdapter mAdapter;
74 private ListView mListView;
82 * Position of a context menu item
84 private int mSelectedPosition;
87 * Id of a context menu item
89 private long mSelectedId;
92 * Song, album, and artist name used in the context menu
94 private String mSongName, mAlbumName, mArtistName;
99 private ProfileTabCarousel mProfileTabCarousel;
102 * Empty constructor as per the {@link Fragment} documentation
104 public AlbumSongFragment() {
111 public void onAttach(final Activity activity) {
112 super.onAttach(activity);
113 mProfileTabCarousel = (ProfileTabCarousel)activity
114 .findViewById(R.id.acivity_profile_base_tab_carousel);
121 public void onCreate(final Bundle savedInstanceState) {
122 super.onCreate(savedInstanceState);
123 // Create the adpater
124 mAdapter = new ProfileSongAdapter(
126 R.layout.list_item_simple,
127 R.layout.faux_carousel,
128 ProfileSongAdapter.DISPLAY_ALBUM_SETTING
136 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
137 final Bundle savedInstanceState) {
138 // The View for the fragment's UI
139 final ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
140 // Initialize the list
141 mListView = (ListView)rootView.findViewById(R.id.list_base);
142 // Set the data behind the list
143 mListView.setAdapter(mAdapter);
144 // Release any references to the recycled Views
145 mListView.setRecyclerListener(new RecycleHolder());
146 // Listen for ContextMenus to be created
147 mListView.setOnCreateContextMenuListener(this);
148 // Play the selected song
149 mListView.setOnItemClickListener(this);
150 // To help make scrolling smooth
151 mListView.setOnScrollListener(new VerticalScrollListener(null, mProfileTabCarousel, 0));
152 // Remove the scrollbars and padding for the fast scroll
153 mListView.setVerticalScrollBarEnabled(false);
154 mListView.setFastScrollEnabled(false);
155 mListView.setPadding(0, 0, 0, 0);
163 public void onActivityCreated(final Bundle savedInstanceState) {
164 super.onActivityCreated(savedInstanceState);
165 // Enable the options menu
166 setHasOptionsMenu(true);
168 final Bundle arguments = getArguments();
169 if (arguments != null) {
170 getLoaderManager().initLoader(LOADER, arguments, this);
178 public void onSaveInstanceState(final Bundle outState) {
179 super.onSaveInstanceState(outState);
180 outState.putAll(getArguments() != null ? getArguments() : new Bundle());
187 public void onCreateContextMenu(final ContextMenu menu, final View v,
188 final ContextMenuInfo menuInfo) {
189 super.onCreateContextMenu(menu, v, menuInfo);
190 // Get the position of the selected item
191 final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
192 mSelectedPosition = info.position - 1;
194 mSong = mAdapter.getItem(mSelectedPosition);
195 mSelectedId = mSong.mSongId;
196 mSongName = mSong.mSongName;
197 mAlbumName = mSong.mAlbumName;
198 mArtistName = mSong.mArtistName;
201 menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
202 getString(R.string.context_menu_play_selection));
205 menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
206 getString(R.string.context_menu_play_next));
208 // Add the song to the queue
209 menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE,
210 getString(R.string.add_to_queue));
212 // Add the song to a playlist
213 final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST,
214 Menu.NONE, R.string.add_to_playlist);
215 MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu);
217 // Make the song a ringtone
218 menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
219 getString(R.string.context_menu_use_as_ringtone));
222 menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
223 getString(R.string.context_menu_delete));
227 public boolean onContextItemSelected(final android.view.MenuItem item) {
228 if (item.getGroupId() == GROUP_ID) {
229 switch (item.getItemId()) {
230 case FragmentMenuItems.PLAY_SELECTION:
231 MusicUtils.playAll(getActivity(), new long[] {
235 case FragmentMenuItems.PLAY_NEXT:
236 MusicUtils.playNext(new long[] {
240 case FragmentMenuItems.ADD_TO_QUEUE:
241 MusicUtils.addToQueue(getActivity(), new long[] {
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 = AlbumSongLoader.makeAlbumSongCursor(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 AlbumSongLoader(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);