OSDN Git Service

Eleven: Make Images load a bit faster
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / ui / fragments / ArtistFragment.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;
13
14 import android.content.Context;
15 import android.os.Bundle;
16 import android.os.SystemClock;
17 import android.support.v4.app.Fragment;
18 import android.support.v4.app.LoaderManager.LoaderCallbacks;
19 import android.support.v4.content.Loader;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AbsListView;
24 import android.widget.AbsListView.OnScrollListener;
25 import android.widget.AdapterView;
26 import android.widget.AdapterView.OnItemClickListener;
27 import android.widget.ListView;
28
29 import com.cyngn.eleven.MusicStateListener;
30 import com.cyngn.eleven.R;
31 import com.cyngn.eleven.adapters.ArtistAdapter;
32 import com.cyngn.eleven.adapters.PagerAdapter;
33 import com.cyngn.eleven.loaders.ArtistLoader;
34 import com.cyngn.eleven.model.Artist;
35 import com.cyngn.eleven.recycler.RecycleHolder;
36 import com.cyngn.eleven.sectionadapter.SectionAdapter;
37 import com.cyngn.eleven.sectionadapter.SectionCreator;
38 import com.cyngn.eleven.sectionadapter.SectionListContainer;
39 import com.cyngn.eleven.ui.activities.BaseActivity;
40 import com.cyngn.eleven.ui.fragments.phone.MusicBrowserFragment;
41 import com.cyngn.eleven.utils.ArtistPopupMenuHelper;
42 import com.cyngn.eleven.utils.MusicUtils;
43 import com.cyngn.eleven.utils.NavUtils;
44 import com.cyngn.eleven.utils.PopupMenuHelper;
45 import com.cyngn.eleven.utils.SectionCreatorUtils;
46 import com.cyngn.eleven.utils.SectionCreatorUtils.IItemCompare;
47 import com.cyngn.eleven.widgets.IPopupMenuCallback;
48 import com.cyngn.eleven.widgets.LoadingEmptyContainer;
49 import com.viewpagerindicator.TitlePageIndicator;
50
51 /**
52  * This class is used to display all of the artists on a user's device.
53  * 
54  * @author Andrew Neal (andrewdneal@gmail.com)
55  */
56 public class ArtistFragment extends MusicBrowserFragment implements
57         LoaderCallbacks<SectionListContainer<Artist>>,
58         OnScrollListener, OnItemClickListener, MusicStateListener {
59
60     /**
61      * Fragment UI
62      */
63     private ViewGroup mRootView;
64
65     /**
66      * The adapter for the grid
67      */
68     private SectionAdapter<Artist, ArtistAdapter> mAdapter;
69
70     /**
71      * The list view
72      */
73     private ListView mListView;
74
75     /**
76      * Pop up menu helper
77      */
78     private PopupMenuHelper mPopupMenuHelper;
79
80     /**
81      * Loading container and no results container
82      */
83     private LoadingEmptyContainer mLoadingEmptyContainer;
84
85     /**
86      * Empty constructor as per the {@link Fragment} documentation
87      */
88     public ArtistFragment() {
89     }
90
91     @Override
92     public int getLoaderId() {
93         return PagerAdapter.MusicFragments.ARTIST.ordinal();
94     }
95
96     /**
97      * {@inheritDoc}
98      */
99     @Override
100     public void onCreate(final Bundle savedInstanceState) {
101         super.onCreate(savedInstanceState);
102
103         mPopupMenuHelper = new ArtistPopupMenuHelper(getActivity(), getFragmentManager()) {
104             @Override
105             public Artist getArtist(int position) {
106                 return mAdapter.getTItem(position);
107             }
108         };
109
110         // Create the adapter
111         final int layout = R.layout.list_item_normal;
112         ArtistAdapter adapter = new ArtistAdapter(getActivity(), layout);
113         mAdapter = new SectionAdapter<Artist, ArtistAdapter>(getActivity(), adapter);
114         mAdapter.setPopupMenuClickedListener(new IPopupMenuCallback.IListener() {
115             @Override
116             public void onPopupMenuClicked(View v, int position) {
117                 mPopupMenuHelper.showPopupMenu(v, position);
118             }
119         });
120     }
121
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
127             final Bundle savedInstanceState) {
128         // The View for the fragment's UI
129         mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
130         initListView();
131
132         // Register the music status listener
133         ((BaseActivity)getActivity()).setMusicStateListenerListener(this);
134
135         return mRootView;
136     }
137
138     @Override
139     public void onDestroyView() {
140         super.onDestroyView();
141
142         ((BaseActivity)getActivity()).removeMusicStateListenerListener(this);
143     }
144
145
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public void onActivityCreated(final Bundle savedInstanceState) {
151         super.onActivityCreated(savedInstanceState);
152         // Enable the options menu
153         setHasOptionsMenu(true);
154         // Start the loader
155         initLoader(null, this);
156     }
157
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public void onPause() {
163         super.onPause();
164         mAdapter.flush();
165     }
166
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public void onScrollStateChanged(final AbsListView view, final int scrollState) {
172         // Pause disk cache access to ensure smoother scrolling
173         if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
174             mAdapter.getUnderlyingAdapter().setPauseDiskCache(true);
175         } else {
176             mAdapter.getUnderlyingAdapter().setPauseDiskCache(false);
177             mAdapter.notifyDataSetChanged();
178         }
179     }
180
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
186             final long id) {
187         Artist artist = mAdapter.getTItem(position);
188         NavUtils.openArtistProfile(getActivity(), artist.mArtistName);
189     }
190
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public Loader<SectionListContainer<Artist>> onCreateLoader(final int id, final Bundle args) {
196         mLoadingEmptyContainer.showLoading();
197         final Context context = getActivity();
198         IItemCompare<Artist> comparator = SectionCreatorUtils.createArtistComparison(context);
199         return new SectionCreator<Artist>(getActivity(), new ArtistLoader(context), comparator);
200     }
201
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public void onLoadFinished(final Loader<SectionListContainer<Artist>> loader,
207                                final SectionListContainer<Artist> data) {
208         // Check for any errors
209         if (data.mListResults.isEmpty()) {
210             mLoadingEmptyContainer.showNoResults();
211             return;
212         }
213
214         // Set the data
215         mAdapter.setData(data);
216     }
217
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public void onLoaderReset(final Loader<SectionListContainer<Artist>> loader) {
223         // Clear the data in the adapter
224         mAdapter.unload();
225     }
226
227     /**
228      * Scrolls the list to the currently playing artist when the user touches
229      * the header in the {@link TitlePageIndicator}.
230      */
231     public void scrollToCurrentArtist() {
232         final int currentArtistPosition = getItemPositionByArtist();
233
234         if (currentArtistPosition != 0) {
235             mListView.setSelection(currentArtistPosition);
236         }
237     }
238
239     /**
240      * @return The position of an item in the list or grid based on the name of
241      *         the currently playing artist.
242      */
243     private int getItemPositionByArtist() {
244         final long artistId = MusicUtils.getCurrentArtistId();
245         if (mAdapter == null) {
246             return 0;
247         }
248
249         int position = mAdapter.getItemPosition(artistId);
250
251         // if for some reason we don't find the item, just jump to the top
252         if (position < 0) {
253             return 0;
254         }
255
256         return position;
257     }
258
259     /**
260      * Restarts the loader.
261      */
262     public void refresh() {
263         // Wait a moment for the preference to change.
264         SystemClock.sleep(10);
265         restartLoader();
266     }
267
268     /**
269      * {@inheritDoc}
270      */
271     @Override
272     public void onScroll(final AbsListView view, final int firstVisibleItem,
273             final int visibleItemCount, final int totalItemCount) {
274         // Nothing to do
275     }
276
277     /**
278      * {@inheritDoc}
279      */
280     @Override
281     public void restartLoader() {
282         // Update the list when the user deletes any items
283         restartLoader(null, this);
284     }
285
286     /**
287      * {@inheritDoc}
288      */
289     @Override
290     public void onMetaChanged() {
291         // Nothing to do
292     }
293
294     @Override
295     public void onPlaylistChanged() {
296         // Nothing to do
297     }
298
299     /**
300      * Sets up various helpers for both the list and grid
301      * 
302      * @param list The list or grid
303      */
304     private void initAbsListView(final AbsListView list) {
305         // Release any references to the recycled Views
306         list.setRecyclerListener(new RecycleHolder());
307         // Show the albums and songs from the selected artist
308         list.setOnItemClickListener(this);
309         // To help make scrolling smooth
310         list.setOnScrollListener(this);
311     }
312
313     /**
314      * Sets up the list view
315      */
316     private void initListView() {
317         // Initialize the grid
318         mListView = (ListView)mRootView.findViewById(R.id.list_base);
319         // Set the data behind the list
320         mListView.setAdapter(mAdapter);
321         // set the loading and empty view container
322         mLoadingEmptyContainer = (LoadingEmptyContainer)mRootView.findViewById(R.id.loading_empty_container);
323         mListView.setEmptyView(mLoadingEmptyContainer);
324         // Set up the helpers
325         initAbsListView(mListView);
326     }
327 }