OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / ui / fragments / profile / FavoriteFragment.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.andrew.apollo.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 import android.widget.TextView;
33
34 import com.andrew.apollo.R;
35 import com.andrew.apollo.adapters.ProfileSongAdapter;
36 import com.andrew.apollo.loaders.FavoritesLoader;
37 import com.andrew.apollo.menu.CreateNewPlaylist;
38 import com.andrew.apollo.menu.DeleteDialog;
39 import com.andrew.apollo.menu.FragmentMenuItems;
40 import com.andrew.apollo.model.Song;
41 import com.andrew.apollo.provider.FavoritesStore;
42 import com.andrew.apollo.recycler.RecycleHolder;
43 import com.andrew.apollo.utils.MusicUtils;
44 import com.andrew.apollo.utils.NavUtils;
45 import com.andrew.apollo.widgets.ProfileTabCarousel;
46 import com.andrew.apollo.widgets.VerticalScrollListener;
47
48 import java.util.List;
49
50 /**
51  * This class is used to display all of the songs in {@link FavoritesStore
52  * }.
53  * 
54  * @author Andrew Neal (andrewdneal@gmail.com)
55  */
56 public class FavoriteFragment 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 = 6;
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      * Artist name used in the context menu
101      */
102     private String 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 FavoriteFragment() {
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_simple,
135                 ProfileSongAdapter.DISPLAY_PLAYLIST_SETTING
136         );
137     }
138
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
144             final Bundle savedInstanceState) {
145         // The View for the fragment's UI
146         mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
147         // Initialize the list
148         mListView = (ListView)mRootView.findViewById(R.id.list_base);
149         // Set the data behind the list
150         mListView.setAdapter(mAdapter);
151         // Release any references to the recycled Views
152         mListView.setRecyclerListener(new RecycleHolder());
153         // Listen for ContextMenus to be created
154         mListView.setOnCreateContextMenuListener(this);
155         // Play the selected song
156         mListView.setOnItemClickListener(this);
157         // To help make scrolling smooth
158         mListView.setOnScrollListener(new VerticalScrollListener(null, mProfileTabCarousel, 0));
159         // Remove the scrollbars and padding for the fast scroll
160         mListView.setVerticalScrollBarEnabled(false);
161         mListView.setFastScrollEnabled(false);
162         mListView.setPadding(0, 0, 0, 0);
163         return mRootView;
164     }
165
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public void onActivityCreated(final Bundle savedInstanceState) {
171         super.onActivityCreated(savedInstanceState);
172         // Enable the options menu
173         setHasOptionsMenu(true);
174         // Start the loader
175         getLoaderManager().initLoader(LOADER, null, this);
176     }
177
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public void onCreateContextMenu(final ContextMenu menu, final View v,
183             final ContextMenuInfo menuInfo) {
184         super.onCreateContextMenu(menu, v, menuInfo);
185         // Get the position of the selected item
186         final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
187         mSelectedPosition = info.position - 1;
188         // Creat a new song
189         mSong = mAdapter.getItem(mSelectedPosition);
190         mSelectedId = mSong.mSongId;
191         mArtistName = mSong.mArtistName;
192
193         // Play the song
194         menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
195                 getString(R.string.context_menu_play_selection));
196
197         // Play next
198         menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
199                 getString(R.string.context_menu_play_next));
200
201         // Add the song to the queue
202         menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE,
203                 getString(R.string.add_to_queue));
204
205         // Add the song to a playlist
206         final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST,
207                 Menu.NONE, R.string.add_to_playlist);
208         MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu, false);
209
210         // View more content by the song artist
211         menu.add(GROUP_ID, FragmentMenuItems.MORE_BY_ARTIST, Menu.NONE,
212                 getString(R.string.context_menu_more_by_artist));
213
214         // Make the song a ringtone
215         menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
216                 getString(R.string.context_menu_use_as_ringtone));
217
218         // Remove from favorites
219         menu.add(GROUP_ID, FragmentMenuItems.REMOVE_FROM_FAVORITES, Menu.NONE,
220                 getString(R.string.remove_from_favorites));
221
222         // Delete the song
223         menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
224                 getString(R.string.context_menu_delete));
225     }
226
227     @Override
228     public boolean onContextItemSelected(final android.view.MenuItem item) {
229         if (item.getGroupId() == GROUP_ID) {
230             switch (item.getItemId()) {
231                 case FragmentMenuItems.PLAY_SELECTION:
232                     MusicUtils.playAll(getActivity(), new long[] {
233                         mSelectedId
234                     }, 0, false);
235                     return true;
236                 case FragmentMenuItems.PLAY_NEXT:
237                     MusicUtils.playNext(new long[] {
238                         mSelectedId
239                     });
240                     return true;
241                 case FragmentMenuItems.ADD_TO_QUEUE:
242                     MusicUtils.addToQueue(getActivity(), new long[] {
243                         mSelectedId
244                     });
245                     return true;
246                 case FragmentMenuItems.NEW_PLAYLIST:
247                     CreateNewPlaylist.getInstance(new long[] {
248                         mSelectedId
249                     }).show(getFragmentManager(), "CreatePlaylist");
250                     return true;
251                 case FragmentMenuItems.PLAYLIST_SELECTED:
252                     final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
253                     MusicUtils.addToPlaylist(getActivity(), new long[] {
254                         mSelectedId
255                     }, mPlaylistId);
256                     return true;
257                 case FragmentMenuItems.MORE_BY_ARTIST:
258                     NavUtils.openArtistProfile(getActivity(), mArtistName);
259                     return true;
260                 case FragmentMenuItems.USE_AS_RINGTONE:
261                     MusicUtils.setRingtone(getActivity(), mSelectedId);
262                     return true;
263                 case FragmentMenuItems.REMOVE_FROM_FAVORITES:
264                     FavoritesStore.getInstance(getActivity()).removeItem(mSelectedId);
265                     getLoaderManager().restartLoader(LOADER, null, this);
266                     return true;
267                 case FragmentMenuItems.DELETE:
268                     DeleteDialog.newInstance(mSong.mSongName, new long[] {
269                         mSelectedId
270                     }, null).show(getFragmentManager(), "DeleteDialog");
271                     SystemClock.sleep(10);
272                     mAdapter.notifyDataSetChanged();
273                     getLoaderManager().restartLoader(LOADER, null, this);
274                     return true;
275                 default:
276                     break;
277             }
278         }
279         return super.onContextItemSelected(item);
280     }
281
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
287             final long id) {
288         if (position == 0) {
289             return;
290         }
291         Cursor cursor = FavoritesLoader.makeFavoritesCursor(getActivity());
292         final long[] list = MusicUtils.getSongListForCursor(cursor);
293         MusicUtils.playAll(getActivity(), list, position - 1, false);
294         cursor.close();
295         cursor = null;
296     }
297
298     /**
299      * {@inheritDoc}
300      */
301     @Override
302     public Loader<List<Song>> onCreateLoader(final int id, final Bundle args) {
303         return new FavoritesLoader(getActivity());
304     }
305
306     /**
307      * {@inheritDoc}
308      */
309     @Override
310     public void onLoadFinished(final Loader<List<Song>> loader, final List<Song> data) {
311         // Check for any errors
312         if (data.isEmpty()) {
313             // Set the empty text
314             final TextView empty = (TextView)mRootView.findViewById(R.id.empty);
315             empty.setText(getString(R.string.empty_favorite));
316             mListView.setEmptyView(empty);
317             return;
318         }
319
320         // Start fresh
321         mAdapter.unload();
322         // Return the correct count
323         mAdapter.setCount(data);
324         // Add the data to the adpater
325         for (final Song song : data) {
326             mAdapter.add(song);
327         }
328     }
329
330     /**
331      * {@inheritDoc}
332      */
333     @Override
334     public void onLoaderReset(final Loader<List<Song>> loader) {
335         // Clear the data in the adapter
336         mAdapter.unload();
337     }
338
339 }