1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.database.DataSetObservable;
20import android.database.DataSetObserver;
21
22/**
23 * Base class for a {@link ExpandableListAdapter} used to provide data and Views
24 * from some data to an expandable list view.
25 * <p>
26 * Adapters inheriting this class should verify that the base implementations of
27 * {@link #getCombinedChildId(long, long)} and {@link #getCombinedGroupId(long)}
28 * are correct in generating unique IDs from the group/children IDs.
29 * <p>
30 * @see SimpleExpandableListAdapter
31 * @see SimpleCursorTreeAdapter
32 */
33public abstract class BaseExpandableListAdapter implements ExpandableListAdapter,
34        HeterogeneousExpandableList {
35    private final DataSetObservable mDataSetObservable = new DataSetObservable();
36
37    public void registerDataSetObserver(DataSetObserver observer) {
38        mDataSetObservable.registerObserver(observer);
39    }
40
41    public void unregisterDataSetObserver(DataSetObserver observer) {
42        mDataSetObservable.unregisterObserver(observer);
43    }
44
45    /**
46     * @see DataSetObservable#notifyInvalidated()
47     */
48    public void notifyDataSetInvalidated() {
49        mDataSetObservable.notifyInvalidated();
50    }
51
52    /**
53     * @see DataSetObservable#notifyChanged()
54     */
55    public void notifyDataSetChanged() {
56        mDataSetObservable.notifyChanged();
57    }
58
59    public boolean areAllItemsEnabled() {
60        return true;
61    }
62
63    public void onGroupCollapsed(int groupPosition) {
64    }
65
66    public void onGroupExpanded(int groupPosition) {
67    }
68
69    /**
70     * Override this method if you foresee a clash in IDs based on this scheme:
71     * <p>
72     * Base implementation returns a long:
73     * <li> bit 0: Whether this ID points to a child (unset) or group (set), so for this method
74     *             this bit will be 1.
75     * <li> bit 1-31: Lower 31 bits of the groupId
76     * <li> bit 32-63: Lower 32 bits of the childId.
77     * <p>
78     * {@inheritDoc}
79     */
80    public long getCombinedChildId(long groupId, long childId) {
81        return 0x8000000000000000L | ((groupId & 0x7FFFFFFF) << 32) | (childId & 0xFFFFFFFF);
82    }
83
84    /**
85     * Override this method if you foresee a clash in IDs based on this scheme:
86     * <p>
87     * Base implementation returns a long:
88     * <li> bit 0: Whether this ID points to a child (unset) or group (set), so for this method
89     *             this bit will be 0.
90     * <li> bit 1-31: Lower 31 bits of the groupId
91     * <li> bit 32-63: Lower 32 bits of the childId.
92     * <p>
93     * {@inheritDoc}
94     */
95    public long getCombinedGroupId(long groupId) {
96        return (groupId & 0x7FFFFFFF) << 32;
97    }
98
99    /**
100     * {@inheritDoc}
101     */
102    public boolean isEmpty() {
103        return getGroupCount() == 0;
104    }
105
106
107    /**
108     * {@inheritDoc}
109     * @return 0 for any group or child position, since only one child type count is declared.
110     */
111    public int getChildType(int groupPosition, int childPosition) {
112        return 0;
113    }
114
115    /**
116     * {@inheritDoc}
117     * @return 1 as a default value in BaseExpandableListAdapter.
118     */
119    public int getChildTypeCount() {
120        return 1;
121    }
122
123    /**
124     * {@inheritDoc}
125     * @return 0 for any groupPosition, since only one group type count is declared.
126     */
127    public int getGroupType(int groupPosition) {
128        return 0;
129    }
130
131    /**
132     * {@inheritDoc}
133     * @return 1 as a default value in BaseExpandableListAdapter.
134     */
135    public int getGroupTypeCount() {
136        return 1;
137    }
138}
139