Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart9.png 32% of files have more coverage
59   290   26   2,95
12   141   0,44   6,67
20     1,3  
3    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  MergeAdapter       Line # 37 54 21 88,9% 0.8888889
  MergeAdapter.EnabledSackAdapter       Line # 262 3 3 66,7% 0.6666667
  MergeAdapter.CascadeDataSetObserver       Line # 278 2 2 0% 0.0
 
No Tests
 
1    /***
2    Copyright (c) 2008-2009 CommonsWare, LLC
3    Portions (c) 2009 Google, Inc.
4   
5    Licensed under the Apache License, Version 2.0 (the "License"); you may
6    not use this file except in compliance with the License. You may obtain
7    a copy of the License at
8    http://www.apache.org/licenses/LICENSE-2.0
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License.
14    */
15   
16    package net.sourceforge.subsonic.androidapp.util;
17   
18    import android.database.DataSetObserver;
19    import android.view.View;
20    import android.view.ViewGroup;
21    import android.widget.BaseAdapter;
22    import android.widget.ListAdapter;
23   
24    import java.util.ArrayList;
25    import java.util.List;
26    import java.util.Arrays;
27   
28    /**
29    * Adapter that merges multiple child adapters and views
30    * into a single contiguous whole.
31    * <p/>
32    * Adapters used as pieces within MergeAdapter must
33    * have view type IDs monotonically increasing from 0. Ideally,
34    * adapters also have distinct ranges for their row ids, as
35    * returned by getItemId().
36    */
 
