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 / LastAddedFragment.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.AbsListView;
29 import android.widget.AdapterView;
30 import android.widget.AdapterView.AdapterContextMenuInfo;
31 import android.widget.AdapterView.OnItemClickListener;
32 import android.widget.ListView;
33 import android.widget.TextView;
34
35 import com.cyngn.eleven.R;
36 import com.cyngn.eleven.adapters.ProfileSongAdapter;
37 import com.cyngn.eleven.loaders.LastAddedLoader;
38 import com.cyngn.eleven.menu.CreateNewPlaylist;
39 import com.cyngn.eleven.menu.DeleteDialog;
40 import com.cyngn.eleven.menu.FragmentMenuItems;
41 import com.cyngn.eleven.model.Song;
42 import com.cyngn.eleven.recycler.RecycleHolder;
43 import com.cyngn.eleven.utils.MusicUtils;
44 import com.cyngn.eleven.utils.NavUtils;
45 import com.cyngn.eleven.widgets.ProfileTabCarousel;
46 import com.cyngn.eleven.widgets.VerticalScrollListener;
47
48 import java.util.List;
49
50 /**
51  * This class is used to display all of the songs the user put on their device
52  * within the last four weeks.
53  * 
54  * @author Andrew Neal (andrewdneal@gmail.com)
55  */
56 public class LastAddedFragment extends Fragment implements LoaderCallbacks<List<Song>>,
57         OnItemClickListener {
58
59     /**
60      * Used to keep context menu items from bleeding into other fragments
61      */
62     private static final int GROUP_ID = 7;
63
64     /**
65      * LoaderCallbacks identifier
66      */
67     private static final int LOADER = 0;
68
69     /**
70      * Fragment UI
71      */
72     private ViewGroup mRootView;
73
74     /**
75      * The adapter for the list
76      */
77     private ProfileSongAdapter mAdapter;
78
79     /**
80      * The list view
81      */
82     private ListView mListView;
83
84     /**
85      * Represents a song
86      */
87     private Song mSong;
88
89     /**
90      * Position of a context menu item
91      */
92     private int mSelectedPosition;
93
94     /**
95      * Id of a context menu item
96      */
97     private long mSelectedId;
98
99     /**
100      * Song, album, and artist name used in the context menu
101      */
102     private String mSongName, mAlbumName, mArtistName;
103
104     /**
105      * Profile header
106      */
107     private ProfileTabCarousel mProfileTabCarousel;
108
109     /**
110      * Empty constructor as per the {@link Fragment} documentation
111      */
112     public LastAddedFragment() {
113     }
114
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public void onAttach(final Activity activity) {
120         super.onAttach(activity);
121         mProfileTabCarousel = (ProfileTabCarousel)activity
122                 .findViewById(R.id.acivity_profile_base_tab_carousel);
123     }
124
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public void onCreate(final Bundle savedInstanceState) {
130         super.onCreate(savedInstanceState);
131         // Create the adpater
132         mAdapter = new ProfileSongAdapter(
133                 getActivity(),
134                 R.layout.list_item_normal,
135                 R.layout.faux_carousel,
136                 ProfileSongAdapter.DISPLAY_PLAYLIST_SETTING
137         );
138     }
139
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
145             final Bundle savedInstanceState) {
146         // The View for the fragment's UI
147         mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
148         // Initialize the list
149         mListView = (ListView)mRootView.findViewById(R.id.list_base);
150         // Set the data behind the list
151         mListView.setAdapter(mAdapter);
152         // Release any references to the recycled Views
153         mListView.setRecyclerListener(new RecycleHolder());
154         // Listen for ContextMenus to be created
155         mListView.setOnCreateContextMenuListener(this);
156         // Play the selected song
157         mListView.setOnItemClickListener(this);
158         // To help make scrolling smooth
159         mListView.setOnScrollListener(new VerticalScrollListener(null, mProfileTabCarousel, 0) {
160             @Override
161             public void onScrollStateChanged(AbsListView view, int scrollState) {
162                 super.onScrollStateChanged(view, scrollState);
163
164                 // Pause disk cache access to ensure smoother scrolling
165                 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING
166                         || scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
167                     mAdapter.setPauseDiskCache(true);
168                 } else {
169                     mAdapter.setPauseDiskCache(false);
170                     mAdapter.notifyDataSetChanged();
171                 }
172             }
173         });
174         // Remove the scrollbars and padding for the fast scroll
175         mListView.setVerticalScrollBarEnabled(false);
176         mListView.setFastScrollEnabled(false);
177         mListView.setPadding(0, 0, 0, 0);
178         return mRootView;
179     }
180
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public void onActivityCreated(final Bundle savedInstanceState) {
186         super.onActivityCreated(savedInstanceState);
187         // Enable the options menu
188         setHasOptionsMenu(true);
189         // Start the loader
190         getLoaderManager().initLoader(LOADER, null, this);
191     }
192
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     public void onCreateContextMenu(final ContextMenu menu, final View v,
198             final ContextMenuInfo menuInfo) {
199         super.onCreateContextMenu(menu, v, menuInfo);
200         // Get the position of the selected item
201         final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
202         mSelectedPosition = info.position - 1;
203         // Creat a new song
204         mSong = mAdapter.getItem(mSelectedPosition);
205         mSelectedId = mSong.mSongId;
206         mSongName = mSong.mSongName;
207         mAlbumName = mSong.mAlbumName;
208         mArtistName = mSong.mArtistName;
209
210         // Play the song
211         menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
212                 getString(R.string.context_menu_play_selection));
213
214         // Play next
215         menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
216                 getString(R.string.context_menu_play_next));
217
218         // Add the song to the queue
219         menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE,
220                 getString(R.string.add_to_queue));
221
222         // Add the song to a playlist
223         final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST,
224                 Menu.NONE, R.string.add_to_playlist);
225         MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu);
226
227         // View more content by the song artist
228         menu.add(GROUP_ID, FragmentMenuItems.MORE_BY_ARTIST, Menu.NONE,
229                 getString(R.string.context_menu_more_by_artist));
230
231         // Make the song a ringtone
232         menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
233                 getString(R.string.context_menu_use_as_ringtone));
234
235         // Delete the song
236         menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
237                 getString(R.string.context_menu_delete));
238     }
239
240     @Override
241     public boolean onContextItemSelected(final android.view.MenuItem item) {
242         if (item.getGroupId() == GROUP_ID) {
243             switch (item.getItemId()) {
244                 case FragmentMenuItems.PLAY_SELECTION:
245                     MusicUtils.playAll(getActivity(), new long[] {
246                         mSelectedId
247                     }, 0, false);
248                     return true;
249                 case FragmentMenuItems.PLAY_NEXT:
250                     MusicUtils.playNext(new long[] {
251                         mSelectedId
252                     });
253                     return true;
254                 case FragmentMenuItems.ADD_TO_QUEUE:
255                     MusicUtils.addToQueue(getActivity(), new long[] {
256                         mSelectedId
257                     });
258                     return true;
259                 case FragmentMenuItems.NEW_PLAYLIST:
260                     CreateNewPlaylist.getInstance(new long[] {
261                         mSelectedId
262                     }).show(getFragmentManager(), "CreatePlaylist");
263                     return true;
264                 case FragmentMenuItems.PLAYLIST_SELECTED:
265                     final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
266                     MusicUtils.addToPlaylist(getActivity(), new long[] {
267                         mSelectedId
268                     }, mPlaylistId);
269                     return true;
270                 case FragmentMenuItems.MORE_BY_ARTIST:
271                     NavUtils.openArtistProfile(getActivity(), mArtistName);
272                     return true;
273                 case FragmentMenuItems.USE_AS_RINGTONE:
274                     MusicUtils.setRingtone(getActivity(), mSelectedId);
275                     return true;
276                 case FragmentMenuItems.DELETE:
277                     DeleteDialog.newInstance(mSong.mSongName, new long[] {
278                         mSelectedId
279                     }, null).show(getFragmentManager(), "DeleteDialog");
280                     SystemClock.sleep(10);
281                     mAdapter.notifyDataSetChanged();
282                     getLoaderManager().restartLoader(LOADER, null, this);
283                     return true;
284                 default:
285                     break;
286             }
287         }
288         return super.onContextItemSelected(item);
289     }
290
291     /**
292      * {@inheritDoc}
293      */
294     @Override
295     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
296             final long id) {
297         if (position == 0) {
298             return;
299         }
300         Cursor cursor = LastAddedLoader.makeLastAddedCursor(getActivity());
301         final long[] list = MusicUtils.getSongListForCursor(cursor);
302         MusicUtils.playAll(getActivity(), list, position - 1, false);
303         cursor.close();
304         cursor = null;
305     }
306
307     /**
308      * {@inheritDoc}
309      */
310     @Override
311     public Loader<List<Song>> onCreateLoader(final int id, final Bundle args) {
312         return new LastAddedLoader(getActivity());
313     }
314
315     /**
316      * {@inheritDoc}
317      */
318     @Override
319     public void onLoadFinished(final Loader<List<Song>> loader, final List<Song> data) {
320         // Check for any errors
321         if (data.isEmpty()) {
322             // Set the empty text
323             final TextView empty = (TextView)mRootView.findViewById(R.id.empty);
324             empty.setText(getString(R.string.empty_last_added));
325             mListView.setEmptyView(empty);
326             return;
327         }
328
329         // Start fresh
330         mAdapter.unload();
331         // Return the correct count
332         mAdapter.setCount(data);
333         // Add the data to the adpater
334         for (final Song song : data) {
335             mAdapter.add(song);
336         }
337     }
338
339     /**
340      * {@inheritDoc}
341      */
342     @Override
343     public void onLoaderReset(final Loader<List<Song>> loader) {
344         // Clear the data in the adapter
345         mAdapter.unload();
346     }
347
348 }