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;
22
23import com.android.setupwizardlib.R;
24
25import java.util.ArrayList;
26
27/**
28 * An abstract item hierarchy; provides default implementation for ID and observers.
29 */
30public abstract class AbstractItemHierarchy implements ItemHierarchy {
31
32    private ArrayList<Observer> mObservers = new ArrayList<>();
33    private int mId = 0;
34
35    public AbstractItemHierarchy() {
36    }
37
38    public AbstractItemHierarchy(Context context, AttributeSet attrs) {
39        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwAbstractItem);
40        mId = a.getResourceId(R.styleable.SuwAbstractItem_android_id, 0);
41        a.recycle();
42    }
43
44    public void setId(int id) {
45        mId = id;
46    }
47
48    public int getId() {
49        return mId;
50    }
51
52    @Override
53    public void registerObserver(Observer observer) {
54        mObservers.add(observer);
55    }
56
57    @Override
58    public void unregisterObserver(Observer observer) {
59        mObservers.remove(observer);
60    }
61
62    public void notifyChanged() {
63        for (Observer observer : mObservers) {
64            observer.onChanged(this);
65        }
66    }
67}
68