37    public class MergeAdapter extends BaseAdapter {
38   
39    private final CascadeDataSetObserver observer = new CascadeDataSetObserver();
40    private final ArrayList<ListAdapter> pieces = new ArrayList<ListAdapter>();
41   
42    /**
43    * Stock constructor, simply chaining to the superclass.
44    */
 
45  16 toggle public MergeAdapter() {
46  16 super();
47    }
48   
49    /**
50    * Adds a new adapter to the roster of things to appear
51    * in the aggregate list.
52    *
53    * @param adapter Source for row views for this section
54    */
 
55  35 toggle public void addAdapter(ListAdapter adapter) {
56  35 pieces.add(adapter);
57  35 adapter.registerDataSetObserver(observer);
58    }
59   
 
60  0 toggle public void removeAdapter(ListAdapter adapter) {
61  0 adapter.unregisterDataSetObserver(observer);
62  0 pieces.remove(adapter);
63    }
64   
65    /**
66    * Adds a new View to the roster of things to appear
67    * in the aggregate list.
68    *
69    * @param view Single view to add
70    */
 
71  2 toggle public ListAdapter addView(View view) {
72  2 return addView(view, false);
73    }
74   
75    /**
76    * Adds a new View to the roster of things to appear
77    * in the aggregate list.
78    *
79    * @param view Single view to add
80    * @param enabled false if views are disabled, true if enabled
81    */
 
82  15 toggle public ListAdapter addView(View view, boolean enabled) {
83  15 return addViews(Arrays.asList(view), enabled);
84    }
85   
86    /**
87    * Adds a list of views to the roster of things to appear
88    * in the aggregate list.
89    *
90    * @param views List of views to add
91    */
 
92  0 toggle public ListAdapter addViews(List<View> views) {
93  0 return addViews(views, false);
94    }
95   
96    /**
97    * Adds a list of views to the roster of things to appear
98    * in the aggregate list.
99    *
100    * @param views List of views to add
101    * @param enabled false if views are disabled, true if enabled
102    */
 
103  33 toggle public ListAdapter addViews(List<View> views, boolean enabled) {
104  33 ListAdapter adapter = enabled ? new EnabledSackAdapter(views) : new SackOfViewsAdapter(views);
105  33 addAdapter(adapter);
106  33 return adapter;
107    }
108   
109    /**
110    * Get the data item associated with the specified
111    * position in the data set.
112    *
113    * @param position Position of the item whose data we want
114    */
 
115  2 toggle @Override
116    public Object getItem(int position) {
117  2 for (ListAdapter piece : pieces) {
118  6 int size = piece.getCount();
119   
120  6 if (position < size) {
121  2 return (piece.getItem(position));
122    }
123   
124  4 position -= size;
125    }
126   
127  0 return (null);
128    }
129   
130    /**
131    * How many items are in the data set represented by this
132    * Adapter.
133    */
 
134  172 toggle @Override
135    public int getCount() {
136  172 int total = 0;
137   
138  172 for (ListAdapter piece : pieces) {
139  380 total += piece.getCount();
140    }
141   
142  172 return (total);
143    }
144   
145    /**
146    * Returns the number of types of Views that will be
147    * created by getView().
148    */
 
149  16 toggle @Override
150    public int getViewTypeCount() {
151  16 int total = 0;
152   
153  16 for (ListAdapter piece : pieces) {
154  35 total += piece.getViewTypeCount();
155    }
156   
157  16 return (Math.max(total, 1)); // needed for setListAdapter() before content add'
158    }
159   
160    /**
161    * Get the type of View that will be created by getView()
162    * for the specified item.
163    *
164    * @param position Position of the item whose data we want
165    */
 
166  340 toggle @Override
167    public int getItemViewType(int position) {
168  340 int typeOffset = 0;
169  340 int result = -1;
170   
171  340 for (ListAdapter piece : pieces) {
172  811 int size = piece.getCount();
173   
174  811 if (position < size) {
175  340 result = typeOffset + piece.getItemViewType(position);
176  340 break;
177    }
178   
179  471 position -= size;
180  471 typeOffset += piece.getViewTypeCount();
181    }
182   
183  340 return (result);
184    }
185   
186    /**
187    * Are all items in this ListAdapter enabled? If yes it
188    * means all items are selectable and clickable.
189    */
 
190  16 toggle @Override
191    public boolean areAllItemsEnabled() {
192  16 return (false);
193    }
194   
195    /**
196    * Returns true if the item at the specified position is
197    * not a separator.
198    *
199    * @param position Position of the item whose data we want
200    */
 
201  2541 toggle @Override
202    public boolean isEnabled(int position) {
203  2541 for (ListAdapter piece : pieces) {
204  6569 int size = piece.getCount();
205   
206  6569 if (position < size) {
207  2541 return (piece.isEnabled(position));
208    }
209   
210  4028 position -= size;
211    }
212   
213  0 return (false);
214    }
215   
216    /**
217    * Get a View that displays the data at the specified
218    * position in the data set.
219    *
220    * @param position Position of the item whose data we want
221    * @param convertView View to recycle, if not null
222    * @param parent ViewGroup containing the returned View
223    */
 
224  80 toggle @Override
225    public View getView(int position, View convertView,
226    ViewGroup parent) {
227  80 for (ListAdapter piece : pieces) {
228  186 int size = piece.getCount();
229   
230  186 if (position < size) {
231   
232  80 return (piece.getView(position, convertView, parent));
233    }
234   
235  106 position -= size;
236    }
237   
238  0 return (null);
239    }
240   
241    /**
242    * Get the row id associated with the specified position
243    * in the list.
244    *
245    * @param position Position of the item whose data we want
246    */
 
247  24 toggle @Override
248    public long getItemId(int position) {
249  24 for (ListAdapter piece : pieces) {
250  42 int size = piece.getCount();
251   
252  42 if (position < size) {
253  24 return (piece.getItemId(position));
254    }
255   
256  18 position -= size;
257    }
258   
259  0 return (-1);
260    }
261   
 
262    private static class EnabledSackAdapter extends SackOfViewsAdapter {
 
263  23 toggle public EnabledSackAdapter(List<View> views) {
264  23 super(views);
265    }
266   
 
267  0 toggle @Override
268    public boolean areAllItemsEnabled() {
269  0 return (true);
270    }
271   
 
272  2067 toggle @Override
273    public boolean isEnabled(int position) {
274  2067 return (true);
275    }
276    }
277   
 
278    private class CascadeDataSetObserver extends DataSetObserver {
 
279  0 toggle @Override
280    public void onChanged() {
281  0 notifyDataSetChanged();
282    }
283   
 
284  0 toggle @Override
285    public void onInvalidated() {
286  0 notifyDataSetInvalidated();
287    }
288    }
289    }
290