OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / ui / fragments / profile / AlbumSongFragment.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.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.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 album.
51  * 
52  * @author Andrew Neal (andrewdneal@gmail.com)
53  */
54 public class AlbumSongFragment 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 = 11;
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 AlbumSongFragment() {
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(
126                 getActivity(),
127                 R.layout.list_item_simple,
128                 ProfileSongAdapter.DISPLAY_ALBUM_SETTING
129         );
130     }
131
132     /**
133      * {@inheritDoc}
134      */
135     @Override
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);
156         return rootView;
157     }
158
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public void onActivityCreated(final Bundle savedInstanceState) {
164         super.onActivityCreated(savedInstanceState);
165         // Enable the options menu
166         setHasOptionsMenu(true);
167         // Start the loader
168         final Bundle arguments = getArguments();
169         if (arguments != null) {
170             getLoaderManager().initLoader(LOADER, arguments, this);
171         }
172     }
173
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public void onSaveInstanceState(final Bundle outState) {
179         super.onSaveInstanceState(outState);
180         outState.putAll(getArguments() != null ? getArguments() : new Bundle());
181     }
182
183     /**
184      * {@inheritDoc}
185      */
186     @Override
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;
193         // Creat a new song
194         mSong = mAdapter.getItem(mSelectedPosition);
195         mSelectedId = mSong.mSongId;
196         mSongName = mSong.mSongName;
197         mAlbumName = mSong.mAlbumName;
198         mArtistName = mSong.mArtistName;
199
200         // Play the song
201         menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
202                 getString(R.string.context_menu_play_selection));
203
204         // Play the song
205         menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
206                 getString(R.string.context_menu_play_next));
207
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));
211
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, true);
216
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));
220
221         // Delete the song
222         menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
223                 getString(R.string.context_menu_delete));
224     }
225
226     @Override
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[] {
232                         mSelectedId
233                     }, 0, false);
234                     return true;
235                 case FragmentMenuItems.PLAY_NEXT:
236                     MusicUtils.playNext(new long[] {
237                         mSelectedId
238                     });
239                     return true;
240                 case FragmentMenuItems.ADD_TO_QUEUE:
241                     MusicUtils.addToQueue(getActivity(), new long[] {
242                         mSelectedId
243                     });
244                     return true;
245                 case FragmentMenuItems.ADD_TO_FAVORITES:
246                     FavoritesStore.getInstance(getActivity()).addSongId(
247                             mSelectedId, mSongName, mAlbumName, mArtistName);
248                     return true;
249                 case FragmentMenuItems.NEW_PLAYLIST:
250                     CreateNewPlaylist.getInstance(new long[] {
251                         mSelectedId
252                     }).show(getFragmentManager(), "CreatePlaylist");
253                     return true;
254                 case FragmentMenuItems.PLAYLIST_SELECTED:
255                     final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
256                     MusicUtils.addToPlaylist(getActivity(), new long[] {
257                         mSelectedId
258                     }, mPlaylistId);
259                     return true;
260                 case FragmentMenuItems.USE_AS_RINGTONE:
261                     MusicUtils.setRingtone(getActivity(), mSelectedId);
262                     return true;
263                 case FragmentMenuItems.DELETE:
264                     DeleteDialog.newInstance(mSong.mSongName, new long[] {
265                         mSelectedId
266                     }, null).show(getFragmentManager(), "DeleteDialog");
267                     refresh();
268                     return true;
269                 default:
270                     break;
271             }
272         }
273         return super.onContextItemSelected(item);
274     }
275
276     /**
277      * {@inheritDoc}
278      */
279     @Override
280     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
281             final long id) {
282         if (position == 0) {
283             return;
284         }
285         Cursor cursor = AlbumSongLoader.makeAlbumSongCursor(getActivity(), getArguments()
286                 .getLong(Config.ID));
287         final long[] list = MusicUtils.getSongListForCursor(cursor);
288         MusicUtils.playAll(getActivity(), list, position - 1, false);
289         cursor.close();
290         cursor = null;
291     }
292
293     /**
294      * {@inheritDoc}
295      */
296     @Override
297     public Loader<List<Song>> onCreateLoader(final int id, final Bundle args) {
298         return new AlbumSongLoader(getActivity(), args.getLong(Config.ID));
299     }
300
301     /**
302      * {@inheritDoc}
303      */
304     @Override
305     public void onLoadFinished(final Loader<List<Song>> loader, final List<Song> data) {
306         // Check for any errors
307         if (data.isEmpty()) {
308             return;
309         }
310
311         // Start fresh
312         mAdapter.unload();
313         // Return the correct count
314         mAdapter.setCount(data);
315         // Add the data to the adpater
316         for (final Song song : data) {
317             mAdapter.add(song);
318         }
319     }
320
321     /**
322      * {@inheritDoc}
323      */
324     @Override
325     public void onLoaderReset(final Loader<List<Song>> loader) {
326         // Clear the data in the adapter
327         mAdapter.unload();
328     }
329
330     /**
331      * Restarts the loader.
332      */
333     public void refresh() {
334         // Scroll to the stop of the list before restarting the loader.
335         // Otherwise, if the user has scrolled enough to move the header, it
336         // becomes misplaced and needs to be reset.
337         mListView.setSelection(0);
338         // Wait a moment for the preference to change.
339         SystemClock.sleep(10);
340         mAdapter.notifyDataSetChanged();
341         getLoaderManager().restartLoader(LOADER, getArguments(), this);
342     }
343 }