OSDN Git Service

Eleven: lots of ui tweaks (fonts, padding, grid layout) and cancel tasks smarter
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / adapters / PagerAdapter.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.adapters;
13
14 import android.app.Activity;
15 import android.os.Bundle;
16 import android.support.v4.app.Fragment;
17 import android.support.v4.app.FragmentActivity;
18 import android.support.v4.app.FragmentPagerAdapter;
19 import android.util.SparseArray;
20 import android.view.ViewGroup;
21
22 import com.cyngn.eleven.R;
23 import com.cyngn.eleven.ui.fragments.AlbumFragment;
24 import com.cyngn.eleven.ui.fragments.ArtistFragment;
25 import com.cyngn.eleven.ui.fragments.GenreFragment;
26 import com.cyngn.eleven.ui.fragments.PlaylistFragment;
27 import com.cyngn.eleven.ui.fragments.RecentFragment;
28 import com.cyngn.eleven.ui.fragments.SongFragment;
29 import com.cyngn.eleven.utils.Lists;
30
31 import java.lang.ref.WeakReference;
32 import java.util.List;
33 import java.util.Locale;
34
35 /**
36  * A {@link FragmentPagerAdapter} class for swiping between playlists, recent,
37  * artists, albums, songs, and genre {@link Fragment}s on phones.<br/>
38  */
39 public class PagerAdapter extends FragmentPagerAdapter {
40
41     private final SparseArray<WeakReference<Fragment>> mFragmentArray = new SparseArray<WeakReference<Fragment>>();
42
43     private final List<Holder> mHolderList = Lists.newArrayList();
44
45     private final FragmentActivity mFragmentActivity;
46
47     private int mCurrentPage;
48
49     /**
50      * Constructor of <code>PagerAdatper<code>
51      * 
52      * @param fragmentActivity The {@link Activity} of the
53      *            {@link Fragment}.
54      */
55     public PagerAdapter(final FragmentActivity fragmentActivity) {
56         super(fragmentActivity.getSupportFragmentManager());
57         mFragmentActivity = fragmentActivity;
58     }
59
60     /**
61      * Method that adds a new fragment class to the viewer (the fragment is
62      * internally instantiate)
63      * 
64      * @param className The full qualified name of fragment class.
65      * @param params The instantiate params.
66      */
67     @SuppressWarnings("synthetic-access")
68     public void add(final Class<? extends Fragment> className, final Bundle params) {
69         final Holder mHolder = new Holder();
70         mHolder.mClassName = className.getName();
71         mHolder.mParams = params;
72
73         final int mPosition = mHolderList.size();
74         mHolderList.add(mPosition, mHolder);
75         notifyDataSetChanged();
76     }
77
78     /**
79      * Method that returns the {@link Fragment} in the argument
80      * position.
81      * 
82      * @param position The position of the fragment to return.
83      * @return Fragment The {@link Fragment} in the argument position.
84      */
85     public Fragment getFragment(final int position) {
86         final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
87         if (mWeakFragment != null && mWeakFragment.get() != null) {
88             return mWeakFragment.get();
89         }
90         return getItem(position);
91     }
92
93     /**
94      * {@inheritDoc}
95      */
96     @Override
97     public Object instantiateItem(final ViewGroup container, final int position) {
98         final Fragment mFragment = (Fragment)super.instantiateItem(container, position);
99         final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
100         if (mWeakFragment != null) {
101             mWeakFragment.clear();
102         }
103         mFragmentArray.put(position, new WeakReference<Fragment>(mFragment));
104         return mFragment;
105     }
106
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public Fragment getItem(final int position) {
112         final Holder mCurrentHolder = mHolderList.get(position);
113         final Fragment mFragment = Fragment.instantiate(mFragmentActivity,
114                 mCurrentHolder.mClassName, mCurrentHolder.mParams);
115         return mFragment;
116     }
117
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void destroyItem(final ViewGroup container, final int position, final Object object) {
123         super.destroyItem(container, position, object);
124         final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
125         if (mWeakFragment != null) {
126             mWeakFragment.clear();
127         }
128     }
129
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public int getCount() {
135         return mHolderList.size();
136     }
137
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public CharSequence getPageTitle(final int position) {
143         return mFragmentActivity.getResources().getStringArray(R.array.page_titles)[position]
144                 .toUpperCase(Locale.getDefault());
145     }
146
147     /**
148      * Method that returns the current page position.
149      * 
150      * @return int The current page.
151      */
152     public int getCurrentPage() {
153         return mCurrentPage;
154     }
155
156     /**
157      * Method that sets the current page position.
158      * 
159      * @param currentPage The current page.
160      */
161     protected void setCurrentPage(final int currentPage) {
162         mCurrentPage = currentPage;
163     }
164
165     /**
166      * An enumeration of all the main fragments supported.
167      */
168     public enum MusicFragments {
169         /**
170          * The artist fragment
171          */
172         ARTIST(ArtistFragment.class),
173         /**
174          * The album fragment
175          */
176         ALBUM(AlbumFragment.class),
177         /**
178          * The song fragment
179          */
180         SONG(SongFragment.class),
181         /**
182          * The playlist fragment
183          */
184         PLAYLIST(PlaylistFragment.class);
185
186         private Class<? extends Fragment> mFragmentClass;
187
188         /**
189          * Constructor of <code>MusicFragments</code>
190          * 
191          * @param fragmentClass The fragment class
192          */
193         private MusicFragments(final Class<? extends Fragment> fragmentClass) {
194             mFragmentClass = fragmentClass;
195         }
196
197         /**
198          * Method that returns the fragment class.
199          * 
200          * @return Class<? extends Fragment> The fragment class.
201          */
202         public Class<? extends Fragment> getFragmentClass() {
203             return mFragmentClass;
204         }
205
206     }
207
208     /**
209      * A private class with information about fragment initialization
210      */
211     private final static class Holder {
212         String mClassName;
213
214         Bundle mParams;
215     }
216 }