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