OSDN Git Service

Eleven: Change playlist to the new layout, rip out dead code
[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.recycler.RecycleHolder;
42 import com.cyngn.eleven.utils.MusicUtils;
43 import com.cyngn.eleven.widgets.ProfileTabCarousel;
44 import com.cyngn.eleven.widgets.VerticalScrollListener;
45
46 import java.util.List;
47
48 /**
49  * This class is used to display all of the songs from a particular album.
50  * 
51  * @author Andrew Neal (andrewdneal@gmail.com)
52  */
53 public class AlbumSongFragment extends Fragment implements LoaderCallbacks<List<Song>>,
54         OnItemClickListener {
55
56     /**
57      * Used to keep context menu items from bleeding into other fragments
58      */
59     private static final int GROUP_ID = 11;
60
61     /**
62      * LoaderCallbacks identifier
63      */
64     private static final int LOADER = 0;
65
66     /**
67      * The adapter for the list
68      */
69     private ProfileSongAdapter mAdapter;
70
71     /**
72      * The list view
73      */
74     private ListView mListView;
75
76     /**
77      * Represents a song
78      */
79     private Song mSong;
80
81     /**
82      * Position of a context menu item
83      */
84     private int mSelectedPosition;
85
86     /**
87      * Id of a context menu item
88      */
89     private long mSelectedId;
90
91     /**
92      * Song, album, and artist name used in the context menu
93      */
94     private String mSongName, mAlbumName, mArtistName;
95
96     /**
97      * Profile header
98      */
99     private ProfileTabCarousel mProfileTabCarousel;
100
101     /**
102      * Empty constructor as per the {@link Fragment} documentation
103      */
104     public AlbumSongFragment() {
105     }
106
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void onAttach(final Activity activity) {
112         super.onAttach(activity);
113         mProfileTabCarousel = (ProfileTabCarousel)activity
114                 .findViewById(R.id.acivity_profile_base_tab_carousel);
115     }
116
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void onCreate(final Bundle savedInstanceState) {
122         super.onCreate(savedInstanceState);
123         // Create the adpater
124         mAdapter = new ProfileSongAdapter(
125                 getActivity(),
126                 R.layout.list_item_simple,
127                 R.layout.faux_carousel,
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);
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.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 = AlbumSongLoader.makeAlbumSongCursor(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 AlbumSongLoader(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 }