AbstractItemHierarchy.java revision ab45bdf67a01ba13efb45334cc43f9632de6f034
1/*
2 * Copyright (C) 2015 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 com.android.setupwizardlib.items;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.util.Log;
23
24import com.android.setupwizardlib.R;
25
26import java.util.ArrayList;
27
28/**
29 * An abstract item hierarchy; provides default implementation for ID and observers.
30 */
31public abstract class AbstractItemHierarchy implements ItemHierarchy {
32
33    /* static section */
34
35    private static final String TAG = "AbstractItemHierarchy";
36
37    /* non-static section */
38
39    private ArrayList<Observer> mObservers = new ArrayList<>();
40    private int mId = 0;
41
42    public AbstractItemHierarchy() {
43    }
44
45    public AbstractItemHierarchy(Context context, AttributeSet attrs) {
46        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwAbstractItem);
47        mId = a.getResourceId(R.styleable.SuwAbstractItem_android_id, 0);
48        a.recycle();
49    }
50
51    public void setId(int id) {
52        mId = id;
53    }
54
55    public int getId() {
56        return mId;
57    }
58
59    public int getViewId() {
60        return getId();
61    }
62
63    @Override
64    public void registerObserver(Observer observer) {
65        mObservers.add(observer);
66    }
67
68    @Override
69    public void unregisterObserver(Observer observer) {
70        mObservers.remove(observer);
71    }
72
73    /**
74     * @see Observer#onChanged(ItemHierarchy)
75     */
76    public void notifyChanged() {
77        for (Observer observer : mObservers) {
78            observer.onChanged(this);
79        }
80    }
81
82    /**
83     * @see Observer#onItemRangeChanged(ItemHierarchy, int, int)
84     */
85    public void notifyItemRangeChanged(int position, int itemCount) {
86        if (position < 0) {
87            Log.w(TAG, "notifyItemRangeChanged: Invalid position=" + position);
88            return;
89        }
90        if (itemCount < 0) {
91            Log.w(TAG, "notifyItemRangeChanged: Invalid itemCount=" + itemCount);
92            return;
93        }
94
95        for (Observer observer : mObservers) {
96            observer.onItemRangeChanged(this, position, itemCount);
97        }
98    }
99
100    /**
101     * @see Observer#onItemRangeInserted(ItemHierarchy, int, int)
102     */
103    public void notifyItemRangeInserted(int position, int itemCount) {
104        if (position < 0) {
105            Log.w(TAG, "notifyItemRangeInserted: Invalid position=" + position);
106            return;
107        }
108        if (itemCount < 0) {
109            Log.w(TAG, "notifyItemRangeInserted: Invalid itemCount=" + itemCount);
110            return;
111        }
112
113        for (Observer observer : mObservers) {
114            observer.onItemRangeInserted(this, position, itemCount);
115        }
116    }
117
118    /**
119     * @see Observer#onItemRangeMoved(ItemHierarchy, int, int, int)
120     */
121    public void notifyItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
122        if (fromPosition < 0) {
123            Log.w(TAG, "notifyItemRangeMoved: Invalid fromPosition=" + fromPosition);
124            return;
125        }
126        if (toPosition < 0) {
127            Log.w(TAG, "notifyItemRangeMoved: Invalid toPosition=" + toPosition);
128            return;
129        }
130        if (itemCount < 0) {
131            Log.w(TAG, "notifyItemRangeMoved: Invalid itemCount=" + itemCount);
132            return;
133        }
134
135        for (Observer observer : mObservers) {
136            observer.onItemRangeMoved(this, fromPosition, toPosition, itemCount);
137        }
138    }
139
140    /**
141     * @see Observer#onItemRangeRemoved(ItemHierarchy, int, int)
142     */
143    public void notifyItemRangeRemoved(int position, int itemCount) {
144        if (position < 0) {
145            Log.w(TAG, "notifyItemRangeInserted: Invalid position=" + position);
146            return;
147        }
148        if (itemCount < 0) {
149            Log.w(TAG, "notifyItemRangeInserted: Invalid itemCount=" + itemCount);
150            return;
151        }
152
153        for (Observer observer : mObservers) {
154            observer.onItemRangeRemoved(this, position, itemCount);
155        }
156    }
157}
158