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