OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / ui / fragments / profile / ArtistSongFragment.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.profile;
13
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;
32
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;
46
47 import java.util.List;
48
49 /**
50  * This class is used to display all of the songs from a particular artist.
51  * 
52  * @author Andrew Neal (andrewdneal@gmail.com)
53  */
54 public class ArtistSongFragment extends Fragment implements LoaderCallbacks<List<Song>>,
55         OnItemClickListener {
56
57     /**
58      * Used to keep context menu items from bleeding into other fragments
59      */
60     private static final int GROUP_ID = 9;
61
62     /**
63      * LoaderCallbacks identifier
64      */
65     private static final int LOADER = 0;
66
67     /**
68      * The adapter for the list
69      */
70     private ProfileSongAdapter mAdapter;
71
72     /**
73      * The list view
74      */
75     private ListView mListView;
76
77     /**
78      * Represents a song
79      */
80     private Song mSong;
81
82     /**
83      * Position of a context menu item
84      */
85     private int mSelectedPosition;
86
87     /**
88      * Id of a context menu item
89      */
90     private long mSelectedId;
91
92     /**
93      * Song, album, and artist name used in the context menu
94      */
95     private String mSongName, mAlbumName, mArtistName;
96
97     /**
98      * Profile header
99      */
100     private ProfileTabCarousel mProfileTabCarousel;
101
102     /**
103      * Empty constructor as per the {@link Fragment} documentation
104      */
105     public ArtistSongFragment() {
106     }
107
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public void onAttach(final Activity activity) {
113         super.onAttach(activity);
114         mProfileTabCarousel = (ProfileTabCarousel)activity
115                 .findViewById(R.id.acivity_profile_base_tab_carousel);
116     }
117
118     /**
119      * {@inheritDoc}
120      */
121     @Override
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);
126     }
127
128     /**
129      * {@inheritDoc}
130      */
131     @Override
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);
152         return rootView;
153     }
154
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public void onActivityCreated(final Bundle savedInstanceState) {
160         super.onActivityCreated(savedInstanceState);
161         // Enable the options menu
162         setHasOptionsMenu(true);
163         // Start the loader
164         final Bundle arguments = getArguments();
165         if (arguments != null) {
166             getLoaderManager().initLoader(LOADER, arguments, this);
167         }
168     }
169
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public void onSaveInstanceState(final Bundle outState) {
175         super.onSaveInstanceState(outState);
176         outState.putAll(getArguments() != null ? getArguments() : new Bundle());
177     }
178
179     /**
180      * {@inheritDoc}
181      */
182     @Override
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;
189         // Creat a new song
190         mSong = mAdapter.getItem(mSelectedPosition);
191         mSelectedId = mSong.mSongId;
192         mSongName = mSong.mSongName;
193         mAlbumName = mSong.mAlbumName;
194         mArtistName = mSong.mArtistName;
195
196         // Play the song
197         menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
198                 getString(R.string.context_menu_play_selection));
199
200         // Play next
201         menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
202                 getString(R.string.context_menu_play_next));
203
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));
207
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);
212
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));
216
217         // Delete the song
218         menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
219                 getString(R.string.context_menu_delete));
220     }
221
222     @Override
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[] {
228                         mSelectedId
229                     }, 0, false);
230                     return true;
231                 case FragmentMenuItems.PLAY_NEXT:
232                     MusicUtils.playNext(new long[] {
233                         mSelectedId
234                     });
235                     return true;
236                 case FragmentMenuItems.ADD_TO_QUEUE:
237                     MusicUtils.addToQueue(getActivity(), new long[] {
238                         mSelectedId
239                     });
240                     return true;
241                 case FragmentMenuItems.ADD_TO_FAVORITES:
242                     FavoritesStore.getInstance(getActivity()).addSongId(
243                             mSelectedId, mSongName, mAlbumName, mArtistName);
244                     return true;
245                 case FragmentMenuItems.NEW_PLAYLIST:
246                     CreateNewPlaylist.getInstance(new long[] {
247                         mSelectedId
248                     }).show(getFragmentManager(), "CreatePlaylist");
249                     return true;
250                 case FragmentMenuItems.PLAYLIST_SELECTED:
251                     final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
252                     MusicUtils.addToPlaylist(getActivity(), new long[] {
253                         mSelectedId
254                     }, mPlaylistId);
255                     return true;
256                 case FragmentMenuItems.USE_AS_RINGTONE:
257                     MusicUtils.setRingtone(getActivity(), mSelectedId);
258                     return true;
259                 case FragmentMenuItems.DELETE:
260                     DeleteDialog.newInstance(mSong.mSongName, new long[] {
261                         mSelectedId
262                     }, null).show(getFragmentManager(), "DeleteDialog");
263                     refresh();
264                     return true;
265                 default:
266                     break;
267             }
268         }
269         return super.onContextItemSelected(item);
270     }
271
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
277             final long id) {
278         if (position == 0) {
279             return;
280         }
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);
285         cursor.close();
286         cursor = null;
287     }
288
289     /**
290      * {@inheritDoc}
291      */
292     @Override
293     public Loader<List<Song>> onCreateLoader(final int id, final Bundle args) {
294         return new ArtistSongLoader(getActivity(), args.getLong(Config.ID));
295     }
296
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public void onLoadFinished(final Loader<List<Song>> loader, final List<Song> data) {
302         // Check for any errors
303         if (data.isEmpty()) {
304             return;
305         }
306
307         // Start fresh
308         mAdapter.unload();
309         // Return the correct count
310         mAdapter.setCount(data);
311         // Add the data to the adpater
312         for (final Song song : data) {
313             mAdapter.add(song);
314         }
315     }
316
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public void onLoaderReset(final Loader<List<Song>> loader) {
322         // Clear the data in the adapter
323         mAdapter.unload();
324     }
325
326     /**
327      * Restarts the loader.
328      */
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);
338     }
339 }